SetMenuItemBitmaps

fangchao918628 2008-12-16 03:13:43

HBITMAP hbmp_Lock;
HINSTANCE g_hThisDll = NULL;

VOID
_LoadResources(VOID)
{
hbmp_Lock = LoadBitmap(g_hThisDll, MAKEINTRESOURCE(IDB_LOCK));
}

SetMenuItemBitmaps(hSubMenu, 0, MF_BYPOSITION, hbmp_Lock, hbmp_Lock);

搞不懂这个函数SetMenuItemBitmaps没起作用,没有什么反应!
...全文
1556 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
cai_niao_yi_zhi 2011-09-06
  • 打赏
  • 举报
回复
楼主总结一下吧!让我们这些初学者也学习学习!
yangkangyou123 2011-07-05
  • 打赏
  • 举报
回复
楼主不厚道啊,问题解决了也不给个结论,典型的过河拆桥哦...
fangchao918628 2008-12-16
  • 打赏
  • 举报
回复
解决了,非常感谢各位!
Tinary3v0 2008-12-16
  • 打赏
  • 举报
回复
CMenu* pMenu = GetMenu();

HMENU hSubMenu = ::GetSubMenu( pMenu->m_hMenu, 0 );

HBITMAP hbmp_Lock;
hbmp_Lock = LoadBitmap( AfxGetResourceHandle(), MAKEINTRESOURCE( IDB_BITMAP1 ) );

SetMenuItemBitmaps( hSubMenu, 0, MF_BYPOSITION, hbmp_Lock, hbmp_Lock );

楼主最重要的还是 hSubMenu取得的不对,我用上面的代码成功的在单文档的文件菜单上的新建(第一项)菜单上 增加了一个位图。

另外楼主需要检查一下 你取得hbmp_Lock的地方 这个位图是否真的加载成功了。

cnzdgs 2008-12-16
  • 打赏
  • 举报
回复
那就是弹出菜单,把CreateMenu改成CreatePopupMenu。
fangchao918628 2008-12-16
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 cnzdgs 的回复:]
是弹出菜单还是主菜单?如果是弹出菜单要用CreatePopupMenu;主菜单不能这样设置。
另外,你给出的是菜单项索引,要用MF_BYPOSITION。
[/Quote]
没想明白!我要实现的功能是在点击文件右键菜单在我添加的菜单前面加个图标!
一是我加的!
1-》1.1
1.2
2
3
5
cnzdgs 2008-12-16
  • 打赏
  • 举报
回复
是弹出菜单还是主菜单?如果是弹出菜单要用CreatePopupMenu;主菜单不能这样设置。
另外,你给出的是菜单项索引,要用MF_BYPOSITION。
fangchao918628 2008-12-16
  • 打赏
  • 举报
回复

HMENU hSubMenu = CreateMenu();

