Create a form using an API (similar to the creation process of VC)

zhaozj2021-02-17  53

The steps are the same in the VB to create forms and VCs, but use the IDE environment creation to encapsulate the process, now we create a form of API, and let us know the process of forming a form, let us Programmers using VB have more understanding of the system.

First, use C to create a form of form:

The entrance of the program: int apientry WinMain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NCMDSHOW)

Enter the structure of the structure WNDCLASSEX WCEX after entering.

Call the APIREGISTERCLASSEX, register form structure, if you call CREATEWINDOW, ShowWindow, UpdateWindow once again, and display it to the user, below the dead cycle of one processing message.

Then we can implement the same effect in VB according to this step:

In C , because the API declaration has been included in the header, it can be used directly, but in VB, it is necessary to declare the API, structure.

Public Declare Function RegisterClass Lib "user32" Alias ​​"RegisterClassA" (Class As WNDCLASS) As LongPublic Declare Function UnregisterClass Lib "user32" Alias ​​"UnregisterClassA" (ByVal lpClassName As String, ByVal hInstance As Long) As LongPublic Declare Function DefWindowProc Lib "user32" alias "DefWindowProcA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongPublic Declare Function GetMessage Lib "user32" alias "GetMessageA" (lpMsg As Msg, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As LongPublic Declare Function TranslateMessage Lib "user32" (lpMsg As Msg) As LongPublic Declare Function DispatchMessage Lib "user32" Alias ​​"DispatchMessageA" (lpMsg As Msg) As LongPublic Declare Function ShowWindow Lib "user32" ( ByVal hwnd As Long, ByVal nCmdShow As Long) As LongPublic Declare Function LoadCursor Lib "user32" Alias ​​"LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Any) As LongPublic Declare Functi on LoadIcon Lib "user32" Alias ​​"LoadIconA" (ByVal hInstance As Long, ByVal lpIconName As String) As LongPublic 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 LongPublic 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 LongPublic Declare Function SetWindowLong Lib "user32" Alias ​​"SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPublic Declare Function GetWindowLong Lib "user32" Alias ​​"GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As LongPublic Declare Function MessageBox Lib "user32" Alias ​​"MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As LongPublic 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 tnd type

Public Type Pointapi X as long y as longed type

Public Type Msg HWND As Long Message As Long WParam As Long LPARAM As Long Time As Long Pt As Pointapiend Type

Public const cs_vredraw = & h1public const cs_hredraw = & h2

Public const CW_USEDEFAULT = & h80000000

Public const ES_MULTILINE = & H4 &

Public Const WS_BORDER = & H800000Public Const WS_CHILD = & H40000000Public Const WS_OVERLAPPED = & H0 & Public Const WS_CAPTION = & HC00000 'WS_BORDER Or WS_DLGFRAMEPublic Const WS_SYSMENU = & H80000Public Const WS_THICKFRAME = & H40000Public Const WS_MINIMIZEBOX = & H20000Public Const WS_MAXIMIZEBOX = & H10000Public 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 = & h2public const wm_lbuttondown = & h201public 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 & H30 &

Declare a few variables we need, constant:

Public const gclassname = "myclassname" public const gappname = "my window capen"

PUBLIC GBUTOLDPROC AS Long Public GHWND As Long, GButtonhWnd As Long, GedithWnd As Long

Entrance function:

Sub main

code show as below:

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 ifcall 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 = loading (0 &, IDC_ARROW) '' default arrow wc.hbrbackground = color_window '' Default a color for window. wc.lpszclassname = GClassName $

RegisterWindowClass = RegisterClass (WC) <> 0 end functionpublic function creationWindows () as boolean '' starts creation form

Main form. GHwnd & = CreateWindowEx (0 &, gClassName $, gAppName $, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 208, 150, 0 &, 0 &, App.hInstance, ByVal 0 &) '' to create a button gButtonHwnd & = CreateWindowEx (0 &, "Button "," Click Here ", WS_CHILD, 58, 90, 85, 25, gHwnd &, 0 &, App.hInstance, 0 &) '' to create a (WS_EX_CLIENTEDGE, ES_MULTILINE style TextBox gEditHwnd & = CreateWindowEx (WS_EX_CLIENTEDGE," Edit "," This IS The Edit Control. "& Vbcrf &" As You Can See, It's Multiline. ", WS_CHILD OR ES_MULTILINE, 0 &, 0 &, 200, 80, GHWND &, 0 &, App.hinstance, 0 &)" Button "," Edit "system I have already registered it, so I have been in order to use the creation. Otherwise, it is hidden.

Call showwindow (ghwnd &, sw_shownormal) Call showWindow (GButtonhWnd &, Sw_Shownormal) Call showWindow (GedithWnd &, Sw_Shownormal)

Write down the button to handle the current address of the fault

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.

Point to new processing address CALL SETWINDOWLONG (GButtonhWnd &, GWL_WndProc, Getdress (Addressof ButtonWndProc)

CreateWindows = (GHWND & <> 0) End Function

'The main function of the form, when registered this form, the 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

Processing the message, here fingeting the WM_DESTROY message

Select Case uMsg & Case WM_DESTROY: '' Since DefWindowProc does not 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

A Button processing process is added

Public Function ButtonWndProc (Byval Hwnd 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 GetDress (Byval LNGADDR AS Long) As Long Getdress = LNGADDR & END FUNCTION

A complete simple app is generated above. We don't need an IDE environment or you can create a form of style we want. Through this example, it has some understanding of the Mechanism of the VB programmer.

Although this example is not actually very significant, we think it is useful, let us know some of the facts behind packaging, so that our ideas are free.

Finally, I want to declare this idea is not what I think. I refer to a code (the specific source of omissions), I want to share with the VB programmer, in fact, as long as I use live VB, there is no so much restrictions, the key Whether the user can jump out of this category.

转载请注明原文地址:https://www.9cbs.com/read-29436.html

New Post(0)