Use VB 6.0 itself to achieve interception of Windows messages!

zhaozj2021-02-08  221

As we all know, VB's function does not have VC . The full-featured development platform such as Delphi is strong, but she is also enough to complete our work, as long as you start the brain, I dare to do it, we can let VB play the biggest effect, make Many amazing software. Develop high difficulties, not just VC and Delphi's patents!

In the past, VB could not be customized to intercept Windows, which can only be programmed by several limited events provided by VB itself, which is very limited. Missing message capture, it is considered to not support the callback function mechanism (mainly because VB does not have a pointer, more can't talk about the function pointer), which causes a large limit of VB programming. In fact, VB can use other ways to achieve this mechanism. The AddressOf operator is provided from VB 5.0, which can obtain the address of the VB custom function using this operator. With a function address, you can use a callback function. Of course, VB still cannot implement address delivery between VB functions, she only supports VB functions to DLL function to boycott. However, this is enough. Below this program, this method is to use this method. There is only one main form in the program, allowing the main form without a border, no title bar, cannot change the size, cannot be hosted by the title bar. However, by intercepting the Windows message, it can cause the mouse to host any position in the form, just like holding down the title bar. This program does not use any additional controls, all using VB code. Note, add a common module to declare some functions and constants. The following code passes in VB 6.0.

'===================================== this is the code attribute vb_name = "module1" option in public modules Explicit

Public const wm_nchittest = & h84public const vk_lbutton = & h1public const htcaption = 2PUBLIC const htclient = 1

Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As IntegerPublic 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 Nindex As Long, Byval Dwnewlong As Long) As long

Public const GWL_WndProc = -4Global LPPREVWNDPROC AS Longglobal GHW as long 'here is a key, I have customized a window function (callback function) to replace the VB form its own default window function. What is the 'window function? It is responsible for handling messages sent by Windows, and filtering, filtering out what is interested in ', mapping becomes an event for us. Each window in VB has a default window function, we can't see it. 'There are many messages being filtered by the default window function of VB. Friends who know the C / C / Delphi program should know these.

Function WindowProc (Byval HW As Long, Byval Umsg As Long, Byval WParam As Long, Byval LParam As Long) As long 'We also make message filtering, but we refer to news that we are interested in' other news we are too lazy to handle, hand it over VB default window function destabled. SELECT CASE UMSG CASE WM_NCHITTEST 'Intercepts WM_NCHITTEST message if getasynckeystate (vk_lbutton) <0 Then' Is there a mouse button in the form client area? 'If yes, the function return value is set to htcaption, deceive Windows, let it think that the mouse is in the title bar' Windows is the return value of the return value by the window function to determine the WindowProc = HTCAPTION EXIT FUNCTION ELSE 'other of the other, or How to make the rules of the rules, how is WindowProc = htclient exit function end if End select 'Here is the key, because other news we don't care, we don't deal with, so you must handle the VB's default handler' lpprevwndproc is actually A function pointer, pointing to the VB default window function WindowProc = CallWindowProc (LPPREVWNDPROC, HW, UMSG, WPARAM, LPARAM) End Function

'============================================================================================================================================================================================================= 0000-00 BorderStyle = 0 'None ClientHeight = 3195 ClientLeft = 0 ClientTop = 0 ClientWidth = 4680 LinkTopic = "Form1" MaxButton = 0' False MinButton = 0 'False ScaleHeight = 3195 ScaleWidth = 4680 ShowInTaskbar = 0' False StartUpPosition = 3 'default window Begin VB.CommandButton Command1 Caption = "Command1" Height = 375 Left = 2160 TabIndex = 0 Top = 720 Width = 1575 EndEndAttribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = FalsePrivate Sub Command1_Click () Unload Me 'Press this button to exit End Sub

Private Sub Form_Load () GHW = Me.hwnd 'Save the Handle' The following is the key, complete two work: 1. Replace our own global function to the new form callback function '2, save the original VB default Window port function address lpprevwndproc = setWindowlong (GHW, GWL_WNDPROC, Addressof WINDOPROC) End Sub

In fact, everything is so simple. With this way, the API function that needs a callback function in Windows We can call, and there are a lot of features we can use VB to be implemented. Note that our custom callback functions can only be a global function defined in the module! Can't define in the form.

It is only necessary to throw the jade, there are many features that don't need to be found everywhere. I am now studying VB 6 DirectX 7/8. The game speed is certainly impossible to reach C , but it is enough to deal with some small and medium-sized game themes, such as RPG. This has a lot of things worthy. Although not a fresh topic, there is still many skills and programming ideas. I hope that friends who have research have been able to exchange a lot of exchanges.

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

New Post(0)