BCMenu 自绘菜单

CButton 类的4个文件 BtnSt,h BtnSt.CPP BCMenu,h BCMenu.cpp CButton 类的4个文件 BtnSt,h BtnSt.CPP BCMenu,h BCMenu.cpp 在网上的文件有错误,我改正后传上了。 立即下载
Cool Owner Drawn Menus with Bitmaps
Version 3.036 June 2005
By
Brent Corkum

 

Update Information

Download MFC MDI Source Code+Example (97KB)
Download MFC Dialog Sorce Code+Example (78KB)

 

 
What's new in Version 3.0

As you can see I've added the new Office XP drawing style for the menu's. I just got a machine with Windows XP on it and I noticed that the menu's in all our applications looked terrible. So I decided to do something about it and after 2 years of not looking at the class, added the new menu drawing style, and added lot's of fixes and user requests. Now the new drawing style isn't exactly like Microsoft's, but I got it so it looks good enough to me. For people that use the old class it's a simple matter of taking exchanging the old BCMenu .cpp and .h files with the new ones.

The class currently uses the new style on XP and the old style on Win9x/NT and 2000. However, if you like the new style and you want to use it on all Windows platforms, just change the following line at the top of the BCMenu.cpp file from:

UINT

to

UINT

Likewise, if you think I did a terrible job you can change the drawing style to the original one on all the platforms.

Other additions include support for images with greater than 16 colors. The example contains images with both 256 and 16 million colors. There is also an option for how to draw disabled options. In XP mode they are not selected but this can be changed. I also fixed the problem with multiple menu items with the same command ID not getting images (only the first one did!). I also went through most of the articles on codeguru and codeproject and fixed a number of bugs that people pointed out. See the update page for more information.

Introduction
This class, BCMenu, implements owner drawn menus derived from the CMenu class. The purpose of which is to mimic the menu style used in Visual C++ 5.0/6.0 and MS Word. I can't take credit for all the code, some portions of it were taken from code supplied by Ben Ashley and Girish Bharadwaj. The difference between their codes and this one is quite simple, this one makes it very easy to build those cool menus with bitmaps into your application. I've removed the Icon loading stuff and replaced it with Bitmaps. The bitmaps allow you to use the 16X15 toolbar bitmaps directly from your toolbars in the resource editor. As well, there is no scaling of the bitmaps so they always look good. You can also load Bitmap resources and define bitmaps for your check marks. I've also added the default checkmark drawing stuff, separators, proper alignment of keyboard accelerator text, keyboard shortcuts, proper alignment of popup menu items, proper system color changes when the Display Appearance changes, plus bug fixes to the Ben Ashley's LoadMenu function for complex submenu systems. I made quite a few other modifications as well, too many to list or remember. If you find any bugs, memory leaks, or just better ways of doing things, please let me know. I used Visual C++ 5.0 and I have not tested compatibility with earlier VC versions. I've tested it on Win 95/NT at various resolutions and color palette sizes.

Installation (MDI Application)

Well, enough of the boring stuff, lets talk about implementation. To make it easy I'm first going to list step by step the method for implementing the menus into a MDI application:
  1. Create your MDI application using the App Wizard.
  2. Insert the BCMenu.cpp and BCMenu.h files into your Workspace.
  3. Add the following public member functions to your CMainFrame class in the MainFrm.h header file:
HMENU NewMenu();
HMENU NewDefaultMenu();
  1. Add the following public member variables to your CMainFrame class in the MainFrm.h header file:
BCMenu m_menu,m_default;
  1. Add the line:
#include "BCMenu.h"

to the top of the MainFrm.h header file.

  1. Open the Mainfrm.cpp implementation file and add the NewMenu and NewDefaultMenu member functions as listed below. IMPORTANT: Make sure you replace the IDR_MYMENUTYPE menu id in the below LoadMenu call to the menu id associated with the menu's in your application. Look in the menus folder of the Resources view.
 
