⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selfpaintmenudlg.cpp

📁 提供一种自画式菜单设计方法及实现
💻 CPP
字号:
// SelfPaintMenuDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SelfPaintMenu.h"
#include "SelfPaintMenuDlg.h"
#include "EZMenu.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSelfPaintMenuDlg dialog

CSelfPaintMenuDlg::CSelfPaintMenuDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSelfPaintMenuDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSelfPaintMenuDlg)
		// NOTE: the ClassWizard will add member initialization here
	m_bLargeIcons = FALSE;
	m_bProgressbar = FALSE;
	m_nProgress = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bLargeIcons=m_bProgressbar=FALSE;
	m_nProgress=80;
}

void CSelfPaintMenuDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSelfPaintMenuDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	DDX_Check(pDX, IDC_CHECK_BIGICON, m_bLargeIcons);
	DDX_Check(pDX, IDC_CHECK_PROGRESSBAR, m_bProgressbar);
	DDX_Text(pDX, IDC_EDIT_PROGRESSLCD, m_nProgress);
	DDV_MinMaxInt(pDX, m_nProgress, 0, 100);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSelfPaintMenuDlg, CDialog)
	//{{AFX_MSG_MAP(CSelfPaintMenuDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_SHOWMENU, OnButtonShowmenu)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSelfPaintMenuDlg message handlers

BOOL CSelfPaintMenuDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSelfPaintMenuDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSelfPaintMenuDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSelfPaintMenuDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSelfPaintMenuDlg::OnButtonShowmenu() 
{
	UpdateData(TRUE);	
	CEZMenu ezmenu;
	CMenu menu;
	menu.LoadMenu(IDR_MENU1);
	ezmenu.Attach(menu.GetSubMenu(0)->GetSafeHmenu());
	CBitmap bmp;
	bmp.LoadBitmap(IDB_MENU_TITLE/*The title bitmap*/);
	ezmenu.SetTotalItems(6);
	ezmenu.SetMenuBitmap((HBITMAP)bmp.Detach());
	ezmenu.SetBitmapDimension(CSize(40,180));
	ezmenu.SetLargeIcon(m_bLargeIcons);
	ezmenu.SetExtensionColor(RGB(255,255,50));
	CSize szIcon=m_bLargeIcons?CSize(32,32):CSize(16,16);
	HICON hItemIconLarge=(HICON)::LoadImage(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON2),IMAGE_ICON,szIcon.cx,szIcon.cy,LR_DEFAULTCOLOR);
	HICON hItemIconSmall=(HICON)::LoadImage(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON3),IMAGE_ICON,szIcon.cx,szIcon.cy,LR_DEFAULTCOLOR);;
	for(int i=0;i<5;++i)
	{
		LPEZMENUITEMDATA lpez=new CEZMenuItemData;
		if(i==0 && m_bProgressbar)
		{
			lpez->bProgress=TRUE;
			lpez->nProgress=(100-m_nProgress);
			lpez->bChecked=TRUE;
			lpez->strItemText="进度条菜单";				
			
		}
		else
		{
			lpez->hItemIconLarge=hItemIconLarge;
			lpez->hItemIconSmall=hItemIconSmall;
			lpez->strItemText.Format("%s%d","菜单项——",i);
		}
		ezmenu.InsertOwnerDrawnMenuItem(ID_EZMENU_ITEMS+i,lpez);
	}
	ezmenu.RemoveMenu(ID_EZMENUITEMTEMP,MF_BYCOMMAND);
	LPEZMENUITEMDATA lpez=new CEZMenuItemData;
	lpez->bSeperator=TRUE;
	ezmenu.InsertOwnerDrawnMenuItem(ID_EZMENU_ITEMS+6,lpez,1);
	CPoint mouse;
	::GetCursorPos(&mouse);
	ezmenu.TrackPopupMenu(TPM_RIGHTALIGN,mouse.x,mouse.y,this);	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -