Using the icon in the C ++ Win32 API Dialog Box

I am trying to create a dialog box with an icon at the top, as shown below.

icon dialog

I am using a resource file to load an icon this way.

IDI_ICON1 ICON ".\\usb.ico" 

I tried to set 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 a window. As a result, I get a blue circle that looks the same as the boot icon for Windows 7 and Vista. I am pretty sure that the icon loads correctly, because when I look at the taskbar, my program has this icon representing my program. If you need the code that I use for the dialog itself, let me know that I will publish it. I am using the mingw32 C ++ compiler on Windows 7. Thank you!

+4
source share
2 answers

Use LoadIcon and pass the 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); 
+5
source

I needed to return the return value of LoadImageW() to HICON to avoid the error:

"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 box

FYI: the used icon was loaded with:

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

Hope this helps!

0
source

All Articles