Windows Programming [17]-change menu: loadmenu, destroymenu, setmenu

Source: Internet
Author: User
In this example:


In this example, two menus are provided in the resource file,ProgramSwitch during running.

Resource file (testres. Rc) used in this example ):

 
Mymenu1 menuexbegin menuitem "open", 101 menuitem "edit", 102 menuitem "help", 103 menuitem "Chinese", 104, mft_rightjustifyendmymenu2 menuexbegin menuitem "open", 201 menuitem "edit ", 202 menuitem "help", 203 menuitem "English", 204, mft_rightjustifyend

  

This exampleCodeFile:

Program project1; {$ R 'testres. res ''testres. RC '} uses Windows, messages; {work required to receive the wm_command message} procedure oncommand (H: hwnd; wparam: integer); var HM: hmenu; begin case wparam of 104,204: begin {104 and 204 are the menu IDs specified in the resource file} If loword (wparam) = 104 then HM: = loadmenu (hinstance, 'mymenu2 ') else HM: = loadmenu (hinstance, 'mymenu1'); destroymenu (getmenu (h); setmenu (H, HM); drawmenubar (h); end; function wndproc (WND: hwnd; MSG: uint; wparam: integer; lparam: integer): integer; stdcall; begin result: = 0; Case MSG of wm_command: oncommand (WND, wparam); {call the oncommand process after receiving the wm_command message} wm_destroy: postquitmessage (0); else result: = defwindowproc (WND, MSG, wparam, lparam); end; function regmywndclass: Boolean; var CLS: twndclass; begin Cls. style: = cs_hredraw or cs_vredraw; Cls. lpfnwndproc: = @ wndproc; Cls. cbclsextra: = 0; Cls. cbwndextra: = 0; Cls. hinstance: = hinstance; Cls. hicon: = 0; Cls. hcursor: = loadcursor (0, idc_arrow); Cls. hbrbackground: = hbrush (color_window + 1); Cls. lpszmenuname: = 'mymenu1'; Cls. lpszclassname: = 'mywnd'; Result: = registerclass (CLS) 0; end; {program entry} const tit = 'new form'; Ws = ws_overlappedwindow; X = 100; y = 100; W = 300; H = 180; var hwnd: thandle; MSG: tmsg; begin regmywndclass; hwnd: = createwindow ('mywnd', tit, WS, X, y, W, H, 0, 0, hinstance, nil); showwindow (hwnd, sw_shownormal); updatewindow (hwnd); While (getmessage (MSG, 0, 0, 0 )) do begin translatemessage (MSG); dispatchmessage (MSG); end.
 
  
 
Related Article

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.