我经常看到一些程序的系统菜单中有自己的菜单(就是关闭,最大化,最小化,还原之类的菜单里),他是怎么加进去的?(40分)

  • 主题发起人 主题发起人 maysoft
  • 开始时间 开始时间
M

maysoft

Unregistered / Unconfirmed
GUEST, unregistred user!
我经常看到一些程序的系统菜单中有自己的菜单(就是关闭,最大化,最小化,还原之类的菜单里),他是怎么加进去的?
 
用一个TACTION控件,你说的那些是标准TACTION,在TACTION控件里增加这些标准TACTION;<br>增加一个空菜单项,把它指向TACTION控件中的标准ACTION就可以了,具体操作你摸索一下吧;
 
托盘消息处理:<br><br>const WM_MYTRAYICONCALLBACK = WM_USER + 1000 ;<br><br>private<br>&nbsp; &nbsp; MyTrayIcon : TNotifyIconData ;<br>&nbsp; &nbsp; procedure WMMyTrayIconCallBack(Var Msg : TMessage);<br>&nbsp; &nbsp; message WM_MYTRAYICONCALLBACK ;//托盘消息处理过程<br>//<br>procedure TFrmMySrv.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Icon.Handle := LoadIcon(Hinstance,'MAINICON');<br>&nbsp; MyTrayIcon.cbSize := SizeOf(TNotifyIconData);<br>&nbsp; MyTrayIcon.Wnd := Handle ;<br>&nbsp; MyTrayIcon.uID := 1 ;<br>&nbsp; MyTrayIcon.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE ;<br>&nbsp; MyTrayIcon.uCallBackMessage := WM_MYTRAYICONCALLBACK ;///////////////将自定义托盘消息传递进去<br>&nbsp; MyTrayIcon.hIcon := Icon.Handle;<br>&nbsp; StrCopy (MyTrayIcon.szTip, PChar(Caption));<br>&nbsp; Shell_NotifyIcon(NIM_ADD,@MyTrayIcon);<br>&nbsp; ShowWindow(Handle,sw_Hide);<br>&nbsp; Visible := False ;<br>&nbsp; Application.ShowMainForm := False ;<br>&nbsp; SetForegroundWindow(Application.Handle);<br>end;<br><br>////消息过程实现<br>procedure TFrmMySrv.WMMyTrayIconCallBack(var Msg: TMessage);<br>var CursorPos : TPoint;<br>begin<br>&nbsp; case Msg.LParam of<br>&nbsp; &nbsp; WM_LBUTTONDBLCLK : //双击消息:弹出主窗口<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Visible := not Visible ;<br>&nbsp; &nbsp; &nbsp; Application.ShowMainForm := Visible ;<br>&nbsp; &nbsp; &nbsp; SetForegroundWindow(Application.Handle);<br>&nbsp; &nbsp; end ;<br>&nbsp; &nbsp; WM_RBUTTONDOWN : //鼠标右键:弹出菜单<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; GetCursorPos(CursorPos);<br>&nbsp; &nbsp; &nbsp; Popupmenu1.Popup(CursorPos.X,CursorPos.Y);//popupmen1里面就可以加入显示主窗口、退出等功能<br>&nbsp; &nbsp; end ;<br>&nbsp; end ;<br>end;<br>
 
搞错了:添加系统菜单应该是这样的:<br>const<br>&nbsp; SC_MyMenuItem=WM_USER+1;<br>//////////////////////////<br>private<br>&nbsp; &nbsp; procedure WMSysCommand(var msg:TWMSysCommand);<br>&nbsp; &nbsp; &nbsp; &nbsp;message WM_SYSCOMMAND;<br>//////////////////////////<br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; //在系统彩旦中添加一个分割符<br>&nbsp; AppendMenu(GetSystemMenu(Handle,False),MF_SEPARATOR,0,'');<br>&nbsp; //在系统菜单中添加自己的菜单项<br>&nbsp; AppendMenu(GetSystemMenu(Handle,False),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MF_STRING+MF_CHECKED,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SC_MyMenuItem,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'我的菜单项[&amp;I]');<br>end;<br><br>//////////////////<br>procedure TForm1.WMSysCommand(var msg: TWMSysCommand);<br>begin<br>&nbsp; if Msg.CmdType=SC_MyMenuItem then<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp;//自定义菜单执行代码<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; inherited;<br>end;
 