HMENU CMainFrame::NewMenu()
{
  static UINT toolbars[]={
     IDR_MAINFRAME
  };
  // Load the menu from the resources
  m_menu.LoadMenu(IDR_MYMENUTYPE);  // ****replace IDR_MENUTYPE with your menu ID****
  // Use ModifyODMenu to add a bitmap to a menu options.The first parameter
  // is the menu option text string.If it's NULL, keep the current text
  // string.The second option is the ID of the menu option, or the menu
  // option text (can use this for adding bitmaps to popup options) to change.
  // The third option is the resource ID of the bitmap.This can also be a
  // toolbar ID in which case the class searches the toolbar for the
  // appropriate bitmap.Only Bitmap and Toolbar resources are supported.
  // m_menu.ModifyODMenu(NULL,ID_WINDOW_NEW,IDB_WINDOW_NEW);
  // m_menu.ModifyODMenu(NULL, "&Tile",IDB_WINDOW_TILE);
  // Another method for adding bitmaps to menu options is through the
  // LoadToolbars member function.This method allows you to add all
  // the bitmaps in a toolbar object to menu options (if they exist).
  // The first function parameter is an array of toolbar id's.
  // The second is the number of toolbar id's. There is also a
  // function called LoadToolbar that just takes an id.
  m_menu.LoadToolbars(toolbars,1);
  return(m_menu.Detach());
}
HMENU CMainFrame::NewDefaultMenu()
{
  m_default.LoadMenu(IDR_MAINFRAME);
  m_default.LoadToolbar(IDR_MAINFRAME);
  return(m_default.Detach());
}
  1. Edit the InitInstance() member function of your CWinApp derived class and add following highlighted code in the position noted:
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  return FALSE;
m_pMainWnd = pMainFrame;
// This code replaces the MFC created menus with the Ownerdrawn versions
pDocTemplate->m_hMenuShared=pMainFrame->NewMenu();
pMainFrame->m_hMenuDefault=pMainFrame->NewDefaultMenu();
 
// This simulates a window being opened if you don't have
// a default window displayed at startup
pMainFrame->OnUpdateFrameMenu(pMainFrame->m_hMenuDefault);
 
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
  1. Add the message handlers for the WM_MEASUREITEM, WM_MENUCHAR, and WM_INITMENUPOPUP messages to your CMainFrame class. Do this by right clicking on the CMainFrame class in the ClassView and selecting Add Windows Message Handler. Choose the Window option from the Filter for messages available to class combo box. Select the message and add the handler. Then edit the handler and add the below code.
//This handler ensure that the popup menu items are drawn correctly
void CMainFrame::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
  BOOL setflag=FALSE;
  if(lpMeasureItemStruct->CtlType==ODT_MENU){
     if(IsMenu((HMENU)lpMeasureItemStruct->itemID)){
       CMenu* cmenu=CMenu::FromHandle((HMENU)lpMeasureItemStruct->itemID);
       if(BCMenu::IsMenu(cmenu)){
         m_menu.MeasureItem(lpMeasureItemStruct);
         setflag=TRUE;
       }
     }
  }
  if(!setflag)CMDIFrameWnd::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}
 
//This handler ensures that keyboard shortcuts work
LRESULT CMainFrame::OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu) 
{
  LRESULT lresult;
  if(BCMenu::IsMenu(pMenu))
     lresult=BCMenu::FindKeyboardShortcut(nChar, nFlags, pMenu);
  else
     lresult=CMDIFrameWnd::OnMenuChar(nChar, nFlags, pMenu);
  return(lresult);
}
 
//This handler updates the menus from time to time
void CMainFrame::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) 
{
  CMDIFrameWnd::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
  if(!bSysMenu){
    if(BCMenu::IsMenu(pPopupMenu))BCMenu::UpdateMenu(pPopupMenu);
  }
}
  1. If you are debugging or you are mixing standard menus with the BCMenu's (maybe you have different Document templates using the standard menu's) then you should turn on RTTI in the project settings.
Well, that's it. Compile the program and look in the File menu. You should see the bitmaps. I've tried the menus with context menus and they seem to work fine. I also have a small sample program (source+exe) that also uses bitmaps for check marks and bitmaps for a few menu options and has a context menu when you right click in a view.
Installation (SDI Application)
If you want to add the menus to a SDI application:
  1. Create your SDI application using the App Wizard.
  2. Insert the BCMenu.cpp and BCMenu.h files into your Workspace.
  3. Add the following public member function to your CMainFrame class in the MainFrm.h header file:
HMENU NewMenu();
  1. Add the following public member variables to your CMainFrame class in the MainFrm.h header file:
BCMenu m_menu;
  1. Add the line:
#include "BCMenu.h"

to the top of the MainFrm.h header file.

  1. Open the Mainfrm.cpp implementation file and add the NewMenu and NewDefaultMenu member functions as listed below. IMPORTANT: Make sure you replace the IDR_MAINFRAME menu id in the below LoadMenu call to the menu id associated with the menu's in your application. Look in the menus folder of the Resources view.
 
