      

    Windows   ,   .  
     -no^eHHjn^Wiridows.   
     ,    
 MFC.      ,    
      . 
      . 

      ? 

     Windows    .     
  .    WINDOWS.H    
  .    : 
    	WM_CHAR	
	WM_PAINT                       
	WM_MOVE              
	WM_CLOSE 
	WM_LBUTTONUP    
	WM_LBUTTONDOWN    
	WM_COMMAND     
	WM_SIZE 
      .   ,  
 .   , ,   
   ,   ,     
,    . 
    Windows      .  
,  ,    ,   
  ,   ,   
.  ,        . 
, -         
  .     ,   
  .     MFC  
 . 
      Windows-   (    
 )       .   
    switch      
  .  ,   
 ,  .     
MFC     . 

       MFC 

    MFC     .   
  .  MFC    
- ,     .  
  -  ,       
,      .    
,   ,      . 
           : 
    1.            
 .	---. 
    2.     -      , 
    . 
    3.          -. 
     . 

    40 MFC:   


         

         ,     
     .    MFC 
    Windows,    
 ON_     .     
 :  WM_COMMAND   ON_COMMAND(). 
   ,  WM_COMMAND    ,  
    MFC. (      
4.)     MFC,   
 : 
    	ON_WM_CHAR()	
	ON_WM_PAINT()                 
	ON_WM_MOVE() 
    	ON_WM_CLOSE()	
	ON_WM_LBUTTONUP()    
	ON_WM_LBUTTONDOWN() 
    	ON_COMMAND()	
	ON_WM_SIZE() 
         ,     
  BEGIN_MESSAGE_MAP()  END_MESSAGE_MAP(). ,  
,      WM_CHAR,   
    : 

    BEGIN_MESSAGE_MAP(CMainWin,   CFrameWnd) 
    ON_W'M_CHAR () END_MESSAGE_MAP() 

    ,       . ,  
     WM_CHAR  WM_LBUT-TONDOWN. 

    BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd) 
    ON_WM_CHAR() 
    ON_WM_LBUTTONDOWN () END_MESSAGE_MAP() 

       ,  WM_CHAR    
  ,   WM_LBUTTONDOWN      . 

          

     ,   ,      
 .      CWnd   
   .  ,  -  
   ( WM_),    On. , 
  WM_CHAR~Ha3biBaeTCH OnChar(),  WM_LBUTTONDO\VN  OnLButtonDown(), 
 WM_PAINT - OnPaint(). 
           ,  
  .        
.     OnPaint()    CMainWin: 

    class CMainWin : public CFrameWnd 
    { 
    public: 


    3.  	41 


    CMainWin(); 
    afx_msg  void  OnPaintO; DECLARE_MESSAGE_MAP() }; 

     ,  OnPaint()    afx_msg.   
      -. ,  
OnPaint()    CMainWin,       
     WM_PAINT. 
             
,     .  ,   , 
       MFC.    
,  -   afx_msg.      
  ,     . 

       

            
  -    ,   
 ,      .  
    ,   , 
    ,    . 
      .      
    ,     ,   
,     . 

       BEGIN_MESSAGE_MAP() 

               ,  
    . , ,     
 .       , 
    BEGIN_MESSAGE_MAP().     : 

    BEGIN_MESSAGE_MAP(, ) 
    I/     END_MESSAGE_MAP() 

     ,    ,   
.      ,   
  -. ,   

    BEGINJMESSAGE_MAP(CMainWin, CFrameWnd) 
    ON__WM_CHAR () 
    ON_WM_LBUTTONDOWN() END_MESSAGE_MAP() 

    -  CMainWin,     
 CFrameWnd,     . 
    ,    ,    
  ,  ,     
 . 

    42	MFC:   


        

         Windows    
 .   WM_CHAR,      
ON_WM_CHAR().  ,       
  .        
    WM_CHAR.   , 
     2  ,     
  . 
      WM_CHAR  OnCharQ.   : 

    afxjnsg void OnChar(UINT Ch, UINT Count, UINT Flags); 

        ,    WM_CHAR. 
ASCII-      /.   
,      ,  
  Count.  Flags    (. 3.1): 

     3.1.   Flags 

    	 

    15	,   ; ,     
    14	,      ,   ; 
