Icon in about box won't display

Started by
3 comments, last by CrazyCdn 18 years, 2 months ago
All I need to do is add an icon to my program's about box. Something like this: Free Image Hosting at www.ImageShack.us So I create a dialog box using Visual Studio's editor, and add a 'picture' control. In the control's properties I change the type to 'icon' and the image field to IDI_FOHN (the id of the icon resource. Note that this same icon is also used for the my app's window and exe and works fine). In the editor, it displays correctly (I print-screened the above image from the editor). But at run-time the dialog always displays an exclamation icon instead. More weird is that if I add another icon to my resource script, and try to use that instead, this new icon won't display at all...? It must be something stupid I'm doing, but I've run out of ideas. Any wild guesses welcome :) Extract from resource.h:

#define IDC_STATICX  1056
#define IDI_FOHN     101
Extract from <program name>.rc (the about box's def)

IDD_ABOUT DIALOGEX 0, 0, 199, 151
style DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | 
    WS_SYSMENU
CAPTION "."
FONT 8, "MS Sans Serif", 0, 0, 0x0
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,129,121,54,14,BS_CENTER | BS_VCENTER
    CONTROL         "",IDC_STATIC,"Static",SS_ETCHEDHORZ,49,61,137,1
    LTEXT           ".",IDC_STATIC,49,67,136,43
    ICON            IDI_FOHN,IDC_STATICX,14,18,21,20,SS_RIGHTJUST
    LTEXT           ".",IDC_STATIC,48,18,133,37
END
About box's calling code and proc:


DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);

...

BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT message, WPARAM wParam,
						   LPARAM lParam)
{
	switch(message)
	{
	case WM_INITDIALOG:
		// This code doesn't make any difference
		/*HICON hIcon;
		HWND  child;
		hIcon = LoadIcon(m_instance, MAKEINTRESOURCE(IDI_FOHN));
		child = GetDlgItem(hDlg, IDC_STATICX);
		SendMessage(child, BM_SETIMAGE, (WPARAM) IMAGE_ICON, 
			(LPARAM) hIcon);*/

		return TRUE;

	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDCANCEL:
		case IDOK:
			EndDialog(hDlg, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}


Advertisement
If your icon is not going to be changed, don't bother manually loading it: You may add an icon/bitmap as a resource then statically set the picture control's image to the resource ID. No code is required.
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
First off LoadIcon is now replaced with LoadImage... (seem MSDN for LoadIcon)

Secondly, BM_SETIMAGE is for buttons, as far as I know you're trying to assign it to a static control so:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/staticcontrols/staticcontrolreference/staticcontrolmessages/stm_setimage.asp

Then it looks like your bitmap is a bigger then 16x16 pixels so you might need IMAGE_BITMAP instead. Another thing to try:
SendMessage(child, BM_SETIMAGE, (WPARAM) IMAGE_ICON, 			(LPARAM) hIcon);


change to:

SendMessage(hDlg, BM_SETIMAGE, (WPARAM) IMAGE_BITMAP, 			(LPARAM) hIcon);


Note the first and third param in SendMessage changed. See if that works. I'd recommend removing LoadIcon code just for your own sanity in the future. But I think the solution is in the link above.

Post back if you still cannot get it. I'm just getting to static controls in my Win32 wrapper.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Swish! I've got it working now using STM_SETIMAGE.

To summarise what I've found then:

1. Setting an app's icon trivially works.
2. Adding an icon control using the resource editor alone, on the other hand, doesn't appear to work (in VS2003).
3. Trying to set the icon's image with BM_SETIMAGE...doesn't work.
4. Trying to set the icon's image by sending a message directly to the dialog box (the line of code Mike2343 wrote)...didn't work either.
5. What worked in the end was this:

BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT message, WPARAM wParam,						   LPARAM lParam){	switch(message)	{	case WM_INITDIALOG:		HICON hIcon;		HWND  child;		hIcon = LoadIcon(g_instance, MAKEINTRESOURCE(IDI_FOHN));		child = GetDlgItem(hDlg, IDC_STATICX);		                SendMessage(child, STM_SETIMAGE, (WPARAM) IMAGE_ICON, 			(LPARAM) hIcon);		return TRUE;        ....


...and note that you need to change the default id for the icon from IDC_STATIC (for my icon I used IDC_STATICX for whatever reason) because IDC_STATIC is the id for controls that are not referenced in code.

Thanks everyone
Glad it worked. :)

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

This topic is closed to new replies.

Advertisement