Realization of the self-painted menu

zhaozj2021-02-11  192

Realization of the self-painted menu

Wenyi translation

A CCustomMmenu is provided here, which is a subclass of CMenu and has self-painting capabilities. It can provide you with the following features:

Set the font color. Set high brightness colors. Set the style of high brightness. Set the icon that is selected and in a menu in a normal state. Set the display icon size.

The structure Menudata is defined in the CCustomU, you must fill this structure according to your needs, and provide the pointer to the structure (call Appendmenu, InsertMenu) when adding menus. Below is an example:

1. Define the instance of CCUSTomMenu, and Menudata structural variables.

CCustomMMmenu M_CCUSTommenu;

Menudata Menudata [8]; // AS Many Menu Items Are Present, You Should Be Able To Use

// new and do the same

2. Call the createMenu () Set the relevant parameters.

m_customMenu.createMenu ();

M_CustomMenu.seticonsize (25, 25); // this is to set the size of the icon.

// this sales be used only overce for any menu

// in Order To Resize It, Destroy and Create The Menu Again with Different Size.

m_customMenu.SethighlightStyle (Normal); // or textonly, if you want the

// Background Color to Remain The Same

// and the text color to change to the highlight color.

// The Following SetXxxcolor Sets The Menu Colors. If you dont want to change any, dont call these member functions.

m_customMenu.SetTextColor (RGB (255, 0, 0));

