尝试使用 ImageList 创建工具栏,但不起作用

发布于 2024-08-27 17:20:47 字数 1538 浏览 5 评论 0原文

我正在尝试让我的工具栏与 ImageList 一起使用。这些图像是单独的 PNG,因此我将它们连续添加到 ImageList 中,但它不起作用。以下是将 Image 添加到 ImageList 的代码:

HIMAGELIST CreateToolBarImages(HINSTANCE hInst)
{
    HIMAGELIST v_ImageList = NULL;
    // IMAGE_LIST v_Img;
    HICON hIcon;
    HBITMAP hBit;
    COLORMAP cMap;
    COLORREF fromColor = RGB( 0,0,0 );

    InitCommonControls();

    v_ImageList = ImageList_Create(32, 32, ILC_MASK, 1, 1);

    cMap.from = fromColor;
    cMap.to = ::GetSysColor(COLOR_BTNFACE);
    hBit = CreateMappedBitmap(hInst, IDB_CONSOLE, 0, &cMap, 1);

    // hBit = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_CONSOLE));
    consoleImg = ImageList_Add(v_ImageList, hBit, 0);
    if (consoleImg == -1)
        return NULL;

    DeleteObject(hBit);

    // [edit Franci Penov]
    return v_ImageList;
}

然后我创建了 ToolBar,但在 Image 函数中失败了。

HWND CreateToolBarButton(HWND hWndParent)
{
    const int ImageID = 0;
    const int numB = 1;
    COLORREF iColor;

    HWND hToolBar = CreateWindowEx(0,
        TOOLBARCLASSNAME,
        NULL,
        WS_CHILD |TBSTYLE_LIST |TBSTYLE_FLAT | WS_VISIBLE,
        0,0,0,0,
        hWndParent, 
        NULL,
        g_hInst, 
        NULL);

    if (hToolBar == NULL)
        return NULL;

    HIMAGELIST ImgList = CreateToolBarImages(g_hInst);
    if (ImgList == NULL)
        MessageBox( hWndParent, L"No Tool Images", L"BOB", MB_OK );

    // [edit Franci Penov]
    return hToolBar;
}

我有什么遗漏或没有做的事情吗?

I am trying to get my toolbar to work with an ImageList. The images are individual PNGs, so I was adding them in the ImageList in succession, Bbt it wasn't working. Here is the code to add the Image to the ImageList:

HIMAGELIST CreateToolBarImages(HINSTANCE hInst)
{
    HIMAGELIST v_ImageList = NULL;
    // IMAGE_LIST v_Img;
    HICON hIcon;
    HBITMAP hBit;
    COLORMAP cMap;
    COLORREF fromColor = RGB( 0,0,0 );

    InitCommonControls();

    v_ImageList = ImageList_Create(32, 32, ILC_MASK, 1, 1);

    cMap.from = fromColor;
    cMap.to = ::GetSysColor(COLOR_BTNFACE);
    hBit = CreateMappedBitmap(hInst, IDB_CONSOLE, 0, &cMap, 1);

    // hBit = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_CONSOLE));
    consoleImg = ImageList_Add(v_ImageList, hBit, 0);
    if (consoleImg == -1)
        return NULL;

    DeleteObject(hBit);

    // [edit Franci Penov]
    return v_ImageList;
}

Then I create The ToolBar, but it fails at the Image function.

HWND CreateToolBarButton(HWND hWndParent)
{
    const int ImageID = 0;
    const int numB = 1;
    COLORREF iColor;

    HWND hToolBar = CreateWindowEx(0,
        TOOLBARCLASSNAME,
        NULL,
        WS_CHILD |TBSTYLE_LIST |TBSTYLE_FLAT | WS_VISIBLE,
        0,0,0,0,
        hWndParent, 
        NULL,
        g_hInst, 
        NULL);

    if (hToolBar == NULL)
        return NULL;

    HIMAGELIST ImgList = CreateToolBarImages(g_hInst);
    if (ImgList == NULL)
        MessageBox( hWndParent, L"No Tool Images", L"BOB", MB_OK );

    // [edit Franci Penov]
    return hToolBar;
}

Is there something I am missing or not doing?

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

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

发布评论

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

评论(4

潦草背影 2024-09-03 17:20:47

什么是“不起作用”。 CreateMappedBitmap 用于加载具有颜色表的图像 - 这意味着它们具有 <= 256 种颜色。

PNG 意味着您正在使用带有 Alpha 通道的 32bpp 图像,这意味着您无法使用 *MappedBitmap 函数。

Whats "not working". CreateMappedBitmap is used for loading images that have a color table - which means they have <= 256 colors.

PNGs imply you are using 32bpp images with an alpha channel which means you cannot use the *MappedBitmap functions.

混吃等死 2024-09-03 17:20:47

我对示例代码中的返回值进行了一些更改,上面我用注释进行了标记。

有关如何使用Win32 Toolbar控件的详细说明,可以阅读这篇 MSDN 文章

特别是,您的代码似乎缺少的是发送 Toolbar 控件发送 TB_SETIMAGELIST 消息来加载您刚刚创建的 ImageList

// Set the image list.
SendMessage(hToolBar, TB_SETIMAGELIST, 0, (LPARAM)ImgList);

当然,您可能还缺少其他内容在那里,但这将是一个很好的起点。

在进行错误检查并提前退出函数的几个地方,您还会遇到一些 GDI 对象内存泄漏。您可能也想清理它们。

I've made some changes around returning values in your example code above which I marked with comments.

For detailed explanation on how to use the Win32 Toolbar control, you can read this MSDN article.

In particular, what your code seems to be missing is sending the TB_SETIMAGELIST message to the Toolbar control to load the ImageList you just created:

// Set the image list.
SendMessage(hToolBar, TB_SETIMAGELIST, 0, (LPARAM)ImgList);

Of course, there might be other things you are missing in there, but that would be a good starting point.

You also have some GDI objects memory leaks around the couple of places you do error checks and exit your functions early. You might want to clean those up as well.

み零 2024-09-03 17:20:47

你好,谢谢你们,我已经解决了我的问题,
克里斯,你是对的,在我发布后我也做了这样的观察,我需要使用 GDI+ 才能使用 PNG,我选择使用图标,现在它工作得很好。 Franci,谢谢你,我也清理了代码。

问候,

Hi and thank you fellas i have resolved my issue,
Chris youre right, after i posted i made that observation as well, I would need to use GDI+ in order to use PNG's, i've opted to go with using Icons instead and it works perfectly now. Franci, thank you i have cleaned up the code as well.

regards,

栩栩如生 2024-09-03 17:20:47

你可以尝试使用这些代码:
https://github.com/Itseez/opencv/blob/master /samples/cpp/imagelist_creator.cpp

对我来说它运行得很好,

然后你需要安装 openCV 库;)

you could try to use these code:
https://github.com/Itseez/opencv/blob/master/samples/cpp/imagelist_creator.cpp

for me it worked so well

before you'll need to install the openCV library ;)

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