HMENU CMainFrame::NewMenu()
{
  // Load the menu from the resources
  m_menu.LoadMenu(IDR_MAINFRAME);  // ****replace IDR_MAINFRAME with your menu ID****
 
  // Use ModifyODMenu to add a bitmap to a menu options.The first parameter
  // is the menu option text string.If it's NULL, keep the current text
  // string.The second option is the ID of the menu option to change.
  // The third option is the resource ID of the bitmap.This can also be a
  // toolbar ID in which case the class searches the toolbar for the
  // appropriate bitmap.Only Bitmap and Toolbar resources are supported.
  // m_menu.ModifyODMenu(NULL,ID_ZOOM,IDB_ZOOM);
 
  // Another method for adding bitmaps to menu options is through the
  // LoadToolbar member function.This method allows you to add all
  // the bitmaps in a toolbar object to menu options (if they exist).
  // The function parameter is an the toolbar id.
  // There is also a function called LoadToolbars that takes an
  // array of id's.
  m_menu.LoadToolbar(IDR_MAINFRAME);
  return(m_menu.Detach());
}
  1. Edit the InitInstance() member function of your CWinApp derived class and add following highlighted code in the position noted:
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
  return FALSE;
 
  CMenu* pMenu = m_pMainWnd->GetMenu();
  if (pMenu)pMenu->DestroyMenu();
  HMENU hMenu = ((CMainFrame*) m_pMainWnd)->NewMenu();
  pMenu = CMenu::FromHandle( hMenu );
  m_pMainWnd->SetMenu(pMenu);
  ((CMainFrame*)m_pMainWnd)->m_hMenuDefault = hMenu;
  1. Add the message handlers for the WM_MEASUREITEM, WM_MENUCHAR, and WM_INITMENUPOPUP messages to your CMainFrame class. Do this by right clicking on the CMainFrame class in the ClassView and selecting Add Windows Message Handler. Choose the Window option from the Filter for messages available to class combo box. Select the message and add the handler. Then edit the handler and add the below code.
//This handler ensure that the popup menu items are drawn correctly
void CMainFrame::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
  BOOL setflag=FALSE;
  if(lpMeasureItemStruct->CtlType==ODT_MENU){
     if(IsMenu((HMENU)lpMeasureItemStruct->itemID)){
       CMenu* cmenu=CMenu::FromHandle((HMENU)lpMeasureItemStruct->itemID);
       if(BCMenu::IsMenu(cmenu)){
         m_menu.MeasureItem(lpMeasureItemStruct);
         setflag=TRUE;
       }
     }
  }
  if(!setflag)CFrameWnd::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}
 
//This handler ensures that keyboard shortcuts work
LRESULT CMainFrame::OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu) 
{
  LRESULT lresult;
  if(BCMenu::IsMenu(pMenu))
     lresult=BCMenu::FindKeyboardShortcut(nChar, nFlags, pMenu);
  else
     lresult=CFrameWnd::OnMenuChar(nChar, nFlags, pMenu);
  return(lresult);
}
 
//This handler updates the menus from time to time
void CMainFrame::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) 
{
  CFrameWnd::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
  if(!bSysMenu){
     if(BCMenu::IsMenu(pPopupMenu))BCMenu::UpdateMenu(pPopupMenu);
  }
}
  1. If you are debugging or you are mixing standard menus with the BCMenu's (maybe you have different Document templates using the standard menu's) then you should turn on RTTI in the project settings.
BCMenu:: original_drawmode= BCMENU_DRAWMODE_XP; BCMenu:: original_drawmode= BCMENU_DRAWMODE_ORIGINAL;

BCMenu的源文件 使用CButtonST的时候会提示BCMenu.h找不到 下载此文件解压到同目录就可以了 立即下载

相关推荐

用BCMenu动态创建弹出菜单示例

用BCMenu动态创建带图标的弹出菜单,代码中有详细说明。 附带包含CImageList的简单使用方法。

按钮

效果一:在按钮上加入Icon,使Icon和文字同时显示 假设按钮ID为IDC_BUTTON11. 添加成员变量CButtonST m_btn;2. 添加Icon资源,设其ID设为IDI_ICON13. 在OnInitDialog函数中初始化按钮m_btn.SubclassDlgItem(IDC_BUTTON1,this);m_btn.SetIcon(IDI_ICON1);m_btn.SetFl

shjte的专栏 917

华为海思IC设计岗位笔试全解析:物理、电路与工艺三大方向备考指南

本文全面解析华为海思IC设计岗位笔试的物理、电路与工艺三大方向备考要点。针对物理设计方向,重点讲解半导体能带理论、载流子输运机制等核心考点;电路设计方向涵盖Verilog语法、UVM验证等关键技术;工艺方向则深入探讨FinFET工艺、光刻技术等前沿知识。提供高效备考策略和应试技巧,助力考生提升通过率。