,      
    13	,     ALT; ,   ALT   
    12	 Windows 
    11	 Windows 
    10	     
    9	     
    8	,        
  ;     
    7  0	 ,    (.. -) 

           /,      
 . , ,     
Windows.       ,   . 
       WM_CHAR      
 ,       
,     ,     
  OnCharQ. , ,  OnCharQ,   
    : 

    char   str[80];   //               'faMrtQ.J[fa\^J!)	> =   
    //       WM_CHAR 
    afx_msg  void  CMainWin::OnChar(UINT   ch,   UINT   count,   UINT   flags) 


    3.  	43 


    { 
    CClientDC dc(this); 
    dc.TextOut (1, 1, "   ", 3); //    
    wsprintf (str, "%c", ch); 
    dc.TextOut (1, 1, str, strlen (str)); 
    I 

       OnCharQ  :     
 (   "").  ,  ,    
      .    
,  Windows       .  
  ,   DC (Device Context)*,  
     CClientDC,   CDC. 
 CClientDC        
.         .  
    .    , , 
  ,       . 
    ,    ,  TextOutO  
   CDC.     .   : 

    	virtual BOOL CDC::TextOut(int X, int Y, LPCSTR JpszStr, int Length); 
	BOOL  CDC::TextOut(int X, int Y, const CString SStrOb); 

        ,   IpszStr,  
     ,     X  . ( 
    .)     
Length.     ,    StrOb. 
CString         MFC.   
   .         
TextOut(). (     CString.)    
 TextOut()       , 
 . 
        OnChar() ,  , 
    wsprintfQ    ,    
 TextOut()        (1,1). 
(wsprintf()    Windows-   sprintf().) 
 sir  ,       
     . 
            (0,0).  
      ,    ,   
          
   . 
        TextOutQ      
.  Windows    ,      

    *     ,      ,    
  :  " "     
.  . . 


    44	MFC:   

     ,          
 . ,   "w"   "i",   
"w"     ,      . 
( ,      TextOut().) 
       ,   TextOutQ    
   .   ,   MFC (   Windows)  
,     .   ,  
    Windows    , 
,    ..        
   . 
         ,   
  MESSAGE!.H,    . 

    //  MESSAGE1 . 
    //               class  CMainWin   :   
public  CFrameWnd ( public: 
    CMainWin () ; 
    afxjnsg  void OnChar(UINT  ch,   UINT  count,   UINT   flags); 
    DECLARE_MESSAGE_MAP() ); 
    //             class   CApp   :   public   
CWinApp ( public: 
    BOOL  Initlnstance () ; ); 
      : 
    //  ,      WM_CHAR 
    finclude   <afxwin.h> 
    #include   <string.h> 
    #include   "messagel.h" 
    char  str[80];   //        
    CMainWin::CMainWin () { 
    Create(NULL,    "Processing  WM_CHAR Messages"); 
    ) 
    //      BOOL  CApp::Initlnstance() { 
    m_pMainWnd  =  new  CMainWin; 
    m_pMainWnd->ShowWindow(m_nCmdShow); 


    3.  	45 


    m_pMainWnd->UpdateWindow() ; 
    return TRUE; } 
    //     BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd) 
    ON_WM_CHAR() ENDJMESSAGEJMAP() 
    //   WM_CHAR 
    afx_msg void CMainWin: .-OnChar (UINT ch, UINT count, UINT flags) 
    { 
    CClientDC dc(this); 
    jdc.. TextOut (1, 1, "   ", 3); //    
    wsprintf(str, "%c", ch) ; dc.TextOut (1, 1, str, strlen(str)); } 

    CApp App; //     . 3.1  , 
  . 

     Processing WM_CHAR Messages 
     

    . 3.1.  ,      WMjCHAR 


    46 MFC:   


      

               
  .   ,    .  
 ,     ,   
 Windows,    ,  
    .    , 
        . 
              
,     .    ,   
   ,   .    
    TextOut()    ,   
  . 
     ,   Windows-   
    GetDC(),      