M_CustomMenu.SetBackColor (RGB (255, 255, 255);

m_customMenu.SethighlightColor (RGB (0,0,255));

3, set the menuData variable and increase the menu item.

LSTRCPY (Menudata [0] .MenutexT, "Text1");

MenuData [0] .Menuiconnormal = IDI_ICON1;

m_customMenu.Appendmenu (MF_OWNERDRAW, 3, (LPCTSTR) MENUDATA);

3, overmeasure the onMeasureItem (...) function in your window.

Void CMYVIEW :: OnMeasureItem (int Nidctl, LpMeasureItemstruct LpMeasureItemstructure)

{

IF (LpMeasureItemstruct-> CTLTYPE == ODT_MENU &&

IsMenu ((HMENU) LPMeasureItemStruct-> ItemID &&

(lpMeasureItemstruct-> itemid == (uint) m_hmenusub))

{

m_customMenu.MeasureItem (lpMeasureItemstruct);

}

Else

// Let MFC's self-drawing handle it

CView :: OnMeasureItem (NidctL, LpMeasureItemstructure);

The following functions will help you set the menu properties.

Void SetTextColor (ColorRef);

Void SetBackColor (ColorRef);

Void SthighlightColor (ColorRef);

Void seticonsize (int, int);

Void SthighlightStyle (HighlightStyle); // HighlightStyle: Enum {Normal, Textonly}

Void STHighlightTextColor (ColorRef);

Here is the file code:

// ******************************************************** *******************************

// CustomMenu.h: Header File

//

#if

! defined (AFX_CUSTOMMENU_H__FE5B01C3_1E02_11D1_B87A_0060979CDF6D__INCluded_)

#define afX_CUSTOMMENU_H__FE5B01C3_1E02_11D1_B87A_0060979CDF6D__included_

#iF _MSC_VER> = 1000

#pragma overce

#ENDIF / / _MSC_VER> = 1000

Class menudata

{

PUBLIC:

Menudata () {menuiconnormal = -1; menuiconselected = -1;};

Char menutext [32];

Uint menuiconnormal;

Uint menuiconselected;

}

Typedef enum {normal, textonly} highlightstyle;

///

//

// ccustomMenu Window

Class CCustomMenu: Public CMenu

{

// construction

PUBLIC:

CcustomMenu ();

// attributes

PUBLIC:

// Operations

PUBLIC:

// Overrides

// ClassWizard Generated Virtual Function Overrides

// {{AFX_VIRTUAL (CCUSTommenu)

//}} AFX_VIRTUAL

// Implementation

PUBLIC:

Virtual ~ ccustomum ();

Virtual void DrawItem (LPDrawItemstruct);

Virtual Void MeasureItem (LPMeasureItemstructure);

Void SetTextColor (ColorRef);

Void SetBackColor (ColorRef);

Void SthighlightColor (ColorRef);

Void seticonsize (int, int);

Void SthighlightStyle (HighlightStyle);

Void STHighlightTextColor (ColorRef);

// generated message map functions

protected:

ColorRef M_CRText;

ColorRef m_clrback;

ColorRef M_Clrtext;

ColorRef M_Clrhilight;

ColorRef M_ClrhilightText;

Logfont M_LF;

CFONT M_FONTMENU;

Uint m_imenuheight; bool m_blbtndown;

CBRUSH M_BRBACKGROUND, M_BRSELECT;

CPEN M_PENBACK;

INT M_ICONX, M_ICONY;

HighlightStyle M_HIGHTSTYLE;

// {{AFX_MSG (CCUSTommenu)

// Note - The ClassWizard Will Add and Remove Member functions here.

//}} AFX_MSG

}

///

//

// {{AFX_INSERT_LOCATION}}

// Microsoft Developer Studio Will Insert Additional Declarations Immediate Line.

#ENDIF / /! Defined (AFX_CUSTOMMMU_H__FE5B01C3_1E02_11D1_B87A_0060979CDF6D__INCLUDED_)

// ******************************************************** *******************************

// CustomMenu.cpp: Implementation File

//

#include "stdafx.h"

#include "custommenu.h"

#ifdef _Debug

#define new debug_new

#undef this_file

Static char this_file [] = __file__;

#ENDIF

///

//

// CCustomMenu

CCustomMenu :: ccustomMenu ()

{

m_clrtext = getSyscolor (color_menutext);

M_clrback = getSyscolor (color_menu);

m_brbackground.createsolidbrush (m_clrback);

m_penback.createpen (ps_solid, 0, m_clrback);

M_CRText = m_clrtext;

m_blbtndown = false;

m_ICONX = GetSystemMetrics (SM_CXMENUCHECK);

m_ICONY = GetSystemMetrics; SM_CYMENUCHECK;

m_clrhilight = getsyscolor (color_highlight);

M_BRSELECT.CREATESOLIDBRUSH (M_Clrhilight);

m_clrhilighttext = getsyscolor (color_highlighttext);

ZeromeMory ((pvoid) & m_lf, sizeof (logfont));

NonclientMetrics nm;

nm.cbsize = sizeof (nonclientmetrics);

// get the system metrics for the capenfromhere

Verify (SYSTEMPARETERSINFO (SPI_GETNONCLIENTMETRICS, 0, & nm, 0));

M_LF = nm.lfmenufont;

m_imenuheight = nm. IMENUHEIGHT;

M_FontMenu.createFontIndirect (& M_LF);

}

CCustomMenu :: ~ ccustomMenu ()

{

IF ((Hbrush) M_BRBACKGROUND! = null)

m_brbackground.deleteObject (); if ((hfont) m_fontmenu! = null)

m_fontmenu.deleteObject ();

IF ((hbrush) m_brselect! = null)

M_BRSELECT.DELETEOBJECT ();

}

///

//

// ccustomMenu Message Handlers

Void CCustomMenu :: DrawItem (LPDrawItemstruct LPDIS)

{

Assert (lpdis! = Null);

CDC * PDC = CDC :: fromHandle (LPDIS-> HDC);

CRECT RECT;

Hicon Hicon;

ColorRef crText = m_crtext;

// Draw the Colored Rectangle Portion

Rect.copyRect (& lpdis-> rcitem);

// DRAW THE UP / DOWN / FOCUSED / DISABLED State

Uint action = lpdis-> itemaction;

Uint State = lpdis-> itemstate;

CString strText;

Logfont LF;

LF = M_LF;

CFont DispFont;

Cfont * pfont;

// getWindowText (strText);

IF (lpdis-> itemdata! = null)

{

Strtext = ((Menudata *) (LPDITA *)) -> Menutext);

IF (((Menudata *)) -> MenuiconNormal) == -1)

Hicon = NULL;

Else IF (state & ods_selected)

{

IF (((Menudata *)) -> MenuiconSelected)! = -1)

Hicon = AFXGETAPP () -> Loadicon ((Menudata *) (LPDITA *) (LPDIS-> ItemData)) -> MenuiconSelected);

Else

Hicon = AFXGetApp () -> Loadicon ((Menudata *) (LPDITA *) (LPDIS-> ItemData)) -> MenuiconNormal);

}

Else

Hicon = AFXGetApp () -> Loadicon ((Menudata *) (LPDITA *) (LPDIS-> ItemData)) -> MenuiconNormal);

Trace1 ("DRAW for% S / N", Strtext);

}

Else

{

strText.empty ();

Hicon = NULL;

}

IF (state & ods_selected))

