码迷,mamicode.com
首页 > 编程语言 > 详细

Hello MFC 小记

时间:2015-05-19 12:32:50      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

在Hello窗口示例中添加:1.当鼠标左键按下时,由窗口左上方向右下方画一条线;  2.当鼠标左键松开时,由窗口左下方向右上方画一条线。

 

// Hello.h
1
class CMyApp : public CWinApp 2 { 3 public: 4 virtual BOOL InitInstance (); 5 }; 6 7 class CMainWindow : public CFrameWnd 8 { 9 public: 10 CMainWindow (); 11   afx_msg void OnLButtonDown(UINT nflags, CPoint point); 12 13 protected: 14 afx_msg void OnPaint (); 15   afx_msg void OnLButtonUp(UINT nflags, CPoint point); 16 DECLARE_MESSAGE_MAP () 17 };
//Hello.cpp
1
#include <afxwin.h> 2 #include "Hello.h" 3 4 CMyApp myApp; 5 6 ///////////////////////////////////////////////////////////////////////// 7 // CMyApp member functions 8 9 BOOL CMyApp::InitInstance () 10 { 11 m_pMainWnd = new CMainWindow; 12 // m_pMainWnd->ShowWindow (m_nCmdShow); 13 m_pMainWnd->ShowWindow(SW_SHOWNORMAL); 14 m_pMainWnd->UpdateWindow (); 15 return TRUE; 16 } 17 18 ///////////////////////////////////////////////////////////////////////// 19 // CMainWindow message map and member functions 20 21 BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd) 22 ON_WM_PAINT () 23 ON_WM_LBUTTONDOWN() 24 ON_WM_LBUTTONUP() 25 END_MESSAGE_MAP () 26 27 CMainWindow::CMainWindow () 28 { 29 Create (NULL, _T ("The Hello Application"),WS_OVERLAPPEDWINDOW,CRect(0,0,720,360)); 30 } 31 32 void CMainWindow::OnPaint () 33 { 34 CRect rect(100,100,500,500); 35 GetClientRect (&rect); 36 37 CPaintDC dc (this); 38 39 dc.DrawText (_T ("Hello, MFC"), -1, &rect, 40 DT_SINGLELINE | DT_CENTER | DT_VCENTER); 41 } 42 43 void CMainWindow::OnLButtonDown(UINT nflags, CPoint point) 44 { 45 CRect rect; 46 GetClientRect(&rect); 47 48 CClientDC dc(this); 49 dc.MoveTo(rect.left, rect.top); 50 dc.LineTo(rect.right, rect.bottom); 51 52 /*dc.MoveTo(rect.left, rect.bottom); 53 dc.LineTo(rect.right, rect.top);*/ 54 } 55 56 void CMainWindow::OnLButtonUp(UINT nflags, CPoint point) 57 { 58 CRect rect; 59 GetClientRect(&rect); 60 61 CClientDC dc(this); 62 dc.MoveTo(rect.left, rect.bottom); 63 dc.LineTo(rect.right, rect.top); 64 }

 

原来只能显示出Hello MFC窗口,扩展程序之后,新添加的两条对角线却无论如何显示不出来,后来想起需要消息映射,好像是把消息映射到对应的消息处理函数,虽然原理不是很明白,但添加上去就可以显示出效果了。

Hello MFC 小记

标签:

原文地址:http://www.cnblogs.com/starnix/p/4514048.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!