1.将菜单栏归零,工具栏放在窗口低部,加载自己新建的工具栏
CMainFrame::OnCreate()函数中 this->SetMenu(0);

2.将窗口初始化为最大化
APP类中:m_pMainWnd->ShowWindow(SW_MAXIMIZE);
3.去掉边框
CMainFrame::PreCreateWindow()函数中 cs.style = WS_POPUP;
4.在FRAME类中添加工具栏命令消息,关闭
通过ON_COMMAND(ToolBar下的图片ID,实现的函数) this->PostMessage(WM_CLOSE);
5.在VIEW类中添加系统消息keydown
void CDRAWView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(nChar == VK_ESCAPE)
{
AfxGetMainWnd()->PostMessage(WM_CLOSE);
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
6.将桌面背景贴到窗口上
在Frame.h中添加成员变量和成员函数
1class CMainFrame : public CFrameWnd 2{ 3 public: 4 int m_ScreenX; 5 int m_ScreenY; 6 stack<CBitmap*> stk;//保存图片,实现撤销操作 7}
Frame.cpp的构造函数和析构函数
1CMainFrame::CMainFrame() 2{ 3 m_ScreenX = GetSystemMetrics(SM_CXSCREEN);//屏幕长(调用函数为获取屏幕分辨率) 4 m_ScreenY = GetSystemMetrics(SM_CYSCREEN);//屏幕宽 5 CWindowDC dc(GetDesktopWindow());//获取桌面屏幕 6 CDC cdc; 7 cdc.CreateCompatibleDC(&dc);//兼容性DC相当于没有画纸的DC 8 CBitmap* bitmap = new CBitmap; 9 bitmap->CreateCompatibleBitmap(&dc,m_ScreenX,m_ScreenY);//创建一个兼容性位图,第一个参数 10 cdc.SelectObject(bitmap);//将画纸放在DC上(只有兼容性DC才可以放画纸) 11 cdc.BitBlt(0,0,m_ScreenX,m_ScreenY,&dc,0,0,SRCCOPY); 12 stk.push(bitmap); 13} 14 15CMainFrame::~CMainFrame() 16{ 17 if(stk.empty() == false) 18 { 19 delete(stk.top()); 20 stk.pop(); 21 } 22}
VIEW.cpp
1void CDRAWView::OnDraw(CDC* /*pDC*/) 2{ 3 CDRAWDoc* pDoc = GetDocument(); 4 ASSERT_VALID(pDoc); 5 if (!pDoc) 6 return; 7 8 CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();//父类指针强转为子类,才可使用子类特有变量 9 CClientDC dc(this); 10 CDC cdc; 11 cdc.CreateCompatibleDC(&dc); 12 cdc.SelectObject(pFrame->stk.top()); 13 dc.BitBlt(0,0,pFrame->m_ScreenX,pFrame->m_ScreenY,&cdc,0,0,SRCCOPY); 14 15}
7.添加工具栏
在资源视图中Toolbar下自行绘制,更改对应ID;
在resource.h中将宏改为连续数字
ON_COMMAND_RANGE(ID_PEN,ID_ELLIPSE,&CDRAWView::ONChangeDrawStyle)//连续宏可使用
声明枚举类型 enum{PEN,LINE,RECTANGLE,TRIANGLE,ELLIPSE};(铅笔,直线,长方形,三角形,椭圆形)与resource.h中的顺序对应
1在View.h中 2 3class CDRAWView : public CView 4{ 5 public: 6 int m_DrawStyle; 7 void ONChangeDrawStyle(UINT uid); 8 bool m_blsFlag; 9 CPoint PointMouseDown; 10};
在View.cpp中
1void CDRAWView::ONChangeDrawStyle(UINT uid) 2{ 3 m_DrawStyle = uid - ID_PEN; 4} 5CDRAWView::CDRAWView() 6{ 7 m_DrawStyle = ID_PEN; 8 m_blsFlag = false; 9} 10void CDRAWView::OnLButtonDown(UINT nFlags, CPoint point) 11{ 12 m_blsFlag = true; 13 PointMouseDown = point; 14 CView::OnLButtonDown(nFlags, point); 15} 16 17 18void CDRAWView::OnLButtonUp(UINT nFlags, CPoint point) 19{ 20 m_blsFlag = false; 21 22 CView::OnLButtonUp(nFlags, point); 23} 24 25 26void CDRAWView::OnMouseMove(UINT nFlags, CPoint point) 27{ 28 CClientDC dc(this); 29 CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd(); 30 if(m_blsFlag) 31 { 32 if(m_DrawStyle == PEN) 33 { 34 dc.MoveTo(PointMouseDown.x,PointMouseDown.y); 35 dc.LineTo(point.x,point.y); 36 PointMouseDown = point; 37 return ; 38 } 39 else 40 { 41 //======================================解决闪屏======================================== 42 CDC MemDC; 43 MemDC.CreateCompatibleDC(&dc); 44 CBitmap* bitmap = new CBitmap; 45 bitmap->CreateCompatibleBitmap(&dc,pFrame->m_ScreenX,pFrame->m_ScreenY); 46 MemDC.SelectObject(bitmap); 47 //-------------------------------------栈顶图片贴图擦除痕迹------------------------------------------------ 48 CDC cdc; 49 cdc.CreateCompatibleDC(&dc); 50 cdc.SelectObject(pFrame->stk.top()); 51 MemDC.BitBlt(0,0,pFrame->m_ScreenX,pFrame->m_ScreenY,&cdc,0,0,SRCCOPY); //------------------------------------------------------------------------------------------------------- 52 switch(m_DrawStyle) 53 { 54 case LINE: 55 MemDC.MoveTo(PointMouseDown.x,PointMouseDown.y); 56 MemDC.LineTo(point.x,point.y); 57 break; 58 case TRIANGLE: 59 { 60 POINT points[3] = {{(PointMouseDown.x+point.x)/2,PointMouseDown.y},{PointMouseDown.x,point.y},{point.x,point.y}}; 61 MemDC.Polygon(points,3); 62 } 63 break; 64 case ELLIPSE: 65 MemDC.Ellipse(PointMouseDown.x,PointMouseDown.y,point.x,point.y); 66 break; 67 case RECTANGLE: 68 MemDC.Rectangle(PointMouseDown.x,PointMouseDown.y,point.x,point.y); 69 break; 70 } 71 dc.BitBlt(0,0,pFrame->m_ScreenX,pFrame->m_ScreenY,&MemDC,0,0,SRCCOPY); 72 //=================================================================================================== 73 } 74 } 75 76 CView::OnMouseMove(nFlags, point); 77}