Thread: Subclassing a toolbar

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    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.

    Has anyone else managed to subclass a toolbar?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    8
    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)
    Last edited by hdood; 09-25-2004 at 03:26 PM.

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Whoops, forgot about CallWindowProc, I'd called a Button's old window procedure directly before and assumed that there wouldn't be any problems...

    Thanx.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cannot insert toolbar into rebar
    By supernater in forum Windows Programming
    Replies: 1
    Last Post: 06-02-2009, 03:35 AM
  2. toolbar button question
    By supernater in forum Windows Programming
    Replies: 1
    Last Post: 05-25-2009, 08:24 PM
  3. Toolbar color
    By mrafcho001 in forum Windows Programming
    Replies: 2
    Last Post: 04-17-2006, 10:55 AM
  4. Dockable Toolbar (Like taskbar or office toolbar)
    By om3ga in forum Windows Programming
    Replies: 2
    Last Post: 11-20-2005, 03:09 AM
  5. Tooltip won't display over toolbar button
    By minesweeper in forum Windows Programming
    Replies: 5
    Last Post: 06-20-2003, 01:21 AM