Proficient in 280 examples of VC ++ practical programming

Source: Internet
Author: User

The control bar of Windows applications includes the toolbar and status bar. You can add buttons and controls in the toolbar to perform quick operations. Some prompts can be displayed in the status bar. In MFC, the toolbar and status bar in the control bar are implemented through the CToolBar class and CstatusBar class respectively. Their parent classes are both CContorlBar classes.

Create a toolbar, create a toolbar resource, construct a CToolBar Class Object, call the CToolBar: CreateEx function to create a toolbar window, and call the CToolBar: LoadToolBar function to load toolbar resources.

// Create an application with multiple documents and add one toolbar resource. The ID is IDR_TOOLBAR. Add the member variable public: CToolBar m_wndDemoToolBar to the CMainFrame class; // Add the WM_CREATE message processing function int CMainFrame: OnCreate (maid) {if (CMDIFrameWnd: OnCreate (LPCREATESTRUCT) {if (CMDIFrameWnd: OnCreate (maid) =-1) return-1; //... // create a toolbar window if (! Compile (this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | reset) {return-1;} // load toolbar resources if (! M_wndDemoToolBar.LoadToolBar (IDR_TOOLBAR) {return-1;} return 0 ;}

You can call the CWnd: SetWindowText function to set the title of the toolbar.

// Add the WM_CREATE message processing function int CMainFrame: OnCreate (maid) {if (CMDIFrameWnd: OnCreate (LPCREATESTRUCT) {if (CMDIFrameWnd: OnCreate (maid) =-1) return-1; if (! M_wndToolBar.CreateEx (this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) |! M_wndToolBar.LoadToolBar (IDR_MAINFRAME) {TRACE0 ("Failed to create toolbar \ n"); return-1; // fail to create} // set the toolbar title CString strText = _ T (""); strText. loadString (AFX_IDS_APP_TITLE); m_wndToolBar.SetWindowText (strText );//... return 0 ;}

Call the CControlBar: EnableDocking function and CFrameWnd: EnableDocking function, and then call the CFrameWnd: DockControlBar function.

// Create an application with multiple documents and add one toolbar resource. The ID is IDR_TOOLBAR. Add the member variable public: CToolBar m_wndDemoToolBar to the CMainFrame class; // Add the DockBar member function void CMainFrame: DockBar (CControlBar * pDestBar, CControlBar * pSrcBar) {RecalcLayout (TRUE); // obtain the toolbar window coordinate CRect rect; pDestBar-> GetWindowRect (& rect); rect. offsetRect (1, 0); // obtain the toolbar style DWORD dwStyle = pDestBar-> GetBarStyle (); UINT nDockBarID = 0; if (dwStyle & CBRS_ALIGN_TOP) {nDockBarID = AFX_ I DW_DOCKBAR_TOP;} else if (dwStyle & Strong) {nDockBarID = Beijing;} else if (dwStyle & CBRS_ALIGN_LEFT) {nDockBarID = Beijing;} else if (dwStyle & CBRS_ALIGN_RIGHT) {nDockBarID = AFX_IDW_DOCKBAR_RIGHT;} // dock the toolbar DockControlBar (pSrcBar, nDockBarID, & rect);} // Add the WM_CREATE message processing function int CMainFrame in the CMainFrame class :: onCreate (maid) {if (CMDIFrameWnd: OnCreat E (lpCreateStruct) =-1) return-1; //... // create a toolbar window if (! Compile (this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | reset) {return-1;} // load toolbar resources if (! M_wndDemoToolBar.LoadToolBar (IDR_TOOLBAR) {return-1;} return 0 ;}

You can call the CToolBar: SetBitmap function to set the toolbar bitmap.

// Create an application with multiple documents, add one toolbar resource, ID is IDR_TOOLBAR, add one bitmap resource, ID is IDB_BITMAP1, and add the member variable public in the CMainFrame class: CToolBar handle; CBitmap m_Bitmap; // Add the WM_CREATE message processing function int CMainFrame: OnCreate (LPCREATESTRUCT lpCreateStruct) {if (CMDIFrameWnd: OnCreate (lpCreateStruct) to the CMainFrame class) =-1) return-1 ;//... // create a toolbar window if (! Compile (this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | reset) {return-1;} // load toolbar resources if (! Inline (IDR_TOOLBAR) {return-1;} // dock the toolbar using (CBRS_ALIGN_ANY); EnableDocking (CBRS_ALIGN_ANY); DockBar (& m_wndToolBar, & m_wndDemoToolBar ); // set the bitmap m_Bitmap.LoadBitmap (IDB_BITMAP1); m_wndDemoToolBar.SetBitmap (HBITMAP) m_Bitmap); return 0 ;}

You can call the CToolBar: SetButtonText function to add text labels for toolbar buttons.

