How to implement toolbars and their properties control with VC ++ 5.0

zhaozj2021-02-11  204

How to use the VC 5.0 implementation toolbar and its properties to control the VC 5.0-style toolbar with a flat appearance, with a "handle" on the left, you can drag this "handle" to move the toolbar with the mouse. The toolbar has a divider (as shown in Figure 1). When the mouse moves on the toolbar, the corresponding button on the toolbar highlights. The enhanced toolbar Cenhancetoolbar, which is discussed herein is derived from the CToolbar class, and is a supplement and extension of the CToolbar class. If you don't care if there is any "handle" in the toolbar, it is very simple to generate a flat toolbar. You only need to add a sentence in the cmainframe's oncreate () function (you must add a function of the toolbar, because the MFC is generated when generating the toolbar): //mainfrm.cpp int CMAINFRAME: ONCREATE (... {... if (! M_wndtoolbar.create (this) // !m_wndtoolbar.loadtoolbar(idr_mainframe) {trace0 ("failed to create toolbar / n"); return -1; // fail To create} m_wndtoolbar.modifystyle (0, tbstyle_flat); // Setting toolbar is flat format ...} If your computer is installed 4.71.1712.3 version of ComctL32.dll (this dynamic library is issued with IE 4.0 ), It is better, your toolbar will automatically draw the separation line. You can move this toolbar through the non-button area of ​​the mouse drag toolbar. The toolbar in GetRight is like this. If you have to get better (more "professional") toolbar, then please do it step by step. 1. Add a new class, this article is called the Cenhancetoolbar class, derived from the CToolbar class. 2. Add a member variable and function to the CENHANCETOOLBAR class and set its access attributes according to your needs.

//EnhanceToolBar.h // Overrides // ClassWizard generated virtual function overrides // {{AFX_VIRTUAL (CEnhanceToolBar) // This function is called virtual void OnUpdateCmdUI (CFrameWnd * pTarget, BOOL bDisableIfNoHndler) button state change; //}} AFX_VIRTUAL / / Implementation Public: Void DrawGrip (CWindowDC * PDC, CRECT & RectWindow); // "Handle" Draw Function Void EraseonClient (); // Erase Non-Customer Void DrawSpace (); // Separation Line Draw Function Void RedRawBackground (); / / Background Redraw Functions Private: int buttonNumber; // Toolbar's button number (including the separated line) ColorRef Hilight, Shadow; // 3D Control Light Color and Shadow Color 3. Adding a member function: //enhancetoolbar.cpp void CenhanceToolBar :: call when OnUpdateCmdUI (CframeWnd * pTarget, BOOL bDisableIfNoHndler) {// this function button state changes in static CUIntArray Styles; int Index; UINT dwStyle; for (Index = 0; Index

RectWindow) {// Draw Handle IF (isfloating ()) // If the toolbar is a floating state, do not draw "handle" return; cRect GripRect = RectWindow; // Get the rectangular area of ​​the handle GripRect.deflateRect (1) The direction of the rectangular area is close to a pixel IF (m_dwstyle & cbrs_orient_horz) // If the toolbar is horizontal, "handle" is left {gripRect.right = GripRect.Left 3; // Draw the first Ridged PDC-> Draw3DRect (GripRect, Hilight, Shadow; GripRect.OffsetRect (4, 0); // Draw a ridge PDC-> Draw3DRect (GripRect, Hilight, Shadow) of the second bulge;} else // If the toolbar is vertical, "Handle" is at the top {GripRect.Bottom = GripRect.top 3; // Draws the ridges of the first bulge PDC-> Draw3DRect (GripRect, Hilight, Shadow); GripRect.OffsetRect (0 , 4); // Draw the ridges PDC-> Draw3DRect (GripRect, Hilight, Shadow);}} void censeoolbar :: DrawSpace () {// Draw Space CclientDC DC (this); for (int index = 0; Index

ScreenToClient (rectWindow); rectClient.OffsetRect (-rectWindow.left, -rectWindow.top); dc.ExcludeClipRect (rectClient); // draw a boundary rectWindow.OffsetRect non-user area (-rectWindow.left, -rectWindow.top); DrawBorders (& DC, RectWindow); // Erase non-drawn part dc.intersectClipRect (RectWindow); sendMessage (WM_ERASEBKGND, (WPARAM) DC.M_HDC); DrawGrip (& DC, RectWindow); // Draw "handle"} Due to planar toolbar It is transparent, so when changing the size and movement (for example, when dragging the toolbar), you need to redraw the background. Similarly, this operation is also required when the button status changes (press or release).

Void Cenhancetoolbar :: RedrawBackground () {// Redraw background CWnd * PParent = getParent (); // Get the parent window pointer if (pParent) {create DrawRect, Rect; getWindowRect (& Re); // get toolbar rectangular area DrawRect = Rect; PParent-> ScreenToClient (& DrawRect); // Convert to parent window coordinate pParent-> InvalIDateRect (& DrawRect); // Re-draw the rectangular area // other toolbar for the parent window for (CWnd * psibling = pParent-> GetWindow (GW_CHILD); pSibling; pSibling = pSibling-> GetNextWindow (GW_HWNDNEXT)) {if (pSibling == this) continue; drawrect = rect; pSibling-> ScreenToClient (& drawrect); // coordinate pSibling- sibling window> InvalidateRect ( & drawrect); //}}} redraw rectangular area 4. use ClassWizard to add message CEnhanceToolBar class map: //EnhanceToolBar.h // {{aFX_MSG (CEnhanceToolBar) afx_msg void OnPaint (); afx_msg void OnNcCalcSize (BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR * lpncsp); afx_msg void OnWindowPosChanging (WINDOWPOS FAR * lpwndpos); //}} aFX_MSG //EnhanceToolBar.cpp BEGIN_MESSAGE_MAP (CEnhanceToolBar, CToolBar) // {{AFX_MSG_MAP (CEnhanceToolBar) ON_WM_PAINT () ON_WM_NCCALCSIZE () ON_WM_WINDOWPOSCHANGING () //}} AFX_MSG_MAP END_MESSAGE_MAP () 5. message mapping function to add the code: void CEnhanceToolBar :: OnPaint () {CToolBar :: OnPaint (); // draw standard toolbar EraseNonClient () ; // Erase the background DrawSpace (); // Draw stereo separation line} void Censeoolbar :: OnnccalcSize (Bool BcalcvalidRects, NCCALCSIZE_PARCVALIDRECTS, NCCALCSIZE_PARAMS FAR * LPNCSP) {// Calculate non-user area for adjustment "handle" ctoolbar :: ONNCCALCSIZE BCALCVALIDRECTS, LPNCSP); if (isfloating ()) // If the toolbar is a floating state, do not draw "handle" return; if (m_dwstyle & cbrs_orient_horz) // If the toolbar is horizontal, "handle" is left left { LPNCSP-> RGRC [0] .left = 2; lpncsp-> RGRC [0] .right = 2;

转载请注明原文地址:https://www.9cbs.com/read-4075.html

New Post(0)