VB指针,VB API编程

原创 2005年03月04日 12:59:00

Rem 简单的VB API编程,只显示出一个白色的窗口,然后写出HELLO
Rem                   天津财经大学 企管系 工商管理 0202
Rem                              (c)邓德荣 2005-3-4
Rem 注:如需修改程序,请联系作者
Rem E-Mail :louisdeng@monternet.com


Public Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Long) As Long
Public Declare Function LoadIcon Lib "user32" Alias "LoadIconA" (ByVal hInstance As Long, ByVal lpIconName As Long) As Long
Public Declare Function RegisterClass Lib "user32" Alias "RegisterClassA" (Class As Long) As Long
Public Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As Long, ByVal lpWindowName As Long, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function UpdateWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As Long, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
Public Declare Function TranslateMessage Lib "user32" (lpMsg As Long) As Long
Public Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As Long) As Long
Public Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
Public Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)
Public Declare Function BeginPaint Lib "user32" (ByVal hwnd As Long, lpPaint As Long) As Long
Public Declare Function EndPaint Lib "user32" (ByVal hwnd As Long, lpPaint As Long) As Long
Public Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long

Public Const WM_SIZE = &H5
Public Const WM_PAINT = &HF
Public Const WM_DESTROY = &H2
Public Const IDC_ARROW = 32512&
Public Const IDI_APPLICATION = 32512&
Public Const CW_USEDEFAULT = &H80000000
Public Const WS_EX_APPWINDOW = &H40000
Public Const WS_OVERLAPPED = &H0&
Public Const WHITE_BRUSH = 0
Public Const CS_HREDRAW = &H2
Public Const CS_VREDRAW = &H1
Public Const WS_CAPTION = &HC00000                  '  WS_BORDER Or WS_DLGFRAME
Public Const WS_SYSMENU = &H80000
Public Const WS_THICKFRAME = &H40000
Public Const WS_MAXIMIZEBOX = &H10000
Public Const WS_MINIMIZEBOX = &H20000
Public Const WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)

Public Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

Public Type PAINTSTRUCT
        hdc As Long
        fErase As Long
        rcPaint As RECT
        fRestore As Long
        fIncUpdate As Long
        rgbReserved(32) As Byte
End Type

Public Type WNDCLASS
    style As Long
    lpfnwndproc As Long
    cbClsextra As Long
    cbWndExtra2 As Long
    hInstance As Long
    hIcon As Long
    hCursor As Long
    hbrBackground As Long
    lpszMenuName As Long
    lpszClassName As Long
End Type

Public Type POINTAPI
        x As Long
        y As Long
End Type

Public Const COLOR_WINDOW = 5

Public Type MSG
    hwnd As Long
    message As Long
    wParam As Long
    lParam As Long
    time As Long
    pt As POINTAPI
End Type

