John Smith
John Smith

Reputation: 109

How to subclass Windows Common Controls?

I have a problem. I subclass my TreeView like this:

SetWindowSubclass(hTV, SubClassProc, 0, 0);

where hTv - handle to WC_TREEVIEW window produced by CreateWindowEx. This is my SubClassProc:

LRESULT CALLBACK    SubClassProc(HWND hWnd, UINT uMsg, WPARAM wParam,
                                 LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    switch (uMsg)
    {
    case TVM_EXPAND:
        // This line of code is never executed
        MessageBox(NULL, _T("I'm expanded"), _T("TreeView"), MB_ICONINFORMATION);
        break;
    case WM_NCDESTROY:
        RemoveWindowSubclass(hWnd, SubClassProc, uIdSubclass);
        break;
    }

    return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}

Why can't I handle TVM_EXPAND message? What's wrong in my code?

I uploaded my project here.

Upvotes: 2

Views: 554

Answers (1)

Barmak Shemirani
Barmak Shemirani

Reputation: 31599

This is handled in main window procedure. There is no need for subclass in this case.

Check for TVN_ITEMEXPANDED, this is a notification received when item is expanded. Send TVM_EXPAND message if you want to expand the item.

See also:
- TreeView send Messages (example: TVM_EXPAND)
- TreeView receive Notifications (example: TVN_ITEMEXPANDED)

BOOL CALLBACK mainProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lParam)
{
    switch (msg)
    {

    case WM_NOTIFY:
    {
        LPNMHDR hdr = (LPNMHDR)lParam;
        if (hdr->hwndFrom == hWndTree && hdr->code == TVN_ITEMEXPANDED)
        {
            MessageBox(hwnd, _T("I'm expanded"), _T("TreeView"), MB_ICONINFORMATION);
            break;
        }
        break;
    }
    ...    
}

Upvotes: 3

Related Questions