Static inline unsigned int alphaBlend( const unsigned int bg, const unsigned int src) I personally use the ICO plug-in for Paint.NET to generate such resources. Icon format is by no ways limited to the predefined square sizes, and is actually better supported than 32-bit BMP format. Note that 32-bit icons are an easy way to represent bitmaps with an alpha channel. Follows the snippet that is compatible with the older Windows. On Windows 2K, though, the alpha channel is ignored by this API. On Windows XP, all you have to do is call DrawIconEx(hdc, left, top, hIcon, width, height, 0, NULL, DI_NORMAL). It might sound funny, but only recently, I faced a requirement to display true-color 32-bit icons on Windows 2000. What if you have 32-bit (XP-style) icons and Win2K? Anyway, the attached code (in the zipped demo) draws an icon in the menu bar, too. The technique presented here does work with the menu bar (i.e., the part that is always visible above the client area of your window, File Edit View Help), but the result is less than perfect (e.g., the underscore is drawn over the image), and requires messing up with the rectangle that Windows provides to the OnDrawItem function. On the other hand, the presented approach may be easily generalized to display custom colorful check signs. But, if some of these items with attached icons are checked, the check mark will override the custom drawing callback. We set the menu style to MNS_CHECKORBMP purely for aesthetic reasons. You will need a special function (published in the comments by b ga) to override this behavior. Unfortunately, the gray icon is displayed in full color when the item is highlighted (selected). Note that all styles like grayed, default, etc. The WM_MEASUREITEM message only asks for the size of the bitmap. If the resource file provides an icon for this item, the bitmap is extracted from the icon. We scan the menus as they are to be displayed, and add a flag that the item bitmap should be owner-drawn. The workaround: set the identifier & (one character) for the icon you used to call IDR_MAINFRAME. The method that maps the menu text to the icon uses an underscore (_) to replace the space character also, note that you can use the & character in an icon identifier, but there is a catch: the Windows Explorer will recognize such an icon as the first in the list and use it to represent your executable. For these cases, you can add an icon whose name corresponds to the name of the sub-menu. Or at least, you cannot set such an ID with the Resource Editor. Unfortunately, the menu items that contain sub-menus do not have menu IDs. A good icon editor (I use Paint.NET with the icon plug-in) can create icons of arbitrary size and color. It is the icon's responsibility to render itself nicely. The icon ID should be the same as the menu ID. To add images next to some menu items, as the ones you see on the screenshot, you simply add icons to your resources. Now, you can compile your application and see that nothing has changed. (WORD)lpmis- >itemID, (LPCTSTR)sItem, bitmap.bmWidth,īitmap.bmHeight, lpmis- >itemWidth, lpmis- >itemHeight) HICON hIcon = GetIconForItem(lpmis- >itemID) ĪfxTrace(_T(_FUNCTION_) _T( " : %d \"%s\"%dx%d => %dx%d\n"), GetMenu()- >GetMenuString(lpmis- >itemID, sItem, MF_BYCOMMAND) If ((lpmis=NULL)||(lpmis- >CtlType != ODT_MENU))ĬFrameWnd::OnMeasureItem(nIDCtl, lpmis) //not for a menu Void CMainFrame::OnMeasureItem( int nIDCtl, LPMEASUREITEMSTRUCT lpmis) PMenu- >SetMenuItemInfo(pos, &minfo, TRUE) ĪfxTrace(_T( " keep for %s id=%u\n"), (LPCTSTR)sItem, (WORD)minfo.wID) (LPCTSTR)sItem, (WORD)minfo.wID, 0) // size.cx) If (hIcon & !(minfo.fType & MFT_OWNERDRAW))ĪfxTrace(_T( " replace for \"%s\" id=%u width=%d\n"), PMenu- >GetMenuItemInfo(pos, &minfo, TRUE) Mnfo.dwStyle = MNS_CHECKORBMP | MNS_AUTODISMISS įor (UINT pos= 0 pos GetMenuItemCount() pos++) Void CMainFrame::OnInitMenuPopup(CMenu* pMenu, UINT nIndex, BOOL bSysMenu)ĪfxTrace(_T(_FUNCTION_) _T( " : %#0x\n"), pMenu- >GetSafeHmenu()) ĬFrameWnd::OnInitMenuPopup(pMenu, nIndex, bSysMenu) ::DestroyIcon(hIcon) // we use LR_SHARED instead HIcon, bitmap.bmWidth, bitmap.bmHeight, 0, NULL, DI_NORMAL) ::DrawIconEx(lpdis- >hDC, lpdis- >rcItem.left, lpdis- >rcItem.top, ::GetObject(iconinfo.hbmColor, sizeof(bitmap), &bitmap) HICON hIcon = GetIconForItem(lpdis- >itemID) If ((lpdis=NULL)||(lpdis- >CtlType != ODT_MENU)) Void CMainFrame::OnDrawItem( int nIDCtl, LPDRAWITEMSTRUCT lpdis) IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_SHARED) HIcon = (HICON)::LoadImage(::AfxGetResourceHandle(), sItem, cannot have resource items with space in name GetMenu()- >GetMenuString(itemID, sItem, MF_BYCOMMAND) MAKEINTRESOURCE(itemID), IMAGE_ICON, 0, 0,ĬString sItem // look for a named item in resources HIcon = (HICON)::LoadImage(::AfxGetResourceHandle(), HICON CMainFrame::GetIconForItem(UINT itemID) const
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |