6.3.7 Message Mapping Tips for Tools
If there is a corresponding menu command on the tool bar, you can directly utilize the mapping of the command message using the class wizard classwizard, otherwise the message map of the command button must be performed by manual method. Since there is a similar function in the command button of the same toolbar, you can use the range command processing mechanism to manage all button commands using the range command processing mechanism, all commands can be managed using the range command processing mechanism to manage all button commands. The message of the button can be managed in the same command message processing function and the update command message processing function, which is not only compact structure and is particularly convenient. In view of the type of command handwizard, the message mapping mechanism must be done by manual use. The button command message can be mapped in the Frame window or in the document and the view, and the document object is usually used to manage the data part in the program, and in the Multi-Document Application (MDI), a document may have more The view is associated with it, so the command button message map in the toolbar is more appropriate in the document. The actual steps are as follows:
(1) Add function definitions and data members in document implementation files CToolDoc.h:
Class CctoOLDOC: PUBLIC CDOCUMENT
{... // Other code
protected:
Uint iposition;
File: // Command Button Message Location
... // Other code
protected:
File: // {{{AFX_MSG (CCToolDoc)
AFX_MSG Void ONTOOL (UINT NID); // Command Message Map
AFX_MSG Void OnUpdateTool (ccmdui * pcmdui); // Update Message Map
File: //}} AFX_MSG
Declare_message_map ()
... // Other code
}
(2) Message mapping in document implementation file ctoolDoc.cpp
Begin_Message_Map (CCToolDoc, CDocument)
FILE: / / {{AFX_MSG_MAP (CCToolDoc)
ON_COMMAND_RANGE (ID_Button_Line,
ID_Button_Sort, ONTOOL);
ON_UPDATE_COMMAND_UI_RANGE (ID_Button_Line,
ID_Button_Sort, OnUpdateTool;
File: //}} AFX_MSG_MAP
END_MESSAGE_MAP ()
The parameter ID_BUTTONLINE and ID_BUTTONSORT in the function are messages in the first button and the last button, and if the number of buttons is to be added, the ID value must be continuous, and the last message in the function needs to be modified. value. The code for the corresponding function is as follows:
Void CToolDoc :: ONTOOL (UINT NID)
{
File: // Button Message Processing Function
iposition = Nid-id_button_line;
Switch (iposition) {
Case 1:
... // Call button one's processing function
Case 2:
... // Call the button two processing function
......
Case N:
... // Call button N processing function
Break;
}
}
Void CToolDoc :: OnupdateTool (ccmdui * pcmdui)
{
FILE: / / Update button function
PCMDUI-> setCheck (iposition == (pcmdui-> m_nid
-ID_Button_Line));
}
Since the command button is more, the processing function code section is omitted here.
6.3.8 Status display update of the toolbar button
(1) Status update of mutual exclusion buttons
Many applications have mutually exclusive toolbar buttons, such as starting and stopping buttons, the stop button is prohibited, when the application performs data communication, finding, and printing, the stop button is valid and the start button is prohibited, After the command ends or press the stop button, the start button is valid and the stop button is disabled. This is the display status update problem of the button in the toolbar. Like the menu bar, the structure of the toolbar, the creation of the destructure process, and its window is managed by the primary frame window of the application. When the user clicks on the menu item of the menu bar, Windows sends a WM_INITMENUPOPUP message to the application so that the application adds or modifies the menu item before the menu display, as long as the MFC class library is programmed, as long as a specific menu item Perform a message mapping, add the corresponding message processing function, the MFC class library will send update messages update_command_ui, so that the menu can disable, gray gray and other processing on the menu item when pop-up. The message processing process of the toolbar is exactly the same as the menu bar, and it also exists that the toolbar display update message update_command_ui is only because the toolbar is always displayed, the call to the message processing function is performed when the application is idle. Processing, this is different from the menu bar. When the function you want to execute in the application command button, the main frame window of the application is innocent when the function of the application command button will execute, if the communication processing or a large amount of data is calculated. So the command button function in the toolbar cannot be called in time, the button's display status cannot be updated in time, which is difficult to update the display status of the toolbar button.
Based on documentation-based applications are generally processed on the documentation, the tool strip button is handled, so the timely update of the button display status must be implemented in the command button response function EnableButton () function enablebutton () function pair button The display status is required, and the steps are as follows:
1 First Using the class wizard ClassWizard to start the CCTOOLVIEW class command message mapping, the COMMAND command message map of the stop button, add the processing function of the corresponding message;
2 Add member variables in implementation files CToolView.h:
PUBLIC:
BOOL M_BCONTIUNE; / / Start button logo
BOOL m_bset; // End button logo
3 Add code to improve the code in the implementation of the file ctoolview.cpp:
... // Other code
Void DrawRecttext (Rect & RC, CDC & DC);
... // Other code
Void cctoolview :: onbuttonstart ()
{
File: // Perfect the start button function function
CToolbar * ptoolbar = (ctoolbar *) // get toolbar object pointer
(GetParentFrame () -> getControlbar (iDR_Docktoolbar);
FILE: / / Strip Control Pointer
Ctoolbarctrl * ptoolbarctrl = & (ptoolbar-> gettoolbarctrl ());
File: // Control Toolbar Button Status
Ptoolbarctrl-> EnableButton (ID_Button_Start, False);
Ptoolbarctrl-> EnableButton (id_button_stop, true);
CClientDC DC (this); // acquire device text
MSG msg; // Define the message
Rect RC;
GetClientRect (& RC); // The following painting grid and display
m_bcontinue = true;
m_bset = false;
For (INT II = 0; II <2; II ) {
IF (m_bcontinue == true) {II = 0; // Status change exits
DrawRectText (RC, DC);
GetMessage (& MSG, NULL, 0, 0); // Check the message
TranslateMessage (& MSG); // Interpretation Message
DispatchMessage (& MSG); // Assignment Message
}
}
ReleaseDC (& DC);
m_bset = true;
}
Void DrawRecttext (Rect & RC, CDC & DC)
{
File: // Display text functions
INT I, J, K, NDIST = 10;
CPEN PEN, * POLDPEN;
Pen.createpen (PS_SOLID, 1, RGB (180, 0, 0));
Poldpen = dc.selectObject (& pen); // Draw background grid
J = rc.right / ndist 1;
K = rc.bottom / ndist 1;
For (i = 0; i Dc.moveto (i * ndist, 0); Dc.LineTo (0, i * ndist); IF (i Dc.moveto (i * ndist, 0); Dc.LineTo (rc.right, (j-i) * ndist); } else { Dc.moveto (0, (i-j) * ndist); DC.LineTo (rc.right, i * ndist); } } Dc.selectObject (Poldpen); DeleteObject (& Pen); CFONT FONT, * POLDFONT; Font.createFont (50, 50, 0, 0, 1000, // Create font 1, 1, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, Clip_Default_Precis, Default_quality, default_pitch, null; PoldFont = Dc.selectObject (& font); Dc.TextOut (10, 10, "button"); Dc.selectObject (PoldFont); DELETEOBJECT (& FONT); } Void cctoolview :: onbuttonstop () { FILE: / / Perfect Stop Button Function m_bcontinue = false; CToolbar * ptoolbar = (ctoolbar *) // get toolbar object pointer (GetParentFrame () -> getControlbar (iDR_Docktoolbar); FILE: / / Strip Control Pointer Ctoolbarctrl * ptoolbarctrl = & (ptoolbar-> gettoolbarctrl ()); File: // Control Toolbar Button Status Ptoolbarctrl-> EnableButton (ID_Button_Start, true); Ptoolbarctrl-> EnableButton (ID_Button_Stop, False); For (int JJ = 0; JJ <2; JJ ) { IF (m_bset == false) { JJ = 0; GetMessage (& MSG1, NULL, 0, 0); // Check Messages TranslateMessage (& MSG1); // Interpretation Message DispatchMessage (& msg1); // Assignment Message } } } In the start button function, when the message is constantly displaying information on the screen, you must check the message queue and interpret the corresponding message. Other buttons cannot be checked to the application's mouse and other messages. Display status cannot be implemented Update; before the update button is displayed, you must first obtain the toolbar object and the corresponding control pointer before implementing the control of the button status, and the update to implement the button status, the update function function of its setting button must be finally completed. Valid, that is, the flag must be determined in the stop button function and detect the message queue. 4 In the Mouse in the Planar Tools, Press the function to add the code to achieve the start and stop button of the mouse, ensure the normal pressing and display status of the button: Void Ctesttoolbar :: ONLBUTTONDOWN (UINT NFLAGS, CPOINT POINT) { File: // Improve the mouse button ... // Other code IF (m_nIndex == Toolbarctrl.commandToIndex (id_button_stop) { Toolbarctrl.SetState (id_button_stop, toolbarctrl.getstate " ID_BUTTON_STOP) & (~ TBSTATE_ENABED & ~ TBSTATE_PRESSED); Toolbarctrl.SetState (id_button_start, ToolbarCtrl. GetState (ID_BUTTON_START) | TBSTATE_ENABED); Toolbarctrl.invalidate (); AfxGetApp () -> OnIdle (-1); M_NINDEX = 0xfff; } IF (m_nIndex == Toolbarctrl.commandToIndex (id_button_start) { Toolbarctrl.SetState (id_button_start, Toolbarctrl.getState) ID_BUTTON_START) & (~ TBSTATE_ENABED & ~ TBSTATE_PRESSED); Toolbarctrl.SetState (id_button_stop, toolbarctrl.getstate " ID_BUTTON_STOP) & TBSTATE_ENABED); Toolbarctrl.invalidate (); AfxGetApp () -> OnIdle (-1); M_NINDEX = 0xfff; } } (2) Button Press the switch update Another button status update is the most common in the point of switching the status, which is the most common in the drawing toolbar and the screen font effect toolbar. The actual steps are as follows: 1 First increase the message range mapping function in mainfrm.cpp ON_COMMAND_RANGE (id_edit_toggle, id_edit_unindent, onbutton) ON_UPDATE_COMMAND_UI_RANGE (id_edit_toggle, ID_EDIT_UNINDENT, ONUPDATEBUTTON) 2 Then in the MAINFRM.H, the final increase of the member function definition and record the member control variable of pressing button sequence number INT M_BINDEX; // Press the button sequence number AFX_MSG Void Onbutton (UINT NID); AFX_MSG Void OnUpdateButton (CCMDUI * PCMDUI); 3 Finally increase the function code for implementing message functions, respectively Void CMAINFRAME :: OnUpdateButton (ccmdui * pcmdui) { FILE: // button Press the check status setting function PCMDUI-> SetCheck (ID_EDIT_TOGGLE m_bindex) == pcmdui-> m_nid); Void CMAINFRAME :: Onbutton (Uint Nid) { File: // Record the serial number of pressing the button m_bindex = (int) (NID-ID_EDIT_TOGGLE); } If there is a change in other controls, it can be increased in these two functions. (3) Some of the button display update The last update tool strip button is displayed in the status of the partial button, which needs to be effectively processed using the setButtons () and setButtonInfo () functions. Proceed as follows: 1 First add to the control menu item in the menu bar; 2 In the application implementation file CTOOL.H, add the following message definition and function definition ... // Other code #define WM_IDLEACTIVATE WM_USER 1 ... // Other code Class CCTRLBARSAPPPPPPPPPP: Public CWINAPP { PUBLIC: ... // Other code Virtual Bool OnIdle (long lcount); ... // Other code } 3 In the application implementation file CTool. CPP adds the following code and copy function function, which is very useful in various display status updates of the toolbar buttons, is a very important programming technique. In the initialization function cctoolapp :: InitInstance () Finally increase the code: m_pmainwnd = pmaInframe; Improve the corresponding functional function: Bool cctoolapp :: OnIdle (long lcount) { FILE: / / Keep an application a copy function IF (lcount == 0) { Assert (m_pmainwnd! = Null); For (hwnd hwnd = :: getWindow (m_pmainwnd-> m_hwnd, Gw_hwndfirst); hwnd! = Null; hwnd = :: getNextWindow HWND, GW_HWNDNEXT)) {// Get the main program sub-window IF (: getParent (hwnd) == m_pmainwnd-> m_hwnd) { IF (getActiveWindow () == hwnd && () == null)) m_pmainwnd-> setActiveWindow (); // Keep the application activity window SendMessage (hwnd, wm_idleupdatecmdui, (wparam) True, 0L); } // Keep a valid copy of the window } } Return CWINAPP :: OnIdle (lcount); } 4 Finally, the reorganization of the toolbar button is made in the menu item processing function, and add a copy processing function in any function that needs to handle the application window copy. Void CMAINFRAME :: OnViewsh () { M_WndmainToolBar.SetButtons (NULL, 7); // Total 7 M_WndmainToolBar.SetButtonInfo (0, ID_Button_Line, TBBS_Button, 1); M_WndmainToolBar.SetButtonInfo (1, ID_Button_Curse, TBBS_Button, 5); ...... M_WndmainToolBar.SetButtonInfo (6, ID_Button_Test, TBBS_Button, 7); m_wndtoolbar.invalidate (); // Update Toolbar AFXGetApp () -> OnIdle (-1); // Window copy processing function } 6.3.9 General Control in Toolsum It is usually not all buttons in the toolbar, such as the input box, pull-down combo box, and check box buttons. This requires the use of the special types of tools in the VC or by derived new categories. (1) Implementation in the new class of derived If you use the process of derived new categories, you can easily implement universal control functions such as text input boxes. The actual steps are as follows: 1 Using the class wizard ClassWizard to establish a new class CEDITBAR and generate implementation files; 2 Use the resource editor to establish a bitmap file of the toolbar; 3 Increase the following function definition and member variables in the new class implementation file editbar.h: Class Ceditbar: Public CToolbar { PUBLIC: Ceditbar (); CSearchbox M_Searchbox; // Text Find Control BOOL INIT (CWND * PParentWnd, Bool Btooltips); Bool setColor (BOOL BCOLOR); Bool setehorizontal (); Bool setVertical (); ... // Other code BOOL M_BVERTICAL; File: // Virtual Csize Calcfixedlayout (Bool Bstretch, Bool Bhorz); Virtual Csize CalcdyNamicLayout (INT NLENGTH, DWORD DWMODE); ... // Other code } 4 Increase the following functions in the new class implementation file editbar.cpp: ... // Other code #define comboBOX_INDEX 5 File: // Define the combination block parameters #define comboBOX_WIDTH 150 #define ComboBox_Height 150 #define ComboBox_bitmap 4 Static uint based_code editbuttons [] = { FILE: / / Requirements to the order of the toolbar graph ID_EDIT_TOGGLE, ID_EDIT_NEXT, ID_EDIT_PREV, ID_EDIT_CLARALL, ID_separator, ID_EDIT_FINDIT, ID_separator, ID_EDIT_FINDINFILES, ID_separator, ID_EDIT_INDENT, ID_EDIT_UNINDENT, ID_separator, ID_WINDOW_NEW, ID_Window_Split, ID_Window_cascade, ID_WINDOW_HORZ, ID_WINDOW_VERT, } ... // Other code Bool CeditBar :: Init (CWnd * PparentWnd, Bool Btooltips) { File: // Initialize the input box function m_bvertical = false; DWORD DWSTYLE = WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_SIZE_DYNAMIC IF (btooltips) DWStyle | = (CBRS_Tooltips | CBRS_FLYBY); IF (! Create (PParentWnd, DWStyle, IDB_CEDIT)) Return False; if (! loadingbitmap (idb_cedit)) Return False; IF (! setButtons (EditButtons, Sizeof (EditButtons) / sizeof (uint))) RETURN FALSE; CRECT RECT (-comboBOX_WIDTH, -COMBOBOX_HEIGHT, 0, 0); // It is very important: one of them can receive information from control, such as dynamic prompts, etc. IF (! m_search | cbs_dropdown | // Establishing text Find CBS_AUTOHSCROLL | WS_VSCROLL | CBS_HASSTRINGS, RECT, THIS, IDC_EDitBar_Searchbox) Return False; HFONT HFONT = (HFONT) GetStockObject (default_gui_font); IF (hfont == null) hfont = (hfont) getStockObject (ANSI_VAR_FONT); m_searchbox.sendMessage (WM_SETFONT, (WPARAM) HFONT); IF (! sethorizontal ()) Return False; Return True; } Bool CeditBar :: sethorizontal () { FILE: / / Set button horizontal arrangement function m_bvertical = false; SetBarsTyle (GetBarsTyle () | CBRS_ALIGN_TOP); SetButtonInfo (ComboBox_index, IDC_EDitbar_searchbox, TBBS_SEPARATOR, ComboBox_Width; IF (m_searchbox.m_hwnd! = null) { CRECT RECT; GetItemRect (comboBOX_INDEX, RECT); M_Searchbox.SetWindowPos (Null, Rect.Left, Rect.top, 0, 0, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSize | SWP_NOCOPYBITS; m_searchbox.showWindow; SW_SHOW } Return True; } Bool Ceditbar :: setVertical () { File: // Set button vertical arrangement function M_Bvertical = true; SetButtonInfo (ComboBox_index, id_edit_find, TBBS_BUTTON, ComboBox_bitmap; IF (m_searchbox.m_hwnd! = NULL) m_searchbox.showwindow (sw_hide); Return True; } CSIZE CEDITBAR :: CalcDynamicLayout (int NLENGTH, DWORD DWMODE) { FILE: / / Set the fixed size toolbar function IF (dwmode & lm_commit) { IF (dWMode & LM_VERTDOCK) IF (! m_bvertage) setVertical (); ELSE IF (M_Bvertical) Sethorizontal (); Return CToolbar :: CalcdyNamicLayout (NLENGTH, DWMODE); } else { Bool bold = m_bvertical; BOOL BSwitch = (dwmode & lm_horz)? Bold:! Bold; IF (bswitch) { IF (bold) setehorizontal (); Else setVertical (); CSIZE SIZERESULT = CToolbar :: CalcDynamicLayout (NLENGTH, DWMODE); IF (bswitch) { IF (bold) setehorizontal (); Else setVertical (); Return sizeresult; } } / * CSIZE CEDITBAR :: CalcFixedlayout (Bool Bstretch, Bool Bhorz) { FILE: / / Set the Variable Size Toolbar Function IF (! Bhorz && (M_DWStyle & CBRS_SIZE_DYNAMIC) &&! (M_DWStyle & CBRS_FLOATIN) { CSIZE SIZE; Size = ccontrolbar :: Calcfixedlayout (Bstretch, Bhorz); CRECT RECT; Rect.seRectempty (); CalcinsideRect (RECT, BHORZ); Size.cx = m_sizevert.cx (m_rectinsidevert.width () - Rect.width ()); Size.cy = m_sizevert.cy (M_RectIndevert.Height () - Rect.Height ()); Return size; } else { IF (m_bvertage) setehorizontal (); Return Ctoolbar :: Calcfixedlayout (Bstretch, Bhorz); } } * / 5 Using the class wizard ClassWizard to create new class CSearchbox and generate implementation files, and add interpretation messages for new classes, that is, in the new class implementation file Searchbox.h, add the following function definition: Virtual Bool PretranslateMessage (MSG * PMSG); 6 Improve the corresponding function in its implementation file searchbox.cpp file Bool CSearchbox :: PretranslateMessage (MSG * PMSG) { FILE: // Message Interpretation Function IF ((PMSG-> Message! = WM_KeyDown) || (PMSG-> WPARAM! = VK_RETURN)) Return CCOMBOBOX :: PretranslateMessage (PMSG); / / Add the last 15 input data in the drop-down list IF ((pmsg-> lparam & 0x40000000) == 0) {// Do not repeat CString strText; GetWindowText (strText); INSERTSTRING (0, strText); SetCurseel (0); IF (getCount ()> 15) DELETESTRING (GetCount () - 1); } Return True; } 7 Add tool strips in main program implementation files Mainfrm.h #include "editbar.h" CeditBar M_Wndeditbar; // With input box toolbar 8 Add code in main program implementation file mainfrm.cpp: File: // Added in the establishment function onCreate IF (! m_wndeditbar.init (this, m_btooltips) { Trace0 ("Bringing Entering Toolbar Establishs Fail / N"); Return -1; ...... DockControlbarleftof (& m_wndeditbar, & m_wndtesttoolbar); / / Connection Toolbar In addition, it is also possible to update the control function such as the toolbar to check the status and other control functions, see the display status update of the toolbar button. (2) Using tool strip style implementation control The drop-down list can be achieved using the tool strip style, and the specific steps are as follows: 1 Set a bitmap button to the toolbar and define the message corresponding to the button, the same; 2 Increase toolbar control and function definitions in implementation files. CStylebar M_WndStylebar; Bool createstylebar () 3 Add control code in implementing file mainfrm.cpp Add a function in establishing a function onCreate If (! createstylebar ()) return -1; increase the code for establishing the corresponding toolbar function Bool CMAINFRAME :: CreateStylebar () { File: // Establish a toolbar function with drop-down list control Const int NDROPHEIGHT = 100; IF (! M_WndStylebar.create (this, ws_child | ws_visible | CBRS_TOP | CBRS_ToolTIPS | CBRS_FLYBY, IDW_Styles) || ! m_wndstylebar.loadbitmap (idb_styles) ||! m_wndstylebar. SetButtons (Styles, SizeOf (Styles) / SizeOf (UINT))) { Trace0 ("Special Style Toolbar Established Fail / N"); Return False; } // Establish a combo box M_WndStyleBar.SetButTonInfo (0, IDW_COMBO, TBBS_SEPARATOR, 100); / / Set the combination box and button distance M_WndStyleBar.SetButTonInfo (1, ID_seParetor, TBBS_SEPARATOR, 12); CRECT RECT; M_WndStyleBar.getItemRect (0, & Re); RECT.TOP = 3; Rect.bottom = Rect.top ndropheight; IF (! m_WndStyleBar.m_comboBox.create (// Establish a combo box CBS_DropDownList | WS_VISIBLE | WS_TABSTOP, RECT, & M_WNDSTYLBAR, IDW_COMBO) { Trace0 ("Combination Frame Establishs Fail / N"); Return False; } CString SzStyle; // Filled Combination Box IF (Szstyle.LoadString)) M_WndStyleBar.m_comboBox.Addstring ((lpctstr) szstyle; ... // Other code of the same class Logfont logfont; / / set font for the combo box MEMSET (& logfont, 0, sizeof (logfont)); IF (! :: getSystemMetrics) { Logfont.lfheight = -12; Logfont.lfWeight = fw_bold; Logfont.lfpitchandfamily = variable_pitch | ff_swiss; CString strdefault; STRDEFAULTFONT.LOADSTRING (IDS_DEFAULT_FONT); Lstrcpy (logfont.lffacename, strdefault); IF (! m_wndstylebar.m_font.createfontindirect (& logfont)) Trace0 ("Combination Box Building Font Failed / N"); Else M_WndStyleBar.m_comboBox.SetFont (& M_WndStyleBar.m_Font); } else { M_WndStyleBar.m_Font.attach (:: getStockObject (system_font)); M_WndStyleBar.m_comboBox.SetFont (& M_WndStyleBar.m_Font); } Return True; } Similarly, other control functions can also be added to this special style toolbar. (3) Establish multi-function tools using dialog box class Using dialog tool clauses, you can create a more powerful toolbar, which can be added to the toolbar in the dialog box. The tool steps are as follows: 1 Use the dialog box to add a dialog IDD_dlgtoolbar and design the ID identifier corresponding to all features and functions in the dialog; 2 Add dialog box tool classes control and member variables in implementing file mainfrm.h CDialogbar m_wnddlgbar; File: // Dialog Toolbar Control BOOL M_BDIALOGTOP; // Top Sign 3 Add control code in implementing file mainfrm.cpp Add the function on the status strip of the function oncreate function to establish a function: UINT NID, NStyle; Int cxwidth; M_WndStatusBar.getPaneInfo (0, NID, NStyle, CXWIDTH); M_WndStatusBar.SetPaneInfo (0, NID, SBPS_STRETCH | SBPS_NORMAL, CXWIDTH); IF (! m_wnddlgbar.create (this, IDD_toolbarctrl, CBRS_LEFT | CBRS_ToolTIPS | CBRS_FLYBY, IDD_TOOLBARCTRL) { TRACE0 ("Dialog Toolbar establishs failed / n"); Return -1; } Be sure to pay attention to the enabledocking (CBRS_Align_Any) function in front of all established functions, and the dialog tool strip is established behind the status strip. 4 Handmade the mapping for all control messages in the dialog resource, and improve the corresponding mapping function, which can be the display and hidden features of the toolbar. The sample function is as follows: Bool CMAINFRAME :: OndockTool (uint nid) { File: // Control the display and hidden functions of the toolbar Bool bvisible = ((m_wnddocktoolbar.getStyle () & ws_visible)! = 0); ShowControlbar (& M_WnddockToolbar,! BVisible, False); RECALCLAYOUT (); Return True; } Void CMAINFRAME :: onupdatedocktool (ccmdui * pcmdui) { FILE: / / Toolbar Check Status Update Bool bvisible = ((m_wnddocktoolbar.getStyle () & ws_visible)! = 0); PCMDUI-> SetCheck (bvisible); } 5 In the menu, add the top control menu item, establish a message function, and follow the method in the docking position of the toolbar to improve the corresponding top control function function. 6.3.10 Toolbar display and hide 6.3.10.1 Display and Hide of Single Toolbar There may be a lot of toolbars in large applications, while users may only need some of them, which requires control of the toolbar to hide. For a single toolbar, the general control method of its menu item is: First open the menu editor, add the submenu item corresponding to the toolbar in the View menu bar, such as the "test" of the child menu of the TestToolBar toolbar is id_botton_test, its caption is "Test Tools Bar", The control submenu item of the other toolbar. Use the class wizard classwizard to increase the ONCOMMAND and ON_UPDATE_COMMAND_UI message mapping function functions of each submenu item, and improve the corresponding functional function in the main frame window class CMAINFRAME. Void CMAINFRAME :: Onbuttontest () { File: // Menu Command Message Function Bool bvisible = ((m_wndtesttoolbar.getStyle () & ws_visible)! = 0); // Get toolbar status ShowControlbar (& M_WndtestToolbar,! BVisible, False); RECALCLAYOUT (); // Update Toolbar } Void CMAINFRAME :: OnUpdateButtontest (ccmdui * pcmdui) { File: // Menu Update Function Bool bvisible = ((m_wndtesttoolbar.getStyle () & WS_Visible)! = 0); // Get tool strip status PCMDUI-> setCheck (bvisible); // Set check mark } 6.3.10.2 Shows and Hide of Special Tools When there is a toolbar with special control functions such as a CStyleBar class, the display and blanking of the toolbar must take advantage of the following functions: Bool CMAINFRAME :: OneTtool (uint nid) { File: / / Have a drop-down combination box function tool bar display and blanking function CWND * PBAR; // Toolbar window pointer IF ((PBAR = getdlgitem (nid)) == null) / / get mouse click option Return False; // Control Tool Tool Window Style PBAR-> ShowWindow ((PBar-> getStyle () & ws_visible) == 0); RECALCLAYOUT (); // Re-display window Return True; } Void CMAINFRAME :: OnUpdateEdittool (ccmdui * pcmdui) { File: / / Have a drop-down combination box function tool strip status update CWND * PBAR; // Toolbar window pointer IF ((PBAR = getdlgitem (pcmdui-> m_nid)) == NULL) { PCMDUI-> ContinuerOuting (); // Continue judgment Return; } File: // Set check status PCMDUI-> SetCheck ((PBar-> getStyle () & ws_visible)! = 0); } 6.3.10.3 Display and Hide of Multi-Tools If there are many toolbars, use the menu control method is inconvenient, it is best to use the method of dialog settings, such as the author controls the display and blanking, button arrangement of the toolbar in the program, buttons, buttons, number of dynamic prompts Hindly. The steps are as follows: 1 First Open the dialog resource in the resource list, use "Insert-> Resource-> Dialog" to insert the dialog IDD_TOOLBAR, create four toolbar check box buttons in the dialog box, four selection buttons for four arrangements and two The radio button of the dynamic prompt (please relevant graphics), and set the ID identity and button name for each button, pay attention to the Group attribute of the first button in each set of buttons; 2 Create new class ctooldlg and two implementation files for dialog box for dialog box for dialog box; 3 Open the dialog to implement the file tooldlg.h, add the following member control variables: Class CToolDlg: Public CDIALOG {... // Other code FILE: / / {{AFX_DATA (CToolDlg) ENUM {IDD = IDD_TOOLBAR}; BOOL M_BMAINTOOL; / / Tool strip control variable Bool M_BTestTool; Bool m_bdocktool; BOOL M_BDOCKNEXT; INT M_NTOOLTIPS; // Dynamic prompt control variable INT M_NCOLUMNS; File: // Button Arramement Variable Variables ... // Other code } 4 Increasing the following control code in implementing the file tooldlg.cpp: CToolDlg :: ctooldlg (cwnd * pparent / * = null * /) : Cdialog (ctooldlg :: IDD, pParent) { File: // Perfect member variable initialization FILE: / / {{AFX_DATA_INIT (CTOOLDLG) m_bmaintool = false; m_btesttool = false; m_bdocktool = false; M_BDockNext = FALSE; m_ntooltips = -1; m_ncolumns = -1; File: //}} AFX_DATA_INIT } Void CToolDlg :: DODATAEXCHANGE (CDataExchange * PDX) { File: // Improve dialog box button status initialization CDIALOG :: DODATAEXCHANGE (PDX); File: // {{{AFX_DATA_MAP (CTOOLDLG) DDX_CHECK (PDX, IDC_MAINTOOL, M_BMAINTOOL); DDX_CHECK (PDX, IDC_TestTool, m_btesttool); DDX_CHECK (PDX, IDC_DOCKTOOL, M_BDOCKTOOL); DDX_CHECK (PDX, IDC_DOCKNEXT, M_BDOCKNEXT); DDX_RADIO (PDX, IDC_SHOW, M_NTOOLTIPS); DDX_RADIO (PDX, IDC_TWO, M_NCOLUMNS); File: //}} AFX_DATA_MAP } 5 Add ToolDlg.h containing files in the main program implementation file mainfrm.cpp, and improve the corresponding menu option function. As the author program is the function of improving the Test option, its control code is as follows: Void CMAINFRAME :: Onbuttontest () { File: // Improve the corresponding menu option CToolDlg Tooldlg; // Tune dialog UINT NCOLUMNS = m_wnddocktoolbar.getColumns (); NCOLUMNS - = 2; // Get the number of buttons File: // Get the visual style of the toolbar Tooldlg.m_bmaintool = ((m_wndmaintoolbar. GetStyle () & ws_visible)! = 0); Tooldlg.m_btestttool = ((m_wndtesttoolbar. GetStyle () & ws_visible)! = 0); Tooldlg.m_bdocktool = ((m_wnddocktoolbar. GetStyle () & ws_visible)! = 0); Tooldlg.m_bdocknext = (M_WnddockNextBar. GetStyle () & ws_visible)! = 0); Tooldlg.m_ncolumns = ncolumns; Tooldlg.m_ntooltips = (m_btooltips)? 0: 1; IF (idok == tooldlg.domodal ()) { File: // Control the display and blanking of the toolbar ShowControlbar (& M_WndmainToolbar, Tooldlg.m_bmaintool, false); ... // Other tool strips control code, basically the same m_btooltips = (ToolDlg.m_NTooltips == 0); IF (m_btooltips) {// Control Dynamic Tips M_WndmainToolBar.Setbarstyle (M_WndmainToolBar.getBarsTyle () | CBRS_Tooltips | CBRS_FLYBY); ... // Other tool strips control code, basically the same } else { m_wndmaintoolbar.setbarstyle (m_wndmaintoolbar. GetBarsTyle () & (CBRS_Tooltips | CBRS_FLYBY)); ... // Other tool strips control code, basically the same } NColumns = ToolDlg.m_ncolumns 2; // Control Toolbar Button Column M_WnddockToolBar.SetColumns (ncolumns); M_WnddockNextBar.SetColumns (ncolumns); m_wndmaintoolbar.invalidate (); // Control each toolbar display update ... // Other tool strips control code, basically the same } } 6.3.11 Property Control of Toolsum The mouse button in Win95 / 98 has a strong feature, and many applications have a property control settings for various functions, and the toolbar in the application increases attribute control functions more practical. In the property table of the toolbar, you can set the blanking and display of the toolbar and the sorting of the toolbar buttons, and other various control functions, which is determined based on the needs of the application. Here, the display and blanking of the control tool strip are used as an example, and the specific steps of its implementation are set forth in detail: (1) First open the menu resource in the resource list, add menu resource IDR_TOOLBAR, build four toolbar menu options in the menu, and set the ID ID and menu item name for each menu option. Note that the menu option identifier is set to the same control. If the main control tool strip ID is idR_WndmainToolbar in this program, the menu option ID identifier of the control master tool must also be idR_WndmainToolbar. After setting, you can use the features of the MFC class library to control the display and blanking of the toolbar, the menu item check status and non-check. (2) Then use the class wizard classwizard to add an interpretation message PretranslateMessage processing function for the application main frame window; (3) Perfect interpretation message functions in the application implementation file mainfrm.cpp: Bool CMAINFRAME :: PretranslateMessage (MSG * PMSG) {IF (PMSG-> Message == WM_RBUTTONDOWN) { CWND * PWND = CWND :: fromHandlepermanent (PMSG-> HWND); CControlbar * PBAR = Dynamic_DownCast (Ccontrolbar, PWND); // Get the control strip handle IF (PBAR! = null) { CMenu Menu; // Establish a pop-up menu Cpoint pt; File: // Mouse pointer position Pt.x = loword (PMSG-> LPARAM); Pt.y = HiWord (PMSG-> LPARAM); PBAR-> ClientToscreen (& Pt); // Convert to screen coordinates IF (Menu.LoadMenu) {// Tune menu resources Cmenu * psubmenu = menu.getsubmenu (0); // get option IF (psubmenu! = null) {// Tracking menu to get an input Psubmenu-> TRACKPOPUPMENU (TPM_LEFTALIGN | TPM_RightButton, Pt.x, Pt.y, this); } } } Return CFrameWnd :: PretranslateMessage (PMSG); } (4) Added after the message mapping function Begin_MESSAGE_MAP (CMAINFRAME, CFRAMEWND) File: // {{{AFX_MSG_MAP (CMAINFRAME) ... // Other message mapping functions File: //}} AFX_MSG_MAP / / Add an extended command message and update message mapping, update the menu with the current MFC function ON_UPDATE_COMMAND_UI (IDR_MAINTOOLBAR, ONUPDATECONTOLBARMENU) ON_COMMAND_EX (iDR_maintoolbar, onbarcheck) ... // The other tool bar mapping function is basically the same, only the identification is different. END_MESSAGE_MAP () (5) Important programming skills steps The above steps 1 and 4 are the most critical steps that implement the tool strip attribute menu effectively control, and the tool is not possible if the menu ID is not set to the ID of the toolbar in step 1, or does not set the tool without setting the corresponding message mapping in step 4. Effective control of the strip menu and toolbar status. Other properties in the toolbar menu can be imitated. The tool strip control drill sample for the author is as follows: Toolbar exercise example results Chapter 7 Exercust CProgress 7.1 Progress Control is mainly used to perform operational progress prompts when operating data read / write, file copy, and disk format, such as installer, etc., accompanying work progress Advances, the rectangular area of the progress bar is constantly filling with the color of the current active window title strip from left to right. The progress bar is controlled in the encapsulation class in the MFC class library as CProgressCtrl, which is usually only controlled as an output class, so its operation is mainly to set the range and the current position of the progress bar, and constantly update the current position. The scope of the progress bar is used to represent the length of time of the entire operating process, and the current position represents the current time of the completion. SetRange () function is used to set the range, the initial range is 0-100, and the setpos () function is used to set the current location, the initial value is 0, the setstep () function is used to set the step size, the initial step length is 10, step () The function is used to update the location according to the current step size, and the offsetpos () function is used to move the current location from a distance. If the range or position changes, the progress bar will automatically redraw the progress area to promptly reflect the progress of the current work. Establishment Method for Scattered Object Screen Control CPROGRESSCTRL & ProgressCtrs CPROGRESSCTRL & PROGRESSCTRL Establishment Strip Control Object Structure CREATE Establishment Screen Control Object and Bind Object Progress Control Class CPROGRESSCTRL :: CREATE The call format is as follows: BOOL CREATE (DWord DWStyle, Const Rect & RECT, CWND * PPARENTWND, UINT NID; these parameter dWStyle is used to determine the control style of the progress bar control; the parameter RECT is used to determine the size and position of the progress bar control; the parameter pParentWnd is used to determine the progress strip parent window pointer; parameter NID To determine the control ID value of the progress bar control. 7.2 Progress Bar Control Class Attribute Progress Strip Controlling Class Attributes include setting progress bar maximum minimum control range setRANGE, setting schedule current location setpos, setting progress bar current position offset value Offsetpos and setting schedule control incremental setstep. 7.3 Operation method of progress bar control The operation method control method is mainly to control the progress bar and redraw the STEPIT function of the progress bar. Application Skills for Progress Bar Control 1, using the application wizard appWizard to generate an object box-based application CProgDLG; 2, set the progress bar and static text control in the dialog, the ID is IDC_Prog and IDCPERCENT; in the dialog initial code Increase the range and position of the control: Two data members in progdlg.h, used to represent the maximum value and step size of the progress bar: file: //progdlg.hclass cprogdlg: public cdialog {. . . . . . // Other code public: int m_nmax, m_nstep; . . . . . File: // Other Code} (2) Set the initial state BOOL CPROGDLG :: OnItDialog () {cdialog :: onInitdialog () ;... . . . . . // Other code file: // Todo: add extra initialization hender = (cProgressCtrl *) getdlgitem (idc_prog); pprogctrl-> setRANGE (0,200); // Set the schedule range. . . . . . // Other code m_nmax = 200; m_nstep = 10; settimer (1, 1000, null); // Set the progress bar update clock return true;} (3) Improve the WM_TIMER message processing, so that the schedule is updated in accordance with the current step size, At the same time the percentage of completion of the progress bar display: void CProgDlg :: OnTimer (UINT nIDEvent) {file: // TODO: Add your message handlerCProgressCtrl * pProgCtrl = (CProgressCtrl *) GetDlgItem (IDC_PROG); int nPrePos = pProgCtrl-> StepIt (); // Get the pre-update position char test [10]; int npercent = (int) ((nprepos m_nstep) /m_nmax*100 0.5); WSPrintf (TEST,% D %%, NPERCENT); getdlgitem (IDC_PERCENT) > SetWindowText (Text); cdialog :: Ontimer (nidevent); Chapter 8 Drive CSlider slider control (Slider Control) is also called track strip control, which is mainly using a small window with track and slide, so that the user chooses a discrete data or a continuous numerical section. . By the mouse or keyboard for data selection operation, this can be seen in many applications in WIN98 / 95, such as mice in the control panel, etc., the slider can either horizontally or vertical. The sliding strip control is as follows: The TBS_HORZ slider is the horizontal TBS_VERT slider is the vertical TBS_LEFT slider located on the left side TBS_RIGHT sliding strips located on the top of the window TBS_BOTTOM slider is located at the bottom TBS_BOTH sliding strip at the bottom TBS_BOTH sliding strip The TBS_AUTICKS slider on both sides of the window has a scale, and the default TBS_NOTICKS slider does not have a scale bar whose slider slider is displayed in each numerical location, and if a value selection interval is displayed on the slider, the style TBS_ENABLSELRANGE, At this time, the selection interval is no longer a scale mark, but a small triangle symbol. In addition, using style TBS_NothUMB will blame the slide. The slider control is encapsulated in the MFC class library to CSLIDERCTRL control, which is the main operation to set the scale range, draw the scale mark, set the selection range and the current slide position. When the user interacts, the slider control will send a message WM_HSCROLL to its parent window, so the onhScroll () member function of the parent window should be overloaded in the application so that the message to properly handle the system sent by the message, slide Position and pointer to the CSLIDERCTRL object, etc. Because of the considering and horizontal roller common same member function, the pointer variable in the onhScroll () function parameter table is defined as a cscrollbar * type. Since the actual message is generated by the slider, this pointer must be put in the program. The variable is forced to convert to a CSLiderCtrl * type. The message code and meaning of the slider and the rolling rod are very similar to TB_BOTTOM, so this processing method is relatively reasonable. The setRange () function is used to set the range, and the setpos () function is used to set the current location. 8.1 Slide strip control of the object structure Sliding strip Control Establishment Method CSLIDERCTRL & SLIDERCTRL Create Sliding Bar Control Object Structure CREATE Creating Sliding Bar Control Objects and Binding Object Slip Slip Control Class CSLIDERCTRL :: CREATE The call format is as follows: BOOL CREATE (DWORD DWSTYLE, Const Rect & Rect, CWND * PParentWnd, Uint Nid); where parameter dWStyle is used to determine the sliding bar control style; the parameter RECT is used to determine the size and position of the slider control; the parameter pParentWnd is used to determine the parent window pointer controlled by the slider; NID is used to determine the control ID value of the slider control. 2, the slider-controlled class property slider control object class attribute includes obtaining the slider size getLinesize, set the slider size setLinesize, get the sliding bar size getpageSize, set the sliding strip size setpageSize, get the maximum position of the slider, get GetRangemax, get The sliding strip minimum position getRangemin, get the slider range getRange, set the slider minimum position setRangemin, set the slider maximum position setRANGEMAX, set the slider range setRange, get the current location of the slider, set the slider Current position setSelection, get the slider Current Location GETPOS and set the slider Current location setpos, etc. 3, the operation method of the sliding strip control Slide strip controls the operation method including clearing the slider Clearsel to verify that the current position of the slider is between VerifyPos and clear the current scale flag Cleartics. Slide strip control application Skills Example 1, using the application wizard AppWizard to generate the object box-based application CSLIDDLG; 2, set the slider control in the dialog, the ID is IDC_SLIDER; 3, increasing control in the initial code of the dialog box Range and location: (1) Set the data member in sliddlg.h, used to indicate the current value of the slider: file: //sliddlg.hclass csliddl: public cdialog {. . . . . . // Other code public: int m_ncur ;. . . . . . // Other code} (2) Set the initial state BOOL CSLIDDLG :: OnItDialog () {cdialog :: onInitdialog () ;. . . . . . // Other code file: // Todo: add extra initialization henderctrl * pslidctrl = (csliderctrl *) getdlgitem (idc_sllider); pslidctrl-> setRANGE (1, 5, true); // Set the slider range PSLIDCTRL-> setpos (2 ) // Set the slider position. . . . . . // Other code RETURN TRUE;} (3) Perfecting the message processing of the slider, use the class wizard classwizard to add the WM_HSCROLL message processing function of the dialog window, and obtain the position value of the slide: void csliddlg :: Onhscroll (uint nsbcode, UINT nPos, CScrollBar * pScrollBar) {file: // TODO: Add your message handlerCdialog :: OnHScroll (nSBCode, nPos, pScrollBar); CSliderCtrl * pSlidCtrl = (CSliderCtrl *) GetDlgItem (IDC_SLLIDER); m_nCur = pSlidCtrl-> getPos () ; // get the current position value} Chapter 9 Exercise CSPIN9.1 Rotation Button Control Rotation Button Control (UP Down Control), the main function is to use a pair of small buttons with opposite direction arrows, by clicking It changes the current value within a certain range. The current value of the rotating button control is usually displayed in a control called the Buddy window, which can be an edit box. As with other controls, it can be controlled by setting the style of the rotating button. Its style is as follows: UDS_HORZ rotation button is the horizontal UDS_VERT rotation button is the vertical UDS_ALIGNLEFT Rotate button located on the left side of the partner window. The UDS_AUTOBUDDY of the partner window is set to the Dialog box's partner window in the dialog box Control, that is, the Tab sequence, one control UDS_SETBUDDYINT rotation button, the current value of the partner window is automatically changed to the value uds_nothousands displayed in the partner window Title, and the current value of each triple in the digital "," separator UDS_WRAP rotation button is beyond the maximum ( The minimum) value is automatically wound to the smallest (maximum) value UDS_ARROWKEYS allows the user to change the current value rotation button to any side of the partner window with cursor keys. If located on one side of the partner window, the width of the partner window should be appropriately reduced to accommodate the down rotation button. The rotation button is controlled in the MFC class library to be encapsulated as a CSPinButtonCtrl class. It is mainly to obtain and set the variation range of the rotary button, the current value, partner window, and partner window display the current data value of decimal or hexadecimal and users. The acceleration of the rate of change when the button is restored. The setRange () function is used to set the range, and the setpos () function is used to set the current location. 9.2 Rotating button Controlled Object Structure 1, CSPINBUTTONCTRL & SPINBUTTONCTRL CSPINBUTTONCTRL & SPINBUTTONCTRL CSPINBUTTONCTRL & SPINBUTTONCTRL CREATE Create Rotate Buttons Control Object and Bind Object Rotate Buttons Control Class CSPinButtonCtrl :: Create The call format is as follows: BOOL CREATE (DWORD DWSTYLE, CONST RECT & RECT, UINT NID; where parameter dWStyle is used to determine the style of the rotation button control; the parameter RECT is used to determine the size and position of the rotation button control; the parameter pParentWnd is used to determine the parent window pointer; parameter NID The control ID value used to determine the rotation button. 2, the category of the category rotation button controlled by the rotation button control includes setting the accelerometer setAccel, obtain the accelerator set, set the base setBase, get the base setBase, set the partner window setBuddy, get the partner window getBuddy, set the current location setpos, get the current location Location GETPOS, set the upper limit set setRange and obtain the upper limit limit GetRange et al. 9.3 Application Skills Controlled by Rotate Buttons 1, using the application wizard AppWizard to generate the object box-based application cspindlg; 2, set the rotation button control and edit box control in the dialog, the ID is IDC_SPIN and IDC_EDIT; in dialog In the initial code, the range and location of the control range: (1) Set the data member in Spindlg.h, used to represent the step size value of the rotary button: file: //sliddlg.hclass csliddlg: public cdialog {. . . . . . / / Other code public: int m_nstep; . . . . . // Other code} (2) Set the initial state BOOL CSLIDDLG :: OnItDialog () {cdialog :: onInitdialog () ;. . . . . . // Other code file: // Todo: add extra initialization hender = (cspinbuttonctrl *) getdlgitem (idc_spin); pspinctrl-> setRANGE (-50, 50); // Set rotation button range pspinctrl-> setpos (10) ; // Set the rotation button position. . . . . . // Other code RETURN TRUE;} (3) Perfecting the message processing of the rotation button, use the class wizard classwizard to increase the EN_CHANGE message processing function of the edit box so that the data of the partner window edit box changes with the rotation button: void cspindlg: : OnChangeEdit () {file: // Todo: add your message handlerm_nstep = getdlgitemint (IDC_EDIT) ;. . . . . . // Other processing}