Windows Programming--child window categorization (window subclassing)

Source: Internet
Author: User

For child window controls, sometimes we might want to get some messages from a subwindow, such as having three buttons under a main window, and if you want to use the Keyboard tab or the Shift-tab key to switch the focus between different buttons, you can use the Subwindow Class (window subclassing) method.

Subcategory of child window:

The window message handler for the button control is internal to Windows. However, by calling GetWindowLong with the Gwl_wndproc identifier as a parameter, you can get the address of this window's message handler. In addition, you can call SetWindowLong to set a new window message handler for the specified button, which is called "window sub-categorization" and is useful. It allows you to set a "hook" for an existing window message handler to process some messages in your own program while passing all other messages to the old window message handlers.

For each of the three buttons, COLORS1 uses SetWindowLong to set the address of the New scroll bar window message handler and obtains the address of the existing ScrollBar window message handler:

 for (i=0;i<3; i++=createwindow (TEXT ("button"), Buttonname[i ],ws_child| Bs_pushbutton, 0,0,0,0= (WNDPROC) SetWindowLong (Hwndbutton[i], Gwl_ WNDPROC, (LONG) newwndproc)); }       

The function Newwndproc now gets all the messages that Windows sends to the scroll bar window message handler in Hwndbutton. The window message handler changes the input focus to the next (or Previous) button window when it receives the tab or Shift-tab key. It uses CallWindowProc to call the old button window message handlers.

LRESULT CALLBACK Newwndproc (HWND hwnd, UINT Message,wparam WPARAM, LPARAM LPARAM) {intID =GetWindowLong (hwnd, gwl_id); Switch(message) { CaseWm_keydown:if(WParam = =vk_tab) SetFocus (GetDlgItem (GetParent (HWND), (ID+ (Getkeystate (Vk_shift) <0?2:1)) %3)) ;  Break ;  CaseWm_setfocus:idfocus=ID;  Break ; }                   returnCallWindowProc (Oldbuttonwndproc[id], hwnd, message, Wparam,lparam);}

Windows Programming--child window categorization (window subclassing)

Related Article

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.