Tip:
1. The checkmenuitem function is used to change the menu item selection status;
2. The setclasslong function can modify all parameters registered before the window class registration. Here, the gcl_hbrbackground parameter indicates that you want to modify the painter;
3. Use the createsolidbrush function to create a solid paint brush based on the color in the constant array;
4. invalidaterect is used to indicate that the customer zone is invalid, resulting in re-painting. Its second parameter is nil, indicating that the rectangle to be operated is the customer zone of the current window;
5. In the resource file, mft_radiocheck is specified for the menu item to display a small dot in the selected menu.
In addition, this is similar to the previous example, but it is quite different. The former changes the color of the user area by filling the rectangle, and the former directly modifies the painting brush of the window class.
In this example:
Resource file (testres. Rc) used in this example ):
Mymenu1 menuexbegin popup "& color" begin menuitem "white", 101, mft_radiocheck, mfs_checked menuitem "red", 102, mft_radiocheck menuitem "green", 103, too menuitem "blue ", 104, mft_radiocheck endend
This exampleCodeFile:
Program project1; {$ R 'testres. res ''testres. RC '} uses Windows, messages; const arrcolor: array [0 .. 3] of colorref = ($ ffffff, $ ff, $ ff00, $ ff0000); idm_white = 101; {101 indicates the first menu item, is specified in the resource file} var idm_id: Word = idm_white; {the work to be done when the wm_command message is received} procedure oncommand (H: hwnd; wparam: integer); var HM: hmenu; W: word; begin HM: = getmenu (h); W: = loword (wparam); Case W of 101 .. 104: Begin checkmenuitem (HM, idm_id, mfs_unchecked); idm_id: = W; checkmenuitem (HM, idm_id, mfs_checked); setclasslong (H, gcl_hbrbackground, createsolidbrush (arrcolor [w-idm_white]); invalidaterect (H, nil, true); end; function wndproc (WND: hwnd; MSG: uint; wparam, 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.