|
楼主 |
发表于 2011-6-21 19:07
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
之七:传递控制菜单
6.菜单出来了,发现不能控制,又再次晕倒,不知所措了
解决办法,是通过OVerride窗体的WndProc方法,将菜单的事件,除了你自己的菜单以外的,都给传到OCX中。
在OCX中增加一个接口方法:ExecuteMenuMessage,它直接调用OnMenuMessage,呵呵,这样就行了,代码如下:
protected override void WndProc(ref Message m)
{
IntPtr lWParam=IntPtr.Zero ;
switch (m.Msg)
{
case WindowsMessages.WM_INITMENU :
case WindowsMessages.WM_ENTERIDLE:
case WindowsMessages.WM_MENUSELECT:
case WindowsMessages.WM_INITMENUPOPUP:
case WindowsMessages.WM_COMMAND :
ExecuteMenuMessage(m.Msg, m.WParam, m.LParam);
//Console.Write(m.Msg.ToString("X"));
//Console.Write(" ");
//Console.Write(lWParam.ToString("X"));
//Console.Write(" ");
//Console.WriteLine(m.LParam.ToString("X"));
break;
}
base.WndProc(ref m);
}
现在菜单信息也传入了,哦,创建菜单的程序如下,酷吧:
private void Form1_Load(object sender, EventArgs e)
{
//if (this.Menu == null)
{
this.Menu = new MainMenu();
CreateFileMenu();
IntPtr hMainMenu = this.Menu.Handle;
this.axFramerControl1.MainFormMenu = hMainMenu;
this.axFramerControl1.MainForm = this.Handle;
}
}
private void CreateFileMenu()
{
IntPtr pFileMenu = MenuHelper.CreatePopupMenu();
MenuHelper.AppendMenu(pFileMenu, MenuFlags.MF_STRING, FrameMenuMessage.MNU_NEW, "新建(&N)...\tCtrl+N");
MenuHelper.AppendMenu(pFileMenu, MenuFlags.MF_STRING, FrameMenuMessage.MNU_OPEN, "打开(&O)...\tCtrl+O");
MenuHelper.AppendMenu(pFileMenu, MenuFlags.MF_STRING, FrameMenuMessage.MNU_CLOSE, "关闭(&C)");
MenuHelper.AppendMenu(pFileMenu, MenuFlags.MF_SEPARATOR, 0, string.Empty);
MenuHelper.AppendMenu(pFileMenu, MenuFlags.MF_STRING, FrameMenuMessage.MNU_SAVE, "保存(&S)\tCtrl+S");
MenuHelper.AppendMenu(pFileMenu, MenuFlags.MF_STRING, FrameMenuMessage.MNU_SAVEAS, "另存为(&A)...");
MenuHelper.AppendMenu(pFileMenu, MenuFlags.MF_SEPARATOR, 0, string.Empty);
MenuHelper.AppendMenu(pFileMenu, MenuFlags.MF_STRING, FrameMenuMessage.MNU_PGSETUP, "页面设置(&U)...");
MenuHelper.AppendMenu(pFileMenu, MenuFlags.MF_STRING, FrameMenuMessage.MNU_PRINTPV, "打印预览(&V)");
MenuHelper.AppendMenu(pFileMenu, MenuFlags.MF_STRING, FrameMenuMessage.MNU_PRINT, "打印(&P)...");
MenuHelper.AppendMenu(pFileMenu, MenuFlags.MF_SEPARATOR, 0, string.Empty);
MenuHelper.AppendMenu(pFileMenu, MenuFlags.MF_STRING, FrameMenuMessage.MNU_PROPS, "属性(&I)");
MenuHelper.AppendMenu(this.Menu.Handle, MenuFlags.MF_BYPOSITION | MenuFlags.MF_POPUP, (uint)pFileMenu.ToInt32() , "文件(&F)");
MenuHelper.DrawMenuBar(this.Handle);
} |
|