title: MFC about handle message
body: <p>I reference to <a href="http://www.codeproject.com/Articles/598/Windows-Message-Handling-Part-2?fid=789&amp;fr=11#xx0xx" rel="nofollow">about handle messsage</a>, and practice to impletement it</p>
  
  <p>I want to send user define message such as WM_MESSAGE, from about dialog use bottom click to send message (SendMessage) for main dialog recieve. 
  In main dialog(HandleMessageDlg.cpp and HandleMessage.h), I create button to show about dialog, 
  In about dialog(AboutDlg.cpp and AboutDlg.h), I ceate button to sent WM_MESSAGE</p>
  
  <p>Implement as follow:
  HandleMessageDlg.cpp<br></p>
  
  <pre><code>// HandleMessageDlg.cpp : 實作檔
  //
  
  #include "stdafx.h"
  #include "HandleMessage.h"
  #include "HandleMessageDlg.h"
  #include "AboutDlg.h"
  
  #ifdef _DEBUG
  #define new DEBUG_NEW
  #endif
  
  
  // 對 App About 使用 CAboutDlg 對話方塊
  
  class CAboutDlg : public CDialog
  {
  public:
      CAboutDlg();
  
  // 對話方塊資料
      enum { IDD = IDD_ABOUTBOX };
  
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支援
  
  // 程式碼實作
  protected:
      DECLARE_MESSAGE_MAP()
  public:
      afx_msg void OnBnClickedButton1();
  };
  
  CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  {
  }
  
  void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  {
      CDialog::DoDataExchange(pDX);
  }
  
  BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
      ON_BN_CLICKED(IDC_BUTTON1, &amp;CAboutDlg::OnBnClickedButton1)
  END_MESSAGE_MAP()
  
  
  // CHandleMessageDlg 對話方塊
  
  
  
  
  CHandleMessageDlg::CHandleMessageDlg(CWnd* pParent /*=NULL*/)
      : CDialog(CHandleMessageDlg::IDD, pParent)
  {
      m_hIcon = AfxGetApp()-&gt;LoadIcon(IDR_MAINFRAME);
  }
  
  void CHandleMessageDlg::DoDataExchange(CDataExchange* pDX)
  {
      CDialog::DoDataExchange(pDX);
  }
  
  BEGIN_MESSAGE_MAP(CHandleMessageDlg, CDialog)
      ON_WM_SYSCOMMAND()
      ON_WM_PAINT()
      ON_WM_QUERYDRAGICON()
      ON_MESSAGE (WM_MESSAGE, OnHandleMessage)
      //}}AFX_MSG_MAP
      ON_BN_CLICKED(IDC_BUTTON1, &amp;CHandleMessageDlg::OnBnClickedButton1)
  END_MESSAGE_MAP()
  
  
  // CHandleMessageDlg 訊息處理常式
  
  BOOL CHandleMessageDlg::OnInitDialog()
  {
      CDialog::OnInitDialog();
  
      // 將 [關於...] 功能表加入系統功能表。
  
      // IDM_ABOUTBOX 必須在系統命令範圍之中。
      ASSERT((IDM_ABOUTBOX &amp; 0xFFF0) == IDM_ABOUTBOX);
      ASSERT(IDM_ABOUTBOX &lt; 0xF000);
  
      CMenu* pSysMenu = GetSystemMenu(FALSE);
      if (pSysMenu != NULL)
      {
          CString strAboutMenu;
          strAboutMenu.LoadString(IDS_ABOUTBOX);
          if (!strAboutMenu.IsEmpty())
          {
              pSysMenu-&gt;AppendMenu(MF_SEPARATOR);
              pSysMenu-&gt;AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
          }
      }
  
      // 設定此對話方塊的圖示。當應用程式的主視窗不是對話方塊時，
      // 框架會自動從事此作業
      SetIcon(m_hIcon, TRUE);         // 設定大圖示
      SetIcon(m_hIcon, FALSE);        // 設定小圖示
  
      // TODO: 在此加入額外的初始設定
  
      return TRUE;  // 傳回 TRUE，除非您對控制項設定焦點
  }
  
  void CHandleMessageDlg::OnSysCommand(UINT nID, LPARAM lParam)
  {
      if ((nID &amp; 0xFFF0) == IDM_ABOUTBOX)
      {
          CAboutDlg dlgAbout;
          dlgAbout.DoModal();
      }
      else
      {
          CDialog::OnSysCommand(nID, lParam);
      }
  }
  
  // 如果將最小化按鈕加入您的對話方塊，您需要下列的程式碼，
  // 以便繪製圖示。對於使用文件/檢視模式的 MFC 應用程式，
  // 框架會自動完成此作業。
  
  void CHandleMessageDlg::OnPaint()
  {
      if (IsIconic())
      {
          CPaintDC dc(this); // 繪製的裝置內容
  
          SendMessage(WM_ICONERASEBKGND, reinterpret_cast&lt;WPARAM&gt;(dc.GetSafeHdc()), 0);
  
          // 將圖示置中於用戶端矩形
          int cxIcon = GetSystemMetrics(SM_CXICON);
          int cyIcon = GetSystemMetrics(SM_CYICON);
          CRect rect;
          GetClientRect(&amp;rect);
          int x = (rect.Width() - cxIcon + 1) / 2;
          int y = (rect.Height() - cyIcon + 1) / 2;
  
          // 描繪圖示
          dc.DrawIcon(x, y, m_hIcon);
      }
      else
      {
          CDialog::OnPaint();
      }
  }
  
  // 當使用者拖曳最小化視窗時，
  // 系統呼叫這個功能取得游標顯示。
  HCURSOR CHandleMessageDlg::OnQueryDragIcon()
  {
      return static_cast&lt;HCURSOR&gt;(m_hIcon);
  }
  
  LRESULT CHandleMessageDlg::OnHandleMessage(WPARAM wParam, LPARAM lParam) {
      TRACE(L"wParam %d, lParam %d", wParam, lParam);
      return 0;
  }
  
  void CAboutDlg::OnBnClickedButton1()
  {
      // TODO: 在此加入控制項告知處理常式程式碼
      GetParent()-&gt;SendMessage(WM_MESSAGE, 0, 0);
  }
  
  void CHandleMessageDlg::OnBnClickedButton1()
  {
      // TODO: 在此加入控制項告知處理常式程式碼
      CAboutDlg AboutDlg;
      AboutDlg.DoModal();
  }
  </code></pre>
  
  <p><a href="https://www.dropbox.com/s/car7htjsu7weybm/HandleMessage.cpp" rel="nofollow">HandleMessageDlg.cpp</a><br>
  HandleMessage.h<br></p>
  
  <pre><code>// HandleMessageDlg.h : 標頭檔
  //
  
  #pragma once
  
  // CHandleMessageDlg 對話方塊
  class CHandleMessageDlg : public CDialog
  {
  // 建構
  public:
      CHandleMessageDlg(CWnd* pParent = NULL);    // 標準建構函式
  
  // 對話方塊資料
      enum { IDD = IDD_HANDLEMESSAGE_DIALOG };
  
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支援
      afx_msg LRESULT OnHandleMessage(WPARAM wParam, LPARAM lParam);
  
  // 程式碼實作
  protected:
      HICON m_hIcon;
  
      // 產生的訊息對應函式
      virtual BOOL OnInitDialog();
      afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
      afx_msg void OnPaint();
      afx_msg HCURSOR OnQueryDragIcon();
  
      DECLARE_MESSAGE_MAP()
  public:
      afx_msg void OnBnClickedButton1();
  };
  </code></pre>
  
  <p><a href="https://www.dropbox.com/s/tqa0al1p44xqe90/HandleMessageDlg.h" rel="nofollow">HandleMessage.h</a><br>
  AboutDlg.cpp<br></p>
  
  <pre><code>// AboutDlg.cpp : 實作檔
  //
  
  #include "stdafx.h"
  #include "HandleMessage.h"
  #include "HandleMessageDlg.h"
  #include "AboutDlg.h"
  
  
  // AboutDlg 對話方塊
  
  IMPLEMENT_DYNAMIC(AboutDlg, CDialog)
  
  AboutDlg::AboutDlg(CWnd* pParent /*=NULL*/)
      : CDialog(AboutDlg::IDD, pParent)
  {
  
  }
  
  AboutDlg::~AboutDlg()
  {
  }
  
  void AboutDlg::DoDataExchange(CDataExchange* pDX)
  {
      CDialog::DoDataExchange(pDX);
  }
  
  
  BEGIN_MESSAGE_MAP(AboutDlg, CDialog)    
  END_MESSAGE_MAP()
  
  // AboutDlg 訊息處理常式
  </code></pre>
  
  <p><a href="https://www.dropbox.com/s/vg515u2fufgsspe/AboutDlg.cpp" rel="nofollow">AboutDlg.cpp</a><br>
  AboutDlg.h<br></p>
  
  <pre><code>#pragma once
  
  #define  WM_MESSAGE WM_USER+0x100
  
  // AboutDlg 對話方塊
  
  class AboutDlg : public CDialog
  {
      DECLARE_DYNAMIC(AboutDlg)
  
  public:
      AboutDlg(CWnd* pParent = NULL);   // 標準建構函式
      virtual ~AboutDlg();
  
  // 對話方塊資料
      enum { IDD = IDD_ABOUTBOX };
  
  
  
  protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支援
  
      DECLARE_MESSAGE_MAP()
  };
  </code></pre>
  
  <p><a href="https://www.dropbox.com/s/udlh491rjsln8gb/AboutDlg.h" rel="nofollow">AboutDlg.h</a><br>
  Now I have problem with </p>
  
  <p>what parameter is relation to WM_MESSAGE, is wParam or lParam, may i 
     if(lParam == WM_MESSAGE) or if(wParam == WM_MESSAGE)</p>
  
  <p>thanks </p>