1)当前桌面样子:
2)添加小按键过程:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
·~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
修改一下 按键的名字:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
看Resource.h的内容:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
现在就是这个样子:
3)添加事件处理函数:
那么这么多的事件 我怎么知道 我选的是哪一个 ,下面 我们用一个按键 来记录 我们使用的是哪一个按键(比如 1是画笔 2是画线 3是画矩形 4是画形 5是画三角)
因为在视图中画 可以将这个变量放到VIEW中 或者 Frame 都行 最好是VIew中
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
VIEW..cpp代码展示:
1 // draw_01View.cpp : Cdraw_01View 类的实现 2 // 3 4 #include "stdafx.h" 5 #include"MainFrm.h" 6 // SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的 7 // ATL 项目中进行定义,并允许与该项目共享文档代码。 8 #ifndef SHARED_HANDLERS 9 #include "draw_01.h" 10 #endif 11 12 #include "draw_01Doc.h" 13 #include "draw_01View.h" 14 15 #ifdef _DEBUG 16 #define new DEBUG_NEW 17 #endif 18 19 enum{BI,XIAN,JUXING,YUAN,SANJIAO}; 20 21 // Cdraw_01View 22 23 IMPLEMENT_DYNCREATE(Cdraw_01View, CView) 24 25 BEGIN_MESSAGE_MAP(Cdraw_01View, CView) 26 // 标准打印命令 27 ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint) 28 ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint) 29 ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview) 30 ON_WM_KEYDOWN() 31 ON_COMMAND_RANGE(ID_HUABI,ID_SANJIAOXING,Cdraw_01View::OnChange) 32 ON_WM_LBUTTONDOWN() 33 ON_WM_LBUTTONUP() 34 ON_WM_MOUSEMOVE() 35 END_MESSAGE_MAP() 36 37 // Cdraw_01View 构造/析构 38 //指的是 事件一改变 更新那个do_type这个变量 39 void Cdraw_01View::OnChange(UINT uint) 40 { 41 do_type=uint-ID_HUABI; 42 } 43 Cdraw_01View::Cdraw_01View() 44 { 45 // TODO: 在此处添加构造代码 46 Is_draw=false; 47 do_type=BI; 48 49 } 50 51 Cdraw_01View::~Cdraw_01View() 52 { 53 } 54 55 BOOL Cdraw_01View::PreCreateWindow(CREATESTRUCT& cs) 56 { 57 // TODO: 在此处通过修改 58 // CREATESTRUCT cs 来修改窗口类或样式 59 60 return CView::PreCreateWindow(cs); 61 } 62 63 // Cdraw_01View 绘制 64 65 void Cdraw_01View::OnDraw(CDC* /*pDC*/) 66 { 67 Cdraw_01Doc* pDoc = GetDocument(); 68 ASSERT_VALID(pDoc); 69 if (!pDoc) 70 return; 71 72 // TODO: 在此处为本机数据添加绘制代码 73 //这里就是将栈顶的图贴到这个VIEW窗口上 74 75 CMainFrame * cmain=(CMainFrame*)AfxGetMainWnd(); 76 CClientDC dc(this); 77 CDC cdc; 78 cdc.CreateCompatibleDC(&dc); 79 cdc.SelectObject(cmain->sk.top()); 80 dc.BitBlt(0,0,cmain->Screen_width,cmain->Screen_height,&cdc,0,0,SRCCOPY); 81 82 83 84 85 } 86 87 88 // Cdraw_01View 打印 89 90 BOOL Cdraw_01View::OnPreparePrinting(CPrintInfo* pInfo) 91 { 92 // 默认准备 93 return DoPreparePrinting(pInfo); 94 } 95 96 void Cdraw_01View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 97 { 98 // TODO: 添加额外的打印前进行的初始化过程 99 } 100 101 void Cdraw_01View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 102 { 103 // TODO: 添加打印后进行的清理过程 104 } 105 106 107 // Cdraw_01View 诊断 108 109 #ifdef _DEBUG 110 void Cdraw_01View::AssertValid() const 111 { 112 CView::AssertValid(); 113 } 114 115 void Cdraw_01View::Dump(CDumpContext& dc) const 116 { 117 CView::Dump(dc); 118 } 119 120 Cdraw_01Doc* Cdraw_01View::GetDocument() const // 非调试版本是内联的 121 { 122 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(Cdraw_01Doc))); 123 return (Cdraw_01Doc*)m_pDocument; 124 } 125 #endif //_DEBUG 126 127 128 // Cdraw_01View 消息处理程序 129 130 131 void Cdraw_01View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 132 { 133 // TODO: 在此添加消息处理程序代码和/或调用默认值 134 //判断是不是esc按键 135 if(nChar==VK_ESCAPE) 136 { 137 CMainFrame * cMain=(CMainFrame*)AfxGetMainWnd(); 138 cMain->PostMessage(WM_CLOSE); 139 } 140 CView::OnKeyDown(nChar, nRepCnt, nFlags); 141 } 142 143 144 void Cdraw_01View::OnLButtonDown(UINT nFlags, CPoint point) 145 { 146 // TODO: 在此添加消息处理程序代码和/或调用默认值 147 //鼠标按下 得记录坐标,叫画的标志是true 148 Is_draw=true; 149 this->ppoint=point; 150 151 CView::OnLButtonDown(nFlags, point); 152 } 153 154 155 void Cdraw_01View::OnLButtonUp(UINT nFlags, CPoint point) 156 { 157 // TODO: 在此添加消息处理程序代码和/或调用默认值 158 //结束画 159 Is_draw=false; 160 161 CView::OnLButtonUp(nFlags, point); 162 } 163 164 165 void Cdraw_01View::OnMouseMove(UINT nFlags, CPoint point) 166 { 167 // TODO: 在此添加消息处理程序代码和/或调用默认值 168 CClientDC cClient(this);//获取当前VIEW的DC 169 170 //首先判断是否画图 171 172 if(Is_draw==true) 173 { 174 //画什么图形 175 if(do_type==BI) 176 { 177 //画曲线 178 cClient.MoveTo(this->ppoint); 179 cClient.LineTo(point); 180 ppoint=point; 181 } 182 else{ 183 switch(do_type) 184 { 185 case XIAN: 186 cClient.MoveTo(this->ppoint); 187 cClient.LineTo(point); 188 //画线 189 break; 190 case JUXING: 191 cClient.Rectangle(ppoint.x,ppoint.y,point.x,point.y); 192 193 //画矩形 194 break; 195 case YUAN: 196 cClient.Ellipse(ppoint.x,ppoint.y,point.x,point.y); 197 //画圆 198 break; 199 case SANJIAO: 200 { 201 POINT pp[] = { 202 {(ppoint.x+point.x)/2,ppoint.y}, 203 {point.x,point.y}, 204 {ppoint.x,point.y} 205 }; 206 cClient.Polygon(pp,3); 207 208 } 209 //画三角 210 break; 211 } 212 213 } 214 } 215 CView::OnMouseMove(nFlags, point); 216 }
VIEW.h代码展示:
1 // draw_01View.h : Cdraw_01View 类的接口 2 // 3 4 #pragma once 5 6 7 class Cdraw_01View : public CView 8 { 9 protected: // 仅从序列化创建 10 Cdraw_01View(); 11 DECLARE_DYNCREATE(Cdraw_01View) 12 13 // 特性 14 public: 15 Cdraw_01Doc* GetDocument() const; 16 17 // 操作 18 public: 19 int do_type;//加的选操作的类型的变量 20 CPoint ppoint;//记录鼠标按下的坐标 21 bool Is_draw;//是否要绘画的标志 22 // 重写 23 public: 24 virtual void OnDraw(CDC* pDC); // 重写以绘制该视图 25 virtual BOOL PreCreateWindow(CREATESTRUCT& cs); 26 protected: 27 virtual BOOL OnPreparePrinting(CPrintInfo* pInfo); 28 virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo); 29 virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo); 30 31 // 实现 32 public: 33 virtual ~Cdraw_01View(); 34 #ifdef _DEBUG 35 virtual void AssertValid() const; 36 virtual void Dump(CDumpContext& dc) const; 37 #endif 38 39 protected: 40 41 // 生成的消息映射函数 42 protected: 43 DECLARE_MESSAGE_MAP() 44 public: 45 afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); 46 afx_msg void Cdraw_01View::OnChange(UINT uint); 47 afx_msg void OnLButtonDown(UINT nFlags, CPoint point); 48 afx_msg void OnLButtonUp(UINT nFlags, CPoint point); 49 afx_msg void OnMouseMove(UINT nFlags, CPoint point); 50 }; 51 52 #ifndef _DEBUG // draw_01View.cpp 中的调试版本 53 inline Cdraw_01Doc* Cdraw_01View::GetDocument() const 54 { return reinterpret_cast<Cdraw_01Doc*>(m_pDocument); } 55 #endif
然后 结果展示:
4)去除 问题: