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

📄 demodlg.cpp

📁 这是MFC经典问答书的光盘内容
💻 CPP
字号:
// DemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ButtonMenu.h"

#include "DemoDlg.h"

#include "DrawDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDemoDlg dialog


CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDemoDlg)
	m_nShape = -1;
	m_nX = 0;
	m_nY = 0;
	//}}AFX_DATA_INIT
}


void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDemoDlg)
	DDX_Control(pDX, IDC_CHOOSE_COLOR, m_btnChooseColor);
	DDX_Control(pDX, IDC_COLOR, m_FillArea);
	DDX_CBIndex(pDX, IDC_SHAPE, m_nShape);
	DDX_Text(pDX, IDC_X, m_nX);
	DDX_Text(pDX, IDC_Y, m_nY);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CDemoDlg)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_CHOOSE_COLOR, OnChooseColor)
	ON_WM_INITMENUPOPUP()
	//}}AFX_MSG_MAP

	// Color handling
	ON_COMMAND_RANGE(ID_COLOR0, ID_COLOR15, OnColor)
	ON_UPDATE_COMMAND_UI_RANGE(ID_COLOR0, ID_COLOR15, OnUpdateColor)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDemoDlg message handlers

void CDemoDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// 1 - Get placeholder coordinates
	CRect rc;      // Placeholder rectangle
	m_FillArea.GetWindowRect( &rc );
	ScreenToClient( &rc );

	// 2 - Draw inside placeholder rectangle
	CBrush brFill( m_crColor );

	CBrush* pOldBrush = dc.SelectObject( &brFill );
	dc.Rectangle( &rc );
	dc.SelectObject( pOldBrush );
}

void CDemoDlg::OnChooseColor() 
{
	// 1 - Load top-level menu from resource
	CMenu mnuTop;
	mnuTop.LoadMenu( IDR_COLOR_POPUP );

	// 2 - Get popup menu from first sub-menu
	CMenu* pPopup = mnuTop.GetSubMenu( 0 );
	ASSERT_VALID( pPopup );

	// 3 - Display popup menu under button
	CRect rc;
	m_btnChooseColor.GetWindowRect( &rc );
	pPopup->TrackPopupMenu(	TPM_LEFTALIGN | TPM_LEFTBUTTON,
							rc.left, rc.bottom,
							this, NULL );
}

void CDemoDlg::OnColor( UINT nID )
{
	// 1 - Store new color
	m_crColor = CDrawDoc::GetColorRef( nID );

	// 2 - Repaint affected area
	CRect rc;      // Placeholder rectangle
	m_FillArea.GetWindowRect( &rc );
	ScreenToClient( &rc );
	InvalidateRect( &rc );
}

void EkUpdateMenuUI( CWnd* pOwner, CMenu* pMenu, BOOL bAutoMenuEnable = TRUE )
{
	// Checks the enabled/checked state of various menu items
	// (adapted from MFC's own CFrameWnd::OnInitMenuPopup() function --
	// WinFrm.cpp)

	ASSERT_VALID( pOwner );
	ASSERT( pMenu != NULL );

	// Create and initialize the famous CCmdUI object
	CCmdUI state;
	state.m_pMenu = pMenu;
	ASSERT(state.m_pOther == NULL);
	ASSERT(state.m_pParentMenu == NULL);

	// determine if menu is popup in top-level menu and set m_pOther to
	//  it if so (m_pParentMenu == NULL indicates that it is secondary popup)
	HMENU hParentMenu;
	if (AfxGetThreadState()->m_hTrackingMenu == pMenu->m_hMenu)
		state.m_pParentMenu = pMenu;    // parent == child for tracking popup
	else if ((hParentMenu = ::GetMenu(pOwner->m_hWnd)) != NULL)
	{
		CWnd* pParent = pOwner->GetTopLevelParent();
			// child windows don't have menus -- need to go to the top!
		if (pParent != NULL &&
			(hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
		{
			int nIndexMax = ::GetMenuItemCount(hParentMenu);
			for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
			{
				if (::GetSubMenu(hParentMenu, nIndex) == pMenu->m_hMenu)
				{
					// when popup is found, m_pParentMenu is containing menu
					state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
					break;
				}
			}
		}
	}

	state.m_nIndexMax = pMenu->GetMenuItemCount();

	// For each menu item...
	for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
	  state.m_nIndex++)
	{
		// Get menu item ID
		state.m_nID = pMenu->GetMenuItemID(state.m_nIndex);
		if (state.m_nID == 0)
			continue; // menu separator or invalid cmd - ignore it

		ASSERT(state.m_pOther == NULL);
		ASSERT(state.m_pMenu != NULL);
		if (state.m_nID == (UINT)-1)
		{
			// Maybe a popup menu, route to first item of
			// that popup
			state.m_pSubMenu = pMenu->GetSubMenu(state.m_nIndex);
			if (state.m_pSubMenu == NULL ||
				(state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
				state.m_nID == (UINT)-1)
			{
				continue;       // first item of popup can't be routed to
			}
			state.DoUpdate(pOwner, FALSE);    // popups are never auto disabled
		}
		else
		{
			// Normal menu item:
			// Auto enable/disable if 'bAutoMenuEnable' argument is set
			// and command is _not_ a system command
			state.m_pSubMenu = NULL;
			state.DoUpdate(pOwner, bAutoMenuEnable && state.m_nID < 0xF000);
		}

		// adjust for menu deletions and additions
		UINT nCount = pMenu->GetMenuItemCount();
		if (nCount < state.m_nIndexMax)
		{
			state.m_nIndex -= (state.m_nIndexMax - nCount);
			while (state.m_nIndex < nCount &&
				pMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
			{
				state.m_nIndex++;
			}
		}
		state.m_nIndexMax = nCount;
	}
}

void CDemoDlg::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) 
{
	CDialog::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
	
	// Delegate the real work to the EkUpdateMenuUI() function
	EkUpdateMenuUI( this, pPopupMenu );
}

void CDemoDlg::OnUpdateColor(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck( CDrawDoc::GetColorRef( pCmdUI->m_nID ) == m_crColor );
}

⌨️ 快捷键说明

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