Menuex

xiaoxiao2021-03-06  66

// MenuEx.h: interface for the CMenuEx class.//CMenuEx// Interface: // void InitMenu (CMenu * pMenu, UINT uToolBar, CToolBar * pToolBar) // void InitPopupMenu (CMenu * pMenu, UINT uToolBar, CToolBar * pToolBar Different from INITMENU: INITMENU does not modify the first-level menu as the self-painted style, and // This function includes the first level menu but must be aware: either the instance of this class can only call this //// one of the two functions can not be used together // void SetHighLightColor (COLORREF crColor) // void SetBackColor (COLORREF) // void SetTextColor (COLORREF) // void SetImageLeft (UINT idBmpLeft) // author: lbird (Zheng Heng) / / 2003.1.10 zh0116@163.net QQ: 10992445 //

#if! defined (AFX_MENUEX_H__FE677F6B_2315_11D7_8869_BB2B2A4F4D45__INCLUDED _) # define AFX_MENUEX_H__FE677F6B_2315_11D7_8869_BB2B2A4F4D45__INCLUDED_ # include "afxtempl.h" #if _MSC_VER> 1000 # pragma once # endif // _MSC_VER> 1000

typedef struct tagMENUITEM {CString strText; UINT uID; #ifdef MENUCHAR UINT uChr; #endif UINT uIndex; int uPositionImageLeft;} MENUITEM; typedef MENUITEM * LPMENUITEM; /// class CMenuEx: public CMenu {public: void InitPopupMenu (CMenu * pPopupMenu, UINT uToolBar, CToolBar * pToolBar); void ChangeStyle (CMenu * pMenu, CToolBar * pToolBar, BOOL bIsMainMenu = FALSE); void SetHighLightColor (COLORREF crColor); void SetBackColor (COLORREF); void SetTextColor (COLORREF); # ifdef mENUCHAR LRESULT menuChar ( UINT nChar); # endif void SetImageLeft (UINT idBmpLeft); void MeasureItem (LPMEASUREITEMSTRUCT lpMIS); void InitMenu (CMenu * pMenu, UINT uToolBar, CToolBar * pToolBar); void DrawItem (LPDRAWITEMSTRUCT lpDIS); CMenuEx (); virtual ~ CMenuEx ( );

protected: int m_nSeparator; CSize m_szImageLeft; CBitmap m_bmpImageLeft; int m_nWidthLeftImage; BOOL m_bHasImageLeft; // Are there side bitmap BOOL m_bInitial; // if the menu has been initialized, ie set up self-drawn style int GetImageFromToolBar (UINT uToolBar, CToolBar * pToolBar, COLORREF crMask = RGB (192,192,192)); CList m_ListMenu; #ifdef mENUCHAR CList m_currentListMenu; // m_ListMenu is a subset of the menu refers to the currently open #endif

COLORREF m_colMenu; COLORREF m_colTextSelected; void DrawImageLeft (CDC * pDC, CRect & rect, LPMENUITEM lpItem); void TextMenu (CDC * pDC, CRect & rect, CRect rtText, BOOL bSelected, BOOL bGrayed, LPMENUITEM lpItem); CImageList m_ImageList; COLORREF m_colText; CSize m_szImage; size void DrawMenuItemImage // menu item bitmap (CDC * pDC, CRect & rect, BOOL bSelected, BOOL bChecked, BOOL bGrayed, BOOL bHasImage, LPMENUITEM lpItem); void GrayString (CDC * pDC, const CString & str, const CRect rect };

#ndif //! Defined (AFX_MENUEX_H___FE677F6B_2315_1D7_8869_BB2B2A4F4D45__included _) / ***************************************************** *********************************************************** ************************************************* *********************************************************** ***************************************************** / // menuex.cpp : Implementation of the cmenuex class.////

#include "stdafx.h" #include "menuex.h"

#ifdef _debug # undef this_filestatic char this_file [] = __ file __; # Define new debug_new # Endif

//// construction / destruction //

CMenuEx :: CMenuEx (): m_szImage (16,15) {m_colMenu = :: GetSysColor (COLOR_MENU); m_colText = :: GetSysColor (COLOR_MENUTEXT); m_colTextSelected = :: GetSysColor (COLOR_HIGHLIGHTTEXT);

m_binitial = false; m_bhasimageleft = false; m_nseparator = 10; // Sparter default height}

Cmenuex :: ~ cmenuex () {m_imagelist.deleteimagelist (); while (! M_listmenu.isempty ()) delete m_listmenu.removehead ();

IF (m_bhasimageleft) m_bmpimageleft.deleteObject ();} /// When the menu item is not available, a gray text void cmenuex :: graystring (CDC * PDC, Const Cstring & Str, Const CRect Rect) {CRECT RT (Rect); / / int nmode = pdc-> setbkmode (transparent);

RT.LEFT = 1; rt.top = 1; PDC-> SetTextColor (RGB (255, 255, 255)); PDC-> DrawText (Str, & RT, DT_EXPANDTABS | DT_VCENTER | DT_SIINGLINE);

RT.LEFT - = 1; rt.top - = 1; PDC-> setTextColor (RGB (127, 127, 127)); PDC-> DrawText (Str, & RT, DT_EXPANDTABS | DT_VCENTER | DT_SIINGLINE);

// pDC-> SetBkMode (nMode);} /// menu item bitmap rendering void CMenuEx :: DrawMenuItemImage (CDC * pDC, CRect & rect, BOOL bSelected, BOOL bChecked, BOOL bGrayed, BOOL bHasImage, LPMENUITEM lpItem) {CRect rt (Rect.LEFT, Rect.top, Rect.LEFT M_SZIMAGE.CX 4, Rect.top m_szimage.cy 4);

If (bCHECKED) {if (bgrayed) {// Menu is not available in grayString (PDC, "√", RT);} else {if (bselected) {// Menu check // When the item is selected to draw only one stereo Rectangular PDC-> Draw3DRECT (& RT, RGB (255, 255, 255), RGB (127, 127, 127);}

Rt.inflatecture (-2, -2); // Draw "√" PDC-> setBkmode (transparent); PDC-> setTextColor (m_coltext); PDC-> DrawText ("√", & r, dt_expandtabs | DT_VCenter | DT_SINGLINE );} Rect.LEFT = m_szimage.cx 4 2; Return;}

IF (bhasimage) {cpoint pt (RT.LEFT 2, RT.TOP 2); uint uStyle = ild_transparent; // cimagelist :: Draw () Draw bitmap {uStyle | = ILD_BLEND50; / / Menu is not available, this bitmap is over dark} else {if (bselected) {// When the item is selected to draw only one stereoscopic PDC-> Draw3DRect (& RT, RGB (255, 255), RGB (127, 127, 127));}}

m_imagelist.draw (PDC, LPITEM-> UINDEX, PT, USTYLE); / / Draw bit // in the menu item // Adjust the size of the rectangle // 4: Bit map External rectangular ratio bit map large 4 // 2: Menu text and bitmap external rectangular intervals of 2 Rect.LEFT = m_szimage.cx 4 2;}} /// Draw menu item // Parameters: RECT: Steady Rectangular Rect // RTTEXT: Menu Text RECTvoid CMenuEx :: TextMenu (CDC * pDC, CRect & rect, CRect rtText, BOOL bSelected, BOOL bGrayed, LPMENUITEM lpItem) {// selected menu item first shown a perspective rectangle if (bSelected) pDC-> draw3dRect (& rect, RGB (127, 127, 127), RGB (255, 255, 255));

IF (bgrayed) {graystring (pdc, lpitem-> strText, rttext);} else {PDC-> DrawText (Lpitem-> Strtext, RtText, DT_LEFT | DT_EXPANDTABS | DT_VCENTER);}}

void CMenuEx :: DrawImageLeft (CDC * pDC, CRect & rect, LPMENUITEM lpItem) {if (m_bHasImageLeft || lpItem-> uPositionImageLeft == - 1!) return; CDC memDC; memDC.CreateCompatibleDC (pDC); CBitmap * oldBmp = (CBitmap *) MEMDC.SELECTOBJECT (& M_BMPIMAGELEFT);

INT CY; / / Set how to draw this menu item

IF (m_szimageleft.cy> = lpitem-> UpositionImageLeft Rect.height ()) {cy = (int) m_szimageleft.cy - lpitem-> UpositionImageLeft - Rect.Height (); assert (cy> = 0);} else CY = 0;

PDC-> Bitblt (Rect.left, Rect.top, M_SzimageLeft.cx, Rect.Height (), & MEMDC, 0, CY, SRCCOPY;

MEMDC.SELECTOBJECT (OLDBMP); MEMDC.DELETEDC ();

Rect.Left = m_szimageleft.cx 1;}

Void Cmenuex :: DrawItem (LPDrawItemstruct LPDIS) {CDC DC; LPMENUITEM LPITEM; CRECT Rect (lpdis-> rcitem); Dc.attach (LPDIS-> HDC); lpitem = (lpMenuItem) lpdis-> itemdata;

IF (LPDIS-> ItemState & ODS_SELECTED) DC.SETTEXTCOLOR (M_ColTextSelected); Else Dc.SetTextColor (m_coltext);

/ / Set the background color CBRUSH Brush (M_Colmenu); DC.FillRect (& Rect, & Brush); // Set the display mode dc.setbkmode (transparent);

// Draw the side view DrawiMageLeft (& DC, Rect, LpItem);

IF (LPITEM-> UID == 0) // Subtile {RECT.TOP = Rect.Height () / 2 Rect.top; Rect.Bottom = Rect.top 2; Rect.Left = 2; RECT. Right - = 2; dc.draw3dRect (RECT, RGB (64, 0, 128), RGB (255, 255, 255));} else {

BOOL bSelected = lpDIS-> itemState & ODS_SELECTED; BOOL bChecked = lpDIS-> itemState & ODS_CHECKED; BOOL bGrayed = lpDIS-> itemState & ODS_GRAYED; BOOL bHasImage = (lpItem-> uIndex = - 1!);

// Set the area CRECT RTTEXT (Rect.LEFT M_SZIMAGE.CX 4 2, Rect.top, Rect.right, Rect.bottom); rtText.inflateRect (-2, -2); // Draw Drawmenuitemimage (& DC, RECT, BSELECTED, BHASIMAGE, LPITEM);

// Draw menu text TextMenu (& DC, Rect, RtText, Bselaped, Bgrayed, Lpitem);

#ifdef menuchar // Add Current Menu List M_CurrentListMenu.addtail (Lpitem); # endif}

Dc.Detach ();} ///-change menu style // Note the second parameter: false: Indicates that Pmenu is not the first level menu void cmenuex :: ChangeStyle (cmenu * Pmenu, ctoolbar * ptoolbar, bool bismainmenu) {Assert (PMENU); Trace ("CHANGESTYLE / N"); // afxMessageBox (""); lpMenuitem Lpitem; cmenu * psubmenu; int m, nposition = 0; // This variable is used to draw the position of the side bits INT INX; UINT IDX, X;

For (int i = (int) pmenu-> getMenuItemcount () - 1; i> = 0; i - {lpitem = new menuItem

Lpitem-> uid = pmenu-> getMenuItemID (i); if (! bismainmenu) // is not the first-level menu Lpitem-> UPOSITIONIMAGEFT = -1; // Level Level 2 Menu does not support the side bits ELSE LPITEM-> UPOSITIONIMAGELEFT = nPosition;

IF (Lpitem-> Uid> 0) {if (bismainMenu) nposition = m_szimage.cy 4;

/ / Save Menu Text Pmenu-> GetMenUString (i, lpitem-> strText, MF_BYPOSITION);

#ifDef menuchar // Save Menu text in the character /////s, if the lpitem-> uchr is 0 int Ret = lpitem-> strText.Find ('&'); lpitem-> uchr = 0; if (RET> = 0) Lpitem-> uchr = lpitem-> strText [RET 1]; // Character uniform into size LPITEM-> UCHR & = ~ 0x20; #ENDIF // is looking for menu items in the toolbar graph / / If there is no UINDEX to -1 Lpitem-> uIndex = -1; if (ptoolbar) {for (m = 0; M <(ptoolbar-> gettoolbarctrl (). GetButtonCount ()); m ) {ptoolbar-> getButTonInfo M, IDX, x, inx); if (idx == lpitem-> uid) {lpitem-> uindex = inc;}}}}

// If there is a submenu under this item, recursively call the function to modify the style of its submenu PSubMenu = Pmenu-> GetSubmenu (i); if (psubmenu, ptoolbar);} else {ixMenu NPosition = m_nseparator;} // Modify the menu style for the self-painted PMenu-> ModifyMenu (i, mf_byposition | mf_ownerdraw, lpitem-> uid, (lpctstr) lpitem); m_listmenu.addtail (lpitem);}} //// Bitmap lists used by the toolbar to generate a list of menus m_imagelistint cmenuex :: getImageFromToolbar (uint utoolbar, ctoolbar * ptoolbar, colorRef crMask / * toolbar graph) {if (! Ptoolbar) Return 0;

CBITMAP BMP; INT NWIDTH, NHEIGHT; Bitmap Bmpinfo;

Bmp.LoadBitmap (UTOOLBAR); bmp.getbitmap (& bmpinfo); // Get a height of bitmap nHEight = bmpinfo.bmheight;

Int ncount = 0; int RET = ptoolbar-> gettoolbarctrl (). getButtonCount ();

// Get the number of NCOUNT for (INT i = 0; i GetItemId (i)! = Id_separator) ncount ; // calculate the width of the bit map NWIDTH = Bmpinfo.bmwidth / ncount;

bmp.deleteObject ();

TRACE ("Menu Bitmap - Width:% D / theight:% D / N", NWIDTH, NHEIGHT); // Creating a bitmap list m_imagelist.create (uToolbar, nwidth, nHEight, Crmask); m_szimage.cx = nwidth; m_szimage.cy = nheight;

Return ncount;}

Void cmenuex :: initmenu (cmenu * pmenu, uint uToolbar, ctoolbar * ptoolbar) {// has set a style if (m_binitial) return; GetImageFromToolbar (UTOOLBAR, PTOOLBAR);

CMenu * psubmenu, * psubsub; menuitem * lpitem;

UINT I; INT J, M; int nposition; // This variable is used to draw the position of the side bits.

For (i = 0; i getMenuItemCount (); i ) {psubmenu = pmenu-> getSubmenu (i);

IF (psubmenu) {nPosition = 0; // Note j must be an int type, if it is checked, it is not checked for J> = 0! for (j = (int) psubmenu-> getMenuItemCount () - 1; j> = 0; J - {LPITEM = New Menuitem;

Lpitem-> uid = psubmenu-> getMenuItemId (j); lpitem-> upositionImageLeft = nposition;

IF (lpitem-> uid> 0) {nposition = m_szimage.cy 4;

Psubmenu-> getmenustring (j, lpitem-> strText, mf_byposition); # ifdef menuchar // Save menu text & after the characters // If there is no lpitem-> uchr is 0 int RET = lpitem-> strText.Find (' & '); Lpitem-> UCHR = 0;

IF (RET> = 0) lpitem-> uchr = lpitem-> strText [RET 1]; // Uniform Little LPITEM-> UCHR & = ~ 0x20; #ENDIF // Looking for the menu item by toolbar graph图 / = -1; for (m = 0; m <(). GetButToncount ()); m ) {int Inx; uint IDX, X; Ptoolbar-> GetButtonInfo (M, IDX, X, Inx);

IF (idx == lpitem-> uid) {lpitem-> uIndex = inc;

} Else {// separator nPosition = m_nseparator;} m_listmenu.addtail (LPITEM);

Psubmenu-> ModifyMenu (J, MF_BYPSITION | MF_OWNERDRAW, LPITEM-> UID, LPCTSTR (LPITEM));

Psubsub = psubmenu-> getSubmenu (j);

// Only the first level menu is obtained by the toolbar !! if (psubsub) ChangeStyle (PSubsub, Ptoolbar);}}}

m_binitial = true;}

void CMenuEx :: MeasureItem (LPMEASUREITEMSTRUCT lpMIS) {MENUITEM * lpItem = (LPMENUITEM) lpMIS-> itemData; if (lpItem-> uID == 0) // divider {lpMIS-> itemHeight = m_nSeparator; // lpMIS-> itemWidth = 50;} else {cdc * PDC = AFXGETMAINWND () -> getDC ();

CString strText = lpitem-> strText; CSIZE SIZE;

Size = PDC-> GetTextExtent (lpitem-> strText);

LPMIS-> ITEMWIDTH = Size.cx m_szimage.cx 4; lpmis-> itemheight = m_szimage.cy 4; AFXGETMAINWND () - -> ReleaseDC (PDC);}}

Void cmenuex :: setimageleft (uint idbmpleft) {m_bmpimageleft.loadBitmap (idBMPLEFT); m_bhasimageleft = true; bitmap bmpinfo; m_bmpimageleft.getbitmap (& BmpInfo);

M_szimageleft.cx = bmpinfo.bmwidth; m_szimageleft.cy = bmpinfo.bmheight;}

#ifdef menucharlresult cmenucha :: menuchar (uint nchar) {nchar & = ~ 0x20; menuitem * lpitem;

For (position pos = m_currentlistmenu.getheadPosition (); pOS;) {LPITEM = (lpMenuItem) m_currentlistmenu.getnext (POS);

IF (lpitem-> uchr == nchar) {AFXGETMAINWND () -> SendMessage (WM_COMMAND, LPITEM-> UID); Return 1L;}} Return 0L;} # ENDIF

Void cmenuex :: setTextColor (colorRef crcolor) {m_coltext = crcolor;}

Void cmenuex :: setBackColor (colorRef crcolor) {m_colmenu = crcolor;

Void Cmenuex :: STHighlightColor (colorRef crcolor) {m_coltextselected = crcolor;

/// The style / / Different from INITMENU in the modification menu are: initmenu does not modify the first-level menu as the self-painted style, and // This function includes the first level menu // But must pay attention: This class One instance can only call one of these two functions, can not use Void CMenuex :: InitPopupmenu (CMenu * PpopUpMenu, Uint UToolbar, CToolbar * Ptoolbar) {// The main window menu style if (m_binitial) returnome ;

GetImageFromToolbar (Utoolbar, Ptoolbar);

ChangeStyle (PpopupMenu, Ptoolbar);

m_binitial = true;}

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

New Post(0)