Can I subclass the same parent window HWND several times

Fernando Gabriel 1 Reputation point
2020-07-29T19:14:21.717+00:00

In case positive, is there a penalty in performance due to this?

I want to use this so each child window can process its own parent notifications. For example, a Listview subclass its parent window and process LVN_ITEMCHANGED, LVN_COLUMNCLICK, ect. An Edit Control process its own EN_CHANGE, WM_CTLCOLOREDIT, ...

In other words, can each child control use SetWindowSubclass / DefSubclassProc / RemoveWindowSubclass with the same parent window? I'm not subclassing the child controls, only the parent window, but several times.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,493 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Rita Han - MSFT 2,161 Reputation points
    2020-07-30T03:27:26.257+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Can I subclass the same parent window HWND several times

    Yes, you can achieve this using SetWindowSubclass.

    Each subclass is uniquely identified by the address of the pfnSubclass and its uIdSubclass. Both of these are parameters of the SetWindowSubclass function. Several subclasses can share the same subclass procedure and the ID can identify each call.

    Call DefSubclassProc in Subclassproc if there are multiple subclass procedures.

    The following is example of subclassing a parent window twice for two buttons.

    //...  
      
       if (SetWindowSubclass(hWnd, forButton1, subclass_id1, NULL))  
       {  
    	   OutputDebugString(L"SUCCESS");  
       }  
      
       if (SetWindowSubclass(hWnd, forButton2, subclass_id2, NULL))  
       {  
    	   OutputDebugString(L"SUCCESS");  
       }  
      
    //...  
      
    LRESULT WINAPI forButton1(  
    	HWND hWnd,  
    	UINT uMsg,  
    	WPARAM wParam,  
    	LPARAM lParam,  
    	UINT_PTR uIdSubclass,  
    	DWORD_PTR dwRefData  
    )  
    {  
    	return DefSubclassProc(hWnd, uMsg, wParam, lParam);  
    }  
      
    LRESULT WINAPI forButton2(  
    	HWND hWnd,  
    	UINT uMsg,  
    	WPARAM wParam,  
    	LPARAM lParam,  
    	UINT_PTR uIdSubclass,  
    	DWORD_PTR dwRefData  
    )  
    {  
    	return DefSubclassProc(hWnd, uMsg, wParam, lParam);  
    }  
    

    Thank you.

    1 person found this answer helpful.
    0 comments No comments

  2. RLWA32 42,366 Reputation points
    2020-07-30T17:57:07.41+00:00

    If you are willing to use MFC you can take advantage of its built-in support to handle message reflection.
    Although MFC Technical Notes have not been updated you can get a good idea of how the mechanism works by reading tn062-message-reflection-for-windows-controls

    0 comments No comments

  3. RLWA32 42,366 Reputation points
    2020-07-31T12:23:26.957+00:00

    When a parent window is subclassed the subclass procedure will receive the notification messages (i.e., WM_COMMAND, WM_NOTIFY) for all of the child window controls. If the parent window is subclassed more than once then each subclass procedure will receive all the control notification messages.

    If you want a child control to handle it's own notifications then a subclass procedure for the parent window must forward the notification messages back to the child control. Only one subclass of the parent window procedure is necessary to accomplish the forwarding

    0 comments No comments