C++ AppendMenu

时间:2023-03-09 12:53:27
C++  AppendMenu

主题

1.  系统菜单下面添加自定义菜单

2.

3.

4.

5.

  

AppendMenu

The AppendMenu function appends a new item to the end of the
specified menu bar, drop-down menu, submenu, or shortcut menu. You can use
this function to specify the content, appearance, and behavior of the menu
item.
Note  The AppendMenu function has been superseded by the
InsertMenuItem function. You can still use AppendMenu, however, if you do
not need any of the extended features of InsertMenuItem.
BOOL AppendMenu(
  HMENU hMenu,        
// handle to menu
  UINT uFlags,        
// menu-item options
  UINT_PTR uIDNewItem, // identifier, menu, or submenu
  LPCTSTR lpNewItem    // menu-item content
);

ID

Value

Description

1.

MF_BITMAP

位图

2.

MF_DISABLED

不可用

3.

MF_ENABLED

可用

4.

MF_GRAYED

灰色

5.

MF_MENUBARBREAK

6.

MF_MENUBREAK

7.

MF_OWNERDRAW

8.

MF_POPUP

9.

MF_SEPARATOR

分隔符号

10.

MF_STRING

字符串

11.

MF_CHECKED

勾上

12.

MF_UNCHECKED

取消勾上

代码::系统菜单下面添加自定义菜单

1.在Resource.h里面
定义1个ID号

#define ID_MENU1 0x1500
2.在::OnInitDialog() 下面添加代码
// TODO: Add extra initialization here
CMenu*    pMenu = GetSystemMenu(FALSE);                    
CString menu1="新菜单";                                
pMenu->AppendMenu(MF_SEPARATOR);                        
pMenu->AppendMenu(MF_STRING, ID_MENU1, menu1);
3.在::OnSysCommand(UINT nID, LPARAM lParam) 下面添加响应单击 这个菜单的代码
void CProject02Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) ==
IDM_ABOUTBOX)
    {
       
CAboutDlg dlgAbout;
       
dlgAbout.DoModal();
    }
    else if
(nID==ID_MENU1)
    {
       
MessageBox("YES");
    }
    else
    {
       
CDialog::OnSysCommand(nID, lParam);
    }

}

效果图:

C++  AppendMenu

//OnSysCommand

ID

nID

备注:OnSysCommand

1.

SC_CLOSE

Close
the CWnd object.

2.

SC_MAXIMIZE

(or
SC_ZOOM) Maximize the CWnd object.

3.

SC_MINIMIZE

(or
SC_ICON) Minimize the CWnd object.

4.

SC_MOVE

Move
the CWnd object

5.

SC_RESTORE

Restore
window to normal position and size.

6.

SC_SIZE

Size
the CWnd object.

7.

SC_NEXTWINDOW

Move
to the next window.

8.

SC_PREVWINDOW

Move
to the previous window.

9.

SC_KEYMENU

Retrieve
a menu through a keystroke.

10.

SC_MOUSEMENU

Retrieve
a menu through a mouse click.

11.

SC_HOTKEY

Activate
the CWnd object associated with the application-specified hot key.

12.

The
low-order word of lParam identifies the HWND of the window to

13.

SC_SCREENSAVE

Executes
the screen-saver application specified in the [boot] section of the
SYSTEM.INI file.

14.

SC_TASKLIST

Execute
or activate the Windows Task Manager application.

15.

SC_VSCROLL

Scroll
vertically.

16.

SC_HSCROLL

Scroll
horizontally.

C++  AppendMenu