如何获取与 Windows 主题关联的图标? [英] How to get an icon associated with Windows theme?

查看:30
本文介绍了如何获取与 Windows 主题关联的图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Windows 中获取某个控件的图标?更具体地说,我想从 ListView Header 中获取排序箭头图标.我尝试使用以下方法获取它:

HRESULT GetSortArrowBmp(HWND hwnd, HEADERSORTARROWSTATES arrowState, HBITMAP** arrow){HTHEME 主题 = OpenThemeData(hwnd,L"HEADER");//hwnd 是头部本身HRESULT res = E_FAIL;如果(主题){res = GetThemeBitmap(theme, HP_HEADERSORTARROW, arrowState, TMT_DIBDATA, GBF_COPY, *arrow);关闭主题数据(主题);}返回资源;}

但它不会返回我期望的小三角形.有什么建议吗?

解决方案

作为一种解决方法,我可以建议使用以下函数来获取排序箭头的位图图像.

HBITMAP GetSortArrowBmp(HWND hwnd, HEADERSORTARROWSTATES arrowState, int width, int height){矩形矩形;//位图的尺寸rect.left = 0;rect.right = 宽度;rect.top = 0;rect.bottom = 高度;HDC 硬盘;HDC hdcMem;HBITMAP hBitmap;hdc = GetDC(hwnd);hdcMem = CreateCompatibleDC(hdc);hBitmap = CreateCompatibleBitmap(hdc, width, height);HTHEME 主题 = OpenThemeData(hwnd, L"HEADER");如果(主题){DrawThemeBackground(theme, hdcMem, HP_HEADERITEM, HIS_ICONNORMAL, &rect, NULL);//绘制排序箭头DrawThemeBackground(theme, hdcMem, HP_HEADERSORTARROW, arrowState, &rect, NULL);}关闭主题数据(主题);删除对象(hdcMem);ReleaseDC(hwnd, hdc);返回 hBitmap;}

虽然我没有在我的代码中使用它.我在所有者绘制标题中应用了相同的 DrawThemeBackground(theme, hdcMem, HP_HEADERSORTARROW, arrowState, &rect, NULL); 函数,这比将其绘制到位图并显示该位图更优雅.>

How to get an icon of some control in Windows? More specifically I would like to get a sort arrow icon from a ListView Header. I tried to get it by using the following method:

HRESULT GetSortArrowBmp(HWND hwnd, HEADERSORTARROWSTATES arrowState, HBITMAP** arrow)
{
    HTHEME theme = OpenThemeData(hwnd,L"HEADER"); // hwnd is header itself
    HRESULT res = E_FAIL;
    if (theme){
        res = GetThemeBitmap(theme, HP_HEADERSORTARROW, arrowState, TMT_DIBDATA, GBF_COPY, *arrow);   
        CloseThemeData(theme);
    }
    return res;
} 

But it doesn't return the tiny triangle I'm expecting. Any suggestions?

解决方案

As a workaround I could suggest the following function to get a bitmap image of the sort arrow.

HBITMAP GetSortArrowBmp(HWND hwnd, HEADERSORTARROWSTATES arrowState, int width, int height){
    RECT rect; // dimensions of a bitmap
    rect.left = 0;
    rect.right = width;
    rect.top = 0;
    rect.bottom = height;

    HDC hdc;
    HDC hdcMem;
    HBITMAP hBitmap;

    hdc = GetDC(hwnd);
    hdcMem = CreateCompatibleDC(hdc);
    hBitmap = CreateCompatibleBitmap(hdc, width, height);

    HTHEME theme = OpenThemeData(hwnd, L"HEADER");

    if(theme){
        DrawThemeBackground(theme, hdcMem, HP_HEADERITEM, HIS_ICONNORMAL, &rect, NULL);
        // drawing sort arrow
        DrawThemeBackground(theme, hdcMem, HP_HEADERSORTARROW, arrowState, &rect, NULL); 
    }
    CloseThemeData(theme);

    DeleteObject(hdcMem);
    ReleaseDC(hwnd, hdc);

    return hBitmap;
}

Although I didn't use this in my code. I applied the same DrawThemeBackground(theme, hdcMem, HP_HEADERSORTARROW, arrowState, &rect, NULL); function inside owner draw header which was more elegant than drawing it to the bitmap and displaying that bitmap.

这篇关于如何获取与 Windows 主题关联的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