2

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!

Baffo rasta
  • 320
  • 1
  • 4
  • 17
  • @Jonathan Potter could you please provide an example? I already found that funcion but couldn't figure out how it works. Thanks! – Baffo rasta Feb 06 '16 at 12:51
  • You do know how to draw the chess board? If you have managed to do that than you know what device context is. In your case, in response to WM_PAINT, you must use the function DrawIconEx. You just need to pass it HDC you got from BeginPaint, and coordinates where you want to put an icon. – AlwaysLearningNewStuff Feb 06 '16 at 13:26
  • @AlwaysLearningNewStuff: Yeah, I've already drawn my chessboard, the problem is placing all of the pieces. The actual solution I got is lousy, I had to get a bitmap for all of the pieces both for white and black positions and I have no idea of how to animate them without showing this stuff, so I thought to make a transparent background image, but can't draw it! – Baffo rasta Feb 06 '16 at 13:31

2 Answers2

2

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!

Baffo rasta
  • 320
  • 1
  • 4
  • 17
  • try [ExtractIconEx](https://msdn.microsoft.com/en-us/library/windows/desktop/ms648069%28v=vs.85%29.aspx) instead of LoadImage – milevyo Feb 06 '16 at 16:44
  • @milevyo: I'll try as soon as I can and let you know, thanks in advance! – Baffo rasta Feb 06 '16 at 17:21
  • @milevyo: That's not exactly antialiased but still got better than it was before. Thanks! – Baffo rasta Feb 06 '16 at 20:37
  • You probably have more than one icon in the same icon file, the system picks the 32x32 icon. You can specify width/height, remove `LR_DEFAULTSIZE` etc. Also, you must call `DestroyIcon` after `DrawIconEx` otherwise you get resource leak. Either that or create the `HICON` on heap so that it's created only once. – Barmak Shemirani Feb 06 '16 at 22:03
  • @BarmakShemirani: Really useful answer, this was exactly what I needed you made my day! Thank you! – Baffo rasta Feb 07 '16 at 13:13
0

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
Nassim
  • 2,879
  • 2
  • 37
  • 39