📄 gongchengview.cpp
字号:
// gongchengView.cpp : implementation of the CGongchengView class
//
#include "stdafx.h"
#include "gongcheng.h"
#include "gongchengDoc.h"
#include "gongchengView.h"
#include "InputDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGongchengView
IMPLEMENT_DYNCREATE(CGongchengView, CView)
BEGIN_MESSAGE_MAP(CGongchengView, CView)
//{{AFX_MSG_MAP(CGongchengView)
ON_COMMAND(ID_OPER_SHOW, OnOperShow)
ON_UPDATE_COMMAND_UI(ID_OPER_SHOW, OnUpdateOperShow)
ON_COMMAND(ID_OPER_STRING, OnOperString)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
ON_COMMAND_RANGE(ID_OPER_RED,ID_OPER_BLUE,OnOperColorChange) //消息处理函数
ON_UPDATE_COMMAND_UI_RANGE(ID_OPER_RED,ID_OPER_BLUE,OnUpdateOperColorChange)
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_POP_SHOW, OnOperShow) //消息响应
ON_UPDATE_COMMAND_UI(ID_POP_SHOW, OnUpdateOperShow)
ON_COMMAND_RANGE(ID_POP_RED,ID_POP_BLUE,OnPopColorChange)
ON_UPDATE_COMMAND_UI_RANGE(ID_POP_RED,ID_POP_BLUE,OnUpdatePopColorChange)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGongchengView construction/destruction
CGongchengView::CGongchengView()
{
// TODO: add construction code here
m_nColors[0] = RGB(255,0,0);
m_nColors[1] = RGB(0,255,0);
m_nColors[2] = RGB(0,0,255);
m_nColorIndex = 0;
m_strShow = "Hello World!";
m_bShow = TRUE;
m_PopMenu.LoadMenu(IDR_MENU_POP); // 创建并加载菜单资源
}
CGongchengView::~CGongchengView()
{
m_PopMenu.DestroyMenu(); // 释放菜单资源
}
BOOL CGongchengView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CGongchengView drawing
void CGongchengView::OnDraw(CDC* pDC)
{
CGongchengDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if(m_bShow)
{
pDC->SetTextColor(m_nColors[m_nColorIndex]);
// 设置输出字符串颜色
pDC->TextOut(100,100,m_strShow); // 输出字符串
}
CDC dcMemory; // 创建内存缓冲DC
dcMemory.CreateCompatibleDC(pDC);
CBitmap bmp1; // 加载256位图
bmp1.LoadBitmap(IDB_BITMAP_256);
BITMAP bmpInfo1;
bmp1.GetBitmap(&bmpInfo1); // 获得位图的尺寸
CBitmap* pOldBitmap1 = dcMemory.SelectObject(&bmp1);
// 选择位图到内存缓冲设备中
pDC->BitBlt(250,0,bmpInfo1.bmWidth,bmpInfo1.bmHeight,&dcMemory,0,0,SRCCOPY); // 绘制到屏幕
CBitmap bmp2;
bmp2.LoadBitmap(IDB_BITMAP_24bit); // 加载24位位图
BITMAP bmpInfo2;
bmp2.GetBitmap(&bmpInfo2);
CBitmap* pOldBitmap2 = dcMemory.SelectObject(&bmp2);
pDC->BitBlt(570,0,bmpInfo2.bmWidth,bmpInfo2.bmHeight, &dcMemory,0,0,SRCCOPY);
dcMemory.SelectObject(pOldBitmap2); // 恢复设备中原来的位图
}
/////////////////////////////////////////////////////////////////////////////
// CGongchengView printing
BOOL CGongchengView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CGongchengView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CGongchengView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CGongchengView diagnostics
#ifdef _DEBUG
void CGongchengView::AssertValid() const
{
CView::AssertValid();
}
void CGongchengView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CGongchengDoc* CGongchengView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGongchengDoc)));
return (CGongchengDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CGongchengView message handlers
void CGongchengView::OnOperShow()
{
// TODO: Add your command handler code here
m_bShow = !m_bShow;
Invalidate(); // 强制程序重新窗口
}
void CGongchengView::OnUpdateOperShow(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bShow);
}
void CGongchengView::OnOperColorChange(WORD nID)
{
m_nColorIndex = nID-ID_OPER_RED;
Invalidate();
}
void CGongchengView::OnUpdateOperColorChange(CCmdUI * pCmdUI)
{
pCmdUI->SetRadio(m_nColorIndex==(pCmdUI->m_nID - ID_OPER_RED));
}
void CGongchengView::OnRButtonDown(UINT nFlags, CPoint point)
{
m_pPop=m_PopMenu.GetSubMenu(0); //获得第一个子菜单
UINT nCheck = m_bShow?MF_CHECKED:MF_UNCHECKED; // 更新【Show】的check状态
m_pPop->CheckMenuItem(ID_POP_SHOW,MF_BYCOMMAND|nCheck);
if(m_nColorIndex==0)
{
m_pPop->CheckMenuItem(ID_POP_RED,MF_CHECKED);
m_pPop->CheckMenuItem(ID_POP_BLUE,MF_UNCHECKED);
m_pPop->CheckMenuItem(ID_POP_GREEN,MF_UNCHECKED);
}
if(m_nColorIndex==1)
{
m_pPop->CheckMenuItem(ID_POP_RED,MF_UNCHECKED);
m_pPop->CheckMenuItem(ID_POP_GREEN,MF_CHECKED);
m_pPop->CheckMenuItem(ID_POP_BLUE,MF_UNCHECKED);
}
if(m_nColorIndex==2)
{
m_pPop->CheckMenuItem(ID_POP_RED,MF_UNCHECKED);
m_pPop->CheckMenuItem(ID_POP_GREEN,MF_UNCHECKED);
m_pPop->CheckMenuItem(ID_POP_BLUE,MF_CHECKED);
}
ClientToScreen(&point); //将坐标由客户坐标转化为屏幕坐标
m_pPop->TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);
// 显示Pop-up菜单
CView::OnRButtonDown(nFlags, point);
}
void CGongchengView ::OnPopColorChange(WORD nID)
{
m_nColorIndex = nID-ID_POP_RED;
Invalidate();
}
void CGongchengView ::OnUpdatePopColorChange(CCmdUI * pCmdUI)
{
pCmdUI->SetRadio(m_nColorIndex==(pCmdUI->m_nID - ID_POP_RED));
}
void CGongchengView::OnOperString()
{
// TODO: Add your command handler code here
CInputDlg dlgInput; // 声明对话框变量
if(dlgInput.DoModal() == IDOK) // 如果用户点击OK按钮
{
m_strShow = dlgInput.m_strInput; // 更改字符串
Invalidate(); // 强制重绘
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -