[C++]LAB8-ON_BN_CLICKED+3button

 โปรแกรมที่เขียนต่อนี้จะเพิ่มเติมจากแลป7 คือ เพิ่มปุ่มเข้าไปและเพิ่มฟังค์ชั่นการalert MessageBox เหมือนแลป7 แต่มี3 ปุ่ม คือ yes no ok เมื่อคลิ๊กจะได้ผลMessageBoxตามลำดับคือ YES NO OK







  • เริ่มจากเขียนไฟล์ .h ก่อน

#include <afxwin.h>

#define IDC_MYBUTTON1 1001     //
#define IDC_MYBUTTON2 1002     // Create space memory for button 
#define IDC_MYBUTTON3 1003     //
class CMyApp : public CWinApp 
{
public:
virtual BOOL InitInstance();
};

class CWin : public CFrameWnd 
{
public:
CButton *myButton0,*myButton1,*myButton2;    //create button
CWin();
~CWin();
afx_msg void OnClick0(),OnClick1(),OnClick2();   //create function

DECLARE_MESSAGE_MAP();
};

  • หลังจากนั้นเขียนไฟล์ .cpp


#include "Lab8a.h"

CMyApp theApp;
BEGIN_MESSAGE_MAP(CWin, CFrameWnd)
ON_BN_CLICKED(IDC_MYBUTTON1,OnClick0) //
ON_BN_CLICKED(IDC_MYBUTTON2,OnClick1) //  import name sapace memmory + function 
ON_BN_CLICKED(IDC_MYBUTTON3,OnClick2) //
END_MESSAGE_MAP()

void CWin::OnClick0()
{
MessageBox("YES");
}
void CWin::OnClick1()
{
MessageBox("NO");
}
void CWin::OnClick2()
{
MessageBox("OK");
}

BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CWin();
m_pMainWnd->ShowWindow(m_nCmdShow);

m_pMainWnd->UpdateWindow();
return TRUE;
}

CWin::CWin()
{

Create(NULL,"Hello Visual c++, Easy MFC 01", // create window
WS_OVERLAPPEDWINDOW, CRect(100,100,480,300));

myButton0 = new CButton();      // create button
(*myButton0).Create("Yes", 
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
CRect(10,10,100,50),this,IDC_MYBUTTON1);

myButton1 = new CButton();      // create button
(*myButton1).Create("No", 
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
CRect(110,10,200,50),this,IDC_MYBUTTON2);

myButton2 = new CButton();      // create button
(*myButton2).Create("OK", 
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
CRect(210,10,300,50),this,IDC_MYBUTTON3);

}

CWin::~CWin()
{
delete myButton0; //
delete myButton1; // deleate button
delete myButton2; //
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

VISITOR