Reputation: 165
what I'm trying to do is to create chess game in C++ through WINAPI, and since I haven't ever studied them at School I'm having some problems (online documentation is quite bad, I wasn't able to find any example of how to do this) with printing an .ico file with transparency inside my window. I aldready managed to do it with a bitmap image but my Photoshop doesn't let me save a .bmp file with alpha channels and I had to go for something supported by WINAPI and allowed transparency (therefore .ico).
My question is, how do you draw a transparent .ico file inside my window?
Thank you!
Upvotes: 2
Views: 3827
Reputation: 2876
If you are using icons from a resource file (resource.rc) you can load the icon with LoadIcon then get the Device contect of the dialog window and then draw it with DrawIconEx wherever you want for examlpe (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
Upvotes: 0
Reputation: 165
I got how to do it, I'll post the code:
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);
But now I'm running into an additional problem: my icon is more than the double of 32x32 (it is 72x78) and my picture is getting aliased. Is there any way to solve this? Thanks!
Upvotes: 2