使用 Win32 API 添加工具栏对话框

发布于 2024-08-15 12:35:39 字数 2136 浏览 1 评论 0原文

我有一个对话框,可以在其中使用资源编辑器添加控件。但我试图在 WM_INITGDIALOG 消息中动态创建一个工具栏,但该工具栏不可见。是否还有其他事情可以使其可见(我不这么认为,但是......)。如果这不可能,如何在资源编辑器中添加工具栏。

正如你所猜到的,我使用 VS 2008。

CreateButtons(HWND hwnd)
{
    HIMAGELIST m_hTBImageList;
    HIMAGELIST m_hTBHottrack;



    HWND hwndSysButtonTB = CreateWindowEx(0,
        TOOLBARCLASSNAME, 
        _T(""), 
        WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT | TBSTYLE_TOOLTIPS | CCS_NORESIZE | CCS_NOPARENTALIGN,
        toolbarRect.left, toolbarRect.top, toolbarRect.right-toolbarRect.left, toolbarRect.bottom-toolbarRect.top, 
        hwnd,
        (HMENU)IDR_TOOLBAR, 
        (HINSTANCE)hAppInstance, 
        NULL);

    m_hTBImageList = ImageList_LoadImage((HINSTANCE)hAppInstance, 
        MAKEINTRESOURCE(IDB_BITMAP_ICONS), toolbarButtonSize.cx, 1, 
        0, IMAGE_BITMAP, LR_CREATEDIBSECTION|LR_SHARED);
    m_hTBHottrack  = ImageList_LoadImage((HINSTANCE)hAppInstance, 
        MAKEINTRESOURCE(IDB_MOUSEOVER), toolbarButtonSize.cx, 1, 
        0, IMAGE_BITMAP, LR_CREATEDIBSECTION|LR_SHARED);

    SendMessage(hwndSysButtonTB, (UINT) TB_SETIMAGELIST, 0, (LPARAM)m_hTBImageList);
    SendMessage(hwndSysButtonTB, (UINT) TB_SETHOTIMAGELIST, 0, (LPARAM)m_hTBHottrack);
    SendMessage(hwndSysButtonTB, (UINT) TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);

    // win2k: set color of hot tracking frame
    COLORSCHEME scheme;
    scheme.dwSize = sizeof(scheme);
    scheme.clrBtnHighlight = RGB(175,175,175);
    scheme.clrBtnShadow = RGB(175,175,175);
    SendMessage(hwndSysButtonTB, (UINT) TB_SETCOLORSCHEME, 0, (LPARAM)&scheme);

    TBBUTTON ButtonEnd =            {0,ID_BUTTON_END,TBSTATE_ENABLED,TBSTYLE_BUTTON};
    TBBUTTON ButtonRefresh =        {1,ID_BUTTON_REFRESH,TBSTATE_ENABLED,TBSTYLE_BUTTON};
    TBBUTTON ButtonOptions =        {2,ID_BUTTON_PROPERTIES,TBSTATE_ENABLED,TBSTYLE_BUTTON};



    SendMessage(hwndSysButtonTB, (UINT) TB_ADDBUTTONS, 1, (LPARAM)&ButtonEnd);
    SendMessage(hwndSysButtonTB, (UINT) TB_ADDBUTTONS, 1, (LPARAM)&ButtonRefresh);
    SendMessage(hwndSysButtonTB, (UINT) TB_ADDBUTTONS, 1, (LPARAM)&ButtonOptions);

} 

I have a dialog box on which controls are added with resource editor. But I am trying to create a toolbar on the fly in WM_INITGDIALOG message but the toolbar is not visible. Is there something else to do make it visible(I dont think so but...). If this is not possible how can add a toolbar in resource editor.

As you guessed I use VS 2008.

CreateButtons(HWND hwnd)
{
    HIMAGELIST m_hTBImageList;
    HIMAGELIST m_hTBHottrack;



    HWND hwndSysButtonTB = CreateWindowEx(0,
        TOOLBARCLASSNAME, 
        _T(""), 
        WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT | TBSTYLE_TOOLTIPS | CCS_NORESIZE | CCS_NOPARENTALIGN,
        toolbarRect.left, toolbarRect.top, toolbarRect.right-toolbarRect.left, toolbarRect.bottom-toolbarRect.top, 
        hwnd,
        (HMENU)IDR_TOOLBAR, 
        (HINSTANCE)hAppInstance, 
        NULL);

    m_hTBImageList = ImageList_LoadImage((HINSTANCE)hAppInstance, 
        MAKEINTRESOURCE(IDB_BITMAP_ICONS), toolbarButtonSize.cx, 1, 
        0, IMAGE_BITMAP, LR_CREATEDIBSECTION|LR_SHARED);
    m_hTBHottrack  = ImageList_LoadImage((HINSTANCE)hAppInstance, 
        MAKEINTRESOURCE(IDB_MOUSEOVER), toolbarButtonSize.cx, 1, 
        0, IMAGE_BITMAP, LR_CREATEDIBSECTION|LR_SHARED);

    SendMessage(hwndSysButtonTB, (UINT) TB_SETIMAGELIST, 0, (LPARAM)m_hTBImageList);
    SendMessage(hwndSysButtonTB, (UINT) TB_SETHOTIMAGELIST, 0, (LPARAM)m_hTBHottrack);
    SendMessage(hwndSysButtonTB, (UINT) TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);

    // win2k: set color of hot tracking frame
    COLORSCHEME scheme;
    scheme.dwSize = sizeof(scheme);
    scheme.clrBtnHighlight = RGB(175,175,175);
    scheme.clrBtnShadow = RGB(175,175,175);
    SendMessage(hwndSysButtonTB, (UINT) TB_SETCOLORSCHEME, 0, (LPARAM)&scheme);

    TBBUTTON ButtonEnd =            {0,ID_BUTTON_END,TBSTATE_ENABLED,TBSTYLE_BUTTON};
    TBBUTTON ButtonRefresh =        {1,ID_BUTTON_REFRESH,TBSTATE_ENABLED,TBSTYLE_BUTTON};
    TBBUTTON ButtonOptions =        {2,ID_BUTTON_PROPERTIES,TBSTATE_ENABLED,TBSTYLE_BUTTON};



    SendMessage(hwndSysButtonTB, (UINT) TB_ADDBUTTONS, 1, (LPARAM)&ButtonEnd);
    SendMessage(hwndSysButtonTB, (UINT) TB_ADDBUTTONS, 1, (LPARAM)&ButtonRefresh);
    SendMessage(hwndSysButtonTB, (UINT) TB_ADDBUTTONS, 1, (LPARAM)&ButtonOptions);

} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2024-08-22 12:35:39

您必须

SendMessage(hwndSysButtonTB, TB_AUTOSIZE, 0, 0); 
ShowWindow(hwndSysButtonTB , SW_SHOW); 

在函数结束时调用。

我认为你应该使用 TBBUTTON 数组而不是三个单独的变量。然后你可以一次性添加它们

SendMessage(hwndSysButtonTB, (UINT) TB_ADDBUTTONS, 3, (LPARAM)&ButtonArray);

You have to call

SendMessage(hwndSysButtonTB, TB_AUTOSIZE, 0, 0); 
ShowWindow(hwndSysButtonTB , SW_SHOW); 

at the end of your function.

And I think you should use an TBBUTTON array instead of three separate variables. Then you can add them all at once with

SendMessage(hwndSysButtonTB, (UINT) TB_ADDBUTTONS, 3, (LPARAM)&ButtonArray);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文