使用API创建窗体(类似VC的创建过程)

原创 2003年06月30日 13:54:00

在VB中用API创建窗体和VC中的步骤是一样的,只不过用IDE环境创建是把过程都封装起来,现在我们用API方式创建,大致让我们了解一个窗体的产生过程,让我们使用VB的程序员对系统的机制多一些了解.

先所以下用C++创建窗体的过程:

程序的入口:int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)

进入后先初始化结构WNDCLASSEX wcex;

调用APIRegisterClassEx,注册窗体结构,如果成功一次调用CreateWindow、ShowWindow、UpdateWindow这样一个主窗体就成功的创建并显示给用户,下面就缺少一个处理消息的死循环。

那么我们我们可以按照这个步骤在VB中实现一样的效果:

在C++因为API声明已经被包涵到头文件所以直接用就可以,但是在VB中就要逐个声明一下用到的API,结构。

Public Declare Function RegisterClass Lib "user32" Alias "RegisterClassA" (Class As WNDCLASS) As Long
Public Declare Function UnregisterClass Lib "user32" Alias "UnregisterClassA" (ByVal lpClassName As String, ByVal hInstance 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 GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As Msg, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
Public Declare Function TranslateMessage Lib "user32" (lpMsg As Msg) As Long
Public Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As Msg) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Any) As Long
Public Declare Function LoadIcon Lib "user32" Alias "LoadIconA" (ByVal hInstance As Long, ByVal lpIconName As String) As Long
Public Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, 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 CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
Public Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)


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 String
    lpszClassName As String
End Type


Public Type POINTAPI
    x As Long
    y As Long
End Type


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

Public Const CS_VREDRAW = &H1
Public Const CS_HREDRAW = &H2

Public Const CW_USEDEFAULT = &H80000000

Public Const ES_MULTILINE = &H4&

Public Const WS_BORDER = &H800000
Public Const WS_CHILD = &H40000000
Public Const WS_OVERLAPPED = &H0&
Public Const WS_CAPTION = &HC00000 ' WS_BORDER Or WS_DLGFRAME
Public Const WS_SYSMENU = &H80000
Public Const WS_THICKFRAME = &H40000
Public Const WS_MINIMIZEBOX = &H20000
Public Const WS_MAXIMIZEBOX = &H10000
Public Const WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)

Public Const WS_EX_CLIENTEDGE = &H200&

Public Const COLOR_WINDOW = 5

Public Const WM_DESTROY = &H2
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202

Public Const IDC_ARROW = 32512&

Public Const IDI_APPLICATION = 32512&

Public Const GWL_WNDPROC = (-4)

Public Const SW_SHOWNORMAL = 1

Public Const MB_OK = &H0&
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.
          ''You can use DestroyWindow to get rid of the window manually.
          Call PostQuitMessage(0&)
    End Select
   

  ''Let windows call the default window procedure since we're done.
  WndProc = DefWindowProc(hwnd&, uMsg&, wParam&, lParam&)

End Function

又添加了一个Button的处理过程

Public Function ButtonWndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    Select Case uMsg&
       Case WM_LBUTTONUP:
          Call MessageBox(gHwnd&, "You clicked the button!", App.Title, MB_OK Or MB_ICONEXCLAMATION)
    End Select
   
  ButtonWndProc = CallWindowProc(gButOldProc&, hwnd&, uMsg&, wParam&, lParam&)
  
End Function


Public Function GetAddress(ByVal lngAddr As Long) As Long
    GetAddress = lngAddr&
End Function

以上一个完整的简单的应用程序就产生了。我们不需要IDE环境也可以创建我们想要的风格的窗体。通过这个例子对于VB程序员是不是对系统的机制有了一些了解。

虽然这个例子实际意义并不大,但是我们认为很有用,让我们能了解一些封装背后隐藏的事实,使我们的思路很自由。

最后要声明这个想法并不是我一人想出来的,我参考一段代码(具体出处不祥)说想到,想与VB程序员共同分享一下,其实只要用活VB并没有我们所说的那么多限制,关键在使用者能否跳出这个范畴。

版权声明:本文为博主原创文章,未经博主允许不得转载。

VC创建不规则窗口

创建不规则窗口无外乎两个API:Set
  • mfcing
  • mfcing
  • 2014年06月20日 23:38
  • 3236

Windows SDK实现不规则窗体

1.原理 所有的 Windows 窗体都位于一个称为“region”中,窗体的大小如果超出“region”的范围,windows 会自动裁剪超出"region"范围那部分的窗体,使其不可见。所以,要创...
  • celte
  • celte
  • 2013年08月29日 22:34
  • 1530

Windows API 编程之创建一个windows窗口

windows API函数 创建一个windows窗口
  • IT_li_wenshun
  • IT_li_wenshun
  • 2016年10月08日 21:04
  • 2365

linux进程创造 - 创建进程API及过程

1. 创建进程函数API1.1 创建进程fork()fork的翻译为“叉子,分叉”,其实在unix编程中,我们来创建进程的时候是深有体会的,感觉创建一个进程就像是走到了一个岔路口,父进程和子进程在叉路...
  • l289123557
  • l289123557
  • 2016年11月20日 00:56
  • 765

VC常用代码之创建进程

作者:朱金灿来源:http://blog.csdn.net/clever101            创建进程是编程开发的常用操作。Windows中的创建进程采用API函数CreateProcess实...
  • clever101
  • clever101
  • 2015年10月31日 23:53
  • 8768

mfc学习之主框架窗口创建过程

在上一篇追踪了mfc程序流程之后回到了自己所写的初始化函数 BOOL CMyWinApp::InitInstance() { CMyFrameWnd * pFrame = new CMyFrame...
  • zhengjuexi4456
  • zhengjuexi4456
  • 2016年07月06日 21:59
  • 1774

VC中控制台程序如何创建窗口

转载出至:http://blog.chinaunix.net/uid-13614124-id-3747923.html 在VC控制台程序中调用D3DVideoRender时,需要创建新的窗口控件显示图...
  • jobsxiao
  • jobsxiao
  • 2016年09月24日 17:18
  • 1186

delphi 窗体的创建和释放

http://www.cnblogs.com/chenmfly/p/4770490.html delphi 窗体的创建和释放 Delphi中的窗体分为模式窗体和无模式窗体。二者...
  • y13156556538
  • y13156556538
  • 2016年10月21日 10:09
  • 3033

Android中窗口的创建过程

在阅读本文之前建议先阅读我的另外一篇Java层Binder机制详解,因为里面有些地方涉及到通过进程间通过Binder机制通信。 在Android将窗口分为三类:应用窗口,子窗口,系统窗口 应用窗口一...
  • yuanzeyao2008
  • yuanzeyao2008
  • 2013年10月23日 22:01
  • 6599

Qt创建窗体的过程

来自:http://blog.csdn.net/tingsking18/article/details/5528666 前言:分析Qt的代码也有一段时间了,以前在进行QT源码解析的时候总是使用ue,...
  • mathlmx
  • mathlmx
  • 2013年01月04日 00:20
  • 298
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:使用API创建窗体(类似VC的创建过程)
举报原因:
原因补充:

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