In this talk, we will learn what window subclasses are and how to use it conveniently in the way you want.
Theory:
If you've ever worked in a Windows environment, you'll sometimes find that there's a ready-made window with almost all the functionality you need, but it's not exactly the same (otherwise there's no need to talk about it). Have you ever been in a situation like this, if you need an Edit control with the ability to filter special characters? Of course the most direct way is to use the code to do it, but it is a time-consuming and difficult task, and window subclass can be used to do this kind of thing.
Window subclasses allow you to take over the quilt-like window so that you have absolute control over it. To illustrate, for example, you need a text edit box that accepts only hexadecimal digits, and if you use a simple edit control, you don't know or do anything when the user enters a character other than hexadecimal. That is, when a user enters a text box to enter the string "zb+q*", it is especially unprofessional to do nothing except to reject the entire string. It is important that you have the ability to input detection, that is, you can detect this character whenever a user enters a character into the edit box.
Now explain the implementation details: When a user enters a character into a text box, Windows sends a WM_CHAR message to the window function of the edit control. This window function itself is parasitic in Windows, so you cannot modify it directly. But we can redirect the message to send it to the window handler we wrote ourselves. If the custom window is to process this message, it can be processed, and if it is not processed, it can be forwarded to its original window handler function. In this way, a custom window-handling function inserts itself between the Windows system and the Edit control.
Look at the following process:
Before a window subclass is instantiated
Window-handling functions for Windows ==>edit controls.
After child class
Windows ==> Custom window-handling Functions ==> the window-handling functions of the Edit control.
Note that subclasses are not limited to controls, you can subclass any window, now we have to focus on how to implement subclasses of a window. Let's think about how Windows knows where the window handler functions for an Edit control are placed. A guess? ... Definitely not. The original WNDCLASSEX structure member Lpfnwndproc pointed out the window function address. If you can replace this member variable with the address of the window function that you have written, Windows does not send the message to the custom window function! We do this by calling the function SetWindowLong, which is the prototype of this function:
SetWindowLong PROTO Hwnd:dword, Nindex:dword, Dwnewlong:dword
HWnd = handle of the window that will implement subclasses
nindex = function Index of function
Gwl_exstyle to set the extended style of the window.
Gwl_style set a new window style
Gwl_wndproc set up a new window handler function address
Gwl_hinstance set a new application handle
gwl_id set a new window ID
Gwl_userdata set a 32-bit data that is related to this window for the user
Dwnewlong = data used for updating
Our work is still relatively simple:
Write a window function to process the message that is sent to the Edit control.
Call the SetWindowLong function with parameter gwl_wndproc, if the call succeeds then the return value is a 32-bit integer associated with the calling function
In our program, the return value is the address of the original window function. We want to save this value for later use. Remember: There are some messages that we don't deal with, we need to send them to the original window function to process, and this uses another function CallWindowProc, the function prototype is:
CallWindowProc PROTO Lpprevwndfunc:dword, Hwnd:dword, Msg:dword, Wparam:dword, Lparam:dword
Lpprevwndfunc = The address of the original function of the window. The remaining four parameters are the arguments to the custom function, which are passed directly to the function CallWindowProc.