[Original] use win api to create top-level menus (do not use resource files)

Source: Internet
Author: User

Author: Zhu Jincan

Source: http://blog.csdn.net/clever101

 

Recently I used code: blocks for my spare time learning (vs200x is too huge for my computer ). I will compile some win APIs.Program. Code: blocks it seems that an additional resource script editor is required to write the resource script (resedit ). I don't want to use this immature external tool either. I plan to use it directly.CodeCreate a UI control. Learn how to create a top menu today. After a bit of exploration, you can understand it.

 

1. Use code: blocks to create a Win32 GUI project, which is called MyApp. This step is skipped.

 

2. Add a Resoure. h to save the resource ID (resource script is not required, resource ID is always required). How to add code:


 
# Define idm_file_new 1024 # define idm_file_open 1025


3. Add the code for creating a top-level menu. First, add the top-level menu handle and create a function. The Code is as follows:

 
Hmenu g_htopmenu = NULL; // top menu global variable bool createtopmenu () {g_htopmenu = createmenu (); // create a pop-up menu handle hmenu hmenupopup = createmenu (); // Add a menu item in the pop-up menu: appendmenu (hmenupopup, mf_string, idm_file_new, "& New");: appendmenu (hmenupopup, mf_string, idm_file_open, "& Open "); // Add the handle of the pop-up menu to the top menu: appendmenu (g_htopmenu, mf_popup, (uint) hmenupopup, "& file ");}


Second, call the following in the winmain function:

Int winapi winmain (hinstance hthisinstance, hinstance hprevinstance, lpstr lpszargument, int ncmdshow) {hwnd;/* This is the handle for our window */MSG messages; /* Here messages to the application are saved */wndclassex wincl;/* Data Structure for the windowclass * // * The window structure */wincl. hinstance = hthisinstance; wincl. lpszclassname = szclassname; wincl. lpfnwndproc = windowpr Ocedure;/* This function is called by Windows */wincl. style = cs_dblclks;/* Catch double-clicks */wincl. cbsize = sizeof (wndclassex);/* use default icon and mouse-pointer */wincl. hicon = loadicon (null, idi_application); wincl. hiconsm = loadicon (null, idi_application); wincl. hcursor = loadcursor (null, idc_arrow); wincl. lpszmenuname = NULL;/* No menu */wincl. cbclsextra = 0;/* No extra Tes after the window class */wincl. cbwndextra = 0;/* structure or the window instance * // * use Windows's default color as the background of the window */wincl. hbrbackground = (hbrush) color_background;/* register the window class, and if it fails quit the program */If (! Registerclassex (& wincl) return 0; // create the main menu createtopmenu ();/* the class is registered, let's create the Program */hwnd = createmediawex (0, /* extended possibilites for variation */szclassname,/* classname */"code: blocks template windows app",/* Title text */ws_overlappedwindow, /* default window */cw_usedefault,/* Windows decides the position */cw_usedefault,/* Where the window ends up on the screen */544, /* The programs width */375,/* and height in pixels */hwnd_desktop,/* the window is a child-window to desktop */g_htopmenu, /* No menu */hthisinstance,/* program instance handler */null/* No window creation data */); /* Make the window visible on the screen */showwindow (hwnd, ncmdshow);/* run the message loop. it will run until getmessage () returns 0 */while (getmessage (& messages, null, 0, 0 )) {/* translate virtual-key messages into character messages */translatemessage (& messages);/* send message to windowprocedure */dispatchmessage (& messages );} /* The program return-value is 0-the value that postquitmessage () gave */return messages. wparam ;}


Then add the name message response code of the menu, as follows:

Lresult callback windowprocedure (hwnd, uint message, wparam, lparam) {Switch (Message)/* handle the messages */{Case wm_destroy: postquitmessage (0 ); /* Send a wm_quit to the Message Queue */break; Case wm_command: Switch (loword (wparam) {Case idm_file_new: MessageBox (hwnd, _ T ("new file"), _ T ("prompt"), mb_ OK); break; Case idm_file_open: MessageBox (hwnd, _ T ("Open File "), _ T ("prompt"), mb_ OK); break; default: break;} break; default:/* for messages that we don't deal with */return defwindowproc (hwnd, message, wparam, lparam);} return 0 ;}


In this way, the Windows API application menu can be created even if the resource script file is not applicable.

 

References:

 

1. Windows API learning notes-menu





Author: clever101 posted on 22:16:21 Original article link Read: 652 comments: 3 views comments

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.