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.