Re-IeaseDC().  Windows      
, ,         
.  ,  MFC  ,    . 
,      CClientDC   
 .     ,   
 ReleaseDC()     .   
     MFC. 
        CClientDC    
,     .   CClientDC : 

    CClientDC(CWnd  *Window); 

     Window    ,    
. ^_ .    this.  
     ,  CCIientDC() 
   CResourceException. 
    ,  ,        
,       CClientDC.  
 ^1,       
-.      WM_PAINT,  
  . 

      WM_PAINT 

      ,       
 .          
.  ,   ,      
. ,     ,    
,     .   :  
  Windows    .   , 
 ,   (  ,   
 WM_PAINT. (  ,    .)  
 ,     ,    
 .           WM_PAINT. 

    3.  	47 

    :         
    . , ,  ,  
      ,    . 
           WM_PAINT. 
  ,  Windows     
.  :         
 ,        .  
       ^,  
  ,       . 
      WM_PAINT  OnPaint().   : 

    afx_msg  void  OnPaintO; 

         ON_WM_PAINT(). 
       WM_PAINT     
    CMainWin,   
       . , 
         
 str, ..   . 

    //   WM_PAINT 
	afx_msg void CMainWin : :OnPaint () { 
    CPaintDC dc (this); 
    //     
	//     
	dc.TextOut (1, 1, str, strlen (str)); } 

        .    
,         
CPaintDC,   CClientDC.       
WM_PAINT        . 
  CPaintDC   : 

    CPaintDC(CWnd *Nindow) ; 

      Window    ,   
 .        this.  
     ,  CPaintDC() 
   CResourceException. ,     
     WM_PAINT. 
     CPaintDC    ,  m_ps,  
   PAINTSTRUCT.     CPaintDC,  
    ,     
 .  PAINTSTRUCT   : 

    48 MFC:   


    typedef   struct   tagPAINTSTRUCT   { 
    HOC  hdc;   //        
    BOOL  fErase;   //  TRUE,            
    RECT   rcPaint;   //       ,      // 
  
    BOOL  fRestore;   //    
    BOOL  fIncUpdate;   //    
    BYTE   rgbReserved[32];   //    }   PAINTSTRUCT; 

     hdc      .   
Windows-       . 
  MFC  ,     
 .      ,   fErase  
  . (     .)  
 rcPaint    ,    
.  RECT   ,   
        . 
  : 

    typedef tagRECT   ( 
    LONG   left,   top;   /*          */ 
    LONG   right,   bottom;   /*          */ }   RECT; 

      rcPaint   ,   
,     . 
       ,   \VM_PAINT.  
  MESSAGE.H,    . 

    // MESSAGE2.H 
    //       class CMainWin : public CFrameWnd 
( public: 
    CMainWin () ; 
    afx_msg void OnChar(UINT ch, UINT count, UINT flags); 
    afx_msg void OnPaintO; 
    DECLARE_MESSAGE_MAP() 
    }; 
    //      class CApp : public CWinApp { public: 
    BOOL Initlnstance (); ); 
      : 
    /* ,   WM_CHAR  WM_PAINT */ 
    3.  	49 

    tinclude	<afxwln.h> 
    #include	<string.h> 
    linclude	"message?.h" 
    char str[80] = "Sample Output"; //    
    CMainWin::CMainWin() { 
    Create(NULL, "Processing WM_PAINT Messages"); } 
    //   BOOL CApp::Initlnstance() { 
    m_pMainWnd = new CMainWin; 
    m_pMainWnd->ShowWindow(m_nCmdShow); 
    m_pMainWnd->UpdateWindow() ; 
    return TRUE; } 
    //     BEGINJMESSAGE_MAP(CMainWin, CFrameWnd) 
    ON_WM_CHAR() 
    ON_WM_PAINT() END_MESSAGE_MAP() 
    //   WM_CHAR 
    afx_msg void CMainWin::OnChar(UINT ch, UINT count, UINT flags) 
    { 
    CClientDC dc(this); 
     
    dc.TextOut (1, 1, "   ", 3); //    
    wsprintf(str, "%c", ch) ; dc.TextOut (1, 1, str, strlen(str)); } 
    //   WM_PAINT afx_msg void CMainWin::OnPaint() { 
    CPaintDC dc (this); 
    //            //       
   dc.TextOut(I,   I,   str,   strlen(str)); } 
    CApp App;   //         

       , ,     