if(hSubMenu)
{
InsertMenu(hSubMenu,
0,
MF_STRING|MF_BYPOSITION,
idCmd++,
szMenuText2);



hbmp_Lock = LoadBitmap(g_hThisDll, MAKEINTRESOURCE(IDB_LOCK));
if(hbmp_Lock !=0)
{
int a = GetLastError();
char buf[10]={0};
itoa(a,buf,10);
MessageBox(NULL,buf,"123",MB_OK);
}


SetMenuItemBitmaps(hSubMenu, 0, MF_BYCOMMAND, hbmp_Lock, hbmp_Lock);

InsertMenu(hSubMenu,1,MF_SEPARATOR|MF_BYPOSITION,0,NULL);

InsertMenu(hSubMenu,
2,
MF_STRING|MF_BYPOSITION,
idCmd++,
szMenuText3);

InsertMenu(hSubMenu,3,MF_SEPARATOR|MF_BYPOSITION,0,NULL);

InsertMenu(hSubMenu,
4,
MF_STRING|MF_BYPOSITION,
idCmd++,
szMenuText4);
fangchao918628 2008-12-16
  • 打赏
  • 举报
回复
判断hSubMenu是否为真通过了 !
fangchao918628 2008-12-16
  • 打赏
  • 举报
回复
hSubMenu = CreateMenu();
孤客天涯 2008-12-16
  • 打赏
  • 举报
回复
hSubMenu你是怎么取的,这个值对吗?
fangchao918628 2008-12-16
  • 打赏
  • 举报
回复
看了 hbmp_Lock 不为空!
改成MF_BYCOMMAND也 没什么反应~!
ilovedrv 2008-12-16
  • 打赏
  • 举报
回复
BYTE CheckOrUncheckMenuItem(BYTE bMenuItemID, HMENU hmenu)
{
DWORD fdwMenu;
static BYTE fbAttributes;

switch (bMenuItemID)
{
case IDM_REGULAR:

// Whenever the Regular menu item is selected, add a
// check mark to it and then remove checkmarks from
// any font-attribute menu items.

CheckMenuItem(hmenu, IDM_REGULAR, MF_BYCOMMAND |
MF_CHECKED);

if (fbAttributes & ATTRIBMASK)
{
CheckMenuItem(hmenu, IDM_BOLD, MF_BYCOMMAND |
MF_UNCHECKED);
CheckMenuItem(hmenu, IDM_ITALIC, MF_BYCOMMAND |
MF_UNCHECKED);
CheckMenuItem(hmenu, IDM_ULINE, MF_BYCOMMAND |
MF_UNCHECKED);
}
fbAttributes = IDM_REGULAR;
return fbAttributes;

case IDM_BOLD:
case IDM_ITALIC:
case IDM_ULINE:

// Toggle the check mark for the selected menu item and
// set the font attribute flags appropriately.

fdwMenu = GetMenuState(hmenu, (UINT) bMenuItemID,
MF_BYCOMMAND);
if (!(fdwMenu & MF_CHECKED))
{
CheckMenuItem(hmenu, (UINT) bMenuItemID,
MF_BYCOMMAND | MF_CHECKED);
fbAttributes |= bMenuItemID;
}
else
{
CheckMenuItem(hmenu, (UINT) bMenuItemID,
MF_BYCOMMAND | MF_UNCHECKED);
fbAttributes ^= bMenuItemID;
}

// If any font attributes are currently selected,
// remove the check mark from the Regular menu item;
// if no attributes are selected, add a check mark
// to the Regular menu item.

if (fbAttributes & ATTRIBMASK)
{
CheckMenuItem(hmenu, IDM_REGULAR,
MF_BYCOMMAND | MF_UNCHECKED);
fbAttributes &= (BYTE) ~IDM_REGULAR;
}
else
{
CheckMenuItem(hmenu, IDM_REGULAR,
MF_BYCOMMAND | MF_CHECKED);
fbAttributes = IDM_REGULAR;
}

return fbAttributes;
}
}
ilovedrv 2008-12-16
  • 打赏
  • 举报
回复
// Function prototypes

LRESULT APIENTRY MainWndProc(HWND, UINT, WPARAM, LPARAM);
HBITMAP GetMyCheckBitmaps(UINT);
BYTE CheckOrUncheckMenuItem(BYTE, HMENU);
The following example shows the portions of the window procedure that create the check-mark bitmaps; set the check-mark attribute of the Bold, Italic, and Underline menu items; and destroy check-mark bitmaps.

LRESULT APIENTRY MainWndProc(HWND hwndMain, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

static HBITMAP hbmpCheck; // handle to checked bitmap
static HBITMAP hbmpUncheck; // handle to unchecked bitmap
static HMENU hmenu; // handle to main menu
BYTE fbFontAttrib; // font-attribute flags

switch (uMsg)
{
case WM_CREATE:

// Call the application-defined GetMyCheckBitmaps
// function to get the predefined checked and
// unchecked check box bitmaps.

hbmpCheck = GetMyCheckBitmaps(CHECK);
hbmpUncheck = GetMyCheckBitmaps(UNCHECK);

// Set the checked and unchecked bitmaps for the menu
// items.

hmenu = GetMenu(hwndMain);
SetMenuItemBitmaps(hmenu, IDM_BOLD, MF_BYCOMMAND,
hbmpUncheck, hbmpCheck);

SetMenuItemBitmaps(hmenu, IDM_ITALIC, MF_BYCOMMAND,
hbmpUncheck, hbmpCheck);
SetMenuItemBitmaps(hmenu, IDM_ULINE, MF_BYCOMMAND,
hbmpUncheck, hbmpCheck);

return 0;

case WM_COMMAND:
switch (LOWORD(wParam))
{
// Process the menu commands.

case IDM_REGULAR:
case IDM_BOLD:
case IDM_ITALIC:
case IDM_ULINE:

// CheckOrUncheckMenuItem is an application-
// defined function that sets the menu item
// checkmarks and returns the user-selected
// font attributes.

fbFontAttrib = CheckOrUncheckMenuItem(
(BYTE) LOWORD(wParam), hmenu);

// Set the font attributes.

return 0;

// Process other command messages.

default:
break;
}

break;

// Process other window messages.

case WM_DESTROY:

// Destroy the checked and unchecked bitmaps.

DeleteObject(hbmpCheck);
DeleteObject(hbmpUncheck);

PostQuitMessage(0);
break;

default:
return DefWindowProc(hwndMain, uMsg, wParam, lParam);
}
return NULL;
}

HBITMAP GetMyCheckBitmaps(UINT fuCheck)
{
COLORREF crBackground; // background color
HBRUSH hbrBackground; // background brush
HBRUSH hbrTargetOld; // original background brush
HDC hdcSource; // source device context
HDC hdcTarget; // target device context
HBITMAP hbmpCheckboxes; // handle to check-box bitmap
BITMAP bmCheckbox; // structure for bitmap data
HBITMAP hbmpSourceOld; // handle to original source bitmap
HBITMAP hbmpTargetOld; // handle to original target bitmap
HBITMAP hbmpCheck; // handle to check-mark bitmap
RECT rc; // rectangle for check-box bitmap
WORD wBitmapX; // width of check-mark bitmap
WORD wBitmapY; // height of check-mark bitmap

// Get the menu background color and create a solid brush
// with that color.

crBackground = GetSysColor(COLOR_MENU);
hbrBackground = CreateSolidBrush(crBackground);

// Create memory device contexts for the source and
// destination bitmaps.

hdcSource = CreateCompatibleDC((HDC) NULL);
hdcTarget = CreateCompatibleDC(hdcSource);

// Get the size of the system default check-mark bitmap and
// create a compatible bitmap of the same size.

wBitmapX = GetSystemMetrics(SM_CXMENUCHECK);
wBitmapY = GetSystemMetrics(SM_CYMENUCHECK);

hbmpCheck = CreateCompatibleBitmap(hdcSource, wBitmapX,
wBitmapY);

// Select the background brush and bitmap into the target DC.

hbrTargetOld = SelectObject(hdcTarget, hbrBackground);
hbmpTargetOld = SelectObject(hdcTarget, hbmpCheck);

// Use the selected brush to initialize the background color
// of the bitmap in the target device context.

PatBlt(hdcTarget, 0, 0, wBitmapX, wBitmapY, PATCOPY);

// Load the predefined check box bitmaps and select it
// into the source DC.

hbmpCheckboxes = LoadBitmap((HINSTANCE) NULL,
(LPTSTR) OBM_CHECKBOXES);

hbmpSourceOld = SelectObject(hdcSource, hbmpCheckboxes);

// Fill a BITMAP structure with information about the
// check box bitmaps, and then find the upper-left corner of
// the unchecked check box or the checked check box.

GetObject(hbmpCheckboxes, sizeof(BITMAP), &bmCheckbox);

if (fuCheck == UNCHECK)
{
rc.left = 0;
rc.right = (bmCheckbox.bmWidth / 4);
}
else
{
rc.left = (bmCheckbox.bmWidth / 4);
rc.right = (bmCheckbox.bmWidth / 4) * 2;
}

rc.top = 0;
rc.bottom = (bmCheckbox.bmHeight / 3);

// Copy the appropriate bitmap into the target DC. If the
// check-box bitmap is larger than the default check-mark
// bitmap, use StretchBlt to make it fit; otherwise, just
// copy it.

if (((rc.right - rc.left) > (int) wBitmapX) ||
((rc.bottom - rc.top) > (int) wBitmapY))
{
StretchBlt(hdcTarget, 0, 0, wBitmapX, wBitmapY,
hdcSource, rc.left, rc.top, rc.right - rc.left,
rc.bottom - rc.top, SRCCOPY);
}

else
{
BitBlt(hdcTarget, 0, 0, rc.right - rc.left,
rc.bottom - rc.top,
hdcSource, rc.left, rc.top, SRCCOPY);
}

// Select the old source and destination bitmaps into the
// source and destination DCs, and then delete the DCs and
// the background brush.

SelectObject(hdcSource, hbmpSourceOld);
SelectObject(hdcTarget, hbrTargetOld);
hbmpCheck = SelectObject(hdcTarget, hbmpTargetOld);

DeleteObject(hbrBackground);
DeleteObject(hdcSource);
DeleteObject(hdcTarget);

// Return a handle to the new check-mark bitmap.

return hbmpCheck;
}
ilovedrv 2008-12-16
  • 打赏
  • 举报
回复
那个地方调用的,hSubMenu和hbmp_Lock是否有效,看看下面这个例子

#include "men3.h"

MainMenu MENU
BEGIN
POPUP "&Character"
BEGIN
MENUITEM "®ular", IDM_REGULAR, CHECKED
MENUITEM SEPARATOR
MENUITEM "&Bold", IDM_BOLD
MENUITEM "&Italic", IDM_ITALIC
MENUITEM "&Underline", IDM_ULINE
END
END

Here are the relevant contents of the application's header file.

// Menu-item identifiers

#define IDM_REGULAR 0x1
#define IDM_BOLD 0x2
#define IDM_ITALIC 0x4
#define IDM_ULINE 0x8

// Check-mark flags

#define CHECK 1
#define UNCHECK 2

// Font-attribute mask

#define ATTRIBMASK 0xe
孤客天涯 2008-12-16
  • 打赏
  • 举报
回复
MF_BYPOSITION改成MF_BYCOMMAND试试
yjgx007 2008-12-16
  • 打赏
  • 举报
回复
hbmp_Lock有值吗?

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