Public Function WndProc(ByVal hwnd As Long, ByVal message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim ps As PAINTSTRUCT
Dim hdc As Long
Dim szBuffer As String
Static cxCLient As Long
Static cyClient As Long

szBuffer = "Hello, VB API"

Select Case message
    Case WM_SIZE
        cxCLient = lParam Mod 65536
        cyClient = lParam / 65536
        WndProc = 0
        Exit Function
       
    Case WM_PAINT
        hdc = BeginPaint(hwnd, ByVal VarPtr(ps))
        TextOut hdc, cxCLient / 2, cyClient / 2, szBuffer, Len(szBuffer)
        EndPaint hwnd, ByVal VarPtr(ps)
        WndProc = 0
        Exit Function
       
    Case WM_DESTROY
        PostQuitMessage 0
        WndProc = 0
        Exit Function
       
End Select
WndProc = DefWindowProc(hwnd, message, wParam, lParam)

End Function

Private Sub SetWndProc(ByRef wc As WNDCLASS, ByVal WndProc As Long)

wc.lpfnwndproc = WndProc
End Sub
   
Sub Main()
Dim szAppName(10) As Byte
Dim pmsg As MSG
Dim hwnd As Long
Dim wc As WNDCLASS

szAppName(0) = 97
szAppName(1) = 112
szAppName(2) = 112
szAppName(3) = 0

wc.cbClsextra = 0
wc.cbWndExtra2 = 0
wc.hbrBackground = GetStockObject(WHITE_BRUSH)
wc.hCursor = LoadCursor(0, IDC_ARROW)
wc.hIcon = LoadIcon(0, IDI_APPLICATION)
wc.hInstance = App.hInstance
SetWndProc wc, AddressOf WndProc
wc.lpszClassName = VarPtr(szAppName(0))
wc.lpszMenuName = 0
wc.style = CS_HREDRAW Or CS_VREDRAW

If RegisterClass(ByVal VarPtr(wc)) = 0 Then MsgBox ("Error")

hwnd = CreateWindowEx(0, ByVal VarPtr(szAppName(0)), ByVal VarPtr(szAppName(0)), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, App.hInstance, 0)
ShowWindow hwnd, 1
UpdateWindow hwnd

Do While GetMessage(ByVal VarPtr(pmsg), 0, 0, 0) <> 0
TranslateMessage ByVal VarPtr(pmsg)
DispatchMessage ByVal VarPtr(pmsg)
Loop

End Sub

vb 指针的操作

VB指针教程1 '先理解下变量的地址与指向这个地址的值。xx省xx市xx街xx号就是一个地址, '指向这个地址的值就是这个地址就有什么。比如这个地址是张三家的地址, '张三的家有什么呢?...
  • tangyanzhi1111
  • tangyanzhi1111
  • 2016年08月10日 16:37
  • 768

VB socket编程入门

利用Winsock控件实现局域网通信   对于局域网用户中的编程爱好者来说,如果能自己编一个局域网通信程序,那么这一切将是多么美妙!可是,如果要从头开始完全由自己来编写一段用于通信的程序,必须对相...
  • lxnkobe
  • lxnkobe
  • 2011年09月27日 21:41
  • 6762

VB6使用API实现串口通信

    需要和客户的产品通讯,但波特率是非常规的,MScomm无法实现,原有的软件框架和条件又不能转用VC开发底层,于是用VB6调用API实现了这个通讯功能,虽然在VB6下这个程序还是单进程的,但实现...
  • jackylingzeng
  • jackylingzeng
  • 2007年05月01日 13:29
  • 4301

在VB中利用API进行串口通信

一般来说,在VB中编写串口通讯程序,首先考虑到是使用MSComm控件,可是该控件不能设置超时,而且对许多内部的参数进行了隐藏,从而不能满足有些具体的工作。而使用API进行串口通信,大多是使用VC,很少...
  • lyserver
  • lyserver
  • 2009年05月06日 05:54
  • 11169

【转】VB的指针使用

原文地址:http://www.cnblogs.com/xw885/articles/105264.html    一、指针是什么?   不需要去找什么标准的定义,它就是一个32位整数,在...
  • JiLuoXingRen
  • JiLuoXingRen
  • 2012年12月16日 23:28
  • 6917

VB中的Winsock编程

  VB中的Winsock编程   一.引言 Socket原意为“插座”,用于计算机通信则代表一种点到点信息传输。早期用于Unix系统上的通信编程规范,通信双方由代表两点的“服务器”和“客户”组成,基...
  • shanhe
  • shanhe
  • 2004年06月24日 10:01
  • 7929

VB Socket编程 框架

VB Socket编程(Winsock控件创建TCP/IP客户机/服务器程序) 好东西,所以收藏      Winsock控件建立在TCP、UDP协议的基础上,完成与远程计算机的通信。即使...
  • snlei
  • snlei
  • 2013年09月03日 13:50
  • 7726

VB真是想不到系列之三:VB指针葵花宝典之函数指针

《VB真是想不到系列》    每次看大师的东西到了精彩之处,我就会拍案叫绝:"哇噻,真是想不到!"。在经过很多次这种感慨之后,我发现只要我们动了脑筋,我们自己也能有让别人想不到的东西。于是想到要把这些...
  • AdamBear
  • AdamBear
  • 2002年03月15日 09:00
  • 5060

一个VB数组指针类

顾名思义了,这个代码就是将指定的内存地址绑定到一个VB数组,即COM中的SafeArray上。所谓绑定,是指在使用该内存地址之前,并不需要申请相应的本地内存缓冲区,也不需要进行内存复制,只是根据该地址...
  • lyserver
  • lyserver
  • 2008年09月30日 11:55
  • 3168

VB中如何传递指针给DLL

 VB默认的传递方式就是传指针(ByRef,也就是把变量的地址传递给所调用的函数)。如果要传值(也就是把变量的值传递给所调用的函数)反要在参数声明前加ByVal。所以如果你传递的参数是普通的数据类型,...
  • njstalk
  • njstalk
  • 2007年02月23日 19:22
  • 2406
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:VB指针,VB API编程
举报原因:
原因补充:

(最多只允许输入30个字)