// Create an application with multiple documents and add the WM_CREATE message processing function int CMainFrame: OnCreate (maid) to the CMainFrame class {if (CMDIFrameWnd: OnCreate (LPCREATESTRUCT) =-1) return-1; if (! M_wndToolBar.CreateEx (this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) |! M_wndToolBar.LoadToolBar (IDR_MAINFRAME) {TRACE0 ("Failed to create toolbar \ n"); return-1; // fail to create} for (int n = 0; n <m_wndToolBar.GetCount (); n ++) {// obtain the toolbar button IDUINT nID = m_wndToolBar.GetItemID (n); CString strText = _ T (""); if (! StrText. loadString (nID) {continue;} int nIndex = strText. find (_ T ('\ n'); if (nIndex <0) {continue;} strText = strText. right (strText. getLength ()-nIndex-1); // set the toolbar button text m_wndToolBar.SetButtonText (n, strText);} CRect rect; m_wndToolBar.GetItemRect (0, & rect ); // set the toolbar button size m_wndToolBar.SetSizes (rect. size (), CSize (16, 15 ));//... return 0 ;}

Add a drop-down arrow to the toolbar button. Call the CToolBarCtrl: SetExtendedStyle function to set the TBSTYLE_EX_DRAWDDARROWS style in the toolbar. Then call the CToolBar: SetButtonStyle function, set the TBSTYLE_DROPDOWN style on each button that adds a drop-down arrow.

// Create an application with multiple documents and add the WM_CREATE message processing function int CMainFrame: OnCreate (maid) to the CMainFrame class {if (CMDIFrameWnd: OnCreate (LPCREATESTRUCT) =-1) return-1; if (! M_wndToolBar.CreateEx (this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) |! M_wndToolBar.LoadToolBar (IDR_MAINFRAME) {TRACE0 ("Failed to create toolbar \ n"); return-1; // fail to create} // set the extended style of the toolbar m_wndToolBar.GetToolBarCtrl (). setExtendedStyle (toolbar); // set the toolbar button style int nIndex = m_wndToolBar.CommandToIndex (ID_FILE_NEW); UINT nStyle = partition (nIndex); nStyle | = TBSTYLE_DROPDOWN; struct (nIndex, nStyle ); //... return 0;} // Add the TBN_DROPDOWN message processing function to the toolbar in the CMainFrame class. // in the MainFrame. h. The declaration of adding the message processing function to the header file: afx_msg void OnDropDownToolBar (NMHDR * pNMHDR, LRESULT * pResult); // In MainFrame. tbin_message_map (CMainFrame, CMDIFrameWnd) // {AFX_MSG_MAP (CMainFrame) ON_NOTIFY (tbdropn_down, success, OnDropDownToolBar () // implement the TBN_DROPDOWN message: void CMainFrame: OnDropDownToolBar (NMHDR * pNMHDR, LRESULT * pResult) {NMTOOLBAR * pNMToolBar = (NMTOOLBAR *) pNMHDR; switch (pNMToolBar-> iItem) {case ID_FILE_NEW: AfxMessageBox (_ T ("new"); break; default: break ;}}

By default, AppWizard uses toolbar resources to set button images. The toolbar resource contains a composite bitmap that is converted into an image list when calling the CToolbar: LoadToolBar function.

To set a hotspot image for the toolbar button, call the CImageList: Create Function to Create a new image list, and then call the CToolBarCtrl: SetHotImageList function, set the image list to the hotspot image list on the toolbar.

// Create an application with multiple documents. Add the member variable and the WM_CREATE message processing function private: CImageList m_ImageList; int CMainFrame: OnCreate (LPCREATESTRUCT lpCreateStruct) to the CMainFrame class) {if (CMDIFrameWnd: OnCreate (lpCreateStruct) =-1) return-1; if (! M_wndToolBar.CreateEx (this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) |! M_wndToolBar.LoadToolBar (IDR_MAINFRAME) {TRACE0 ("Failed to create toolbar \ n"); return-1; // fail to create} // create image list if (! M_ImageList.Create (IDB_BITMAP1, 128,128,128, RGB () {return-1;} // set the toolbar image list m_wndToolBar.GetToolBarCtrl (). setHotImageList (& m_ImageList );//... return 0 ;}

You can call the CControlBar: SetBarStyle function to add or delete the CBRS_TOOLTIPS style.

// Create an application with multiple documents, add a sub menu to the IDR_MAINFRAME and IDR_DEMOTYPE menu resources, and add one menu item to the sub menu (ID: ID_ENABLE_TOOLTIPS) // Add the command handler void CMainFrame: OnEnableTooltips () {// obtain the toolbar style DWORD dwStyle = m_wndToolBar.GetBarStyle () in the CMainFrame class (); // if (dwStyle & CBRS_TOOLTIPS) {dwStyle & = ~ CBRS_TOOLTIPS;} else {dwStyle | = CBRS_TOOLTIPS;} // set the toolbar style m_wndToolBar.SetBarStyle (dwStyle);} void CMainFrame: OnUpdateEnableTooltips (CCmdUI * pCmdUI) {// obtain the toolbar style DWORD dwStyle = m_wndToolBar.GetBarStyle (); if (dwStyle & CBRS_TOOLTIPS) {pCmdUI-> SetCheck (TRUE );} else {pCmdUI-> SetCheck (FALSE );}}

