【API】VB用API编写Win32图形界面程序的方法(别学我)
<meta http-equiv="Content-Type" content="text/html; charset=GBK"><title>VB用API编写Win32图形界面程序的方法</title>
<style type="text/css">
span {
font-family: '新宋体';
font-size: 9pt;
color: #000000;
}
.sc0 {
}
.sc1 {
color: #008000;
}
.sc2 {
}
.sc3 {
color: #000080;
}
.sc4 {
}
.sc6 {
}
.sc7 {
}
</style>
<div style="float: left; white-space: pre; line-height: 1; background: #FFFFFF; "><span class="sc3">Option</span><span class="sc0"> </span><span class="sc3">Explicit</span><span class="sc0"> </span><span class="sc1">'保存为bas文件
</span><span class="sc0">
</span><span class="sc1">'VB完全可以不这么来的,因为VB本身就是这些API封装好了的语言。
'当然我这样写其实是为了演示Windows程序如何通过API进行窗口创建和消息循环。
'这也证明了VB在这方面的实力。
'从VB6开始,VB的程序不再是解释执行的了。
</span><span class="sc0">
</span><span class="sc1">'结构体定义
</span><span class="sc3">Type</span><span class="sc0"> </span><span class="sc7">WNDCLASSEX</span><span class="sc0">
</span><span class="sc7">cbSize</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">style</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">lpfnWndProc</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">cbClsExtra</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">cbWndExtra</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">hInstance</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">hIcon</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">hCursor</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">hbrBackground</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">lpszMenuName</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">String</span><span class="sc0">
</span><span class="sc7">lpszClassName</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">String</span><span class="sc0">
</span><span class="sc7">hIconSm</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">End</span><span class="sc0"> </span><span class="sc3">Type</span><span class="sc0">
</span><span class="sc3">Type</span><span class="sc0"> </span><span class="sc7">POINTAPI</span><span class="sc0">
</span><span class="sc7">X</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">Y</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">End</span><span class="sc0"> </span><span class="sc3">Type</span><span class="sc0">
</span><span class="sc3">Type</span><span class="sc0"> </span><span class="sc7">Msg</span><span class="sc0">
</span><span class="sc7">hWnd</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">Message</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">wParam</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">lParam</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">time</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">pt</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc7">POINTAPI</span><span class="sc0">
</span><span class="sc3">End</span><span class="sc0"> </span><span class="sc3">Type</span><span class="sc0">
</span><span class="sc1">'API声明
</span><span class="sc3">Declare</span><span class="sc0"> </span><span class="sc3">Function</span><span class="sc0"> </span><span class="sc7">LoadCursor</span><span class="sc0"> </span><span class="sc3">Lib</span><span class="sc0"> </span><span class="sc4">"user32"</span><span class="sc0"> </span><span class="sc3">Alias</span><span class="sc0"> </span><span class="sc4">"LoadCursorA"</span><span class="sc0"> </span><span class="sc6">(</span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">hInstance</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">lpCursorName</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc7">Any</span><span class="sc6">)</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">Declare</span><span class="sc0"> </span><span class="sc3">Function</span><span class="sc0"> </span><span class="sc7">RegisterClassEx</span><span class="sc0"> </span><span class="sc3">Lib</span><span class="sc0"> </span><span class="sc4">"user32"</span><span class="sc0"> </span><span class="sc3">Alias</span><span class="sc0"> </span><span class="sc4">"RegisterClassExA"</span><span class="sc0"> </span><span class="sc6">(</span><span class="sc7">pcWndClassEx</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc7">WNDCLASSEX</span><span class="sc6">)</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">Declare</span><span class="sc0"> </span><span class="sc3">Function</span><span class="sc0"> </span><span class="sc7">CreateWindowEx</span><span class="sc0"> </span><span class="sc3">Lib</span><span class="sc0"> </span><span class="sc4">"user32"</span><span class="sc0"> </span><span class="sc3">Alias</span><span class="sc0"> </span><span class="sc4">"CreateWindowExA"</span><span class="sc0"> </span><span class="sc6">(</span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">dwExStyle</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">lpClassName</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc7">Any</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">lpWindowName</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">String</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">dwStyle</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">X</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">Y</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">nWidth</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">nHeight</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">hWndParent</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">hMenu</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">hInstance</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">lpParam</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc7">Any</span><span class="sc6">)</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">Declare</span><span class="sc0"> </span><span class="sc3">Function</span><span class="sc0"> </span><span class="sc7">ShowWindow</span><span class="sc0"> </span><span class="sc3">Lib</span><span class="sc0"> </span><span class="sc4">"user32"</span><span class="sc0"> </span><span class="sc6">(</span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">hWnd</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">nCmdShow</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">)</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">Declare</span><span class="sc0"> </span><span class="sc3">Function</span><span class="sc0"> </span><span class="sc7">GetMessage</span><span class="sc0"> </span><span class="sc3">Lib</span><span class="sc0"> </span><span class="sc4">"user32"</span><span class="sc0"> </span><span class="sc3">Alias</span><span class="sc0"> </span><span class="sc4">"GetMessageA"</span><span class="sc0"> </span><span class="sc6">(</span><span class="sc7">lpMsg</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc7">Msg</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">hWnd</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">wMsgFilterMin</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">wMsgFilterMax</span><span class="sc0"> <span style="color: rgb(0, 0, 128); font-size: 12px;">As</span> </span><span class="sc3">Long</span><span class="sc6">)</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">Declare</span><span class="sc0"> </span><span class="sc3">Function</span><span class="sc0"> </span><span class="sc7">TranslateMessage</span><span class="sc0"> </span><span class="sc3">Lib</span><span class="sc0"> </span><span class="sc4">"user32"</span><span class="sc0"> </span><span class="sc6">(</span><span class="sc7">lpMsg</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc7">Msg</span><span class="sc6">)</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">Declare</span><span class="sc0"> </span><span class="sc3">Function</span><span class="sc0"> </span><span class="sc7">DispatchMessage</span><span class="sc0"> </span><span class="sc3">Lib</span><span class="sc0"> </span><span class="sc4">"user32"</span><span class="sc0"> </span><span class="sc3">Alias</span><span class="sc0"> </span><span class="sc4">"DispatchMessageA"</span><span class="sc0"> </span><span class="sc6">(</span><span class="sc7">lpMsg</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc7">Msg</span><span class="sc6">)</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">Declare</span><span class="sc0"> </span><span class="sc3">Function</span><span class="sc0"> </span><span class="sc7">UnregisterClass</span><span class="sc0"> </span><span class="sc3">Lib</span><span class="sc0"> </span><span class="sc4">"user32"</span><span class="sc0"> </span><span class="sc3">Alias</span><span class="sc0"> </span><span class="sc4">"UnregisterClassA"</span><span class="sc0"> </span><span class="sc6">(</span><span class="sc7">lpClassName</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc7">Any</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">hInstance</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">)</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">Declare</span><span class="sc0"> </span><span class="sc3">Function</span><span class="sc0"> </span><span class="sc7">UpdateWindow</span><span class="sc0"> </span><span class="sc3">Lib</span><span class="sc0"> </span><span class="sc4">"user32"</span><span class="sc0"> </span><span class="sc6">(</span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">hWnd</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">)</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">Declare</span><span class="sc0"> </span><span class="sc3">Function</span><span class="sc0"> </span><span class="sc7">DefWindowProc</span><span class="sc0"> </span><span class="sc3">Lib</span><span class="sc0"> </span><span class="sc4">"user32"</span><span class="sc0"> </span><span class="sc3">Alias</span><span class="sc0"> </span><span class="sc4">"DefWindowProcA"</span><span class="sc0"> </span><span class="sc6">(</span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">hWnd</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">wMsg</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">wParam</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">lParam</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">)</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">Declare</span><span class="sc0"> </span><span class="sc3">Sub</span><span class="sc0"> </span><span class="sc7">PostQuitMessage</span><span class="sc0"> </span><span class="sc3">Lib</span><span class="sc0"> </span><span class="sc4">"user32"</span><span class="sc0"> </span><span class="sc6">(</span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">nExitCode</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">)</span><span class="sc0">
</span><span class="sc1">'常数定义
</span><span class="sc3">Global</span><span class="sc0"> </span><span class="sc3">Const</span><span class="sc0"> </span><span class="sc7">WM_DESTROY</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc2">&H2</span><span class="sc0">
</span><span class="sc3">Global</span><span class="sc0"> </span><span class="sc3">Const</span><span class="sc0"> </span><span class="sc7">IDC_ARROW</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc2">32512</span><span class="sc0">
</span><span class="sc3">Global</span><span class="sc0"> </span><span class="sc3">Const</span><span class="sc0"> </span><span class="sc7">CW_USEDEFAULT</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc2">&H80000000</span><span class="sc0">
</span><span class="sc3">Global</span><span class="sc0"> </span><span class="sc3">Const</span><span class="sc0"> </span><span class="sc7">WS_CAPTION</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc2">&HC00000</span><span class="sc0">
</span><span class="sc3">Global</span><span class="sc0"> </span><span class="sc3">Const</span><span class="sc0"> </span><span class="sc7">WS_SYSMENU</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc2">&H80000</span><span class="sc0">
</span><span class="sc3">Global</span><span class="sc0"> </span><span class="sc3">Const</span><span class="sc0"> </span><span class="sc7">WS_THICKFRAME</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc2">&H40000</span><span class="sc0">
</span><span class="sc3">Global</span><span class="sc0"> </span><span class="sc3">Const</span><span class="sc0"> </span><span class="sc7">WS_MINIMIZEBOX</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc2">&H20000</span><span class="sc0">
</span><span class="sc3">Global</span><span class="sc0"> </span><span class="sc3">Const</span><span class="sc0"> </span><span class="sc7">WS_MAXIMIZEBOX</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc2">&H10000</span><span class="sc0">
</span><span class="sc3">Global</span><span class="sc0"> </span><span class="sc3">Const</span><span class="sc0"> </span><span class="sc7">WS_VISIBLE</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc2">&H10000000</span><span class="sc0">
</span><span class="sc3">Global</span><span class="sc0"> </span><span class="sc3">Const</span><span class="sc0"> </span><span class="sc7">WS_OVERLAPPEDWINDOW</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc7">WS_VISIBLE</span><span class="sc0"> </span><span class="sc3">Or</span><span class="sc0"> </span><span class="sc7">WS_CAPTION</span><span class="sc0"> </span><span class="sc3">Or</span><span class="sc0"> </span><span class="sc7">WS_SYSMENU</span><span class="sc0"> </span><span class="sc3">Or</span><span class="sc0"> </span><span class="sc7">WS_THICKFRAME</span><span class="sc0"> </span><span class="sc3">Or</span><span class="sc0"> </span><span class="sc7">WS_MINIMIZEBOX</span><span class="sc0"> </span><span class="sc3">Or</span><span class="sc0"> </span><span class="sc7">WS_MAXIMIZEBOX</span><span class="sc0">
</span><span class="sc3">Global</span><span class="sc0"> </span><span class="sc3">Const</span><span class="sc0"> </span><span class="sc7">SW_SHOWNORMAL</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc2">1</span><span class="sc0">
</span><span class="sc1">'定义变量
</span><span class="sc3">Global</span><span class="sc0"> </span><span class="sc7">g_WCEx</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc7">WNDCLASSEX</span><span class="sc0">
</span><span class="sc3">Global</span><span class="sc0"> </span><span class="sc7">g_ClassAtom</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">Global</span><span class="sc0"> </span><span class="sc7">g_hWnd</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">Sub</span><span class="sc0"> </span><span class="sc7">Main</span><span class="sc6">()</span><span class="sc0">
</span><span class="sc3">With</span><span class="sc0"> </span><span class="sc7">g_WCEx</span><span class="sc0">
</span><span class="sc6">.</span><span class="sc7">cbSize</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc7">LenB</span><span class="sc6">(</span><span class="sc7">g_WCEx</span><span class="sc6">)</span><span class="sc0">
</span><span class="sc6">.</span><span class="sc7">lpfnWndProc</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc7">GetAddressOfFunction</span><span class="sc6">(</span><span class="sc3">AddressOf</span><span class="sc0"> </span><span class="sc7">WndProc</span><span class="sc6">)</span><span class="sc0">
</span><span class="sc6">.</span><span class="sc7">hInstance</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc7">App.hInstance</span><span class="sc0">
</span><span class="sc6">.</span><span class="sc7">hCursor</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc7">LoadCursor</span><span class="sc6">(</span><span class="sc2">0</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">IDC_ARROW</span><span class="sc6">)</span><span class="sc0"> </span><span class="sc1">'默认光标
</span><span class="sc0"> </span><span class="sc6">.</span><span class="sc7">hbrBackground</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc6">(</span><span class="sc7">vbButtonFace</span><span class="sc0"> </span><span class="sc3">And</span><span class="sc0"> </span><span class="sc2">&H7FFFFFFF</span><span class="sc6">)</span><span class="sc0"> </span><span class="sc6">+</span><span class="sc0"> </span><span class="sc2">1</span><span class="sc0"> </span><span class="sc1">'去掉最高位就是COLOR_BTNFACE了
</span><span class="sc0"> </span><span class="sc6">.</span><span class="sc7">lpszClassName</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc4">"VBWIN32APPBYUSINGAPI"</span><span class="sc0"> </span><span class="sc1">'翻译成中文就是“用API的VB的Win32的应用程序”
</span><span class="sc3">End</span><span class="sc0"> </span><span class="sc3">With</span><span class="sc0">
</span><span class="sc7">g_ClassAtom</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc7">RegisterClassEx</span><span class="sc6">(</span><span class="sc7">g_WCEx</span><span class="sc6">)</span><span class="sc0">
</span><span class="sc3">If</span><span class="sc0"> </span><span class="sc7">g_ClassAtom</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc2">0</span><span class="sc0"> </span><span class="sc3">Then</span><span class="sc0">
</span><span class="sc7">MsgBox</span><span class="sc0"> </span><span class="sc4">"注册窗口类失败。"</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">vbExclamation</span><span class="sc0">
</span><span class="sc3">Exit</span><span class="sc0"> </span><span class="sc3">Sub</span><span class="sc0">
</span><span class="sc3">End</span><span class="sc0"> </span><span class="sc3">If</span><span class="sc0">
</span><span class="sc7">g_hWnd</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc7">CreateWindowEx</span><span class="sc6">(</span><span class="sc2">0</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">g_ClassAtom</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc4">"VB的API窗口"</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">WS_OVERLAPPEDWINDOW</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">CW_USEDEFAULT</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">CW_USEDEFAULT</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc2">888</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc2">666</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc2">0</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc2">0</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">App.hInstance</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc2">0</span><span class="sc6">)</span><span class="sc0">
</span><span class="sc3">If</span><span class="sc0"> </span><span class="sc7">g_hWnd</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc2">0</span><span class="sc0"> </span><span class="sc3">Then</span><span class="sc0">
</span><span class="sc7">MsgBox</span><span class="sc0"> </span><span class="sc4">"创建窗口失败。"</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">vbExclamation</span></div><div style="float: left; white-space: pre; line-height: 1; background: #FFFFFF; "><span class="sc0"> </span><span class="sc7">UnregisterClass</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">g_ClassAtom</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">App.hInstance</span><span class="sc0">
</span><span class="sc3">Exit</span><span class="sc0"> </span><span class="sc3">Sub</span><span class="sc0">
</span><span class="sc3">End</span><span class="sc0"> </span><span class="sc3">If</span><span class="sc0">
</span><span class="sc7">ShowWindow</span><span class="sc0"> </span><span class="sc7">g_hWnd</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">SW_SHOWNORMAL</span><span class="sc0">
</span><span class="sc7">UpdateWindow</span><span class="sc0"> </span><span class="sc7">g_hWnd</span><span class="sc0">
</span><span class="sc3">Dim</span><span class="sc0"> </span><span class="sc7">Message</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc7">Msg</span><span class="sc0">
</span><span class="sc3">Do</span><span class="sc0"> </span><span class="sc3">While</span><span class="sc0"> </span><span class="sc7">GetMessage</span><span class="sc6">(</span><span class="sc7">Message</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc2">0</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc2">0</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc2">0</span><span class="sc6">)</span><span class="sc0">
</span><span class="sc7">TranslateMessage</span><span class="sc0"> </span><span class="sc7">Message</span><span class="sc0">
</span><span class="sc7">DispatchMessage</span><span class="sc0"> </span><span class="sc7">Message</span><span class="sc0">
</span><span class="sc3">Loop</span><span class="sc0">
</span><span class="sc7">UnregisterClass</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">g_ClassAtom</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">App.hInstance</span><span class="sc0">
</span><span class="sc3">End</span><span class="sc0"> </span><span class="sc3">Sub</span><span class="sc0">
</span><span class="sc3">Function</span><span class="sc0"> </span><span class="sc7">WndProc</span><span class="sc6">(</span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">hWnd</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">wMsg</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">WP</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">LP</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">)</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc3">Select</span><span class="sc0"> </span><span class="sc3">Case</span><span class="sc0"> </span><span class="sc7">wMsg</span><span class="sc0">
</span><span class="sc3">Case</span><span class="sc0"> </span><span class="sc7">WM_DESTROY</span><span class="sc0">
</span><span class="sc7">PostQuitMessage</span><span class="sc0"> </span><span class="sc2">0</span><span class="sc0">
</span><span class="sc3">Case</span><span class="sc0"> </span><span class="sc3">Else</span><span class="sc0">
</span><span class="sc7">WndProc</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc7">DefWindowProc</span><span class="sc6">(</span><span class="sc7">hWnd</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">wMsg</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">WP</span><span class="sc6">,</span><span class="sc0"> </span><span class="sc7">LP</span><span class="sc6">)</span><span class="sc0">
</span><span class="sc3">End</span><span class="sc0"> </span><span class="sc3">Select</span><span class="sc0">
</span><span class="sc3">End</span><span class="sc0"> </span><span class="sc3">Function</span><span class="sc0">
</span><span class="sc3">Function</span><span class="sc0"> </span><span class="sc7">GetAddressOfFunction</span><span class="sc6">(</span><span class="sc3">ByVal</span><span class="sc0"> </span><span class="sc7">Value</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc6">)</span><span class="sc0"> </span><span class="sc3">As</span><span class="sc0"> </span><span class="sc3">Long</span><span class="sc0">
</span><span class="sc7">GetAddressOfFunction</span><span class="sc0"> </span><span class="sc6">=</span><span class="sc0"> </span><span class="sc7">Value</span><span class="sc0">
</span><span class="sc3">End</span><span class="sc0"> </span><span class="sc3">Function</span><span class="sc0">
</span></div>
我运行,怎么会死机啊? 东东哥 发表于 2021-10-3 18:46
我运行,怎么会死机啊?
你不能直接运行,你要编译为exe来运行。另外,这份代码本身仅作为API调用创建窗体的示范,相当于告诉你如果不使用VB6的界面编辑器,你依然可以进行Win32图形界面开发。请了解。 感谢大神,在此学习了。 谢谢分享,一会下载测试!
页:
[1]