PDA

Click to See Complete Forum and Search --> : Icon for a Dialog box


Sep 30th, 2000, 12:15 PM
Is there anyway to add an icon to a dialog box?

parksie
Sep 30th, 2000, 12:21 PM
Huh?

As an system menu icon for the dialogue, or as a picture in its client area?

Sep 30th, 2000, 02:48 PM
As a system menu icon.

parksie
Sep 30th, 2000, 05:04 PM
Use:

HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON);
SendMessage(hWndDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);

...this should go in WinMain, so that you can use the hInstance value (although it's nearly always 0x400000 or something like that). Replace IDI_ICON with the resource ID of your icon.
Use ICON_BIG to set the large icon.

Sep 30th, 2000, 09:28 PM
Thanks, that was exactly what i was looking for. You seem to know a bit, so heres another question...

I have an about dialog, and i want to display the icon in the client area next to the text. How do i do that?

parksie
Oct 1st, 2000, 04:23 AM
In your About dialogue (within the Dialog Editor), insert a Picture, and set it to Icon. It will then allow you to choose the icon's ID from a combo box. (I hope)

Oct 1st, 2000, 12:54 PM
Yup that worked, thanks alot for the help!

Oct 1st, 2000, 08:17 PM
Got another one for ya. How do i put an icon next to a menu item?

Vlatko
Oct 2nd, 2000, 04:51 AM
I don't know about VC++ but in VB the SetMenuItemBitmaps API function worked just fine(put bitmaps next to menu items) It should work in VC++ ,after all it's API.

Yonatan
Oct 2nd, 2000, 08:31 AM
First, you have to get the handle of the parent menu.
If the menu item is in the System Menu (I haven't tried adding icons to this, but it might work), then use this to get the handle:

HMENU hMenu = GetSystemMenu(hWndOfTheWindowWithTheMenuToWhichYouWantToAddIcons, FALSE);

If the menu item is in a "normal" menu, then first you should use GetMenu to get the handle of the menu bar, and then GetSubMenu to get the specific menu in the menu bar.
You start the count with zero. (Aka zero-based counting)
For example, if your menu bar looks like this:

File Edit View Help

Then the position of the File menu is zero, the position of the Edit menu is one, View is two, and Help is three.
Simple enough?

HMENU hMenu = GetMenu(hWndOfTheWindowWithTheMenuToWhichYouWantToAddIcons);
hMenu = GetSubMenu(hMenu, nTheZeroBasedPositionOfTheMenuInTheMenuBar);

if(hMenu == NULL) {
// There is no menu with that position
}

Now, after you used one of the above two methods to get the menu handle, you should load a bitmap, and set the bitmap to the menu item.
This is done with a call to SetMenuItemBitmaps.
Again, the position is zero-based:
If you picked the File menu, and its contents are New, Open, Save, Exit, then New is zero, Open is one, etc.

HBITMAP hBitmap = // Some bitmap-loading routine goes here...
// The bitmap size should be 13x13 pixels.
// Well, it usually is 13x13... The exact size is:
// GetSystemMetrics(SM_CXMENUCHECK) x GetSystemMetrics(SM_CYMENUCHECK)

BOOL bSuccess = SetMenuItemBitmaps(hMenu,
nTheZeroBasedPositionOfTheMenuItemInTheParentMenuWhichYouShouldAlreadyHaveSelected,
MF_BYPOSITION, hBitmap, hBitmap);

TIP: Use different variable names. :rolleyes:

If you want different bitmaps depending on the check state of the menu item, you can load two bitmaps, and then instead of hBitmap, hBitmap use hBitmapToUseWhenItIsNormal, hBitmapToUseWhenItIsChecked.
Or something like that. :rolleyes:

Then, you can test the bSuccess variable. If it is TRUE, then the function succeeded. If it is FALSE, then there's an error.

To get extended error information, call GetLastError.

Oct 2nd, 2000, 12:35 PM
Thanks for all the help Yonaton, but i cant seem to get it to work. Does anyone have any sample projects?

Oct 2nd, 2000, 02:45 PM
Ok, i got it working now. But it changes color when the mouse moves over it. If you look at the icons in the menus in VC++ they dont change color. How do i do that?

parksie
Oct 2nd, 2000, 02:53 PM
You'd need to use owner-drawn menus for that - look in the Platform SDK for the details. Also, V(ery) has some VB code to do this - look at that, and then you can transfer it to C++.