To add a combo box to the toolbar, you must first call the CToolBar: SetButtonInfo function, create a blank area (TBBS_SEPARATOR) for the combo box, and then call the CToolBar: GetItemRect function, obtain the size and position of the blank area, call the CComboBox: Create function, and Create a combo box in the blank area.

// Create an application with multiple documents and add the member variable CComboBox m_ComboBox to the CMainFrame class; // Add the CreateComboBox member function BOOL CMainFrame: CreateComboBox (UINT nID) to the CMainFrame class) {int nIndex = 0; CRect rect; // Search button while (m_wndToolBar.GetItemID (nIndex )! = NID) {nIndex ++;} // create a blank area m_wndToolBar.SetButtonInfo (nIndex, nID, TBBS_SEPARATOR, 80) for the combo box; // obtain the blank area size and position m_wndToolBar.GetItemRect (nIndex, & rect); // the size and position of the combo rect. top + = 0; rect. bottom + = 200; // create a combo box if (! M_combox.create (WS_CHILD | WS_VISIBLE | items | CBS_DROPDOWNLIST | CBS_HASSTRINGS, rect, & m_wndToolBar, nID) {TRACE0 ("Failed to create combo-box \ n"); return FALSE ;} // display the box m_combox.showwindow (SW_SHOW); // Add the string m_combox.addstring ("500%") to the box; m_ComboBox.AddString ("200%"); m_ComboBox.AddString ("150% "); m_combox.addstring ("100%"); m_ComboBox.AddString ("75%"); m_ComboBox.AddString ("50%"); m _ ComboBox. addString ("25%"); m_combox.addstring ("10%"); m_combox.setcursel (3); return TRUE ;}// Add the WM_CREATE message to the CMainFrame class to process the int CMainFrame function :: onCreate (maid) {if (CMDIFrameWnd: OnCreate (maid) =-1) return-1; if (! M_wndToolBar.CreateEx (this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) |! M_wndToolBar.LoadToolBar (IDR_MAINFRAME) {TRACE0 ("Failed to create toolbar \ n"); return-1; // fail to create} // create a combo box if (! CreateComboBox (ID_COMBOBOX) {TRACE0 ("Failed to create combo box \ n"); return-1 ;}//... return 0;} // Add the CBN_SELCHANGE message processing function void CMainFrame: OnSelchangeCombobox () {CString strText = _ T (""); m_ComboBox.GetWindowText (strText) to the combo box in the CMainFrame class ); afxMessageBox (strText );}

Create a status bar, construct a CStatusBar Class Object, call the CStatusBar: Create Function to Create the status bar window, and call the CStatusBar: SetIndicators function to set the status bar indicator.

// Create an application with multiple documents and add the member variable protected: CStatusBar m_wndDemoStatusBar to the CMainFrame class; // Add the WM_CREATE message processing function int CMainFrame to the CMainFrame class :: onCreate (maid) {if (CMDIFrameWnd: OnCreate (maid) =-1) return-1 ;//... // create the status bar window if (! M_wndDemoStatusBar.Create (this) {return-1 ;}// ID array UINT IDArray [2]; for (int n = 0; n <2; n ++) {IDArray [n] = 10000 + n;} // set the status bar indicator values (IDArray, sizeof (IDArray)/sizeof (UINT); // set the width of the pane m_wndDemoStatusBar.SetPaneInfo (0, IDArray [0], SBPS_NORMAL, 100); m_wndDemoStatusBar.SetPaneInfo (1, IDArray [1], SBPS_STRETCH, 0); // you can specify 0, _ T ("status bar: "); m_wndDemoStatusBar.SetPaneText (1, _ T (" "); return 0 ;}

To add a progress bar to the status bar, call the CProgressCtrl: Create Function to Create a progress bar, call the CStatusBar: GetItemRect function to obtain the size and position of the indicator, and call the CWnd :: the MoveWindow function moves the progress bar window.

