CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2009
    Posts
    46

    Need help with creating toolbar

    I want to create a Window that has a toolbar at the top. I can already create a window and add menus to it. Here is my code so far:

    -------------------------------------------------------------------------------------------------------------------------
    #include <windows.h>
    #include <tchar.h>
    #include <commctrl.h>
    #include "resource.h"

    HINSTANCE hInst;
    HWND wndHandle, hTool;

    int width=640;
    int height=480;

    bool InitWindow(HINSTANCE hInstance, int width, int height);
    LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

    int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
    {
    if( !InitWindow( hInstance, width, height) )
    {
    return false;
    }

    MSG msg = {0};
    while (WM_QUIT !=msg.message)
    {
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)==TRUE)
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    }
    return (int) msg.wParam;
    }

    bool InitWindow(HINSTANCE hInstance, int width, int height)
    {
    WNDCLASSEX wcex;

    wcex.cbSize=sizeof(WNDCLASSEX);
    wcex.style=CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc=(WNDPROC)WndProc;
    wcex.cbClsExtra=0;
    wcex.cbWndExtra=0;
    wcex.hInstance=hInstance;
    wcex.hIcon=0;
    wcex.hCursor=LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName=NULL;
    wcex.lpszClassName=TEXT("DirectXExample");
    wcex.hIconSm=0;

    RegisterClassEx(&wcex);

    RECT rect={0, 0, width, height};
    AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);

    wndHandle=CreateWindow(TEXT("DirectXExample"),
    TEXT("DirectXExample"),
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    rect.right - rect.left,
    rect.bottom - rect.top,
    NULL,
    NULL,
    hInstance,
    NULL);

    hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
    wndHandle, (HMENU)IDC_MAIN_TOOL, GetModuleHandle(NULL), NULL);

    // Send the TB_BUTTONSTRUCTSIZE message, which is required for
    // backward compatibility.
    SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);

    TBBUTTON tbb[3];
    TBADDBITMAP tbab;

    tbab.hInst = HINST_COMMCTRL;
    tbab.nID = IDB_STD_SMALL_COLOR;
    SendMessage(hTool, TB_ADDBITMAP, 0, (LPARAM)&tbab);

    ZeroMemory(tbb, sizeof(tbb));
    tbb[0].iBitmap = STD_FILENEW;
    tbb[0].fsState = TBSTATE_ENABLED;
    tbb[0].fsStyle = TBSTYLE_BUTTON;
    tbb[0].idCommand = ID_FILE_NEW;

    tbb[1].iBitmap = STD_FILEOPEN;
    tbb[1].fsState = TBSTATE_ENABLED;
    tbb[1].fsStyle = TBSTYLE_BUTTON;
    tbb[1].idCommand = ID_FILE_OPEN;

    tbb[2].iBitmap = STD_FILESAVE;
    tbb[2].fsState = TBSTATE_ENABLED;
    tbb[2].fsStyle = TBSTYLE_BUTTON;
    tbb[2].idCommand = ID_FILE_SAVEAS;

    SendMessage(hTool, TB_ADDBUTTONS, sizeof(tbb)/sizeof(TBBUTTON), (LPARAM)&tbb);

    if(!wndHandle)
    {
    return false;
    }

    ShowWindow(wndHandle, SW_SHOW);
    UpdateWindow(wndHandle);

    return true;
    }

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    switch(message)
    {
    case WM_KEYDOWN:
    switch(wParam)
    {
    case VK_ESCAPE:
    PostQuitMessage(0);
    break;
    }
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
    }
    ------------------------------------------------------------------------------------------------------------------------
    Header file:

    #define IDC_MAIN_TOOL 101
    #define IDB_BITMAP1 102
    #define ID_FILE_NEW 110
    #define ID_FILE_OPEN 115
    #define ID_FILE_SAVEAS 120
    ------------------------------------------------------------------------------
    The program compiles, but no toolbar is shown. Any help? Thanks. And, 'yes' I have the ComCtl32.lib file included in my project.

  2. #2
    Join Date
    Apr 2008
    Posts
    214

    Re: Need help with creating toolbar

    Need to add InitCommonControls(); before creating the toolbar:

    Code:
    InitCommonControls();
    hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
    wndHandle, (HMENU)IDC_MAIN_TOOL, GetModuleHandle(NULL), NULL);

  3. #3
    Join Date
    Jul 2009
    Posts
    46

    Re: Need help with creating toolbar

    Quote Originally Posted by zaryk View Post
    Need to add InitCommonControls(); before creating the toolbar:

    Code:
    InitCommonControls();
    hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
    wndHandle, (HMENU)IDC_MAIN_TOOL, GetModuleHandle(NULL), NULL);
    Thanks. Worked perfect.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured