¸Þ½ÃÁö ·çÇÁ¿¡CMessageLoopŬ·¡½º¸¦ »ç¿ëÇϸé CMessageFilterŬ·¡½º¸¦ »ç¿ëÇØ ¸Þ¼¼Áö ÇÊÅ͸¦ Ãß°¡ÇÒ ¼ö°¡ ÀÖ½À´Ï´Ù. ¸Þ¼¼Áö ÇÊÅ͸¦ Ãß°¡Çϸé CMessageLoopŬ·¡½º³»ÀÇ ¸Þ½ÃÁö ·çÇÁ·Î ::TranslateMessage()°¡ ºÒ·Á °¡±â Àü¿¡ ¹«¾ùÀΰ¡ ó¸®¸¦ ½Ãų ¼ö°¡ ÀÖ½À´Ï´Ù.

¿©±â¿¡¼­´Â  ÀÌCMessageFilterŬ·¡½º¸¦ »ç¿ëÇØ  ÀüȸÀÇ ¸Þ½ÃÁö ·çÇÁ¸¦ º¯°æÇÑ ¡¸Hello, ATL/WTL¡¹ÇÁ·Î±×·¥¿¡ ¸Þ¼¼Áö ÇÊÅ͸¦ Ãß°¡ÇÕ´Ï´Ù. º¯°æÇÏ´Â °ÍÀºCMyWindowŬ·¡½º¸¸ÀÔ´Ï´Ù.

// stdafx.h¾È

#include <atlbase.h>

#include <atlapp.h>

extern CAppModule _Module;  // CComModuleÀ¸·ÎºÎÅÍCAppModule¿¡ ¿Å°Ü³õ´Â

#include <atlwin.h>

 

#include <atlcrack.h> // WTL¸Þ¼¼Áö Å©·¡Ä¿¸¦ »ç¿ëÇϱâ (À§ÇØ)¶§¹®¿¡

                      

 

// MainWindow.hµ¿¾È

// base class¿¡CMessageFilter¸¦ Ãß°¡

class CMyWindow : public CWindowImpl<CMyWindow>, public CMessageFilter

{

public:

    // À©µµ¿ì Ŭ·¡½º¸íÀ» µî·Ï

    DECLARE_WND_CLASS(_T("Hello"));

 

private:

    // ¸Þ¼¼Áö ÇÊÅÍ Ã³¸®

    virtual BOOL PreTranslateMessage(MSG* pMsg){

        return FALSE;

    }

 

    // ¸Þ¼¼Áö ¸Ê

    BEGIN_MSG_MAP_EX(CMyWindow)

        MSG_WM_PAINT(OnPaint)

        MSG_WM_CREATE(OnCreate)

        MSG_WM_DESTROY(OnDestroy)

    END_MSG_MAP()

 

    void OnPaint(HDC /*hDC*/){

        PAINTSTRUCT ps;

        HDC hDC = BeginPaint(&ps);

        RECT rect;

        GetClientRect(&rect);

        DrawText(hDC, _T("Hello, ATL/WTL"),

            -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

        EndPaint(&ps);

    }

 

    LRESULT OnCreate(LPCREATESTRUCT lpcs){

        // ¸Þ½ÃÁö ·çÇÁ¿¡ ¸Þ¼¼Áö ÇÊÅ͸¦ Ãß°¡

        CMessageLoop* pLoop = _Module.GetMessageLoop();

        pLoop->AddMessageFilter(this);

        return 0;

    }

 

    void OnDestroy(){

        PostQuitMessage(0);

    }

};

                      

 

// hello.cpp³»

#include "stdafx.h"

 

#include "MainWindow.h"

 

CAppModule _Module;  // CComModule·ÎºÎÅÍCAppModule¿¡ ¿Å°Ü³õ´Â

 

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR lpCmdLine, int nCmdShow)

{

    _Module.Init(NULL, hInstance);

 

    CMessageLoop theLoop;

    _Module.AddMessageLoop(&theLoop);

 

    // µ¶ÀÚ À©µµ¿ì¸¦ ÀÛ¼º

    CMyWindow wnd;

    wnd.Create(NULL, CWindow::rcDefault,

        _T("Hello, ATL/WTL"), WS_OVERLAPPEDWINDOW | WS_VISIBLE);

 

    int nRet = theLoop.Run();

 

    _Module.RemoveMessageLoop();

 

    _Module.Term();

 

    return nRet;

}

                      


¡¡CMessageFilterŬ·¡½º´Â  ¸ÞÀÎ À©µµ¿ìÀÎCMyWindowŬ·¡½ºÀÇ base class·Î¼­ »ç¿ëÇϰí ÀÖ½À´Ï´Ù. CMessageFilterŬ·¡½º¿¡´ÂPreTranslateMessage()À̶ó°í ÇÏ´Â ¼ø¼ö °¡»ó ÇÔ¼ö°¡ ÇÑ °³ ÀÖÀ» »ÓÀÔ´Ï´Ù.

// atlapp.h¾È

class CMessageFilter

{

public:

    virtual BOOL PreTranslateMessage(MSG* pMsg) = 0;

};

                      


CMessageFilterŬ·¡½ºÀÇ »ó¼Ó Ŭ·¡½ºÀÎCMyWindowŬ·¡½º ¿¡¼­´Â  ÀÌPreTranslateMessage()¸¦ ±¸ÇöÇÕ´Ï´Ù. À̹ø ¿¹¿¡¼­´ÂFALSEÀ» µ¹·ÁÁÖ°í ÀÖÀ» »ÓÀÔ´Ï´Ù¸¸  À̰ÍÀº ±»ÀÌ ÇÏÁö ¾Ê°í Åë»óÀÇ Ã³¸®¸¦ °è¼ÓÇÑ´Ù°í ÇÏ´Â °ÍÀ» ÀǹÌÇÕ´Ï´Ù.

¡¡CMyWindowŬ·¡½º¿¡¼­´ÂWM_CREATE¸Þ¼¼Áö Çڵ鷯¸¦ Ãß°¡ÇØ  °Å±â¼­AddMessageFilter()¸¦ È£ÃâÇÏ´Â °Í¿¡ ÀÇÇØ  ¸Þ½ÃÁö ·çÇÁ¿¡ ÀÌ À©µµ¿ì¿ëÀÇ ¸Þ¼¼Áö ÇÊÅ͸¦ Ãß°¡Çϰí ÀÖ½À´Ï´Ù. À̰Ϳ¡ ÀÇÇØ CMessageLoopŬ·¡½º³»ÀÇ ¸Þ½ÃÁö ·çÇÁ·Î ::TranslateMessage()°¡ ºÒ·Á °¡±â Àü¿¡CMyWindowŬ·¡½º¿¡¼­ ±¸ÇöµÇ¾ú´ø PreTranslateMessage()°¡ ºÒ·Á °¡°Ô µË´Ï´Ù.