vc Windows自訂訊息實現詳解

來源:互聯網
上載者:User

VC自訂訊息實現有以下幾種方法:

一:重載PreTranslateMessage函數
如:
BOOL CTunnelsDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == BN_CLICKED)
{
m_strEdit = "響應成功!";
this->UpdateData(FALSE);
}

return CDialog::PreTranslateMessage(pMsg);
}

從視窗
.h檔案中加入
protected:
afx_msg void OnButton(UINT nID);

.cpp檔案中加入
BEGIN_MESSAGE_MAP(CVNSLView, CView)
//{{AFX_MSG_MAP(CVNSLView)
ON_WM_DESTROY()
ON_WM_CREATE()
//}}AFX_MSG_MAP
ON_COMMAND(IDC_BUTTON1, OnButton)
END_MESSAGE_MAP()

// IDC_BUTTON1就是從視窗上你要單擊的那個按鈕的ID

void CVNSLView::OnButton(UINT nID)
{
// 向父視窗發送按鈕單擊訊息
::PostMessage( this->GetParent()->GetSafeHwnd(), BN_CLICKED, nID, 0 );
}

 

二:重載虛函數DefWindowProc

首先定義一個全域的控制代碼
HWND GWnd_MydlgWND;//

其次在你需要響應的視窗下(class CTestDlg : public CDialog)聲明為外部參考
extern HWND GWnd_MydlgWND;

同時在該類下重載虛函數DefWindowProc ,用於接收訊息

LRESULT CTestDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
switch(message)
{
case 0x401:MessageBox("測試程式");
break;

}
return CDialog::DefWindowProc(message, wParam, lParam);
}

再次是在你想要獲得訊息的視窗初始化函數(BOOL CTestDlg::OnInitDialog())中添加

// TODO: Add extra initialization here
GWnd_MydlgWND = GetSafeHwnd();

準備完畢

然後在你想要發送訊息的地方發送
::PostMessage(GWnd_MydlgWND,0x401,0L,0L);//寄送訊息

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.