mainfrm.cpp

来自「深入浅出Visual C++入门进阶与应用实例 随书光盘 作者 何志丹」· C++ 代码 · 共 171 行

CPP
171
字号
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "Ex030203.h"

#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_INITMENU()
	ON_WM_MEASUREITEM()
	ON_WM_DRAWITEM()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnInitMenu(CMenu* pMenu) 
{
	CFrameWnd::OnInitMenu(pMenu);
	
	CMenu *pSubMenu;
	UINT nCount,nSubCount,nID;
	nCount=pMenu->GetMenuItemCount();//子菜单的列数
	for(UINT i=0;i<nCount;i++)             
	{
		pSubMenu =pMenu->GetSubMenu(i);
		nSubCount=pSubMenu->GetMenuItemCount();//此列菜单菜单项的个数
		for(UINT j=0;j<nSubCount;j++)
		{
			nID=pSubMenu->GetMenuItemID(j);			
			//将框架菜单所有菜单都添加MF_OWNERDRAW标志
			pSubMenu->ModifyMenu(j,MF_BYPOSITION|MF_OWNERDRAW,nID); 
		}	
	}
}

void CMainFrame::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{	
		//只处理菜单,其它的交给基类处理
	if(ODT_MENU != lpMeasureItemStruct->CtlType )
		CFrameWnd::OnMeasureItem(nIDCtl, lpMeasureItemStruct);

	if(ID_SEPARATOR != lpMeasureItemStruct->itemID)     //设定非分隔线的大小	
	{
		lpMeasureItemStruct->itemHeight = 18;    
		lpMeasureItemStruct->itemWidth = 138;	
	}	
	else//设定分隔线的大小	
	{		
		lpMeasureItemStruct->itemHeight = 1;     		
		lpMeasureItemStruct->itemWidth = 138;	
	}
}

void CMainFrame::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{	
		LPDRAWITEMSTRUCT& lpDraw=lpDrawItemStruct;  //起个别名,好用一点
	
	//只处理菜单,其它的交给基类处理
	if( ODT_MENU == lpDrawItemStruct->CtlType)							 
	{
		//取得菜单的标题
		char szMenuString[100] = {0} ;
		GetMenuString((HMENU)lpDraw->hwndItem,lpDraw->itemID,szMenuString,99,MF_BYCOMMAND);
		
		//得到菜单的设备指针,用来绘制菜单
		CDC* pDC=CDC::FromHandle(lpDraw->hDC);  
		
		CBrush brushBK(RGB(255,255,255)) ;
		pDC->FillRect(&lpDraw->rcItem,&brushBK);//填充背景,"白纸"
		pDC->SetBkMode(TRANSPARENT);//透明的,有文字和没文字的地方背景一样
		pDC->SetTextColor(RGB(0,0,0));//文字的颜色,"黑字"
		pDC->TextOut(lpDraw->rcItem.left+30,lpDraw->rcItem.top+5,szMenuString);  //输出菜单标题
	}
	else
		CFrameWnd::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

⌨️ 快捷键说明

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