这不是windows自己的功能吗?
 
这个其实使用的就是以下api函数,我记得《Mastering Delphi 7》里面就有完整的例子,<br>楼主可以去 www.51delphi.com 下载。<br><br>The GetSystemMenu function allows the application to access the window menu (also known as the System menu or the Control menu) for copying and modifying. <br><br>HMENU GetSystemMenu(<br><br>&nbsp; &nbsp; HWND hWnd, // handle of window to own window menu &nbsp;<br>&nbsp; &nbsp; BOOL bRevert // reset flag<br>&nbsp; &nbsp;); <br>&nbsp;<br><br>Parameters<br><br>hWnd<br><br>Identifies the window that will own a copy of the window menu. <br><br>bRevert<br><br>Specifies the action to be taken. If this parameter is FALSE, GetSystemMenu returns the handle of the copy of the window<br>&nbsp;menu currently in use. The copy is initially identical to the window menu, but it can be modified. <br>If this parameter is TRUE, GetSystemMenu resets the window menu back to the Windows default state. The previous window menu, if any, is destroyed. <br><br>&nbsp;<br><br>Return Values<br><br>If the bRevert parameter is FALSE, the return value is the handle of a copy of the window menu. If the bRevert parameter is TRUE, the return value is NULL. <br><br>Remarks<br><br>Any window that does not use the GetSystemMenu function to make its own copy of the window menu receives the standard <br>window menu. <br>The window menu initially contains items with various identifier values, such as SC_CLOSE, SC_MOVE, and SC_SIZE. <br>Menu items on the window menu send WM_SYSCOMMAND messages. <br>All predefined window menu items have identifier numbers greater than 0xF000. If an application adds commands to the window menu, it should use identifier numbers less than 0xF000. <br><br>Windows automatically grays items on the standard window menu, depending on the situation. The application can perform its own checking or graying by responding to the WM_INITMENU message that is sent before any menu is displayed. <br><br>See Also<br><br>GetMenu, WM_INITMENU, WM_SYSCOMMAND
 
To hongxing_dl:<br>&nbsp; 你的系统菜单加入方法是正解。已测试通过。<br>&nbsp; 托盘消息处理有问题。[Error] Undeclared identifier: 'TNotifyIconData' 它定义在哪个系统单元呀?
 
uses shellapi;
 
To hongxing_dl:<br>&nbsp; 添加一个菜单项是没问题,要是添加两个或以上菜单项呢?怎么处理消息?请指教!
 
Ok,搞定!如果加多个菜单项,可以相应地声明几个常量:<br>const<br>&nbsp;SC_MyMenuItem1 = WM_USER + 1;<br>&nbsp;SC_MyMenuItem2 = WM_USER + 2;<br>并在加入菜单项时:AppendMenu(GetSystemMenu(Handle,False),MF_STRING+MF_CHECKED,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SC_MyMenuItem1,'我的菜单项1[&amp;I]');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AppendMenu(GetSystemMenu(Handle,False),MF_STRING+MF_CHECKED,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SC_MyMenuItem2,'我的菜单项2[&amp;I]');<br>处理消息时:procedure TForm1.WMSysCommand(var msg: TWMSysCommand);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Case Msg.CmdType of <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SC_MyMenuItem1 : begin &nbsp; &nbsp;//自定义菜单项1执行代码 end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SC_MyMenuItem2 : begin &nbsp; &nbsp;//自定义菜单项2执行代码 end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else inherited;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br><br><br>
 
你的问题已由hongxing_dl解决了
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
933
DelphiTeacher的专栏
D
D
回复
0
查看
888
DelphiTeacher的专栏
D
后退
顶部