-
Subclassing a toolbar
Hello,
I've written something like this:-
Code:
g_hwndToolbar = CreateWindow(TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | CCS_NODIVIDER | CCS_NORESIZE | TBSTYLE_TOOLTIPS | TBSTYLE_WRAPABLE, 64, -2, 640, 24, hwnd, NULL, g_hInstance, NULL);
DefToolbarProc = (WNDPROC)SetWindowLong(g_hwndToolbar, GWL_WNDPROC, (long)ToolbarProc);
Where g_hwndToolbar is a window handle to the toolbar, DefToolbarProc is a pointer to the toolbar's original window procedure and ToolbarProc is the name of my replacement window procedure, which looks like this:-
Code:
LRESULT ToolbarProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
return DefToolbarProc(hwnd, uMsg, wParam, lParam);
However, upon execution I get a lovely 0xC0000005 Access Violation error and the Visual C++ debugger has me looking at memory address 0xFFFF01BB, which I can assume is well out of range of my program. :eek:
Has anyone else managed to subclass a toolbar?
-
Try using CallWindowProc, to call the original window process instead of calling it directly. Also, remember to give all your windows unique (within the parent window) identifiers.
Code:
#define IDC_MYTOOLBAR 100
LRESULT CALLBACK ToolbarProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
WNDPROC wp = (WNDPROC)GetWindowLong(hWnd, GWL_USERDATA);
return CallWindowProc(wp, hWnd, uMsg, wParam, lParam);
}
(...)
WNDPROC wp;
g_hwndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
WS_CHILD|WS_VISIBLE|CCS_NODIVIDER |CCS_NORESIZE|TBSTYLE_TOOLTIPS |TBSTYLE_WRAPABLE,
64, -2, 640, 24, hwnd, (HMENU)IDC_MYTOOLBAR, g_hInstance, NULL);
wp = (WNDPROC)SetWindowLong(g_hwndToolbar, GWL_WNDPROC, (LONG)ToolbarProc);
SetWindowLong(g_hwndToolbar, GWL_USERDATA, (LONG)wp);
(...)
(Or Get/SetWindowLongPtr for 64-bit compatability)
-
Whoops, forgot about CallWindowProc, I'd called a Button's old window procedure directly before and assumed that there wouldn't be any problems... :o
Thanx. :)