MFC

. .

       

., . WINDOWS 95/NT 4. MFC. 2: . ." ", 1998. . Visual C++ 5. : . . .:" ", 1998.-688 . , . . Visual C++ 5. .: . . - .: , 1997.-816 . , . . Visual C++6. .: , 1998. - 496 ., . ., . Visual C++ MFC. Windows NT Windows 95. 3. .:-BHV, 1997. . ., . . Microsoft Visual C++ MFC. Windows 95 Windows NT. - .: , 1996. - 288 . - ( ; . 24) . ., . . Microsoft Visual C++ MFC. Windows 95 Windows NT. - .: , 1997. - 272 . - ( ; . 28) ., ., . Microsoft Visual C++ 6.0 . . .± : ; : , 2001. ± 864 .: .

        

ftp://ermak.cs.nstu.ru/metodic/makar/MFC www.codenet.ru www.codeguru.com www.firststeps.ru www.realcoding.net www.codegu.ru www.rsdn.ru 2004.doc . Visual ++

0 ± 1990      ( Clipboard.1. ActiveX)  Windows 95   ( Thread ) .   32Windows NT         « DCOM ± - »  Windows XP      64- .Windows  Windows 3. WINDOWS 3. COM. OLE. POSIX. . OS/2«) (MS DOS.

. Windows SDK (Software Development Kit)      .« . .dll ± gdi32.dll ± . ± . .dll ± kernel32. . . . . .  Windows API (Application Program Interface)      2000 user32.

        ( bitmap ) .

WM_QUIT? ? .

// DispatchMessage(&msg). 0. 0.while ( GetMessage(&msg.// } WM_CHAR . 0)) { TranslateMessage(&msg).

. lpMsg . WM_MOUSELAST Return Value ± WM_QUIT. WM_KEYLAST ± .WM_QUIT. BOOL GetMessage( LPMSG lpMsg. wMsgFilterMax [in] . GetLastError. UINT wRemoveMsg ). WM_KEYFIRST ± . GetMessage . UINT wMsgFilterMin. UINT wMsgFilterMin. BOOL PeekMessage( LPMSG lpMsg. . HWND hWnd. -1. lpMsg [out] hWnd [in] . UINT wMsgFilterMax). WM_MOUSEFIRST ± . . HWND hWnd. . Windows XP: WM_INPUTWM_INPUT. . . MSG . wMsgFilterMin wMsgFilterMax .GetMessage Function GetMessage . .0± wMsgFilterMin [in] . . UINT wMsgFilterMax. . . .0.

} MSG. *PMSG. . hwnd . UINT message. pt . . WPARAM wParam LPARAM lParam. lParam . . message . . POINT pt. . wParam .MSG Structure MSG typedef struct { HWND hwnd. time . DWORD time.

hwnd. 0)) { LRESULT CALLBACK WndProc(HWND. if (!TranslateAccelerator (msg. LPARAM). LPSTR lpCmdLine. HINSTANCE hPrevInstance. 0. /* */ char szTitle[] = APPNAME": CHAR szFileName[_MAX_PATH].Windows /* */ #define APPNAME "MINAPIWIN" #include <windows. if (!hPrevInstance) { /* if (!InitApplication(hInstance)) return (FALSE). /* HWND hwndEdit. &msg)) { TranslateMessage(&msg). nCmdShow)) return (FALSE). /* char szAppName[] = APPNAME. } } return (msg. while (GetMessage(&msg. szAppName). WPARAM. int nCmdShow) { MSG msg.h> #include "resource. CHAR szFileTitle[_MAX_PATH].wParam). ATOM APIWinRegisterClass(CONST WNDCLASS *). UINT. */ */ /* */ ". /* */ int WINAPI WinMain(HINSTANCE hInstance. } /* */ if (!InitInstance(hInstance. BOOL InitApplication(HINSTANCE). hAccelTable. DispatchMessage(&msg). } . NULL. */ /* */ hAccelTable = LoadAccelerators (hInstance. int). HANDLE hAccelTable.h" /* /* Win32® API */ */ /* */ HINSTANCE hInst. /* */ BOOL InitInstance(HINSTANCE.

cbWndExtra = 0. UpdateWindow(hWnd). UINT message. return RegisterClass(&wc). SW_RESTORE). } /* : WM_DESTROY */ LRESULT CALLBACK WndProc( HWND hWnd. wc.lpszMenuName = szAppName.lpszClassName = szAppName. wc. CW_USEDEFAULT. hInst = hInstance. wc. szTitle. NULL. wc. 0. default: return (DefWindowProc(hWnd. if (hwnd) { /* */ if (IsIconic(hwnd)) ShowWindow(hwnd. } .style = CS_HREDRAW | CS_VREDRAW. szAppName). SetForegroundWindow (hwnd). int nCmdShow) { HWND hWnd.lpfnWndProc = (WNDPROC)WndProc.hCursor = LoadCursor(NULL. if (!hWnd) return (FALSE). hInstance. wc. } return (0). break./* BOOL InitApplication(HINSTANCE hInstance) { WNDCLASS wc. WS_OVERLAPPEDWINDOW. ShowWindow(hWnd. wc. wc. } /* */ BOOL InitInstance(HINSTANCE hInstance. /* */ return (FALSE). nCmdShow). LPARAM lParam) { switch (message) { case WM_DESTROY: PostQuitMessage(0). hWnd = CreateWindow(szAppName. return (TRUE). IDC_ARROW). NULL). /* Win32 API hPrevInstance */ NULL. wc. CW_USEDEFAULT. NULL).cbClsExtra = 0.hbrBackground = (HBRUSH)(COLOR_WINDOW+1). WPARAM wParam. message. } wc. NULL.hInstance = hInstance. wc.hIcon = LoadIcon (hInstance. ( */ hwnd = FindWindow (szAppName. HWND hwnd. lParam)). 0. wParam.

!". int nCmdShow) { MessageBox(0. .Windows #include "windows. MB_OK|MB_ICONEXCLAMATION).h" int APIENTRY WinMain(HINSTANCE hInstance." ." MessageBeep(MB_ICONEXCLAMATION). } Windows". LPTSTR lpCmdLine. HINSTANCE hPrevInstance.

UINT uMsg. return TRUE.h" #include "resource.(DLGPROC)DlgProc). WPARAM wParam. !". HINSTANCE hPrevInstance." .  #include "windows.MB_OK|MB_ICONEXCLAMATION). } } } return FALSE. int nCmdShow) { MessageBox(0. LPARAM lParam) { switch (uMsg) { case WM_COMMAND: { WPARAM wId = wParam.0). MessageBeep(MB_MB_ICONEXCLAMATION).h" BOOL CALLBACK DlgProc(HWND hwnddlg.0. switch(wId) { case IDOK: case IDCANCEL: EndDialog(hwnddlg.MAKEINTRESOURCE(IDD_DIALOG1). DialogBox(hInstance. LPTSTR lpCmdLine. } int APIENTRY WinMain(HINSTANCE hInstance." Windows". } .

WPARAM wParam.h" BOOL CALLBACK DlgProc(HWND hwnddlg. } int APIENTRY WinMain(HINSTANCE hInstance. MSG msg. SW_SHOW). SW_SHOW). while (GetMessage(&msg. UINT uMsg. HWND hhh = CreateDialog(hInstance. } } } return FALSE.MAKEINTRESOURCE(IDD_DIALOG1).(DLGPROC)DlgProc). return TRUE.wParam. HINSTANCE hPrevInstance. 0 )) { TranslateMessage(&msg).(DLGPROC)DlgProc)." Windows".MB_OK|MB_ICONEXCLAMATION).(DLGPROC)DlgProc).#include "windows. LPARAM lParam) { switch (uMsg) { case WM_COMMAND: { WPARAM wId = wParam. ShowWindow(hhh.h" #include "resource. switch(wId) { case IDOK: case IDCANCEL: EndDialog(hwnddlg. ShowWindow(hhh. hhh = CreateDialog(hInstance.MAKEINTRESOURCE(IDD_DIALOG1). } .0. DialogBox(hInstance.0.MAKEINTRESOURCE(IDD_DIALOG1). LPTSTR lpCmdLine. NULL. int nCmdShow) { MessageBox(0. } return msg.0.0). 0." . DispatchMessage(&msg). MessageBeep(MB_ICONEXCLAMATION). !".

GDI Windows ± .   Windows GDI (Graphics Device Interface) device context.

.    . Windows . ( wysiwyg ) . . exe. . . .

DLL ±  - .   DLL .

. Windows. . . Microsoft Windows. .. . ² . MFC - . . . . . .. . . .MFC  K MFC . . . . .

Windows. Win32 API. Win32    . . . . Win32 API . API. . C++ . :   Windows.

MFC- .

Visual Studio                ++ (cpp -> obj ) (rc ->res) (obj + res -> exe) AppWizard ClassWizard (Spy. PVIEW) (Visual SourceSafe ) (ActiveX ) MFC ATL (Microsoft Active Template Library) .

Windows  Win32  ++   Win32 MFC ( OWL ) .

. . . RAD . . . . " Windows / ++ API. . . Windows. . . ++).MFC  MFC ++. . " ++ ". . . .  " MFC. . . MFC Windows API. . ++ . ++. Microsoft Visual ++. . . ++. . ( . MFC .    C++.

MFC .

. . MFC . . . . . CObject.CObject ±  . .  ++ RTTI. . . . . . RUNTIME_CLASS().  CObject. MFC.

. . . CWnd. . (" ") . . CFile. GDI. . CFrameWnd. CGDIObject    CMenu. MFC- . . . . CDC. mdTarget File CDC GDIMenu CCmdTarget     CCmdTarget. : CWinApp. CWinThread. CCmdTarget. . . CGDIObject . . CObject. . . . . . . .

API . . . . MFC MFC. .. API . MFC. MFC.

MFC. .H. ( MFC Application Framework. AFXWIN. AfxMessageBox(). WINDOWS. .H. AFX. . . MessageBox(). MFC. " " API. . . ). Windows API . . MFC. Afx. MFC .H MFC- . . AFX . AFXWIN.  .

h> // class CMainWin: public CFrameWnd { public: CMainWin().h> #include <string. simpwin. CFrameWnd /* */) END_MESSAGE_MAP() . // // // // . return TRUE. // CApp::m_pMainWnd // . } CMainWin::CMainWin() { // . // DECLARE_MESSAGE_MAP() }. // MFC // .cpp #include <afxwin. this->Create(0.h #include <afxwin. // BOOL CApp::InitInstance() { // // . // MFC // . }.H" // // CApp App.MFCsimpwin.h> #include "SIMPWIN. m_pMainWnd = new CMainWin. . m_pMainWnd->UpdateWindow(). m_pMainWnd->ShowWindow(SW_RESTORE). InitInstance() class CApp: public CWinApp { public: BOOL InitInstance(). " MFC"). 0. } // BEGIN_MESSAGE_MAP(CMainWin /* */. .

CWnd* MFC. CWinThread. . CMainWin. m_pMainWnd. TRUE FALSE . . . . .CApp   CWinApp ± virtual BOOL CWinApp::InitInstance(). . .

- . : BEGIN_MESSAGE_MAP(CMainWin /* END_MESSAGE_MAP() */. MFC. . . CFrameWnd /* */) . . . - . : SW_HIDE SW_MAXIMIZE SW_MINIMIZE SW_SHOW SW_RESTORE . BOOL CWnd::ShowWindow(int How). m_nCmdShow. . .

. // CWnd *Parent = 0. // DWORD ExStyle = 0. // DWORD Style = WS_OVERLAPPEDWINDOW. . . " " MFC Windows. // WindowsWS_OVERLAPPED WS_MAXIMIZEBOX WS_MINIMIZEBOX WS_SYSMENU WS_HSCROLL WS_VSCROLL LPCSTR Title. BOOL CFrameWnd::Create( LPCSTR ClassName. Style . ). ClassName. .Create(). // const RECT &XYSize = rectDefault. . Visual C++ XYSize. . . . . // LPCSTR MenuName = 0. . . // CCreateContext *Context = 0 // . .

x . RECT . 32Windows 3. .x . Win32 16. . Unicode DBCS Unicode DBCS . API) CallBack.( BOOL BSTR BYTE COLORREF DWORD LONG LPARAM LPCSTR LPSTR LPCTSTR LPTSTR LPVOID LRESULT UINT WNDPROC WORD WPARAM POSITION LPCRECT HINSTANCE 32323232832323232323232323232323216. . . . Win32 16² Windows 3.

. . . . CWinApp.    . IpszModuleName. HCURSOR hCursor =0. . BOOL AFXAPI AfxFreeLibrary(HINSTANCE hlnstLib) DLL. hbrBackground² . . . DLL .     . . . . . . . (. hlcon ² . HINSTANCE AfxGetlnstanceHandle() .DLL. hCursor² . . WNDCLASS. . HINSTANCE AfxGetResourceHandle() void AfxSetResourceHandle(HINSTANCE hlnstResource) DLL. . HBRUSH hbrBackground = 0.    MFC. : nClassStyle ² Windows. NULL. USRDLLMFC. ).DLL . . . ). CWinApp* AfxGetApp() CWinApp LPCNSRT AfxGetAppName() . OLE. hlnstLib. HICON hlcon = 0) . . HINSTANCE . CWnd* AfxGetMainWhd() rn_pMainWnd ² . BOOL AFXAPI AfxRegisterClass(WNDCLASS* IpWndClass) TRUE FALSE . LPCTSTR AFXAPI AfxRegisterWndClass( UINT nClassStyle. HINSTANCE AFXAPI AfxLoadLibrary(LPCTSTR IpszModuleName) (. .

MB_ICONERROR« . ) ) ) : . nIDHelp nIDHelp IDABORT ² IDCANCEL ² IDIGNORE ² IDNO ² IDOK ² IDRETRY ² IDYES ² ² Abort Cancel Ignore No Retry Yes . : MB_DEFBUTTONl ² MB_DEFBUTTON2 ² MB_DEFBUTTON3 ² IpszText . No Cancel ( . .int AfxMessageBox( LPCTSTR IpszText. . UINT nIDHelp =0) int AfxMessageBox ( UINT nIDPrompt. . UINT nIDHelp = (UINT) -1) . -1. nIDPrompt : nIDPrompt. UINT nType = MB_OK. . MB_APPLMODAL ² MB_SYSTEMMODAL ² MB_TASKMODAL ² MB_ICONEXCLAMATION. Retry Ignore ( MBOK ² MB_OKCANCEL ² Cancel (OK ) MB_RETRYCANCEL ² Retry Cancel ( MB_YESNO ² Yes No ( ) MB_YESNOCANCEL ² Yes. MB_ICONSTOP. UINT nType = MB_OK. : MB_ABORTRETRYIGNORE ² Abort.

MFC  » .

² . . DialogBox . . . . . CDialog ²     . . CWinApp ² MFC . ).  : . . . ) ). . . Loadlcon ( ( DrawText ( . . . m_pMainWnd. CWnd ² . "class" ( ). m_ MFC). . . ( . XtoY( X . MFC m_ ( class member ² ). .MFC  . . . Y).

. . . .  #include ³stdafx. . . .h´ . . .

. switch . . . Windows ( API) . . . MFC . Windows . .

. . .MFC  MFC . . .  . :   . . . .

. WM_CHAR . ON_ : WM_COMMAND ON_COMMAND().  BEGIN_MESSAGE_MAP(CMainWin. . - . BEGIN_MESSAGE_MAP() . CFrameWnd) ON_WM_CHAR() END_MESSAGE_MAP() . END_MESSAGE_MAP(). . . Windows. . WM_CHAR. : .

POINT. } POINT. CPoint Loc). . . CPoint : typedef struct tagPOINT { LONG x. afx_msg void OnLButtonDown(UINT Flags. afx_msg void OnRButtonDown(UINT Flags. . CPoint Loc). LONG y. .

. } WM_PAINT CPaintDC. Windows. CString. . y = 20: afx_msg void CMainWin::OnPaint() { CPaintDC paintDC(this). . paintDC. . 20. . . . . . char* CString ( Visual C++.TextOut(10. . . . . ). " " x = 10. ON_WM_PAINT(). WM_PAINT : afx_msg void OnPaint(). WM_PAINT. MFC . MFC. CString(" ")).WM_PAINT  Windows . .

. . . . . . WM_PAINT . . . . WM_PAINT. . . . . Windows Windows . . . WM_PAINT. ). . . . WM_PAINT. ( . CPaintDC. . . . . Windows .( ) . . . WM_PAINT .

WM_PAINT 

WM_PAINT, , .

: void CWnd::InvalidateRect(LPRECT pRegion, BOOL EraseBackground = TRUE); pRegion . , EraseBackground ). , . , , (

0.

void CChildView::OnPaint() { CPaintDC dc(this); // device context for painting dc.TextOut(nOldMouseX, nOldMouseY, " dc.TextOut(nMouseX, nMouseY, pszMouseStr); dc.TextOut(1, 1, " "); dc.TextOut(1, 1, str); // Do not call CWnd::OnPaint() for painting messages } void CChildView::OnLButtonDown(UINT nFlags, CPoint loc) { nOldMouseX = nMouseX; nOldMouseY = nMouseY; strcpy(pszMouseStr, " "); nMouseX = loc.x; nMouseY = loc.y; this->InvalidateRect(0); } void CChildView::OnRButtonDown(UINT nFlags, CPoint loc) { nOldMouseX = nMouseX; nOldMouseY = nMouseY; strcpy(pszMouseStr, " "); nMouseX = loc.x; nMouseY = loc.y; this->InvalidateRect(0); } void CChildView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) { sprintf(str, "%c", nChar); // WM_PAINT // this->InvalidateRect(0); }

");

class CChildView : public CWnd { char str[50]; int nMouseX, nMouseY, nOldMouseX, nOldMouseY; char pszMouseStr[50]; // Construction public: CChildView();

CChildView::CChildView() { strcpy(str, ""); strcpy(pszMouseStr, ""); nMouseX = nMouseY = nOldMouseX = nOldMouseY = 0; }

. . Windows. . . . . . . . . . "i" "W" ..

55 . 0. .WM_TIMER Windows . ( . Id . void (CALLBACK EXPORT *TFunc)(HWND. 55. Id. UINT. . UINT Interval. : afx_msg void OnTimer(UINT Id). 0. DWORD)). . . . . . UINT. . . ) . . . Interval ( ) . : UINT CWnd::SetTimer(UINT Id. 1. . . WM_TIMER. . . . .

: BOOL CWnd::KillTimer(int Id). .WM_DESTROY . . . . : afx_msg void OnDestroy(). Id. . . . .

rect). rect. WS_OVERLAPPEDWINDOW. // RECT rect. ""). 500. . // // 1 500 m_pMainWnd->SetTimer(1. // WM_TIMER afx_msg void OnTimer(UINT ID). DECLARE_MESSAGE_MAP() }. return TRUE. // WM_DESTROY afx_msg void OnDestroy(). } . 0). . CMainFrame::CMainFrame() { // . rect.top = 10. . afx_msg void OnPaint(). m_pMainWnd->UpdateWindow(). RECT. rect. // // this->Create(0. m_pMainWnd->ShowWindow(SW_RESTORE). strcpy(str.left = rect. } BOOL CApp::InitInstance() { m_pMainWnd = new CMainFrame. char str[50]. .class CMainFrame: public CFrameWnd { public: CMainFrame().right = 200.bottom = 60. "CLOCK".

this->InvalidateRect(0). } . . // CTime curtime = CTime::GetCurrentTime().GetLocalTm(). MFC. asctime(newtime)). str[strlen(str) . 3). 1.afx_msg void CMainFrame::OnPaint() { CPaintDC dc(this). 1. } afx_msg void CMainFrame::OnTimer(UINT ID) { // . " ". KillTimer(1). sprintf(str. newtime = curtime. } WM_PAINT -- . dc. tm *newtime.1] = '\0'. // ID.TextOut(1. CTime. afx_msg void CMainFrame::OnDestroy() { // // . // dc. str). // // .TextOut(1.

 ( ? CWnd . . 100 ?)  MFC . ClassWizard .

.

    » MDI-SDI ± .

: Windows. TRACE("Executable filename = %s\n". AfxGetApp()->m_pszExeName). AfxGetResourceHandle HINSTANCE AfxGetResourceHandle( ). . AfxGetApp()->m_pszExeName). TRACE("Executable filename = %s\n".CWinApp. InitInstance. MAKEINTRESOURCE(IDR_PANEL)). TRACE("Application instance handle is 0x%0X\n". AfxGetAppName()). LPCTSTR AFXAPI AfxGetAppName( ). AfxGetInstanceHandle()). AfxGetApp CWinApp CWinApp* AFXAPI AfxGetApp( ). TRACE("Application name is %s\n". HINSTANCE AFXAPI AfxGetInstanceHandle( ). AfxGetInstanceHandle . . AfxGetAppName . HMENU hMenu = ::LoadMenu(AfxGetResourceHandle().

MFC  CWinThread::Run() .

but quit on WM_QUIT if (!PumpMessage()) return ExitInstance(). } } while (::PeekMessage(&(pState->m_msgCur). for (. // assume "no idle" state } // phase2: pump messages while available do { // pump message. // acquire and dispatch messages until a WM_QUIT message is received. } } . PM_NOREMOVE)) { // call OnIdle while in bIdle state if (!OnIdle(lIdleCount++)) // bIdle = FALSE. // reset "no idle" state after pumping "normal" message //if (IsIdleMessage(&m_msgCur)) if (IsIdleMessage(&(pState->m_msgCur))) { bIdle = TRUE. _AFX_THREAD_STATE* pState = AfxGetThreadState(). // by default don't enter run loop } // main running routine until thread exits int CWinThread::Run() { ASSERT_VALID(this).BOOL CWinThread::InitInstance() { ASSERT_VALID(this). PM_NOREMOVE)). NULL. NULL. LONG lIdleCount = 0. NULL..) { // phase1: check to see if we can do idle work while (bIdle && !::PeekMessage(&(pState->m_msgCur). NULL. NULL. NULL. // for tracking the idle time state BOOL bIdle = TRUE. lIdleCount = 0. return FALSE.

1 3 2 PostMessage 1 SendMessage .

« . .. : . . WM_.  Windows WM_COMMAND CWnd -  ...

 MFC  BEGIN_MESSAGE_MAP END_MESSAGE_MAP DECLARE_MESSAGE_MAP .

DDX/DDV support // // Implementation protected: HICON m_hIcon. // standard constructor protected: virtual void DoDataExchange(CDataExchange* pDX). . DECLARE_MESSAGE_MAP() public: CString myEdit. afx_msg HCURSOR OnQueryDragIcon(). LPARAM lParam). afx_msg void OnPaint(). // Generated message map functions virtual BOOL OnInitDialog(). // Dialog Data enum { IDD = IDD_DIALOG_DIALOG }.class CDialogDlg : public CDialog { // Construction public: CDialogDlg(CWnd* pParent = NULL). }. afx_msg void OnSysCommand(UINT nID.

h #ifdef _AFXDLL #define DECLARE_MESSAGE_MAP() \ private: \ static const AFX_MSGMAP_ENTRY _messageEntries[]. \ #endif . \ virtual const AFX_MSGMAP* GetMessageMap() const. \ protected: \ static const AFX_MSGMAP messageMap. \ virtual const AFX_MSGMAP* GetMessageMap() const.afxwin. \ protected: \ static const AFX_MSGMAP messageMap. \ #else #define DECLARE_MESSAGE_MAP() \ private: \ static const AFX_MSGMAP_ENTRY _messageEntries[]. \ static const AFX_MSGMAP* PASCAL GetThisMessageMap().

CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP END_MESSAGE_MAP() cpp .BEGIN_MESSAGE_MAP(CDialogDlg.

\ AFX_COMDAT const AFX_MSGMAP_ENTRY theClass::_messageEntries[] = \ {\ #endif #define END_MESSAGE_MAP() \ {0. &theClass::_messageEntries[0] }. } \ AFX_COMDAT const AFX_MSGMAP theClass::messageMap = \ { &baseClass::GetThisMessageMap. AfxSig_end. 0. (AFX_PMSG)0 } \ }. \ AFX_COMDAT const AFX_MSGMAP_ENTRY theClass::_messageEntries[] = \ {\ #else #define BEGIN_MESSAGE_MAP(theClass. \ . } \ const AFX_MSGMAP* theClass::GetMessageMap() const \ { return &theClass::messageMap. } \ AFX_COMDAT const AFX_MSGMAP theClass::messageMap = \ { &baseClass::messageMap.#ifdef _AFXDLL #define BEGIN_MESSAGE_MAP(theClass. baseClass) \ const AFX_MSGMAP* theClass::GetMessageMap() const \ { return &theClass::messageMap. 0. &theClass::_messageEntries[0] }. 0. baseClass) \ const AFX_MSGMAP* PASCAL theClass::GetThisMessageMap() \ { return &theClass::messageMap.

. // control code or WM_NOTIFY code UINT nID. // signature type (action) or pointer to message # AFX_PMSG pfn.AFX_MSGMAP_ENTRY struct AFX_MSGMAP_ENTRY { UINT nMessage. // used for entries specifying a range of control id's UINT_PTR nSig. // routine to call (or special value) }. // windows message UINT nCode. // control ID (or 0 for windows messages) UINT nLastID.

 Windows : ON_WM_xxx ON_WM_LBUTTONUP .

0. 0. CPoint) . 0. CPoint) > (OnLButtonUp)) }. afxmsg. // void (CWnd*. AfxSig_vwp. \ (AFX_PMSG)(AFX_PMSGW) \ (static_cast< void (AFX_MSG_CALL CWnd::*)(UINT.h #define ON_WM_LBUTTONUP() \ { WM_LBUTTONUP. AfxSig_vwp.

 ± CWnd afx_msg void OnLButtonUp(UINT nFlags. CPoint point). .

// DDX/DDV support // Implementation protected: HICON m_hIcon. // standard constructor // Dialog Data enum { IDD = IDD_DIALOG_DIALOG }. // Generated message map functions virtual BOOL OnInitDialog(). }. DECLARE_MESSAGE_MAP() public: CString myEdit. afx_msg HCURSOR OnQueryDragIcon().class CDialogDlg : public CDialog { // Construction public: CDialogDlg(CWnd* pParent = NULL). h . LPARAM lParam). afx_msg void OnLButtonUp(UINT nFlags. protected: virtual void DoDataExchange(CDataExchange* pDX). afx_msg void OnSysCommand(UINT nID. afx_msg void OnPaint(). CPoint point).

BEGIN_MESSAGE_MAP(CDialogDlg. CDialog::OnLButtonUp(nFlags. CPoint point) { // TODO: Add your message handler code here and/or call default //AfxMessageBox(³ ´). CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_WM_LBUTTONUP() END_MESSAGE_MAP() void CDialogDlg::OnLButtonUp(UINT nFlags. } pp . point).

(WORD)id. .// for general controls #define ON_CONTROL(wNotifyCode. (WORD)id. AfxSigCmd_v. .   afx_msg void CWnd::Fxn(). id. (WORD)wNotifyCode. \ (static_cast< AFX_PMSG > (memberFxn)) }. memberFxn) \ { WM_COMMAND. AfxSigCmd_v. // void () .

CPoint point). // Generated message map functions virtual BOOL OnInitDialog(). afx_msg HCURSOR OnQueryDragIcon(). afx_msg void OnEnChangeEdit1(). afx_msg void OnLButtonUp(UINT nFlags. }. afx_msg void OnSysCommand(UINT nID. // standard constructor protected: virtual void DoDataExchange(CDataExchange* pDX). // Dialog Data enum { IDD = IDD_DIALOG_DIALOG }.class CDialogDlg : public CDialog { // Construction public: CDialogDlg(CWnd* pParent = NULL). . // DDX/DDV support // Implementation protected: HICON m_hIcon. afx_msg void OnPaint(). DECLARE_MESSAGE_MAP() public: CString myEdit. LPARAM lParam).

// TODO: Add your control notification handler code here } . OnEnChangeEdit1) END_MESSAGE_MAP() void CDialogDlg::OnEnChangeEdit1() { // TODO: If this is a RICHEDIT control.SetEventMask() // with the ENM_CHANGE flag ORed into the mask.BEGIN_MESSAGE_MAP(CDialogDlg. CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_WM_LBUTTONUP() ON_EN_CHANGE(IDC_EDIT1. the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().

#define ON_NOTIFY(wNotifyCode. (WORD)id. AfxSigNotify_v. id. \ (AFX_PMSG) \ (static_cast< void (AFX_MSG_CALL CCmdTarget::*)(NMHDR*. LRESULT*) . LRESULT*) > \ (memberFxn)) }. memberFxn) \ { WM_NOTIFY. // void (NMHDR*. AfxSigNotify_v. (WORD)(int)wNotifyCode. (WORD)id.

afx_msg HCURSOR OnQueryDragIcon(). // Dialog Data enum { IDD = IDD_DIALOG_DIALOG }. // Generated message map functions virtual BOOL OnInitDialog(). afx_msg void OnPaint(). }. // DDX/DDV support // Implementation protected: HICON m_hIcon. .class CDialogDlg : public CDialog { // Construction public: CDialogDlg(CWnd* pParent = NULL). afx_msg void OnSysCommand(UINT nID. LPARAM lParam). afx_msg void OnNMDblclkTree1(NMHDR *pNMHDR. afx_msg void OnEnChangeEdit1(). afx_msg void OnLButtonUp(UINT nFlags. CPoint point). DECLARE_MESSAGE_MAP() public: CString myEdit. // standard constructor protected: virtual void DoDataExchange(CDataExchange* pDX). LRESULT *pResult).

LRESULT *pResult) { // TODO: Add your control notification handler code here *pResult = 0. IDC_TREE1.BEGIN_MESSAGE_MAP(CDialogDlg. OnNMDblclkTree1) END_MESSAGE_MAP() void CDialogDlg::OnNMDblclkTree1(NMHDR *pNMHDR. OnEnChangeEdit1) ON_NOTIFY(NM_DBLCLK. } . CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_WM_LBUTTONUP() ON_EN_CHANGE(IDC_EDIT1.

// NM_ code } NMHDR. NM_RCLICK The user has clicked the right mouse button within the control. NM_DBLCLK The user has double-clicked the left mouse button within the control. use CWnd::FromHandle. NM_OUTOFMEMORY The control could not complete an operation because there is not enough memory available. NM_RDBLCLK The user has double-clicked the right mouse button within the control. NM_KILLFOCUS The control has lost the input focus. UINT_PTR idFrom.// identifier of control sending message UINT code.typedef struct tagNMHDR { HWND hwndFrom. . // handle of control sending message //To convert this handle to a CWnd pointer. ± ( NM_SETFOCUS The control has received the input focus. and the user has pressed the ENTER key. NM_CLICK The user has clicked the left mouse button within the control. ) . NM_RETURN The control has the input focus.

id. \ static_cast<AFX_PMSG> (memberFxn) }. OnBar) or ON_BN_CLICKED(0. id. CN_COMMAND. OnBar) is the same as // ON_CONTROL(0. OnBar) AfxSigCmd_v. // void () . memberFxn) \ { WM_COMMAND. AfxSigCmd_v. (WORD)id.#define ON_COMMAND(id. // ON_COMMAND(id. (WORD)id.

AfxSigCmdUI. // void (CCmdUI*) . \ (AFX_PMSG) \ (static_cast< void (AFX_MSG_CALL CCmdTarget::*)(CCmdUI*) > \ (memberFxn)) }. (WORD)id. AfxSigCmdUI.#define ON_UPDATE_COMMAND_UI(id. memberFxn) \ { WM_COMMAND. CN_UPDATE_COMMAND_UI. (WORD)id.

.

. ( .: . ). . .

    . . .

CWinApp::OnFileOpen) // Standard print setup command ON_COMMAND(ID_FILE_PRINT_SETUP. void CmessApp::On32771() { int a = 33. afx_msg void OnUpdate32771(CCmdUI *pCmdUI). CWinApp::OnFileNew) ON_COMMAND(ID_FILE_OPEN. // TODO: Add your command update UI handler code here } // Overrides public: virtual BOOL InitInstance(). On32771) ON_UPDATE_COMMAND_UI(ID_32771. // Implementation afx_msg void OnAppAbout(). // TODO: Add your command handler code here } void CmessApp::OnUpdate32771(CCmdUI *pCmdUI) { int a = 33. }. CWinApp) ON_COMMAND(ID_APP_ABOUT.class CmessApp : public CWinApp { public: CmessApp(). DECLARE_MESSAGE_MAP() afx_msg void On32771(). CWinApp::OnFilePrintSetup) ON_COMMAND(ID_32771. OnUpdate32771) END_MESSAGE_MAP() . BEGIN_MESSAGE_MAP(CmessApp. OnAppAbout) // Standard file based document commands ON_COMMAND(ID_FILE_NEW.

memberFxn) \ { WM_COMMAND. CN_COMMAND. // void (CCmdUI*. id. (WORD)id. \ (AFX_PMSG) \ (static_cast< void (AFX_MSG_CALL CCmdTarget::*)(UINT) > \ (memberFxn)) }. idLast. AfxSigCmdUI_RANGE. (WORD)id. UINT) .#define ON_COMMAND_RANGE(id. memberFxn) \ { WM_COMMAND. (WORD)idLast. \ (AFX_PMSG) \ (static_cast< void (AFX_MSG_CALL CCmdTarget::*)(CCmdUI*) > \ (memberFxn)) }. OnBar) AfxSigCmd_RANGE. AfxSigCmdUI_RANGE. (WORD)idLast. idLast. // ON_COMMAND_RANGE(id. AfxSigCmd_RANGE. idLast. // void (UINT) #define ON_UPDATE_COMMAND_UI_RANGE(id. OnBar) is the same as // ON_CONTROL_RANGE(0. idLast. CN_UPDATE_COMMAND_UI.

\ (AFX_PMSG) \ (static_cast< void (AFX_MSG_CALL CCmdTarget::*)(UINT) > (memberFxn)) }. NMHDR*. (WORD)idLast. AfxSigNotify_RANGE void (UINT. AfxSigCmd_RANGE. (WORD)wNotifyCode. id. // void (UINT) #define ON_NOTIFY_RANGE(wNotifyCode. AfxSigCmd_RANGE. LRESULT*) . idLast. (WORD)id. LRESULT*) > \ (memberFxn)) }. \ (AFX_PMSG) \ (static_cast< void (AFX_MSG_CALL CCmdTarget::*)(UINT.#define ON_CONTROL_RANGE(wNotifyCode. AfxSigNotify_RANGE. (WORD)id. memberFxn) \ { WM_COMMAND. (WORD)idLast. (WORD)(int)wNotifyCode. idLast. id. NMHDR*. memberFxn) \ { WM_NOTIFY.

(WORD)idLast.#define ON_COMMAND_EX(id. CN_COMMAND. AfxSigCmd_EX. // BOOL (UINT) . idLast. AfxSigCmd_EX. (WORD)id. memberFxn) \ { WM_COMMAND. memberFxn) \ { WM_COMMAND. \ (AFX_PMSG) \ (static_cast< BOOL (AFX_MSG_CALL CCmdTarget::*)(UINT) > \ (memberFxn)) }. (WORD)id. #define ON_COMMAND_EX_RANGE(id. (WORD)id. CN_COMMAND. AfxSigCmd_EX. \ (AFX_PMSG) \ (static_cast< BOOL (AFX_MSG_CALL CCmdTarget::*)(UINT) > \ (memberFxn)) }.

. . ON_UPDATE_COMMAND_UI. User-Interface Item Menu item Toolbar button . Enable Check. or indeterminat e Sets pop-out or normal border Checks or unchecks check box (Not applicable) SetRadio Checks using dot (‡) Same as SetCheck SetText Sets item text (Not applicable) Status-bar pane Makes text visible or invisible Enables or disables Enables or disables Same as SetCheck Same as SetCheck (Not applicable) Sets pane text Normal button in CDialogBar Normal control in CDialogBar Sets button text Sets window text . CCmdUI. . Enable Enables or disables Enables or disables SetCheck Checks (×) or unchecks Selects.CCmdUI CCmdUI CCmdUI . ON_UPDATE_COMMAND_UI CCmdTarget. unselects.

void CFileDoc::OnUpdateFileSave(CCmdUI* pCmdUI) { // Enable the menu item if the file has been modified. } . ON_UPDATE_COMMAND_UI(ID_FILE_SAVE. FALSE to disable it. OnUpdateFileSave) END_MESSAGE_MAP() . Parameters bOn TRUE to enable the item.. pCmdUI->Enable(m_fModified).CCmdUI::Enable virtual void Enable( BOOL bOn = TRUE ). CDocument) // Many ON_* macros removed. Example BEGIN_MESSAGE_MAP(CFileDoc. .. .

CMyParentWndClass) ON_MESSAGE(WM_MYMESSAGE.// inside the class declaration afx_msg LRESULT OnMyMessage(WPARAM wParam... . pWnd->SendMessage(WM_MYMESSAGE). OnMyMessage) END_MESSAGE_MAP() LRESULT OnMyMessage(WPARAM wParam. LPARAM lParam) { } CWnd* pWnd = . pWnd->PostMessage(WM_MYMESSAGE).. #define WM_MYMESSAGE (WM_USER + 100) BEGIN_MESSAGE_MAP(CMyWnd. LPARAM lParam).

.::RegisterWindowMessage . class CMyWnd : public CMyParentWndClass { public: CMyWnd(). CMyParentWndClass) //{{AFX_MSG_MAP(CMyWnd) ON_REGISTERED_MESSAGE(WM_FIND. //{{AFX_MSG(CMyWnd) afx_msg LRESULT OnFind(WPARAM wParam. OnFind) //}}AFX_MSG_MAP END_MESSAGE_MAP() 0xC000 to 0xFFFF. BEGIN_MESSAGE_MAP(CMyWnd. . //}}AFX_MSG DECLARE_MESSAGE_MAP() }.ON_REGISTERED_MESSAGE. static UINT NEAR WM_FIND = RegisterWindowMessage("COMMDLG_FIND"). LPARAM lParam).

it depends on the message sent. Msg [in] Specifies the message to be sent. If this parameter is HWND_BROADCAST. Return Value The return value specifies the result of the message processing. UINT Msg. Parameters hWnd [in] Handle to the window whose window procedure will receive the message. . but the message is not sent to child windows. LPARAM lParam ). wParam [in] Specifies additional message-specific information. the message is sent to all top-level windows in the system.LRESULT SendMessage( HWND hWnd. overlapped windows. and pop-up windows. WPARAM wParam. including disabled or invisible unowned windows. lParam [in] Specifies additional message-specific information.

CWnd::SendMessage
Sends the specified message to this window. LRESULT SendMessage( UINT message, WPARAM wParam = 0, LPARAM lParam = 0 ); Parameters message Specifies the message to be sent. wParam Specifies additional message-dependent information. lParam Specifies additional message-dependent information. Return Value The result of the message processing; its value depends on the message sent. Remarks The SendMessage member function calls the window procedure directly and does not return until that window procedure has processed the message. This is in contrast to the PostMessage member function, which places the message into the window's message queue and returns immediately.

The PostMessage function places (posts) a message in the message queue associated with the thread that created the specified window and returns without waiting for the thread to process the message. To post a message in the message queue associate with a thread, use the PostThreadMessage function. Syntax BOOL PostMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ); Parameters hWnd [in] Handle to the window whose window procedure is to receive the message. The following values have special meanings. HWND_BROADCAST The message is posted to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows. The message is not posted to child windows. NULL The function behaves like a call to PostThreadMessage with the dwThreadId parameter set to the identifier of the current thread. Msg [in] Specifies the message to be posted. wParam [in] Specifies additional message-specific information. lParam [in] Specifies additional message-specific information. Return Value If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

CWnd::PostMessage Places a message in the window's message queue and then returns without waiting for the corresponding window to process the message. BOOL PostMessage( UINT message, WPARAM wParam = 0, LPARAM lParam = 0 ); Parameters message Specifies the message to be posted. wParam Specifies additional message information. The content of this parameter depends on the message being posted. lParam Specifies additional message information. The content of this parameter depends on the message being posted. Return Value Nonzero if the message is posted; otherwise 0. Remarks Messages in a message queue are retrieved by calls to the GetMessage or PeekMessage Windows function. The Windows PostMessage function can be used to access another application.

If the function fails. Syntax BOOL SendNotifyMessage( HWND hWnd. To get extended error information. and SendMessageCallback). the operation will fail. including disabled or invisible unowned windows. LPARAM lParam ). and pop-up windows. SendNotifyMessage. it does not wait for the window procedure to finish processing the message. you must do custom marshalling. To send other messages (those above WM_USER) to another process. The system only does marshalling for system messages (those in the range 0 to WM_USER). . UINT Msg. SendNotifyMessage passes the message to the window procedure and returns immediately. WPARAM wParam. overlapped windows. If the window was created by a different thread. Otherwise.SendNotifyMessage Function The SendNotifyMessage function sends the specified message to a window or windows. wParam [in] Specifies additional message-specific information. SendNotifyMessage calls the window procedure for the window and does not return until the window procedure has processed the message. its message parameters cannot include pointers. the message is sent to all top-level windows in the system. the return value is zero. but the message is not sent to child windows. Return Value If the function succeeds. The functions will return before the receiving thread has had a chance to process the message and the sender will free the memory before it is used. the return value is nonzero. Applications that need to communicate using HWND_BROADCAST should use the RegisterWindowMessage function to obtain a unique message for inter-application communication. Remarks If you send a message in the range below WM_USER to the asynchronous message functions (PostMessage. If this parameter is HWND_BROADCAST. Parameters hWnd [in] Handle to the window whose window procedure will receive the message. If the window was created by the calling thread. call GetLastError. lParam [in] Specifies additional message-specific information. Msg [in] Specifies the message to be sent.

. it does not wait for the window procedure to finish processing the message.CWnd::SendNotifyMessage Sends the specified message to the window. Parameters message Specifies the message to be sent. WPARAM wParam. BOOL SendNotifyMessage( UINT message. If the window was created by a different thread. Return Value Nonzero if the function is successful. lParam Specifies additional message-dependent information. otherwise 0. SendNotifyMessage calls the window procedure for the window and does not return until the window procedure has processed the message. LPARAM lParam ). Remarks If the window was created by the calling thread. SendNotifyMessage passes the message to the window procedure and returns immediately. wParam Specifies additional message-dependent information.

Sign up to vote on this title
UsefulNot useful