Search code examples
winapiiconstransparencydrawalpha

How do you draw an icon inside a window in C++ WINAPI?


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!


Solution

  • 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!