.    ,      
  .   ,      
    .  ,   

    50 MFC:   

     str   "Sample Output",  
     .   ,   
    . \VM_PAINT. 
         WM_PAINT   
 ,  ,      
   ,       
 . 
            
     ,      
  .     . 
-,      .   
 ,      . 
-,      , 
  ,   ,     
.  ,        
,      ,  ^  
 .    . 
    (    .)    ,  
 .        , 
         ,  
     .    , 
  ,    Windows,  
    . 

       

     Windows   ,    
    ,    Windows  
     .    
,  Windows     .    
     , WMJLBUTTONDO 
    WN  WM_RBUTTON-DOWN,        , 
.      OnLButtonDown() 
 OnRButton-Down().   : 
    afx_msg  void  OnLButtonDown(UINT   Flags,   CPoint   Loc); afxjnsg  void  
OnRButtonDown(UINT   Flags,   CPoint   Loc); 
      Flags   ,     
   [CTRL], [SHIFT]    .  
      : 

    MK_CONTROL MK_SHIFT MK_MBUTTON MK_RBUTTON MK LBUTTON 


    3.  	51 

            [CTRL],   
Flags   MK_CONTROL.     [SHIFT],  
 Flags  MK_SHIFT.       
   ,   MK_RBUTTON. , ,  
     ,   
    MK_LBUTTON.           
  (  ),   Flags   
