程式語言 - Visual C++ 6.0 - Microsoft Fundation Class (MFC) - Single Document Interface (SDI) - Handle Event



main.cpp

#include <afxwin.h>

#define ID_TEST 100

class CMyFrame : public CFrameWnd {
public:
    CMyFrame();
    DECLARE_MESSAGE_MAP()

protected:
    afx_msg void OnTest();

private:
    CButton m_Button;
};

BEGIN_MESSAGE_MAP(CMyFrame, CFrameWnd)
    ON_COMMAND(ID_TEST, OnTest)
END_MESSAGE_MAP()

void CMyFrame::OnTest() {
    AfxMessageBox("Test");
}

CMyFrame::CMyFrame() {
    Create(NULL, "main", WS_VISIBLE, CRect(0, 0, 320, 240));
    m_Button.Create("Test", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(10, 10, 60, 40), this, ID_TEST);
}
   
class CMyApp : public CWinApp {
public:
    BOOL InitInstance();
};
   
CMyApp myApp;
    
BOOL CMyApp::InitInstance() {
    CMyFrame *pFrame = new CMyFrame;

    m_pMainWnd = pFrame;
    pFrame->ShowWindow(SW_SHOW);
    pFrame->UpdateWindow();

    return TRUE;
}

編譯、執行

$ myx86 nmake -f main.mak

視窗


按下按鈕後