{

// Draw the down edges

CPEN * POLDPEN = PDC-> SelectObject (& M_Penback);

// you need only text highlight and That What you get

IF (M_HIGHTSTYLE! = NORMAL)

{

PDC-> FillRect (Rect, & M_BRBackground);

}

Else

{

PDC-> FillRect (Rect, & M_BRSELECT);

}

PDC-> SelectObject (Poldpen); PDC-> Draw3DRect (Rect, getsyscolor), getsyscolor (color_3dshadow);

Lf.lfweight = fw_bold;

IF ((hfont) DispFont! = null)

Dispfont.deleteObject ();

DispFont.createFontIndirect (& lf);

Crtext = m_clrhilighttext;

// While Selected Move The Text A Bit

TRACE0 ("SELECT, SELECTED / N");

}

Else

{

CPEN * POLDPEN = PDC-> SelectObject (& M_Penback);

PDC-> FillRect (Rect, & M_BRBackground);

PDC-> SELECTOBJECT (POLDPEN);

// DRAW THE UP EDGES

PDC-> DRAW3DRECT (Rect, M_ClrBack, M_clrback);

IF ((hfont) DispFont! = null)

Dispfont.deleteObject ();

DispFont.createFontIndirect (& lf); // Normal

TRACE0 ("SELECT, NORMAL / N");

}

// Draw the text if there is any

// we have to pieint the text only if the image is nonextantant

IF (Hicon! = NULL)

{

IF (Drawiconex (pdc-> getsafehdc (), rest.left, rect.top, hicon,

(m_iconx)? m_iconx: 32, (m_icony)? m_ICONY: 32, 0, NULL, DI_NORMAL))

Trace0 ("Wrote the icon servicefully / n");

Else

TRACE0 ("sorry.nogo / n");

}

// this is neededing Always So That We Can Have The Space for Check Marks

Rect.Left = Rect.Left ((m_iconx)? m_iconx: 32);

IF (! strText.isempty ())

{

// pfont-> getLogfont (& lf);

INT IOLDMODE = PDC-> getBKMode ();

PDC-> setbkmode (transparent);

PDC-> setTextColor (CRText);

Pfont = PDC-> SelectObject (& Dispfont);

Trace1 ("About to DrawText% S / N", Strtext);

PDC-> DRAWTEXT (Strtext, Rect, DT_LEFT | DT_SIINGLINE | DT_VCENTER);

Trace0 ("DONE / N");

PDC-> setBkmode (IOldMode);

PDC-> SELECTOBJECT (PFONT); // set it to the Old Font

}

Dispfont.deleteObject ();

}

Void CCustomu :: MeasureItem (LpMeasureItemstruct LPMIS)

{

CDC * PDC = AFXGETAPP () -> m_pmainwnd-> getdc ();

CFont * pfont = PDC-> SelectObject (& m_fontmenu);

INT ICONX = 0, ICONY = 0;

TextMetric TM;

PDC-> GetTextMetrics; & TM

PDC-> SelectObject (PFont);

AFXGetApp () -> m_pmainwnd-> releasedc (PDC);

IF (m_iconx)

Iconx = m_iconx;

IF (m_icony)

Icony = m_ICONY;

LPMIS-> ITEMWIDTH = ICONX TM.TMAVECHARWIDTH * LSTRLEN ((Menudata *) (LPMIS-> ItemData) -> Menutext) 10;

LPMIS-> ITEMHEIGHT = (iCONY> (M_IMenuHeight 1))? Icony: M_IMenuHeight 1;

}

Void CCustomMenu :: seticonsize (int width, int hotht)

{

m_iconx = width;

m_icony = height;

}

Void CcustomMenu :: SetTextColor (ColorRef CLRText)

{

m_CRText = CLRTEXT;

}

Void CCustomu :: setBackColor (ColorRef CLRBACK)

{

m_clrback = clrback;

IF ((Hbrush) M_BRBACKGROUND! = NULL)

m_brbackground.deleteObject ();

M_brbackground.createsolidbrush (CLRBACK);

}

Void CCustomMenu :: SthighlightColor (ColorRef Clrhilight)

{

m_clrhilight = clrhilight;

IF ((hbrush) m_brselect! = null)

M_BRSELECT.DELETEOBJECT ();

M_BrSelect.createsolidbrush (Clrhilight);

}

Void CCustomMenu :: SthighlightTextColor (ColorRef ClrhilightText)

{

m_clrhilighttext = clrhilighttext;

}

Void CcustomMenu :: SthighlightStyle (HighlightStyle HilightStyle)

{

M_HIGHTSTYLE = HilightStyle;

}

// ******************************************************** *******************************

Excerpted from Yu Haiyuan http://www.mfc2000.yeah.net

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

New Post(0)