Description: Windows subclass of Win32

Source: Internet
Author: User

CodeDownload: subclass (Please click to enter the forum to reply and then download)

You may need a Special edit to limit the input of floating point numbers, but the existing edit cannot do this-because it can only limit the case or number. When you asked for help on the Forum, a netizen told you: "UseSubclass." You may have a series of problems while looking at the Dawn: What isSubclass? What is the principle of subclass? How to Implement subclass? Next let me start with a simple C ++ProgramStart, unlock your doubts step by step.
First, I will list the following C ++ programs for you:

01. # Include <iostream> 02. Using Namespace STD; 03. Class Parent 04. { 05. Public : 06. Void Func ( Void ) {Cout < "Func of parent" <Endl ;} 07. }; 08. Class Child: Public Parent 09. { 10. Public : 11. Void Func ( Void ) {Cout < "Func of child" <Endl ;} 12. }; 13. Void Main () 14. { 15. Parent P; 16. Child C; 17. P. func (); 18. C. func (); 19. } </Iostream>

Let me explain it now. In this Code, I defined two C ++ classes: parent class and Child class, and the Child classes inherit from the parent class. They have a member function func with the same name. In the main function, I constructed the objects of the parent class and subclass respectively, and called their respective member functions func. The result is as follows:

1. Func of parent 2. Func of child

To put it simply, this code is used to rewrite the func member function based on your own needs. The subclass principle of Win32 is similar to that of Win32, except that subclass does not actually reload a function like C ++. Instead, it intercepts some messages in Windows for processing. For example, you can refer to the following simple window callback process:

01. Lresult Callback procmain ( Hwnd Hdlg, Uint MSG, Wparam Wparam, Lparam Lparam) 02. { 03. Switch (MSG) 04. { 05. Case Wm_close: 06. Enddialog (hdlg, 0 ); 07. Break ; 08. Case Wm_destroy: 09. Postquitmessage (0 ); 10. Break ; 11. } 12. Return 0; 13. }

In this callback, I manually process two messages: When I click the "close" button (wm_close), I close the dialog box (enddialog); when the dialog box is destroyed (wm_destroy) I sent an Exit message to the system message queue to complete the work (postquitmessage ). That is to say, if you change the response code of wm_close:

1. Case Wm_close: 2. Showwindow (hdlg, sw_minimize ); 3. Break ;

In this way, this dialog box will be the same as MSN. After you click "close", the minimal work will be completed. Then, how will the system control that has been defined in the window process manually respond to its message?
We can use the function pointer to intercept the messages we are interested in and then process the predefined window. This process is roughly as follows:

1. Wndproc oldproc; 2. Oldproc = (wndproc) setwindowslong (hwnd, gwl_wndproc ,( Long ) Newproc );

Of course, newproc is implemented by you in advance. After the above code is executed, when the system processes the hwnd window message, it will first enter the newproc callback process you implemented, and then after processing the message you are interested in, use the callwindowproc function and your pre-saved oldproc to return to the original callback process to complete the remaining work.
The above is the Principle Analysis of window subclass. I will use an example to explain how to subclass the window.

The interface for this example is as follows:

 

The edit box on the top of the interface is used to restrict floating point input. Below is a normal hyperlink.
Now, I start to subclass the two windows by step:
Step 1: When initializing the main window dialog box, save the original window process and set a new window process. The Code is as follows:

1. Case Wm_initdialog: 2. Editproc = (wndproc) setwindowlong (getdlgitem (hdlg, idc_edit), gwl_wndproc ,( Long ) Procfloat ); 3. Staticproc = (wndproc) setwindowlong (getdlgitem (hdlg, idc_st_homepage), gwl_wndproc ,( Long ) Proclink ); 4. Break ;

Step 2: implement the Window Process of the floating-point edit box:

01. Lresult Callback procfloat ( Hwnd Hwnd, Uint MSG, Wparam Wparam, Lparam Lparam) 02. { 03. If (MSG = wm_char & wparam! = '' . '' & (Wparam <= '' 0 '' | Wparam> = '' 9 '' ) & Wparam! = Vk_back) 04. { 05. Messagebeep (mb_ OK ); 06. Return 0; 07. } 08. Else 09. Return Callwindowproc (editproc, hwnd, MSG, wparam, lparam ); 10. }

It must be explained that, due to the needs of the control itself, only one message needs to be intercepted, that is, the wm_char receiving character. When the character entered by the user is not a decimal point, 0 ~ 9. For other messages, call the original callback function for processing.
Step 3: implement the superlink Window Process:

01. Lresult Callback proclink ( Hwnd Hwnd, Uint MSG, Wparam Wparam, Lparam Lparam) 02. { 03. Switch (MSG) 04. { 05. Case Wm_setcursor: 06. Setcursor (loadcursor (null, idc_hand )); 07. Break ; 08. Case Wm_lbuttondown: 09. ShellExecute (null, "Open" , Http://home.ncust.edu.cn /~ Titilima" , Null, null, sw_shownormal ); 10. Break ; 11. Default : 12. Return Callwindowproc (staticproc, hwnd, MSG, wparam, lparam ); 13. } 14. Return 0; 15. }

This code is easy to understand: it completes two things, one is to set the cursor pointer to the hand shape (NOTE: For earlier Windows systems, there is no predefined idc_hand pointer, in this case, you need to draw a hand pointer in the EXE resource. For example, the hand pointer in Delphi is drawn by yourself ), the second is to open the webpage link when you click the left mouse button.
In fact, for hyperlink, its main thing is to implement it outside of subclass-Its font color (note that this code is implemented in the callback process of the main window dialog box ):

1. Case Wm_ctlcolorstatic: 2. If (Getdlgitem (hdlg, idc_st_homepage) = ( Hwnd ) Lparam) 3. { 4. Settextcolor (( HDC ) Wparam, 0xff0000 ); 5. Setbkmode (( HDC ) Wparam, transparent ); 6. Return ( Lresult ) Createsolidbrush (getsyscolor (color_btnface )); 7. } 8. Break ;

Note the following points:
1. Your static hyperlink must have a unique resource ID, for example, idc_st_homepage. In this way, you can use getdlgitem to obtain its handle to complete subclass;
2. You must set the ss_notify style for it to ensure that it can notify the parent window dialog box when you click it;
3. Click it to open the web page. You can also put it in a subclass. You can also process the wm_command message in the main window dialog box.

Here is an introduction to Windows subclass of Win32. You can click here to download the documentationSource codeThe Code has detailed comments.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.