// Create an application with multiple documents and add a class CProgressStatusBar whose base class is CStatusBar. // Add the member variable public: CProgressCtrl m_Progress to the CProgressStatusBar class; // Add the WM_CREATE and WM_SIZE message processing functions int CProgressStatusBar: OnCreate (LPCREATESTRUCT failed) {if (CStatusBar: OnCreate (lpCreateStruct) =-1) to the CProgressStatusBar class) return-1; // create progress bar m_Progress.Create (WS_CHILD | WS_VISIBLE, CRect (0, 0, 0, 0), this, 1); return 0;} void CProgressStatusBar: OnSize (UINT nType, int cx, int cy) {CStatusBar: OnSize (nType, cx, cy); // obtain the indicator size and position CRect rect; GetItemRect (0, & rect ); // move the progress bar window m_Progress.MoveWindow (rect. left + 1, rect. top, rect. width ()-2, rect. height ();} // Add the member variable protected: CProgressStatusBar member to the CMainFrame class; // Add the WM_CREATE message processing function int CMainframe: OnCreate (LPCREATESTRUCT lpCreateStruct) to the CMainFrame class) {if (CMDIFrameWnd: OnCreate (lpCreateStruct) =-1) return-1; //... // create a status bar window if (! M_wndProgressStatusBar.Create (this) {return-1 ;}// ID array UINT IDArray [2]; for (int n = 0; n <2; n ++) {IDArray [n] = 10000 + n;} // set the status bar indicator values (IDArray, sizeof (IDArray)/sizeof (UINT); // set the width of the pane m_wndProgressStatusBar.SetPaneInfo (0, IDArray [0], SBPS_NORMAL, 200); m_wndProgressStatusBar.SetPaneInfo (1, IDArray [1], SBPS_STRETCH, 0); // you can specify m_wndProgressStatusBar.SetPaneText (0, _ T (""); m_wndProgressStatusBar.SetPaneText (1, _ T (""); // set the progress bar m_wndProgressStatusBar.m_Progress.SetRange (0,100); Progress (10); Progress (50 ); return 0 ;}

You can call the CFrameWnd: ShowControlBar function to display or hide the toolbar and status bar.

// Create an application with multiple documents, add a sub menu to the IDR_MAINFRAME and IDR_DEMOTYPE menu resources, and add two menu items to the sub menu (display toolbar: ID_SHOW_TOOLBAR, display Status Bar: ID_SHOW_STATUSBAR) void CMainFrame: OnShowToolbar () {if (m_wndToolBar.GetStyle () & WS_VISIBLE) {// hide the toolbar ShowControlBar (& m_wndToolBar, FALSE, FALSE );} else {// display toolbar ShowControlBar (& m_wndToolBar, TRUE, FALSE) ;}} void CMainFrame: OnUpdateShowToolbar (CCmdUI * pCmdUI) {if (m_wndToolBar.GetStyle () & WS_VISIBLE) {pCmdUI-> SetCheck (TRUE) ;}else {pCmdUI-> SetCheck (FALSE) ;}} void CMainFrame: OnShowStatusbar () {if (m_wndStatusBar.GetStyle () & WS_VISIBLE) {// hide the status bar ShowControlBar (& m_wndStatusBar, FALSE, FALSE);} else {// display the status bar ShowControlBar (& m_wndStatusBar, TRUE, FALSE);} void CMainFrame :: onUpdateShowStatusbar (CCmdUI * pCmdUI) {if (m_wndStatusBar.GetStyle () & WS_VISIBLE) {pCmdUI-> SetCheck (TRUE) ;}else {pCmdUI-> SetCheck (FALSE );}}

First, call the CReBar: Create Function to Create the combo bar, and then call the CReBar: AddBar function to add the band in the combo bar.

// In resource. h. Add the macro definition in the header file # define IDC_COMBOBOX 1000 # define IDC_TEST 1001 // Add the member variable protected: CReBar m_wndReBar; CComboBox m_ComboBox; CButton m_Button in the CMainFrame class; // Add the WM_CREATE message processing function int CMainFrame: OnCreate (maid) {if (CMDIFrameWnd: OnCreate (LPCREATESTRUCT) {if (CMDIFrameWnd: OnCreate (maid) =-1) return-1; //... // create a combo box if (! M_combox.create (WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWN, CRect (0, 0,100,200), this, IDC_COMBOBOX) {return FALSE ;} // create button m_Button.Create (_ T ("OK"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect (0, 0, 50, 20), this, IDC_TEST ); // create Rebarif (! M_wndReBar.Create (this) {return-1;} // Add the toolbar, combo box, And button to the Rebar m_wndReBar.AddBar (& m_wndToolBar); m_wndReBar.AddBar (& m_ComboBox, NULL, NULL, RBBS_NOGRIPPER | RBBS_BREAK); trim (& m_Button, NULL, NULL, RBBS_NOGRIPPER); return 0 ;}// Add the BN_CLICKED message processing function void CMainFrame for the Button control in the CMainFrame class :: onTest () {CString strText = _ T (""); m_combox.getwindowtext (strText); if ((! StrText. IsEmpty () & (CB_ERR = m_combox.findstring (-1, strText) {m_ComboBox.AddString (strText);} AfxMessageBox (strText );}

 

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.