使用对话框窗口 C++ 上的图标Win32 API

发布于 2024-12-04 08:29:58 字数 544 浏览 2 评论 0原文

我正在尝试创建一个顶部带有图标的对话框,如下所示。

icondialog

我正在使用资源文件来加载图标,如下所示。

IDI_ICON1          ICON           ".\\usb.ico"

我尝试使用以下代码设置窗口图标。

SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)IDI_ICON1);
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)IDI_ICON1);

hwnd 是窗口。结果,我看到一个蓝色圆圈,看起来就像 Windows 7 和 Vista 的加载图标。我几乎确信图标已正确加载,因为当我查看任务栏时,我的程序有代表我的程序的图标。如果您需要我用于对话框窗口本身的代码,请告诉我,我将发布它。我在 Windows 7 上使用 mingw32 C++ 编译器。谢谢!

I am trying to create a dialog box with an icon at the top like so.

icon dialog

I am using a resource file to load the icon like so.

IDI_ICON1          ICON           ".\\usb.ico"

I have tried setting the window icon using the following code.

SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)IDI_ICON1);
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)IDI_ICON1);

hwnd is the window. As a result, I get a blue circle that looks just like the loading icon for Windows 7 and Vista. I am almost positive the icon is being loaded correctly as when I look at the task bar, my program has that icon representing my program. If you need the code I am using for the dialog window itself, let me know I will post it. I am using mingw32 C++ compiler on Windows 7. Thanks!

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

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

发布评论

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

评论(3

以往的大感动 2024-12-11 08:29:58

使用 LoadIcon 并将图标句柄传递给 WM_SETICON。

HICON hicon = LoadImageW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDI_ICONMAIN), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_DEFAULTSIZE);
SendMessageW(hwnd, WM_SETICON, ICON_BIG, hicon);

Use LoadIcon and pass an icon handle to WM_SETICON.

HICON hicon = LoadImageW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDI_ICONMAIN), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_DEFAULTSIZE);
SendMessageW(hwnd, WM_SETICON, ICON_BIG, hicon);
时常饿 2024-12-11 08:29:58

我必须将 LoadImageW() 的返回值转换为 HICON ,以避免错误:


类型“HANDLE”的值不能分配给实体
输入“HICON”....“

这对我有用:

.... 
//hDlg is the handle to my dialog window
case WM_INITDIALOG:
    {
        HICON hIcon;

        hIcon = (HICON)LoadImageW(GetModuleHandleW(NULL),
            MAKEINTRESOURCEW(IDI_ICON1),
            IMAGE_ICON,
            GetSystemMetrics(SM_CXSMICON),
            GetSystemMetrics(SM_CYSMICON),
            0);
        if (hIcon)
        {
            SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
        }
    }
    break;

这是结果

win32 对话框图标

仅供参考:使用的图标是从以下位置下载的:

http://www.iconsdb.com/orange-icons/stackoverflow-6-icon .html

希望有帮助!

I had to cast the return value of LoadImageW() to HICON , to avoid the error :

"
a value of type "HANDLE" cannot be assigned to an entity of
type "HICON" ...."

this worked for me :

.... 
//hDlg is the handle to my dialog window
case WM_INITDIALOG:
    {
        HICON hIcon;

        hIcon = (HICON)LoadImageW(GetModuleHandleW(NULL),
            MAKEINTRESOURCEW(IDI_ICON1),
            IMAGE_ICON,
            GetSystemMetrics(SM_CXSMICON),
            GetSystemMetrics(SM_CYSMICON),
            0);
        if (hIcon)
        {
            SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
        }
    }
    break;

and here is the result

win32 Dialog icon

FYI: the used icon was downloaded from :

http://www.iconsdb.com/orange-icons/stackoverflow-6-icon.html

Hope that helps !

纸短情长 2024-12-11 08:29:58

虽然已经三年了。我想为此添加另一个解决方案。我已经在 Visual Studio 2017 上尝试过此操作。

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
SendMessage(WM_SETICON, ICON_BIG, (LPARAM)m_hIcon);

Even though it's three years. I would like to add another solution to this. I have tried this on Visual Studio 2017.

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
SendMessage(WM_SETICON, ICON_BIG, (LPARAM)m_hIcon);

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