weixin_29196613的博客 1086

BCMenu一个好用的图形菜单类

//*************************************************************************// BCMenu.h : header file// Version : 3.036// Date : June 2005// Author : Brent Corkum// Email :  corkum@rocscience.com// Lat

darkduck9989的专栏 6520

菜鸟-需求预测与分仓规划

通过修改模型的损失函数对问题进行求解,比其它单模型的效果好很多。

永不理解_fdu的博客 4882

主题045:四足机器人的运动仿真

四足机器人(Quadruped Robot)是模仿哺乳动物四肢运动设计的机器人系统,具有优异的地形适应性和运动稳定性。与双足机器人相比,四足机器人在静态和动态稳定性方面具有天然优势,能够在复杂地形(如楼梯、斜坡、碎石地面)上稳定行走,因此在野外探索、救援任务、军事应用等领域具有广阔的应用前景。本教程将介绍四足机器人的运动学建模、步态规划、稳定性分析和控制策略,并通过Python仿真验证相关理论。典型的四足机器人每条腿包含3个关节:常见的腿部构型包括:对于单腿,足部位置 pfoot\mathbf{p}_{fo

kkchenjj的博客 683

全功能的自绘菜单BCmenu类

翻译来源:https://www.codeproject.com/Articles/22/Cool-Owner-Drawn-Menus-with-Bitmaps-Version 这个类很常见,在很多源码中都有见到。 包含BCMenu.cpp和BCMenu.h 该类实现了一个所有者绘制的菜单类,模仿XP,Office和Visual C ++中使用的菜单样式 下载M

Greless的后花园 1525

【YOLO系列】YOLOv8详解:模型结构、损失函数、训练方法及代码实现

YOLOv8是Ultralytics团队2023年推出的高效目标检测框架,在速度与精度平衡、多任务支持和易用性方面实现突破。模型采用改进的CSPDarknet主干网络、C2f模块和SPPF加速结构,结合PAN-FPN特征融合和解耦检测头设计,支持目标检测、实例分割和关键点检测任务。创新性地使用CIoU定位损失、Focal Loss置信度损失和动态样本分配策略,显著提升检测性能。实验表明,YOLOv8较前代模型mAP提升5-10%,在TensorRT加速下可达1000FPS,提供5种尺寸灵活选择,成为实时目标

一碗白开水一 2779

关于BCMenu在VC6和Vc9,VC2008一些用法差异

关于BCMenu在VC6和Vc9,VC2008一些用法差异 SystemParametersInfo SPI_GETNONCLIENTMETRICS 总是返回失败的原因

opposeeee的专栏 1258

2025机械考研复试面试问题汇总篇(含13门科目),考研机械复试专业面试常见重点问题总结!考研机械复试专业面试准备看这一篇就够了!

(完整版内容:)1.汇集了本科各科的重点知识及多个机械名校800多个常见问题,共8.7万字185页,涵盖机械所以专业课的重点知识和常见问答。 2.涉及机械大类重点科目,包括:材料力学、控制工程、数控技术、机械制造技术基础、理论力学、液压传动、机械设计、机械原理、工程材料、互换性与技术测量、测试技术、汽车理论、程序设计与C语言等13门必修课。

weixin_56510835的博客 1862

大数据在电力行业的应用案例解析 -【电力技术】(十)电力数字化运营与大数据精准服务体系建设

电力数字化运营,是大数据在电力行业从技术场景走向业务价值的最终落点。它以数据为纽带,将用户、设备、服务、收益、风险全部纳入统一闭环,实现从 “管好电” 向 “运营好能源资产” 升级。在新型电力系统与市场化改革双重驱动下,谁能用好数据、建好闭环、做好运营,谁就能在服务、成本、效益、安全上建立长期优势。

getusushu的博客 70

Cool Owner Drawn Menus with Bitmaps - Version 3.03

Cool Owner Drawn Menus with Bitmaps - Version 3.03 By Brent Corkum, 28 Apr 2002    4.92 (178 votes)   Download MFC MDI Examp

Never too late to study 1478

无人机视角下基于YOLOv5/v8/v10的车辆检测系统:从算法原理到UI界面实战

随着无人机技术的普及和智慧城市的发展,无人机视角下的车辆检测在交通监控、战场侦察、灾害救援等领域展现出巨大的应用价值。然而,无人机航拍图像存在目标尺度小、视角多变、背景复杂等挑战。本文深入探讨了如何利用YOLOv5、YOLOv8、YOLOv10三个代表性目标检测算法,构建一套完整的无人机车辆检测系统。文章不仅详细解析了各版本YOLO的核心技术演进,还提供了从数据集准备、模型训练、性能评估到基于PyQt5/PySide6的图形界面开发的完整代码实现。通过本文,读者将能够掌握构建无人机实时车辆检测系统的全流程技

YOLO 172

BCMenu 菜单类

BCMenu一个好用的图形菜单类

学习是遗忘的最好方法! 2810

OpenCV入门(一)Python环境的搭建

因为有点Python基础,并且Python是比较好入门的编程语言,所以,机器视觉后面打算在Python这个平台下进行。

小幽余生不加糖 8123

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

BCMenu 使用教程

在 SDI 中使用 BCMenu,首先,自然是建立一个 SDI 工程,然后将“BCMenu.h”和“BCMenu.cpp”放入文件夹下,单击"工程"->"增加到工程"->"文件",将"BCMenu.h"和"BCMenu.cpp"两个文件加入工程。     在 MainFrm.h

802

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

自绘菜单

<br /><br />完美实现真彩自绘菜单<br /><br />作者:阿福(geforce_zf)<br />下载源代码<br /><br />一、提出问题 <br /><br />  在VCKBASE上读到《自绘菜单的实现》[作者:querw]。应用的我自己的正在进行的工程后发现效果不错,可是有存在许多问题。整个类的设计方面存在很多缺陷(先天,后天的),存在的主要问题如下:当应用在多文档界面(MDI)中的时候,无法对系统自动添加菜单和文档模板菜单进行自绘(比如无法对文件->最近文件(MRU)菜单项中的

caowei880123的专栏 3241

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

BCMenu类的使用说明

1、 把BCMenu.cpp 和 BCMenu.h 文件添加到工程中;2、 在MainFrm.h头文件中包含BCMenu.h头文件;3、 在MainFrm.h头文件中添加公有成员变量和成员函数:#include "BCMenu.h"public:       BCMenu m_menu;       HMENU NewMenu();4、 在MainFrm.cpp文件中实

1281

菜单设置图标 BCMenu

BCMenu *menu = new BCMenu; menu->SetMenuDrawMode(TRUE); menu->LoadMenu(IDR_MAINFRAME); menu->SetBitmapBackground(RGB(255,0,255)); me

_free 1216

菜单的制作暨BCMenu类的使用说明

xuexiuyin的专栏 2291

实现自绘菜单

一、提出问题   在VCKBASE上读到《自绘菜单的实现》[作者:querw]。应用的我自己的正在进行的工程后发现效果不错,可是有存在许多问题。整个类的设计方面存在很多缺陷(先天,后天的),存在的主要问题如下: 当应用在多文档界面(MDI)中的时候,无法对系统自动添加菜单和文档模板菜单进行自绘(比如无法对文件->最近文件(MRU)菜单项中的文件列表就是系统自动添加)。原因是类内部没有

透明果冻 2008

在Dialog(基本对话框)中使用BCMenu

如果说在 SDI 和 MDI 中使用 BCMenu 很麻烦的话,那么在 Dialog 中使用起来就是相当的简单了,不过因为在网页上没有说明 Dialog 里使用 BCMenu 的方法,只有一个很麻烦的示例,所以我的做法是新建一个基本对话框后和示例进行对比得到的,如果出现问题的话(至少我还没发现问题),请与示例文件进行对比。     首先新建一个MFC基于Dialog的程序,然后自然是创建一个菜单

3408

使用ButtonST中BCMenu实现XP菜单时,没有XP菜单效果

本地在调试,发行版中使用如下代码,在弹出的菜单中都有XP风格 BCMenu menu; menu.LoadMenu(IDR_TASK_MENU); BCMenu *pPopupMenu = (BCMenu *)menu.GetSubMenu(0); CPoint point; GetCursorPos(&point); AfxGetMainWnd()->SetForegroundW

zttjhm的专栏 427

当使用BCMenu 时,发现菜单不显示不一致时的处理

 当使用网络上的BCMenu 时,发现菜单不显示不一致时的处理,如右键菜单的 大小不一样,需要重载下面这函数。 void CMymenuView::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct) { BOOL setflag=FALSE; if(lpMeasureItemStruct->CtlType

guoquan2003的专栏 828
上一篇: RedrawWindow 和 InvalidateRect 刷新
下一篇: 对FillRectangle理解有误 TranslateTransform的应用
海浪
博客等级 码龄20年 27粉丝 61原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值