[C++]LAB9-ON_BN_CLICKED+3button+textEdit


   โปรแกรมในlab นี้จะเป็นโปรแกรมอย่างง่ายที่ มีกรอบtext edit และมีปุ่ม clear,copy,paste,exit
-เมื่อทำการกดปุ่ม clear จะลบข้อความที่อยู่ในกรอบtext edit ออกทั้งหมด 
-เมื่อกดปุ่มcopy จะคัดลอกข้อความที่อยุ่ในกรอบtext edit ทั้งหมด
-เมื่อกดปุ่มpaste จะวางข้อความลงไปในกรอบtext edit
-เมื่อกดปุ่มexit จะมี MessageBox เด้งขึ้นมาแล้วแสดงข้อความว่า "Do you want to quit!!\n Yes or No?"และแสดงปุ่ม yes และ no ให้กด ถ้ากด yes จะออกจากโปรแกรม ถ้าno ไม่ออกจากโปรแกรม


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



#include <afxwin.h>
#define IDC_PAD 100    //
#define IDC_CLSBUTTON 101  //
#define IDC_COPYBUTTON 102  // create name for memory space 
#define IDC_PASTEBUTTON 103  //
#define IDC_EXITBUTTON 104  //

class CApp:public CWinApp{
public: virtual BOOL InitInstance();
};

class CWin:public CFrameWnd{

int response;
CEdit *EditPad;             // create Edit
CButton *CLSButton,*CopyButton,*PasteButton,*ExitButton;  // create button

public: CWin();
  ~CWin();
afx_msg void onExit(),onCopy(),onPaste(),onCLS();    // create function alert MessageBox on click

DECLARE_MESSAGE_MAP();  //DECLARE massage
};

  • เขียนไฟล์ .cpp 

#include "lab9a.h"

CApp app;

BEGIN_MESSAGE_MAP(CWin, CFrameWnd)   //  
ON_BN_CLICKED(IDC_EXITBUTTON, onExit)  //
ON_BN_CLICKED(IDC_COPYBUTTON, onCopy)  // import name space memory + function
ON_BN_CLICKED(IDC_PASTEBUTTON, onPaste)  //
ON_BN_CLICKED(IDC_CLSBUTTON, onCLS)   //
END_MESSAGE_MAP();

void CWin::onExit(){
response=MessageBox("Do you want to quit!!\n Yes or No?",
"Lab9: Little note pad", MB_ICONQUESTION|MB_YESNO);
if (response==IDYES) DestroyWindow();
}

void CWin::onCopy(){
EditPad->SetSel(0,-1,TRUE);  //เลือกครอบทุกข้อความ
EditPad->Copy();
EditPad->SetFocus();   //ทำไฮไลต์ให้ตรงตัวอักษร
}

void CWin::onCLS(){
EditPad->SetSel(0,-1,TRUE);
EditPad->Clear();
EditPad->SetFocus();
}

void CWin::onPaste(){
EditPad->SetSel(0,-1,TRUE);
EditPad->Paste();
EditPad->SetFocus();
}

BOOL CApp::InitInstance(){
m_pMainWnd=new CWin();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
CWin::CWin(){
Create(NULL,"Little note pad",          //สร้างหน้าต่าง
WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX, CRect(0,0,290,245));  //

EditPad=new CEdit();   //
CLSButton=new CButton();  //
CopyButton=new CButton();  // สร้างอ็อปเจ็ค 
PasteButton=new CButton();  //
ExitButton=new CButton();  //

EditPad->Create                     //
(WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP| \ES_AUTOVSCROLL|ES_AUTOHSCROLL|ES_MULTILINE,   //
CRect(10,10,270,140), this, IDC_PAD);               //
                        //
CLSButton->Create("Clear",WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP,        //
CRect(10,145,90,170), this, IDC_CLSBUTTON);              //
                        //
CopyButton->Create("Copy",WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP,        // เอาอ็อปเจ็คมาสร้างปุ่มลงไปบนหน้าต่าง 
CRect(100,145,180,170), this, IDC_COPYBUTTON);             //
                        //
PasteButton->Create("Paste",WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP,       //
CRect(190,145,270,170), this, IDC_PASTEBUTTON);             //
                        //
ExitButton->Create("Exit",WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP,        //
CRect(10,175,270,200), this, IDC_EXITBUTTON);             //
}

CWin::~CWin(){
delete EditPad;  //
delete CLSButton; //
delete CopyButton; // ลบอ็อปเจ็คที่จองไว้ในหน่วยความจำ
delete PasteButton; //
delete ExitButton; //

}

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

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

VISITOR