How does window subclass prevent conflicts?
The getwindowlong and setwindowlong APIs are used to modify the address of the created Window Process and replace the original address with the new address of the window process, then, filter out the messages you are interested in the new address you write. Generally, we use the original window function to call the messages that we do not process, to implement our small modification requirements on the features of a window, subclass is generally used on the control, but can also be used on any type of window.
Take the VB Code as an example:
1. First, intercept messages sent to all forms in the form_load event and send them to the address of the Self-compiled callback function newwindowproc. Note that different API functions and constants must be declared.
Oldwindowproc = setwindowlong (Me. hwnd, gwl_wndproc, addressof newwindowproc)
Registerhotkey me. hwnd, 1, mod_alt, vbkeyn
2. process the messages of interest in the Self-compiled callback function newwindowproc.
We recommend that you place all messages whose intercept handle is the hwnd control in a callback function and process them according to the message number MSG and the high parameter wparam that comes with the message.
If it is placed in different callback functions, conflicts may occur.
Public Function newwindowproc (byval hwnd as long, byval MSG as long, byval wparam as long, byval lparam as long) as long
Dim MX as integer
If MSG = tray_callback then 'if you click the icon in the tray, you can check whether you have clicked the left button or right-click the icon.
If lparam = wm_lbuttonup then ', if you click the left button of the tray
Theform. Visible = Not theform. Visible hide it once
Elseif lparam = wm_rbuttonup then 'Right-click the tray
Theform. popupmenu themenu
Exit Function
End if
End if
If MSG = wm_hotkey then 'is a hotkey message
If wparam = 1 then
'If it is defined by this program (the wparam parameter in the system message represents the hotkey identifier in the hotkey message, it is an integer defined when registerhotkey registers the hotkey, if the hotkey is defined by the system, the value of the identifier is-1 or-2. For details, see the beginning.
Frm_main.visible = Not frm_main.visible the specified process is called after the hotkey is matched.
'In this way, after running, press Alt + N repeatedly to hide and display the window.
Exit function 'The message has been processed and does not need to be sent back to the window.
End if
End if
Newwindowproc = callwindowproc (oldwindowproc, hwnd, MSG, wparam, lparam) 'if it is another type of message, it is passed to the original default Window Function
End Function
3. release resources in the form_unload event.
Setwindowlong me. hwnd, gwl_wndproc, oldwindowproc 'restores the address of the Window Process
Unregisterhotkey me. hwnd, 1' release the hotkey for other applications
How does window subclass prevent conflicts?