Beautifully practical menu

xiaoxiao2021-04-10  571

Talking about the dynamic change menu in VC

Bull77 Yesky

Most of the Windows applications use the drop-down menu to implement your own specific functions, which makes the programming, and does not need to add multiple buttons to the program to complete these operations. In most cases, after the compilation of our program, the menu is determined and cannot be modified. However, in many cases, the program is necessary to generate different menus based on the user's own settings to accommodate different users, which requires our dynamic changing menu. Next, we analyze how different menus are generated.

first step:

Run AppWizard Generate an engineering MyMenu to accept all the default settings, except for the following: Single Document in Step 1, click the Finish button, and we have generated a project. Compilation, we can discover the menu generated by the program default, then we have to modify this menu.

Step 2:

Add a menu resource, follow these steps: INSERTàResouceàMenuànew in the menu, we can see the menu resource added to add one IDR_Menu1, as for the various items in the menu, you will add it, for us, please add Several, the first column contains several submenus.

third step:

Add a dialog resource, follow these steps: InsertàresouceàDialogànew in the menu, we can see the dialog resource added to IDD_DIALOG1, add six buttons, as shown below:

The ID number is in turn to id_clear, id_goone, id_gotwo, id_add, id_additem, id_edit, define class name cchangeMenu for dialog box, double-click the added six buttons to increase the processing function, with the default function name.

the fourth step:

Add one in the IDR_MAINFRAME menu, the ID number is id_set, named "Popup Settings Dialog", add processing functions to it, add the following code:

CChangeMenu DLG;

Dlg.domodal ();

Of course, don't forget to add a #include "ChangeMenu. H" at the beginning of the file.

the fifth step:

Find the handset of the added six buttons, add the following code:

Void cchangemenu :: OnClear ()

{

AFXGETMAINWND () -> setMenu (NULL);

AFXGETMAINWND () -> DRAWMENUBAR ();

}

Void cchangemenu :: ONGOONE ()

{

IF (! menu1) {

Menu1.LoadMenu (idR_Menu1);

AFXGETMAINWND () -> SETMENU (& Menu1);

AFXGETMAINWND () -> DRAWMENUBAR ();

Menu1.detach ();

}

}

Void cchangemenu :: ONGOTWO ()

{

IF (! menu2) {

Menu2.LoadMenu (IDR_MAINFRAME);

AFXGETMAINWND () -> SETMENU (& Menu2);

AFXGETMAINWND () -> DRAWMENUBAR ();

Menu2.detach ();

}

}

void cchangemenu :: onadd ()

{

STATIC INT X = 400;

AFXGETMAINWND () -> getMenu () -> appendmenu (mf_string, x, "mynew");

AFXGETMAINWND () -> DRAWMENUBAR (); x ;

}

void cchangemenu :: onadditem ()

{

Static int y = 500;

AFXGETMAINWND () -> getMenu () -> getSubmenu (0) -> InsertMenu (1, MF_BYPOSITION, Y, "MynewItem");

AFXGETMAINWND () -> DRAWMENUBAR ();

y ;

}

Void cchangemenu :: OneIt ()

{

AFXGETMAINWND () -> getMenu () -> ModifyMenu (0, MF_BYPOSITION, 402, "DFD");

AFXGETMAINWND () -> DRAWMENUBAR ();

}

Ok, all our functions here are all realized, so I'm running! how about it? Still satisfied?

How to add bitmaps or icons in the menu with VC 5

Tianya windbell

When we use Windows 95, we can notice that there is an icon before the menu item in the Start group, and there is an icon before the menu item in Word 97. These icons not only let us clearly understand the links between the various tools buttons on the screen, but also add to the aesthetics of the application interface. So, how do I use Visual C 5.0 to add icons in the application menu?

The MFC CMenu class has a member function setMenuItemBitmaps that can be used to add icons to the menu. The specific method is as follows:

Add a bitmap you want to add in your application's resource file and name it IDB_Open1 and IDB_Open2; Add the following code in the constructor of the view class:

Bm_open1.loadbitmap (idb_open1); bm_open2.loadbitmap (idb_open2); (function loadbitmap) Object loadBitmap is used to load bitmaps to CBITMAP classes) Add the following code in the member function ondraw () of the view class:

CWND * PARENT = getParent (); cmenu * pmenubar = parent - $ # @ 62; getMenu ();

CMenu * pmenu = pmenubar - $ # @ 62; getSubmenu (2);

PMENU - $ # @ 62; setmenuitembitmaps (1, mf_byposition, & bm_open1, & bm_open2);

The first three lines get a pointer to the menu, the fourth line call function setMenuItemBitmaps add icons to the menu, and the meaning of the specific parameters can see the book about introducing the MFC and its member functions.

Self-painted menu

Wenyi

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 shop be able to us

// 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 folly setxxxcolor sets the menu colors. // if you dont want to change, 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)

{

IF (LpMeasureItemstruct-> CTLTYPE == ODT_MENU &&

IsMenu (HMENU) LpMeasureItemstruct-> i id) &&

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

{

m_customMenu.MeasureItem (lpMeasureItemstruct);

}

Else

// Let MFCS Self-Drawing Handle IT

CView :: OnMeasureItem (NidctL, LpMeasureItemstruct);

}

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 Handlersvoid 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 (Color_3DHIGHT), 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;

}

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

Add submenu in the system menu

CPCW

The System menu is similar to other menus, you can add or remove items, which requires the CMenu class member function. The following code adds a new menu item after your system menu:

Cmenu * sysmenu;

Sysmenu = m_pmainwnd-> getSystemMenu (false);

Sysmenu-> appendmenu (mf_string, 1000, "xxx");

See the CMenu class in the MFC Help file

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

New Post(0)