In the current environment, programming in VC has become a habit of using MFC quickly. Many people do not know how to process resource files. I wrote this article, it is mainly to give a simple explanation of the resource file processing in VC.
1. Add icon
First, add by ID
Wndclass. hicon = loadicon (hinstance, makeintresource (idi_icon1 ));
Here, we actually convert the icon resource ID to a string pointer.
Second, add strings
Wndclass. hicon = loadicon (hinstance, text ("myicon "));
However, when using this method, you should not include the # define statement in resource. h.
Third, use the setclasslong method to set
Setclasslong (hwnd, gcl_hicon, loadicon (hinstance, makeintresource (idi_icon2 )));
This method is mainly used for dynamic setting.
2. Add string Resources
Loadstring (hinstance, ids_icon1, szappname, sizeof (szappname)/sizeof (tchar ));
ID is the ID, szappname is the pointer to the received string array, and the fourth parameter is the maximum number of characters.
Iii. Menu Resources
3.1 When you select a menu, Windows usually sends several messages to the window process:
Wm_initmenu: wparam: Main menu handle;
Lparam: 0;
Wm_menuselect: loword (wparam): selected item: menu ID or pop-up menu handle;
Hiword (wparam): select a flag;
Lparam: contains the menu handle of the selected item;
Wm_initmenupopup: wparam: pop-up menu handle;
Loword (lparam): pop-up menu index;
Hiword (lparam): The system menu is 1, and the others are 0;
Wm_command: loword (wparam): menu ID;
Hiword (wparam): 0;
Lparam: 0;
Wm_syscommand: wparam: menu ID;
Lparam: 0;
Note: If the wm_syscommand message is generated by mouse, loword (lparam) and hiword (lparam) will contain the screen coordinates of X and Y at the cursor position.
Wm_menuchar: loword (wparam): character code (ASCII or Unicode );
Hiword (wparam): select a code;
Lparam: menu handle;
The selection code is:
A. 0. No pop-up menu is displayed.
B. mf_popup. the pop-up menu is displayed.
C. mf_sysmenu: displays the system pop-up menu.
3.2 menu processing APIs
Hmenu = getmenu (hwnd );
// Obtain the menu handle
Checkmenuitem (hmenu, iselection, mf_unchecked );
Checkmenuitem (hmenu, iselection, mf_checked );
// Used to cancel or select a menu item
Enablemenuitem (hmenu, idm_timer_start, mf_grayed );
// Used for gray menu items
3.3 How to define menus
Hmenu = createmenu ();
Hmenupopup = createmenu ();
Appendmenu (hmenupopup, mf_string, idm_file_new, "& New ");
......
......
Appendmenu (hmenu, mf_popup, hmenupopup, "& file ");
3.4 floating pop-up menu
Case wm_create:
Hmenu = loadmenu (hinst, szappname );
Hmenu = getsubmenu (hmenu, 0 );
Return 0;
Case wm_rbuttonup:
Point. x = loword (lparam );
Point. Y = hiword (lparam );
Clienttoscreen (hwnd, & Point );
Trackpopupmenu (hmenu, tpm_rightbutton, point. X, point. y,
0, hwnd, null );
3.5 use the System Menu
Hmenu = getsystemmenu (hwnd, false );
3.6 Other menu commands
Drawmenubar (hwnd );
// Force refresh menu
Hmenupopup = getsubmenu (hmenu, iposition );
// Obtain the handle of the pop-up menu
Icount = getmenuitemcount (hmenu );
// Obtain the top menu or the number of items in the current pop-up menu
Id = getmenuitemid (hmenupopup, iposition );
// Iposition is the position of the menu item in the pop-up menu (starting from 0)
Icharcount = getmenustring (hmenu, ID, pstring, imaxcount, IFAG );
// IFAG is mf_bycommand (where ID is the menu ID) and mf_byposition (where ID is the location index). The function returns the number of words to be copied and copies imaxcount bytes to pstring.
Iflags = getmenustate (hmenu, ID, iflag );
/// IFAG is mf_bycommand (where ID is the menu ID) and mf_byposition (where ID is the location index). The function returns a combination of the current attributes.
Destorymenu (hmenu );
// The menu handle is invalid.
3.7 Informal commands for processing menus
Setmenu (hwnd, hmenu );