📄 mainfrm.cpp
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "MFCDemo.h"
#include "MainFrm.h"
#include "LoginDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(ID_MOVE1, OnMove1)
ON_COMMAND(ID_MOVE2, OnMove2)
ON_COMMAND(ID_COLOR_RED, OnColorRed)
ON_COMMAND(ID_COLOR_BLUE, OnColorBlue)
ON_COMMAND(ID_COLOR_BLACK, OnColorBlack)
ON_COMMAND(ID_LINE_SINGLE, OnLineSingle)
ON_COMMAND(ID_LINE_DOUBLE, OnLineDouble)
ON_COMMAND(ID_LINE_TRIPLE, OnLineTriple)
ON_COMMAND(ID_TOOLS_LINE, OnToolsLine)
ON_COMMAND(ID_TOOLS_RECTANGLE, OnToolsRectangle)
ON_COMMAND(ID_TOOLS_CIRCLE, OnToolsCircle)
ON_WM_INITMENU()
ON_UPDATE_COMMAND_UI(ID_COLOR_RED, OnUpdateColorRed)
ON_UPDATE_COMMAND_UI(ID_COLOR_BLUE, OnUpdateColorBlue)
ON_UPDATE_COMMAND_UI(ID_COLOR_BLACK, OnUpdateColorBlack)
ON_UPDATE_COMMAND_UI(ID_LINE_SINGLE, OnUpdateLineSingle)
ON_UPDATE_COMMAND_UI(ID_LINE_DOUBLE, OnUpdateLineDouble)
ON_UPDATE_COMMAND_UI(ID_LINE_TRIPLE, OnUpdateLineTriple)
ON_UPDATE_COMMAND_UI(ID_TOOLS_LINE, OnUpdateToolsLine)
ON_UPDATE_COMMAND_UI(ID_TOOLS_RECTANGLE, OnUpdateToolsRectangle)
ON_UPDATE_COMMAND_UI(ID_TOOLS_CIRCLE, OnUpdateToolsCircle)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_MFCDemo,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
// 实初始化主窗口
m_CurrentColor = RGB(0, 0, 0);
m_CurrentThickness = 1;
// 装载位图
bLineBmp.LoadBitmap(IDB_BITMAP1);
bRectBmp.LoadBitmap(IDB_BITMAP2);
bCircleBmp.LoadBitmap(IDB_BITMAP3);
// 工具条对应的变量的初始化
m_CurrentTool = ID_TOOLS_LINE;
m_IndexColorCmd = ID_COLOR_BLACK;
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
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
}
// 主窗口用于创建工具条
if(!m_ToolBar.Create(this) || !m_ToolBar.LoadToolBar(IDR_MAINFRAME))
return -1;
// 工具条属性
m_ToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_ToolBar);
m_ToolBar.SetBarStyle(m_ToolBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY);
// 对话框
CLoginDialog dlg;
// 模态对话框
if(dlg.DoModal() == IDCANCEL)
return -1;
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::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
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::OnMove1()
{
// TODO: Add your command handler code here
MessageBox("悟空你也太调皮了, 我跟你说过叫你不要乱扔东西, 你怎么又...", "月光宝盒");
}
void CMainFrame::OnMove2()
{
// TODO: Add your command handler code here
MessageBox("曾经有一份真挚的爱情摆在我的面前, 但是我没有珍惜...", "仙履奇缘");
}
void CMainFrame::OnColorRed()
{
// TODO: Add your command handler code here
m_CurrentColor = RGB(255, 0, 0);
m_IndexColorCmd = ID_COLOR_RED; // 工具条对应的变量
}
void CMainFrame::OnColorBlue()
{
// TODO: Add your command handler code here
m_CurrentColor = RGB(0, 0, 255);
m_IndexColorCmd = ID_COLOR_BLUE; // 工具条对应的变量
}
void CMainFrame::OnColorBlack()
{
// TODO: Add your command handler code here
m_CurrentColor = RGB(0, 0, 0);
m_IndexColorCmd = ID_COLOR_BLACK; // 工具条对应的变量
}
void CMainFrame::OnLineSingle()
{
// TODO: Add your command handler code here
m_CurrentThickness = 1;
}
void CMainFrame::OnLineDouble()
{
// TODO: Add your command handler code here
m_CurrentThickness = 2;
}
void CMainFrame::OnLineTriple()
{
// TODO: Add your command handler code here
m_CurrentThickness = 3;
}
void CMainFrame::OnToolsLine()
{
// TODO: Add your command handler code here
CPen newPen;
CPen *oldPen;
newPen.CreatePen(PS_SOLID, m_CurrentThickness, m_CurrentColor);
CClientDC dc(this);
CRect rect;
GetClientRect(&rect);
dc.FillRect(&rect, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
oldPen = dc.SelectObject(&newPen);
dc.MoveTo(50, 30);
dc.LineTo(300, 200);
dc.SelectObject(&oldPen);
}
void CMainFrame::OnToolsRectangle()
{
// TODO: Add your command handler code here
CPen newPen;
CPen *oldPen;
newPen.CreatePen(PS_SOLID, m_CurrentThickness, m_CurrentColor);
CClientDC dc(this);
CRect rect;
GetClientRect(&rect);
dc.FillRect(&rect, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
oldPen = dc.SelectObject(&newPen);
dc.Rectangle(50, 30, 300, 200);
dc.SelectObject(&oldPen);
}
void CMainFrame::OnToolsCircle()
{
// TODO: Add your command handler code here
CPen newPen;
CPen *oldPen;
newPen.CreatePen(PS_SOLID, m_CurrentThickness, m_CurrentColor);
CClientDC dc(this);
CRect rect;
GetClientRect(&rect);
dc.FillRect(&rect, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
oldPen = dc.SelectObject(&newPen);
dc.Ellipse(50, 30, 300, 200);
dc.SelectObject(&oldPen);
}
void CMainFrame::OnInitMenu(CMenu* pMenu)
{
CFrameWnd::OnInitMenu(pMenu);
// TODO: Add your message handler code here
pMenu->ModifyMenu(ID_TOOLS_LINE, MF_BITMAP, ID_TOOLS_LINE, &bLineBmp);
pMenu->ModifyMenu(ID_TOOLS_RECTANGLE, MF_BITMAP, ID_TOOLS_RECTANGLE, &bRectBmp);
pMenu->ModifyMenu(ID_TOOLS_CIRCLE, MF_BITMAP, ID_TOOLS_CIRCLE, &bCircleBmp);
}
void CMainFrame::OnUpdateColorRed(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_IndexColorCmd == ID_COLOR_RED ? 1 : 0);
}
void CMainFrame::OnUpdateColorBlue(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_IndexColorCmd == ID_COLOR_BLUE ? 1 : 0);
}
void CMainFrame::OnUpdateColorBlack(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_IndexColorCmd == ID_COLOR_BLACK ? 1 : 0);
}
void CMainFrame::OnUpdateLineSingle(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_CurrentThickness == 1 ? 1 : 0);
}
void CMainFrame::OnUpdateLineDouble(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_CurrentThickness == 2 ? 1 : 0);
}
void CMainFrame::OnUpdateLineTriple(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_CurrentThickness == 3 ? 1 : 0);
}
void CMainFrame::OnUpdateToolsLine(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_CurrentTool == ID_TOOLS_LINE ? 1 : 0);
}
void CMainFrame::OnUpdateToolsRectangle(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_CurrentTool == ID_TOOLS_RECTANGLE ? 1 : 0);
}
void CMainFrame::OnUpdateToolsCircle(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_CurrentTool == ID_TOOLS_CIRCLE ? 1 : 0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -