In the Hello window example, add: 1. When the left mouse button is pressed, a line is drawn from the upper left of the window to the right; 2. When the mouse Zuo, draw a line from the lower left of the window to the right.
//Hello.h
1 classCMyApp: PublicCWinApp2 {3 Public:4 VirtualBOOL InitInstance ();5 };6 7 classCMainWindow: PublicCFrameWnd8 {9 Public:Ten CMainWindow (); Oneafx_msgvoidOnLButtonDown (UINT nflags, CPoint point); A - protected: -afx_msgvoidOnPaint (); theafx_msgvoidOnLButtonUp (UINT nflags, CPoint point); - Declare_message_map () -};
//hello.cpp
1#include <afxwin.h>2#include"Hello.h"3 4 CMyApp myApp;5 6 /////////////////////////////////////////////////////////////////////////7 //CMyApp member functions8 9 BOOL cmyapp::initinstance ()Ten { Onem_pMainWnd =NewCMainWindow; A //M_pmainwnd->showwindow (m_ncmdshow); -M_pmainwnd->ShowWindow (SW_SHOWNORMAL); -M_pmainwnd->UpdateWindow (); the returnTRUE; - } - - ///////////////////////////////////////////////////////////////////////// + //CMainWindow message map and member functions - + Begin_message_map (CMainWindow, CFrameWnd) A On_wm_paint () at On_wm_lbuttondown () - On_wm_lbuttonup () - End_message_map () - - Cmainwindow::cmainwindow () - { inCreate (NULL, _t ("The Hello application"), Ws_overlappedwindow,crect (0,0,720, the)); - } to + voidCmainwindow::onpaint () - { theCRect Rect ( -, -, -, -); *GetClientRect (&rect); $ Panax NotoginsengCPAINTDC DC ( This); - thedc. DrawText (_t ("Hello, MFC"), -1, &Rect, +Dt_singleline | Dt_center |dt_vcenter); A } the + voidCmainwindow::onlbuttondown (UINT nflags, CPoint point) - { $ CRect rect; $GetClientRect (&rect); - -CCLIENTDC DC ( This); the DC. MoveTo (Rect.left, rect.top); - DC. LineTo (Rect.right, rect.bottom);Wuyi the /*DC. MoveTo (Rect.left, rect.bottom); - DC. LineTo (Rect.right, rect.top);*/ Wu } - About voidCmainwindow::onlbuttonup (UINT nflags, CPoint point) $ { - CRect rect; -GetClientRect (&rect); - ACCLIENTDC DC ( This); + DC. MoveTo (Rect.left, rect.bottom); the DC. LineTo (Rect.right, rect.top); -}
Originally can only show the Hello MFC window, after the extension, the newly added two diagonal lines are not displayed anyway, and then think of the need for message mapping, as if the message map to the corresponding message processing function, although the principle is not very clear, but add up can show the effect.
Hello MFC Small Note