如何在 C++ WINAPI 中的 window 中绘制图标?
How do you draw an icon inside a window in C++ WINAPI?
我想做的是通过 WINAPI 在 C++ 中创建国际象棋游戏,并且由于我从未在学校学习过它们,所以我遇到了一些问题(在线文档非常糟糕,我不是能够找到如何执行此操作的任何示例)在我的 window 中打印一个具有透明度的 .ico 文件。我已经设法用位图图像来完成它,但我的 Photoshop 不允许我保存带有 alpha 通道的 .bmp 文件,我不得不寻求 WINAPI 支持的东西并允许透明度(因此 .ico)。
我的问题是,如何在我的 window 中绘制一个透明的 .ico 文件?
谢谢!
我知道怎么做了,我会post代码:
hIcon = (HICON) LoadImage( // returns a HANDLE so we have to cast to HICON
NULL, // hInstance must be NULL when loading from a file
"favicon.ico", // the icon file name
IMAGE_ICON, // specifies that the file is an icon
0, // width of the image (we'll specify default later on)
0, // height of the image
LR_LOADFROMFILE| // we want to load a file (as opposed to a resource)
LR_DEFAULTSIZE| // default metrics based on the type (IMAGE_ICON, 32x32)
LR_SHARED // let the system release the handle when it's no longer used
);
DrawIconEx( hdc, 100, 200,hIcon, 72, 78, 0, NULL, DI_NORMAL);
但现在我 运行 遇到了另一个问题:我的图标大于 32x32 的两倍(现在是 72x78)并且我的图片出现锯齿。有什么办法可以解决这个问题吗?
谢谢!
如果您使用资源文件 (resource.rc) 中的图标,您可以使用 LoadIcon 加载图标,然后获取对话框的 Device contect window,然后使用 DrawIconEx 将其绘制到任何您想要的位置例如 (x,y) = (10,10)
HICON hIcon = LoadIcon(hInst, MAKEINTRESOURCEA(IDI_ICON)); // Load Icon from resource file
HDC hDcDlg = GetDC(hwndDlg); // get device context of the Dialog Window by using its handle
DrawIconEx (hDcDlg , 10, 10, hIcon, 0, 0, 0, NULL, DI_NORMAL); // draw it
我想做的是通过 WINAPI 在 C++ 中创建国际象棋游戏,并且由于我从未在学校学习过它们,所以我遇到了一些问题(在线文档非常糟糕,我不是能够找到如何执行此操作的任何示例)在我的 window 中打印一个具有透明度的 .ico 文件。我已经设法用位图图像来完成它,但我的 Photoshop 不允许我保存带有 alpha 通道的 .bmp 文件,我不得不寻求 WINAPI 支持的东西并允许透明度(因此 .ico)。
我的问题是,如何在我的 window 中绘制一个透明的 .ico 文件?
谢谢!
我知道怎么做了,我会post代码:
hIcon = (HICON) LoadImage( // returns a HANDLE so we have to cast to HICON
NULL, // hInstance must be NULL when loading from a file
"favicon.ico", // the icon file name
IMAGE_ICON, // specifies that the file is an icon
0, // width of the image (we'll specify default later on)
0, // height of the image
LR_LOADFROMFILE| // we want to load a file (as opposed to a resource)
LR_DEFAULTSIZE| // default metrics based on the type (IMAGE_ICON, 32x32)
LR_SHARED // let the system release the handle when it's no longer used
);
DrawIconEx( hdc, 100, 200,hIcon, 72, 78, 0, NULL, DI_NORMAL);
但现在我 运行 遇到了另一个问题:我的图标大于 32x32 的两倍(现在是 72x78)并且我的图片出现锯齿。有什么办法可以解决这个问题吗? 谢谢!
如果您使用资源文件 (resource.rc) 中的图标,您可以使用 LoadIcon 加载图标,然后获取对话框的 Device contect window,然后使用 DrawIconEx 将其绘制到任何您想要的位置例如 (x,y) = (10,10)
HICON hIcon = LoadIcon(hInst, MAKEINTRESOURCEA(IDI_ICON)); // Load Icon from resource file
HDC hDcDlg = GetDC(hwndDlg); // get device context of the Dialog Window by using its handle
DrawIconEx (hDcDlg , 10, 10, hIcon, 0, 0, 0, NULL, DI_NORMAL); // draw it