这是一篇旧文档,以前学C++的时候无聊写的,尝试完全用系统API来创建一个Windows窗体。
哥哥有练过,好孩子不要学哦~
Public Class mainCls
Declare Function GetModuleHandleA Lib "kernel32" (ByVal lpModuleName As String) As Integer
Declare Function GetCommandLineA Lib "kernel32" () As String
Declare Function LoadIconA Lib "user32" (ByVal hInstance As Integer, ByVal lpIconName As String) As Integer
Declare Function LoadCursorA Lib "user32" (ByVal hInstance As Integer, ByVal lpCursorName As String) As Integer
Declare Function RegisterClassExA Lib "user32" (ByRef pcWndClassEx As WNDCLASSEX) As Integer
Declare Function CreateWindowExA Lib "user32" (ByVal dwExStyle As Integer, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hWndParent As Integer, ByVal hMenu As Integer, ByVal hInstance As Integer, ByVal lpParam As Integer) As Integer
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
Declare Function UpdateWindow Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function GetMessageA Lib "user32" (ByRef lpMsg As MSG, ByVal hwnd As Integer, ByVal wMsgFilterMin As Integer, ByVal wMsgFilterMax As Integer) As Integer
Declare Function TranslateMessage Lib "user32" (ByRef lpMsg As MSG) As Integer
Declare Function DispatchMessageA Lib "user32" (ByRef lpMsg As MSG) As Integer
Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Integer)
Declare Function DefWindowProcA Lib "user32" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Delegate Function DWndProc(ByVal hwnd As Integer, ByVal uMsg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Const WM_DESTROY = &H2
Const SW_SHOWNORMAL = 1
Const CW_USEDEFAULT = &H80000000
Const WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)
Const WS_OVERLAPPED = &H0
Const WS_CAPTION = &HC00000
Const WS_SYSMENU = &H80000
Const WS_THICKFRAME = &H40000
Const WS_MINIMIZEBOX = &H20000
Const WS_MAXIMIZEBOX = &H10000
Const IDC_ARROW = 32512
Const IDI_APPLICATION = 32512
Const COLOR_WINDOW = 5
Const CS_VREDRAW = &H1
Const CS_HREDRAW = &H2
Const SW_SHOWDEFAULT = 10
Const NULL = 0
Structure WNDCLASSEX
Dim cbSize As Integer
Dim style As Integer
Dim lpfnWndProc As DWndProc
Dim cbClsExtra As Integer
Dim cbWndExtra As Integer
Dim hInstance As Integer
Dim hIcon As Integer
Dim hCursor As Integer
Dim hbrBackground As Integer
Dim lpszMenuName As String
Dim lpszClassName As String
Dim hIconSm As Integer
End Structure
Structure MSG
Dim hwnd As Integer
Dim message As Integer
Dim wParam As Integer
Dim lParam As Integer
Dim time As Integer
Dim pt As Point
End Structure
' .NET程序入口
Shared Function Main(ByVal cls As String()) As Integer
' 获取主程序句柄
Dim hInstance As Integer = GetModuleHandleA(Nothing)
' 获取命令行句柄,只是告诉你方法,.NET已经封装在了程序入口内的参数中
' 如果你使用本方法,可以获得一个程序主模块地址+命令行参数的文本
Dim CommandLine As String = GetCommandLineA
' 开始执行自定义的主程序入口
Dim i = WinMain(hInstance, NULL, CommandLine, SW_SHOWDEFAULT)
Return i '退出代码
End Function
Shared Function WinMain(ByVal hInst As Integer, ByVal hPrevInst As Integer, ByVal CmdLine As String, ByVal CmdShow As Integer) As Integer
Dim wc As New WNDCLASSEX '窗体信息
Dim msg As New MSG '循环消息
Dim hwnd As Integer '窗体句柄
Dim ClassName As String = "mywindow_class" '注册类名
Dim TitleName As String = "Vb.Net Is Cool!" '窗体标签
' 开始详细定义窗体信息
With wc
.cbSize = Runtime.InteropServices.Marshal.SizeOf(GetType(WNDCLASSEX)) '获取结构的非托管内存大小
.style = CS_HREDRAW Or CS_VREDRAW '窗体显示风格
.lpfnWndProc = AddressOf WndProc '设置消息循环的委托方法
.cbClsExtra = Nothing
.cbWndExtra = Nothing
.hInstance = hInst
.hbrBackground = COLOR_WINDOW + 1 '背景色
.lpszMenuName = Nothing
.lpszClassName = ClassName
.hIcon = LoadIconA(NULL, IDI_APPLICATION) '载入图标
.hIconSm = .hIcon
.hCursor = LoadCursorA(NULL, IDC_ARROW) '载入指针
End With
RegisterClassExA(wc) '注册窗体
' 创建并获取窗体句柄
hwnd = CreateWindowExA(NULL, ClassName, TitleName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL)
ShowWindow(hwnd, SW_SHOWNORMAL) '显示窗体
UpdateWindow(hwnd) '更新窗体
' 开始无限循环
While GetMessageA(msg, NULL, 0, 0) <> 0
TranslateMessage(msg)
DispatchMessageA(msg)
End While
Return msg.wParam
End Function
' 这个函数处理窗体的消息
Shared Function WndProc(ByVal hwnd As Integer, ByVal uMsg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Dim i As Integer = 0
Select Case uMsg
Case WM_DESTROY
PostQuitMessage(NULL)
Case Else
i = DefWindowProcA(hwnd, uMsg, wParam, lParam)
End Select
Return i
End Function
End Class