Interpretation of Win32 window subclass

zhaozj2021-02-16  48

Maybe you need a special Edit to limit the input of floating point, but existing Edit can't complete this work - because it can only limit case or pure numbers. When you ask for help on the forum, a netizen tells you: "Usage." You may have a series of questions while seeing the front line of dawn: What is the child? What is the principle of subclass? How to achieve subclass? Let me start from a simple C program, step down on your doubt. First of all, I list the following C programs: #include using namespace std; class parent {public: void func (void) {cout << "func of perent" << endl;}}; class child: Public Parent {public: void func (void) {cout << "func of child" << end1;}}; void main () {parent p; child C; p.func (); c.func ();} Now I will explain it. In this code I defined two C classes: parent class and subclass, and subclasses are inherited from parent class; they have a member function of the same name. In the main function, I constructed the object of the parent class and subclass, and called their respective member functions func. The results are as follows: Func of ParentFunc of Child briefly, this code is that the subclasses have rewritten the FUNC member functions according to their needs. The principle of subclass of Win32 is similar to this, but does not actually serve which functions like C actually, but rely on some of the messages in the Windows system from others to handle it. For example, please see the following simple window callback processes: LRESULT CALLBACK PROCMAIN (HWND HDLG, UINT MSG, WPARAM WPARAM, LPARAM LPARAM) {Switch (MSG) {Case WM_Close: Enddialog (HDLG, 0); Break; Case WM_DESTROY: PostquitMessage (0); Break;} Return 0;} In this callback, I manually handled two messages: When you click the "Close" button (WM_CLOSE), I will close the dialog box (EndDialog " ); When the dialog is destroyed (WM_DESTROY), I sent an exit message to the system message queue to complete the end work (PostQuitMessage). That is, if the WM_CLOSE's response code is changed to: Case WM_Close: ShowWindow (HDLG, SW_MINIMIZE); Break; this dialog will be the same as MSN, will be completed after clicking "Off" Minimized work. So, how do you manually respond to its messages for the window procedure? We can use a function pointer to intercept the message we are interested in and processes the predefined window process after processing.

This process is roughly as follows: WndProc OldProc; OldProc = (WndProc) Setwindowslong (hwnd, gwl_wndproc, (long) newProc); of course, the new window process newProc is pre-implemented in advance. After the above code is executed, the system will first enter the newProc callback process you implemented, and then after processing the message you interested, go back to the original. Complete the remaining work during the callback process. The above is the principle analysis of the window subclass. Here I actually explain how to subclassify the window through an example. The interface of this example is as follows:

The edit box above the interface is used to limit floating point input, and below is a normal hyperlink. Ok, let me start to complete the subclass of these two windows by step: First step, save the original window process when the main window dialog is initialized, and set the new window process. Code is as follows: case WM_INITDIALOG: EditProc = (WNDPROC) SetWindowLong (GetDlgItem (hDlg, IDC_EDIT), GWL_WNDPROC, (LONG) ProcFloat); StaticProc = (WNDPROC) SetWindowLong (GetDlgItem (hDlg, IDC_ST_HOMEPAGE), GWL_WNDPROC, (LONG) ProcLink); Break; Step 2, Floating Point Editing Floating Window Process: LRESULT CALLBACK Procfloat (HWND HWND, UINT MSG, WPARAM WPARAM, LPARAM LPARAM) {IF (MSG == WM_CHAR && WPARAM! = '.' && (WPARAM <= '0' || WPARAM> = '9') && WParam! = VK_BACK) {MessageBeep (MB_OK); Return 0;} else Return CallWindowProc (EditProc, HWND, MSG, WPARAM, LPARAM);} What to explain here is, Due to the requirements of the control itself, just intercept a message, it is the WM_CHAR that receives characters. When the character entered by the user is not a decimal point, 0 ~ 9, and the backup key (notice not to have a retracted key, otherwise you will find that your edit box cannot delete the incorrect number), send a sound to prompt input error. As for other messages, the original callback function is called. A third step of the process of implementing the window hyperlink: LRESULT CALLBACK ProcLink (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {switch (Msg) {case WM_SETCURSOR: SetCursor (LoadCursor (NULL, IDC_HAND)); break; case WM_LBUTTONDOWN : Shellexecute (NULL, "Open", "http://home.ncust.edu.cn/ ~titilima", null, null, sw_shownormal; break; default: Return CallWindowProc (staticproc, hwnd, msg, wparam, lparam) This code is easy to understand: It has completed two things, one is to set the cursor pointer as a hand shape (note: For the earlier Windows system, there is no predefined IDC_HAND pointer, this time you You need to draw a hand pointer yourself in the exe, such as the hand pointer in Delphi is drawn by yourself). Second, when you click the left mouse button, open the web link you want to open.

In fact, for the hyperlink, it is more important to implement in subclass - it is its font color (note this code is implemented during the callback process of the main window dialog): Case WM_CTLCOLORSTATIC: IF GetDlgItem (hDlg, IDC_ST_HOMEPAGE) == (HWND) lParam) {SetTextColor ((HDC) wParam, 0xff0000); SetBkMode ((HDC) wParam, TRANSPARENT); return (LRESULT) CreateSolidBrush (GetSysColor (COLOR_BTNFACE));} break; further There are a few points to explain: 1. Your static hyperlink must have a unique resource ID, such as my Idc_st_homepage, so you can use getDlgitem to get its handle to complete subclasses; 2, you must It sets the SS_NOTIFY style to ensure that it can inform the parent window dialog when clicking it; Realize this. About Win32 window subcatenation is introduced here, you can click here to download the supporting source code of this article, in detail in the code.

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

New Post(0)