首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用我的位图图像创建工具栏

用我的位图图像创建工具栏
EN

Stack Overflow用户
提问于 2013-05-13 16:37:44
回答 1查看 2K关注 0票数 1

这是一个用msdn创建工具栏的示例代码,但此示例使用的是系统的标准图像。

要使用资源文件中的图像,我需要在此代码中进行哪些更改,例如:IDB_COPY BITMAP "copy.bmp"IDB_CUT BITMAP "cut.bmp"IDB_PASTE BITMAP "paste.bmp"

代码语言:javascript
复制
HIMAGELIST g_hImageList = NULL;

HWND CreateSimpleToolbar(HWND hWndParent)
{
    // Declare and initialize local constants.
    const int ImageListID    = 0;
    const int numButtons     = 3;
    const int bitmapSize     = 16;

    const DWORD buttonStyles = BTNS_AUTOSIZE;

    // Create the toolbar.
    HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, 
                                  WS_CHILD | TBSTYLE_WRAPABLE, 0, 0, 0, 0, 
                                  hWndParent, NULL, g_hInst, NULL);

    if (hWndToolbar == NULL)
        return NULL;

    // Create the image list.
    g_hImageList = ImageList_Create(bitmapSize, bitmapSize,   // Dimensions of individual bitmaps.
                                ILC_COLOR16 | ILC_MASK,   // Ensures transparent background.
                                numButtons, 0);

    // Set the image list.
    SendMessage(hWndToolbar, TB_SETIMAGELIST, 
            (WPARAM)ImageListID, 
            (LPARAM)g_hImageList);

    // Load the button images.
    SendMessage(hWndToolbar, TB_LOADIMAGES, 
            (WPARAM)IDB_STD_SMALL_COLOR, 
            (LPARAM)HINST_COMMCTRL);

    // Initialize button info.
    // IDM_NEW, IDM_OPEN, and IDM_SAVE are application-defined command constants.

    TBBUTTON tbButtons[numButtons] = 
    {
        { MAKELONG(STD_FILENEW,  ImageListID), IDM_NEW,  TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"New" },
        { MAKELONG(STD_FILEOPEN, ImageListID), IDM_OPEN, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"Open"},
        { MAKELONG(STD_FILESAVE, ImageListID), IDM_SAVE, 0,               buttonStyles, {0}, 0, (INT_PTR)L"Save"}
    };

    // Add buttons.
    SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
    SendMessage(hWndToolbar, TB_ADDBUTTONS,       (WPARAM)numButtons,       (LPARAM)&tbButtons);

    // Resize the toolbar, and then show it.
    SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0); 
    ShowWindow(hWndToolbar,  TRUE);

    return hWndToolbar;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-13 23:56:49

我找到了这个解决方案:

代码语言:javascript
复制
const int ID_TB_STANDARD = 0;

const int ID_IL_STANDARD = 0;

HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, 
            WS_CHILD | TBSTYLE_TOOLTIPS, 0, 0, 0, 0, hWnd, (HMENU)ID_TB_STANDARD, hInstance, NULL);

HIMAGELIST hImageList = ImageList_LoadBitmap(hInstance, MAKEINTRESOURCEW(IDB_CUT), 16, 0, RGB(255, 0, 255));
ImageList_Add(hImageList, LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_COPY)), NULL);
ImageList_Add(hImageList, LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_PASTE)), NULL);

SendMessage(hWndToolbar, TB_SETIMAGELIST, (WPARAM)ID_IL_STANDARD, (LPARAM)hImageList);
SendMessage(hWndToolbar, (UINT) TB_SETHOTIMAGELIST, 0, (LPARAM)hHotImageList);
SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);

TBBUTTON tbb[3] = 
{
    {0,ID_CUT,TBSTATE_ENABLED,TBSTYLE_BUTTON},
    {1,ID_COPY,TBSTATE_ENABLED,TBSTYLE_BUTTON},
    {2,ID_PASTE,TBSTATE_ENABLED,TBSTYLE_BUTTON},
};



SendMessage(hWndToolbar, (UINT) TB_ADDBUTTONS, 3, (LPARAM)&tbb);

SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0);
ShowWindow(hWndToolbar , SW_SHOW);
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16517866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档