MK_MBUTTON.       . 
      ,   
  . 
     Loc,    CPoint,    
    .  CPoint    POINT, 
  : 

    typedef struct tagPOINT { 
    LONG x; 
    LONG y; } POINT; 

     POINT   (X,Y)  .  , 
 X    Loc.x,       Loc.. 
            , 
    .       
     ,     
 .   ,    : 

    // MESSAGES.H 
    //       
    class CMainWin : public CFrameWnd	, 
    { 
    public: 
    CMainWin(); 
    afx_msg void OnChar(UINT ch, UINT count, UINT flags); 
    afx_msg void OnPaintO; 
    afx_msg void OnLButtonDown(UINT flags, CPoint loc); 
    afx_msg void OnRButtonDown (UINT flags, CPoint loc); 
    DECLAREJMESSAGE_MAP() }; 
    //      class CApp : public CWinApp { public: 
    BOOL Initlnstance(); }; 


    52 MFC:   

       : 

    // ,    
    #include	<afxwin.h> 
    tinclude	<string.h> 
    #include	"messages.h" 
    char str[80] = "Sample Output"; //    
    CMainWin::CMainWin() { 
    Create(NULL, "Processing Mouse Messages"); } 
    //   BOOL CApp::Initlnstance() ( 
    m_pMainWnd = new CMainWin; 
    m_pMainWnd->ShowWindow(m_nCmdShow); 
    m_pMainWnd->UpdateWindow(); 
    return TRUE; } 
    //     BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd) 
    ON_WM_CHAR() 
    ON_WM__PAINT () 
    ON_WM_LBUTTONDOWN() 
    ON_WM_RBUTTONDOWN() END_MESSAGE_MAP()	' 
    //   WM_CHAR 
    afx_msg void CMainWin::OnChar(UINT ch, UINT count, UINT flags) 
    ( 
    CClientDC dc(this); 
    dc.TextOut (1, I, "   ", 3); //    
    wsprintf(str, "%c", ch) ; dc.TextOut (1, 1, str, strlen(str)); } 
    //   WM_PAINT afx_msg void CMainWin::OnPaint() { 
    CPaintDC dc (this); 
    //     
    //     
    dc.TextOut(I, I, str, strlen(str)); 


    3.  	53 

    //      
    afx_msg void CMainWin::OnLButtonDown(DINT flags, CPoint loc) 
    CClientDC dc(this); 
    wsprintf(str, "Left button is down."); dc.TextOut(loc.x, loc.y, str, 
strlen (str)); s 
    //      
    afx_msg void CMainWin::OnRButtonDown(UINT flags, CPoint loc) 
    { 
    CClientDC dc(this); 
    wsprintf(str, "Right button is down."); dc.TextOut(loc.x, loc.y, str, 
strlen (str)); 
    1 
    CApp  App;   //          

     . 3.2.    . 

    Left button is down. 
    Right button is down. Left button is down. 
    Left button is down. 
    Left button is down. 
    Left button is down. 
    Left button is down.	I 
    Left button is down.	i; 
    \ 
    Right button is down.	' 

    . .2.       


    54 MFC:   


      WM PAINT 

          WM_PAINT.   
,   , , ,     
 ,    .    
. ,         
,  Windows          
  ,       
.      Windows   
 ,     ,     
.    Windows    
    .     
      ,      WM_PAINT. 
        WM_PAINT ,     
 .         WM_PAINT,  
   -     
Windows  ,      WM_PAINT  ,    
.    ,  Windows   .  
  ,     ,   
 ,   WM_PAINT  ,  Windows  
 '' 
      Windows   WM_PAINT,   
  InvalidateRect(),    CWnd.  
 : 

    void  CWnd::InvalidateRect(LPCRECT   IpRegion,   BOOL   Erase =   TRUE); 

      IpRegion      CRect, 
   ,    .  
CRect     RECT,  .  
     RECT.    
  NULL,      .  
 Erase   ,     . (   
.)     ,      . 
       InvalidateRect()    ,  
      . ,   , 
  ,  Windows    WM_PAINT. 
         ,   
 ^_     WM_PAINT. 
 ,    ,   WM_PAINT. 
       
,     InvalidateRect().   , 
   : 

    // MESSAGE4. 
    //       


    3.  	55 


    class CMainWin : public CFrameWnd 
    { 
    public: 
    CMainWin (); 
    afx_msg void OnChar(UINT ch, UINT count, UINT flags); 
    afx_jnsg void OnPaintO; 
    afx_msg void OnLButtonDown(UINT flags, CPoint loc); 
    afx_msg void OnRButtonDown(UINT flags, CPoint loc); 
    DECLARE_MESSAGE_MAP() } ; 
    //      class CApp : public CWinApp { public: 
    BOOL Initlnstance (); }; 
      : 
    /* ,      WM_PAINT 
    finclude <afxwin.h> finclude <string.h> finclude "message4.h" 
    char str[80] = "Sample Output"; //    int X = 1, Y = 1; // 
   
    CMainWin::CMainWin() { 
    Create(NULL, "Routing Output Through WM_PAINT"); 
    ! 
    //   BOOL CApp: :Initlnstance () { 
    m_pMainWnd = new CMainWin; 
    m_pMainWnd->ShowWindow(m_nCmdShow) ; 
    m_pMainWnd->UpdateWindow(); 
    return TRUE; } 
    //     BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd) 
    ON_WM_CHAR() 
    ON_WM_PAINT() 
    ON_WM_LBUTTONDOWN() 
    ON_WM_RBUTTONDOWN() END_MESSAGE_MAP() 
    //   WM_CHAR 
    afx_msg void CMainWin::OnChar(UINT ch, UINT count, UINT flags) 


    56 MFC:   


    i 
    X = Y = 1; 
    wsprintf(str, "%c", ch) ; InvalidateRect(NULL); } 
    //   WM_PAINT afx_msg void CMainWin::OnPaint() { 
    CPaintDC dc (this); 
    //     
    //     
    dc.TextOut(X, Y, str, strlen (str)); 
    I 
    //      
    afxjnsg void CMainWin::OnLButtonDown(UINT flags, CPoint loc) 
    { 
    wsprintf(str, "Left button is down."); 
    X = loc.x; 
    Y = loc.y; 
    InvalidateRect(NULL); l 
    //      
    afxjnsg void CMainWin::OnRButtonDown(UINT flags, CPoint loc) 
    ( 
    wsprintf (str, "Right button is down."); 
    X = loc.x; 
    Y = loc.y; 
    InvalidateRect(NULL); 
    CApp App; //    

    ,       _1 , X 
 Y,    ,       
  WM_PAINT. 
     ,      WM_PAINT  
     ,   ,  
.     ,    Windows 
   OJTOM,      . 
    :  (  )  Windows    
  WM__PAINT  ,  .   
       ,  
    .        
 . 

    3.   57 


      WM__DESTROY 

       ,      
: WM_DESTROY.   ,     , 
  .         
,   .       
    .   WM_DESTRO\ 
 OnDestroyO,    : 

    afx_msg void OnDestroyO; 

             , 
           
  .   ,  ,  . , 
    ,    OnDestroyO. 
        . 

       

     ,    ,  WM_TIMER.   
Windows    ,     
.  ,    ,  
  WM_TIMER.     "" 
.   ,      . 
         SetTimerQ,   
 CWnd.   : 

    UINT CWnd::SetTimer(UINT ID, UINT Length, 
    void(CALLBACK EXPORT *TFunc) (HWND, UINT, UINT, 
    DWORD) 

      ID   ,   
   . (     
.)  Length     , .. 
 ,    . ,   
  TFunc,        
.      ,   VOID 
CALLBACK      ,     . 
   TFunc  NULL,    ,    
    .       
  WMJT 
    IMER        ,  
   .      .   
    ID.   ,  . 
      ,      
   ,      CWnd::KillTimer(), 
   : 

    BOOL CWnd:iKillTimer(int ID); 


    58 MFC:   

      ID    . 
 KillTimerQ        
    . 
        OnTimer(),    : 

    afx_msg  void  OnTimer(UINT   ID); 

         OnTimer()  ID   
 ,   .     
 ,      ID,  
,     . 
         ,    
   "".        
       CTime. ( CTime  
         C++.)   
 ,     ,   
.  ,      . 
        , 
    WM_DESTROY  "" .  
  ,    . 

    // MESSAGES. H 
    //       class CMainWin : public CFrameWnd 
    { 
    public: 
    CMainWin (); 
    afx_msg void OnPaintO; afx__msg void OnTimer (UINT ID); afx_msg void 
OnDestroy(); DECLARE_MESSAGE_MAP() 
    }; 
    //             class   CApp   :   public   CWinApp 
    { 
    public: 
    BOOL Initlnstance () ; }; 
      : 
    // -"" 
    #include	<afxwin.h> 
    #include	<string.h> 
    #include	<time.h> 
    #include	"messages.h" 


    3.   59 


    char str[80] = ""; //    
    CMainWin::CMainWin() { 
    RECT r; 
    r.left = r.top = 10; 
    r.right = 200; 
    r.bottom = 60; 
    Create(NULL, "A Clock Program", WS__OVERLAPPEDWINDOW, r); 
    } 
    //   BOOL CApp: : Initlnstance() { 
    m_pMainWnd = new CMainWin; 
    //   
    if (m_pMainWnd->SetTimer(1, 1000, NULL) != 1) return FALSE; 
    m_pMainWnd->ShowWindow(m_nCmdShow); m_pMainWnd->UpdateWindow() ; 
    return TRUE; } 
    //     BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd) 
    ON_WM_PAINT() 
    ON_WM_TIMER 
    ON_WM_DESTROY() END_MESSAGE_MAP() 
    //   WM_PAINT afx_msg void CMainWin::OnPaint() { 
    CPaintDC dc (this); 
    //     
    //     
    dc.TextOut(1, 1, str, strlen (str)); 
    } 
    //   WM__TIMER afx_msg void CMainWin::OnTimer(UINT ID) { 
    CTime curtime = CTime::GetCurrentTime(); 
    struct tm *newtime; 
    newtime = curtime.GetLocalTm(); 
    wsprintf (str, asctime(newtime)); 
    str[strlen(str)-1] = '\0'; //   /r/n 


    60	MFC:   


    InvalidateRect(NULL, 0); 
    //   
    afx_msg void CMainWin::OnDestroy() 
    { 
    KillTimer(1); 
    CApp App;   //          

        . 3.3. 


     A Clock Program 
    Sat Jan 11 16:25:241997 

    . 3.3. -'" 

             
CTime.   GetCurrentTime()    
.  GetLocalTmQ      , 
   C/C++.      
  asctime(),     , 
   .       
  ,     CTime. 
    ,     ,  Windows-  
,        .  
   . 

    3.   61 