โปรแกรมที่เขียนวันนี้เป็นโปรแกรมหน้ากากโทรศัพท์ ซึ่งจะเขียนด้วยภาษา C++ ส่วนประกอบที่เราจะเขียนมีอยุ่2ส่วนด้วยกันคือ ไฟล์ .h และ ไฟล์ .cpp
- เริ่มจากเขียน ไฟล์ .h ขึ้นมา ผมใช้ชื่อว่า phone.h
#include <afxwin.h>
#define IDC_MYBUTTON 1001 // (1) define
class CMyApp : public CWinApp // create application
{
public:
virtual BOOL InitInstance();
};
class CMyFrame : public CFrameWnd //create window
{
public:
CMyFrame();
~CMyFrame();
CButton *pmyButton[12]; // (2) declare button control
CEdit *pmyEdit;
};
เขียนไฟล์ .cpp ผมตั้งชื่อว่า phone.cpp
#include <afxwin.h>
#include "phone.h"
CMyApp theApp; // create application object
BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMyFrame(); // create windows frame object and link to application
m_pMainWnd->ShowWindow(m_nCmdShow); //show frame windows
m_pMainWnd->UpdateWindow(); // update frame windows
return TRUE;
}
const int ButtonNum = 12;
const int ButtonCall = 2;
char* name[] = {"1", "2","3","4","5","6","7","8","9","*","0","#"};
char* name2[] = {"Call", "Cancel"};
CMyFrame::CMyFrame()
{
Create(NULL,"SAMSUNG",
WS_OVERLAPPEDWINDOW, CRect(100,100,340,500));
pmyEdit = new CEdit; // (3) memory allocation
pmyEdit->Create(
WS_VISIBLE | WS_CHILD | WS_BORDER,
CRect(10,10,210,110),
this,
IDC_MYBUTTON );
for(int i = 0; i<ButtonCall ; i++) // (4) create control
{
pmyButton[i] = new CButton(); // (3) memory allocation
pmyButton[i]->Create(name2[i],
WS_VISIBLE | WS_BORDER,
CRect(((i*70)+10+(i*60)),120,(((i+1)*80)+(i*50)),170),
this,
IDC_MYBUTTON +i+1 );
}
for(int i = 0; i<ButtonNum; i++) // (4) create control
{ if(i<=2){
pmyButton[i] = new CButton(); // (3) memory allocation
pmyButton[i]->Create(name[i],
WS_VISIBLE | WS_BORDER ,
CRect((i*70)+10,180,(i+1)*70,210),
this,
IDC_MYBUTTON +i );
}
else
if(i>2 & i<=5){
pmyButton[i] = new CButton(); // (3) memory allocation
pmyButton[i]->Create(name[i],
WS_VISIBLE | WS_BORDER ,
CRect(((i-3)*70)+10,220,((i-3)+1)*70,250),
this,
IDC_MYBUTTON +i );
}
else
if(i>5 & i<=8){
pmyButton[i] = new CButton(); // (3) memory allocation
pmyButton[i]->Create(name[i],
WS_VISIBLE | WS_BORDER ,
CRect(((i-6)*70)+10,260,((i-6)+1)*70,290),
this,
IDC_MYBUTTON +i );
}
else
{
pmyButton[i] = new CButton(); // (3) memory allocation
pmyButton[i]->Create(name[i],
WS_VISIBLE | WS_BORDER ,
CRect(((i-9)*70)+10,300,((i-9)+1)*70,330),
this,
IDC_MYBUTTON +i );
}
}
}
CMyFrame::~CMyFrame()
{
delete pmyEdit;
for(int i = 0; i<ButtonNum; i++)
{
delete pmyButton[i];
}
}
หลังจากนั้น เริ่มรันไฟล์จะได้ผลดังภาพ
ไม่มีความคิดเห็น:
แสดงความคิดเห็น