NO MFC, ONLY API
-
Creation and use of menus:
Before using the menu, it is worth mentioning that the window with the menu must be a popup-up or overlapped style, which may have a menu.
There are three methods for the creation of the menu: 1. Specify the identifier of the menu resource in WindowClass; 2. Specify the menu handle in the CREATEWINDOWEX function parameter; The menu bar of the program.
The menu is included: add a new menu item to the existing menu, delete one of the menu, so that the menu item is selected, so that the menu item is invalid, create a floating menu, add, delete options to the Windows system menu and many more.
There are two ways to locate menu items: 1. Use the unique identifier of the menu item; 2, with the logical index of the menu item.
The logical index of the menu item is specified, and the items in the menu bar are from left to right indexes are 0, 1, 2 ..., the items in the pop-up menu are 0, 1, 2 ....
If you want to get the handle of a menu, you will get the handle by getting the handle of the menu strip first. E.g:
HMENU = GetMenu (hwnd); // hwnd is the window handle
HPopupMenu = getSubmenu (HMENU, 0); // 0 represents the first option of the menu
Sample 1: Various operations in demo menu
// begin.cpp: Defines the entry point for the application.
//
#define Win32_Lean_and_mean
#include "stdafx.h"
#include "resource.h"
#define id_newItem 1013 // Defines the identifier of new menu items
#define id_newsystem 60441 // Define a new system menu item identifier
LResult Callback MainProc (HWND, UINT, WPARAM, LPARAM); // Main window function
BOOL WINAPI DODISPPPUPMENU (HWND, INT, INT); // Create a pop-up menu
Void WinApi DodispMessage (INT); // Display related information
Void WinAPI DoInsertMenuItem (Void); // Insert a new item to the existing menu
Void WinApi DodeleteMenuItem (Void); // Remove the options in the menu
Void WinApi DocheckMenuItem (Void); // Settings menu items to select the flag
Void WinAPI DocheckRadio (Void); // Settings menu items for radio flags
Void WinApi Doinsertsysmenu (Void); / / Insert a new item to the system menu
Char szappname [] = "begin";
Hinstance hins;
HWND HMAINWND;
Bool isinsert; // Insert Menu Sign
Bool ischeck; // Select the flag
INT iDRADIO = 2; // radio sign
Int apientry Winmain (Hinstance Hinstance,
Hinstance Hprevinstance,
LPSTR LPCMDLINE,
INT ncmdshow)
{
MSG msg;
Hwnd hwnd;
WNDCLASSEX WC;
wc.cbclsextra = 0;
wc.cbsize = sizeof (wndclassex);
wc.cbWndextra = 0;
Wc.hbrbackground = (Hbrush) getStockObject (White_brush);
Wc.hcursor = loadingCursor (NULL, IDC_ARROW);
Wc.hicon = loading;
Wc.hiconsm = 0;
wc.hinstance = hinstance;
Wc.lpfnWndProc = (WndProc) mainproc;
wc.lpszclassname = szappname;
Wc.lpszMenuname = makeintResource (iDR_Menu1);
wc.style = CS_HREDRAW | CS_VREDRAW |
CS_DBLCLKS | CS_OWNDC;
RegisterClassex (& WC);
HWND = CREATEWINDOWEX (0, Szappname, Szappname, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, HINSTANCE, NULL;
Hmainwnd = hwnd;
Hins = hinstance;
/ / Insert menu items from the system menu here
Doinsertsysmenu ();
ShowWindow (HWND, NCMDSHOW);
UpdateWindow (HWND);
While (GetMessage (& MSG, NULL, 0, 0))
{
TranslateMessage (& MSG);
DispatchMessage (& MSG);
}
Return msg.wparam;
}
LResult Callback Mainproc (HWND HWND, UINT MESSAGE,
WPARAM WPARAM, LPARAM LPARAM)
{
HDC HDC;
Paintstruct PS;
Switch (Message)
{
Case WM_CREATE:
Return 0;
Case WM_Paint:
HDC = BeginPaint (HWND, & PS);
// Do Drawing
Endpaint (hwnd, & ps);
Return 0;
Case WM_RBUTTONDOWN:
// The mouse is right when the right button is pressed, pops up the floating menu
IF (DoDisppopupmenu (Hwnd, Loword (LPARAM), HiWord (LPARAM))
Return 0; // function call success
Else
Break; // Function call failed
Case WM_COMMAND:
Switch (loword (wparam))
{
Case ID_INSERT:
IF (isinsert == 0)
{// Insert a new menu item
DoinsertMenuItem ();
// Display related information
DodispMessage (4);
}
Break;
Case ID_newItem:
DODISPMESSAGE (3); Break;
Case id_delete:
IF (isinsert! = 0)
{// Remove menu item
DodeleteMenuItem ();
DodispMessage (5);
}
Break;
Case IDExit:
PostquitMessage (0);
Break;
Case ID_Check: // Tap before menu option
Docheckmenuitem ();
Break;
Case ID_RADIO1: // Do a round mark before the menu option, indicating this item selected,
/ / And clear the old sign
Idradio = 2;
Docheckradio ();
Break;
Case ID_Radio2: //
Idradio = 3;
Docheckradio ();
Break;
Case ID_Radio3: //
IDRADIO = 4;
Docheckradio ();
Break;
Case ID_float1: // Options for Floating Menu are selected
DODISPMESSAGE (0);
Break;
Case id_float2: //
DODISPMESSAGE (1);
Break;
Case id_float3: //
DodispMessage (2);
Break;
}
Return 0;
Case WM_Close:
PostquitMessage (0);
Return 0;
}
Return DefWindowProc (Hwnd, Message, WPARAM, LPARAM);
}
Bool WinApi DodisppopUpMenu (HWND HWND, INT X, INT Y)
{
Hmenu Hmenu;
HMENU HPOPMENU;
POINT PT = {x, y};
/ / Load the menu resources from the resource file
HMENU = LoadMenu (hins, makeintresource (idr_menu2));
// Get the handle of the submenu
HPopupmenu = GetSubmenu (HMENU, 0);
// Window coordinates transform to screen coordinates
Clienttoscreen (HmainWnd, & Pt);
// Create a floating menu
IF (HPopupUpMenu (HPopupmenu, TPM_LEFTALIGN | TPM_RightButton,
Pt.x, pt.y, 0, hmainwnd, null == false)
Return False;
// After use, recover memory
DestroyMenu (HMENU);
Return True;
}
Void WinApi DodispMessage (INT ID)
{// When the menu event occurs, output the corresponding string
HDC HDC;
Char * message [] = {"Command floating menuitem 1",
"Command Floating Menuitem 2",
"Command Floating Menuitem 3",
"Command New Menu Item",
"New item has been inserted",
"New item has been deleded"}
INVALIDATERECT (HmainWnd, Null, False);
HDC = Getdc (Hmainwnd);
Textout (HDC, 0, 0, Mess [ID], Strlen (MESS [ID]));
ReleaseDC (HmainWnd, HDC);
}
Void WinApi doinsertMenuItem ()
{// Insert menu item
Hmenu Hmenu; HMENU HPOPUPMENU;
Menuiteminfo Info;
/ / Each newly inserted menu item must be filled in MenuItemInfo structure
Info.cbsize = sizeof (menuiteminfo);
Info.fmask = MFT_STRING | MIIM_DATA | MIIM_ID | MIIM_TYPE
INFO.FTYPE = MFT_STRING;
INFO.FSTATE = 0;
INFO.WID = ID_NEWITEM;
INFO.HSUBMENU = NULL;
Info.hbmpChecked = null;
INFO.HBMPunchecked = NULL;
Info.dwitemdata = ID_NEWITEM;
Info.dwtypedata = "new item";
INFO.CCH = 8;
// Get the handle of the existing menu
HMENU = GetMenu (HmainWnd);
// Get the handle of the submenu
HPopupmenu = GetSubmenu (HMENU, 0);
// Insert menu item
INSERTMENUITEM (HPopupmenu, 1, True, & Info);
// Set the insert sign of 1
Isinsert = 1;
}
Void WinApi DodeleTeMenuItem ()
{// Remove menu item
Hmenu Hmenu;
HMENU HPOPMENU;
HMENU = GetMenu (HmainWnd);
HPopupmenu = GetSubmenu (HMENU, 0);
RemoveMenu (HPopupMenu, ID_NewItem, MF_BYCOMMAND);
Isinsert = 0;
}
Void WinApi DocheckMenuItem ()
{// This function first judges whether the menu item is hook, if not, make a hook mark
// If you already have, remove the hook tag
Hmenu Hmenu;
HMENU HPOPMENU;
HMENU = GetMenu (HmainWnd);
HPopupmenu = GetSubmenu (HMENU, 1);
IF (ischeck)
{
CheckMenuItem (HPopupmenu, 0, MF_BYPOSITION | MF_UNCHECKED);
ISCHECK = 0;
}
Else
{
CheckMenuItem (HPopupmenu, 0, MF_BYPOSITION | MF_CHECKED);
ISCHECK = 1;
}
}
Void WinApi Docheckradio ()
{
Hmenu Hmenu;
HMENU HPOPMENU;
HMENU = GetMenu (HmainWnd);
HPopupmenu = GetSubmenu (HMENU, 1);
CheckmenuradioItem (HPopupMenu, 2, 4, Idradio, MF_BYPOSITION);
}
Void WinApi doinsertsysmenu ()
{// Insert menu item in the system menu
HMENU HSYSMENU;
Menuiteminfo Info;
// Get the handle of the system menu
HSYSMENU = GetSystemMenu (HmainWnd, False);
Info.cbsize = sizeof (menuiteminfo);
Info.fmask = MFT_STRING | MIIM_DATA | MIIM_ID | MIIM_TYPE; Info.fType = MFT_STRING;
Info.fstate = MFS_DEFAULT;
INFO.WID = ID_NEWSYSITEM;
INFO.HSUBMENU = NULL;
Info.hbmpChecked = null;
INFO.HBMPunchecked = NULL;
INFO.DWITEMDATA = 15;
Info.dwtypedata = "new system item";
INFO.CCH = 15;
INSERTMENUITEM (HSYSMENU, 6, TRUE, & INFO);
}
There is the following definition in the resource file
IDR_MENU1 Menu Discardable
Begin
Popup "Menu & 1"
Begin
Menuitem "INSERTITEM / T [I]", ID_INSERT
Menuitem "DeleteItem / T [D]", ID_DELETE
MenuItem "& exit / talt f4", IDEXIT
End
POPUP "Menu & 2"
Begin
Menuitem "CheckText", ID_Check
Menuitem Separator
Menuitem "Radio1", ID_RADIO1
Menuitem "Radio2", ID_RADIO2
Menuitem "Radio3", ID_RADIO3
Menuitem Separator
Menuitem "About", ID_about
End
End
IDR_MENU2 menu Discardable
Begin
Popup "Test"
Begin
Menuitem "floating menu item1", id_float1, menubreak
Menuitem "floating menu item2", id_float2
Menuitem "floating menu item3", id_float3
End
End
There is any opinion, it is recommended to please email: y_h_zhang@sohu.com.