Public Const MB_ICONEXCLAMATION = &H30&
声明几个我们需要的变量、常量:
Public Const gClassName = "MyClassName"
Public Const gAppName = "My Window Caption"
Public gButOldProc As Long
Public gHwnd As Long, gButtonHwnd As Long, gEditHwnd As Long
入口函数:
Sub Main
代码如下:
Public Sub Main()
Dim wMsg As Msg
''Call procedure to register window classname. If false, then exit.
If RegisterWindowClass = False Then Exit Sub
''Create window
If CreateWindows Then
''Loop will exit when WM_QUIT is sent to the window.
Do While GetMessage(wMsg, 0&, 0&, 0&)
''TranslateMessage takes keyboard messages and converts
''them to WM_CHAR for easier processing.
Call TranslateMessage(wMsg)
''Dispatchmessage calls the default window procedure
''to process the window message. (WndProc)
Call DispatchMessage(wMsg)
Loop
End If
Call UnregisterClass(gClassName$, App.hInstance)
End Sub
Public Function RegisterWindowClass() As Boolean
Dim wc As WNDCLASS
wc.style = CS_HREDRAW Or CS_VREDRAW
wc.lpfnwndproc = GetAddress(AddressOf WndProc) ''Address in memory of default window procedure.
wc.hInstance = App.hInstance
wc.hIcon = LoadIcon(0&, IDI_APPLICATION) ''Default application icon
wc.hCursor = LoadCursor(0&, IDC_ARROW) ''Default arrow
wc.hbrBackground = COLOR_WINDOW ''Default a color for window.
wc.lpszClassName = gClassName$
RegisterWindowClass = RegisterClass(wc) <> 0
End Function
Public Function CreateWindows() As Boolean
''开始创建窗体
主窗体.
gHwnd& = CreateWindowEx(0&, gClassName$, gAppName$, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 208, 150, 0&, 0&, App.hInstance, ByVal 0&)
''创建一个按钮
gButtonHwnd& = CreateWindowEx(0&, "Button", "Click Here", WS_CHILD, 58, 90, 85, 25, gHwnd&, 0&, App.hInstance, 0&)
''创建一个(WS_EX_CLIENTEDGE、ES_MULTILINE风格的TextBox
gEditHwnd& = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "This is the edit control." & vbCrLf & "As you can see, it's multiline.", WS_CHILD Or ES_MULTILINE, 0&, 0&, 200, 80, gHwnd&, 0&, App.hInstance, 0&)
"Button ","Edit"系统中已经注册过了所以这里直接用
创建完别忘了显示出来否则是隐藏的
Call ShowWindow(gHwnd&, SW_SHOWNORMAL)
Call ShowWindow(gButtonHwnd&, SW_SHOWNORMAL)
Call ShowWindow(gEditHwnd&, SW_SHOWNORMAL)
记下按钮处理过错的当前所在地址
gButOldProc& = GetWindowLong(gButtonHwnd&, GWL_WNDPROC)
''Set default window procedure of button to ButtonWndProc. Different
''settings of windows is listed in the MSDN Library. We are using GWL_WNDPROC
''to set the address of the window procedure.
指向新的处理过程地址
Call SetWindowLong(gButtonHwnd&, GWL_WNDPROC, GetAddress(AddressOf ButtonWndProc))
CreateWindows = (gHwnd& <> 0)
End Function
'窗体运行的主函数,在注册这个窗体时已经指定的
Public Function WndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim strTemp As String
处理消息,这里指处理了WM_DESTROY消息
Select Case uMsg&
Case WM_DESTROY:
''Since DefWindowProc doesn't automatically call
''PostQuitMessage (WM_QUIT). We need to do it ourselves.
来源:www.upschool.com.cn
作者:szyicol
关键字:VB,API创建窗体
发表日期:2007-2-9 17:12:21
网页显示有限 阅读全文请下载本文完整版WORD文档
上一篇:VB 列出SQL SERVER数据库中所有表及字段信息 下一篇:
共3页
9 7 [
1] [
2] [
3]
8 :>