You are on page 1of 71

i Hc S Phm Tp.

H Ch Minh Khoa Ton Tin Hc

LP TRNH WINDOWS

K THUT LP TRNH VISUAL C++


(MFC Microsoft Foundation Classes)

Gii thiu v MFC, Visual C++ 6.0 Cc loi ng dng trong Visual C++ 6.0 X l chut, bn phm MessageBox
Trn Ngc Bo Email: tnbao.dhsp@gmail.com

NI DUNG
Gii thiu v MFC Microsoft Visual C++ 6.0 Cc loi ng dng trong windows Chng trnh MFC u tin X l chut v bn phm Message Box

NI DUNG
Gii thiu v MFC Microsoft Visual C++ 6.0 Cc loi ng dng trong windows Chng trnh MFC u tin X l chut v bn phm Message Box

Gii thiu v MFC (1/2)


MFC Microsoft Foundation Class MFC l mt th vin cc lp (class, OOP) trong ngn ng Visual C++, c s dng xy dng cc ng dng trn Windows MFC c xy dng trn c s cc hm th vin API ca Windows Trong mt ng dng MFC, ta s khng gi trc tip cc hm Windows API m s to cc object t nhng lp ca MFC, sau gi cc hm ca object ny a s cc hm ca lp MFC c cng tn vi nhng hm Windows API

Gii thiu v MFC (2/2)


MFC to ra mt Application Framework gip cho lp trnh vin thit k kin trc ca ng dng nht qun v khoa hc hn Thng qua cc class trong MFC, ngi lp trnh c th xy dng ng dng nhanh v t tn cng sc hn so vi vic s dng n thun cc hm th vin API ca Windows Trong MFC ta vn c th gi cc hm Windows API MFC c nhiu version 1, 2, 3,..6. Version 6.0 c khong 200 class

Mt s tnh nng ca MFC (1/4)


Version 1: (Visual C++ 1.0)
Cc lp List, Array, String, Time, Date, File access, Cc lp giao din c bn MDI, OLE 1.0

Version 2: (Visual C++ 1.0)


File open, save Print preview, printing Scrolling window, Spliter window Tool bar, status bar Access to Visual Basic controls Context-sensitive help An improved interface to OLE version 1.0 DLL

Mt s tnh nng ca MFC (2/4)


Version 2.5: (Visual C++ 1.5)
H tr kt ni ODBC (Open Database Connectivity) cho php truy xut n cc CSDL Access, FoxPro, SQL Server OLE 2.01

Version 3: (Visual C++ 1.51)


H tr Tab dialog (property sheet) Docking tool bar

Version 3.1: (Visual C++ 2.1)


H tr cc control chun ca Windows 95 ODBC Level 2 Cc lp Winsock phc v lp trnh TCP/IP

Mt s tnh nng ca MFC (3/4)


Version 4.0: (Visual C++ 4.0)
H tr kt ni DAO (Data Acess Object) Windows 95 docking control bar B sung thm Treeview v RichEdit Cc lp ng b ha tiu trnh OLE control container

Version 4.2: (Visual C++ 4.2)


Cc lp WinInet Cc lp ActiveX document server Cc tnh nng m rng ca ActiveX control Ci tin mt s tnh nng ca ODBC

Mt s tnh nng ca MFC (4/4)


Version 4.21: (Visual C++ 5.0)
Sa mt s li trong version 4.2 Redesigned IDE, Developer Studio 97 H tr lp trnh COM (Component Object Model)

Version 6.0: (Visual C++ 6.0)


H tr Cc lp cho nhng control chun trong ID 4.0 H tr Dynamic HTML, cho php to lp ng cc trang HTML Active Document Containment, cho php ng dng MFC c th cha cc Active document OLE DB v ADO

NI DUNG
Gii thiu v MFC Microsoft Visual C++ 6.0 Cc loi ng dng trong windows Chng trnh MFC u tin X l chut v bn phm Message Box

Cc thnh phn trong ng dng Visual C++


M ngun Ti nguyn

Ca s chnh ca MS Visual C++ 6.0

Ca s chnh ca MS Visual C++ 6.0

MS Visual C++ 6.0 Class View

MS Visual C++ 6.0 Resource View

MS Visual C++ 6.0 File View

Ca s Debugger trong MS Visual C++ 6.0

NI DUNG
Gii thiu v MFC Microsoft Visual C++ 6.0 Cc loi ng dng trong windows Chng trnh MFC u tin X l chut v bn phm Message Box

Cc loi ng dng (1/2)


Win32 Console: ng dng 32 bits, vi giao din dng DOS command line Win32 (SDK): ng dng 32 bits, ch s dng th vin SDK Win32 DLL: ng dng 32 bits, dng th vin lin kt ng (Dynamic Linked Library) s dng SDK Win32 LIB: ng dng 32 bits, dng th vin lin kt tnh (Static Linked Library) s dng SDK MFC EXE: ng dng 32 bits, s dng th vin MFC MFC DLL: ng dng 32 bits, dng th vin lin kt ng (Dynamic Linked Library) s dng MFC

Cc loi ng dng (2/2)

NI DUNG
Gii thiu v MFC Microsoft Visual C++ 6.0 Cc loi ng dng trong windows Chng trnh MFC u tin X l chut v bn phm Message Box

ng dng MFC u tin Hello.h


class CMyApp : public CWinApp { public: virtual BOOL InitInstance (); }; class CMainWindow : public CFrameWnd { public: CMainWindow (); protected: afx_msg void OnPaint (); DECLARE_MESSAGE_MAP () };

ng dng MFC u tin Hello.cpp


#include <afxwin.h> #include "Hello.h" CMyApp myApp; ///////////////////////////////////////////////////////////////////////// // CMyApp member functions BOOL CMyApp::InitInstance () { m_pMainWnd = new CMainWindow; m_pMainWnd->ShowWindow (m_nCmdShow); m_pMainWnd->UpdateWindow (); return TRUE; } /////////////////////////////////////////////////////////////////////////

ng dng MFC u tin Hello.cpp (tt)


// CMainWindow message map and member functions BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd) ON_WM_PAINT () END_MESSAGE_MAP () CMainWindow::CMainWindow () { Create (NULL, _T ("The Hello Application")); } void CMainWindow::OnPaint () { CPaintDC dc (this); CRect rect; GetClientRect (&rect); dc.DrawText (_T ("Hello, MFC"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); }

Kt qu chng trnh Hello

Cc class ca ng dng Hello


CObject CCmdTarget

CWinThread

CWnd

CWinApp

CFrameWnd

CMyApp

CMainWindow

Cc bc thc thi ng dng


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR WinMain int nCmdShow) { // TODO: Place code here. ng k lp ca s MSG msg;
To lp ca s

lpCmdLine,

MyRegisterClass(hInstance); Hin th ca s // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) Message Loop { return FALSE; } // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }

ng k lp ca s
WinMain
ng k lp ca s To lp ca s Hin th ca s Message Loop

CMyApp
BOOL CMyApp::InitInstance () { m_pMainWnd = new CMainWindow; m_pMainWnd->ShowWindow (m_nCmdShow); m_pMainWnd->UpdateWindow (); return TRUE; }

ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style wcex.lpfnWndProc wcex.cbClsExtra wcex.cbWndExtra wcex.hInstance wcex.hIcon wcex.hCursor wcex.hbrBackground wcex.lpszMenuName wcex.lpszClassName wcex.hIconSm return RegisterClassEx(&wcex); }

= CS_HREDRAW | CS_VREDRAW; = (WNDPROC)WndProc; = 0; = 0; = hInstance; = LoadIcon(hInstance, (LPCTSTR)IDI_BAI02); = LoadCursor(NULL, IDC_ARROW); = (HBRUSH)(COLOR_WINDOW+1); = (LPCSTR)IDC_BAI02; = szWindowClass; = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

To lp ca s
WinMain
ng k lp ca s To lp ca s Hin th ca s Message Loop

CMainWindow
CMainWindow::CMainWindow () { Create (NULL, _T ("The Hello Application")); }

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; }

Hm to lp ca s
CMainWindow
CMainWindow::CMainWindow () { Create (NULL, _T ("The Hello Application")); }

BOOL Create (LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle = WS_OVERLAPPEDWINDOW, const RECT& rect = rectDefault, CWnd* pParentWnd = NULL, LPCTSTR lpszMenuName = NULL, DWORD dwExStyle = 0, CCreateContext* pContext = NULL)

Cc tham s trong hm to lp ca s
Thuc tnh
lpszClassName dwStyle rect pParentWnd lpszMenuName dwExStyle pContext
Tn lp ca s Kiu dng ca s Kch thc v v tr ban u ca ca s Ca s cha, NULL ca s khng c ca s cha Thc n lin quan n ca s, NULL ngha lca s khng c Menu Kiu m rng ca ca s S dng trong ng dng Documents/Views

ngha

lpszWindowName Tiu ca s

Cc kiu dng chun ca ca s


Kiu
WS_BORDER WS_CAPTION WS_CHILD WS_OVERAPPEP WS_OVERAPPEPWI NDOW WS_DISABLED WS_DLGFRAME WS_MAXIMIZE WS_MAXIMIZBOX WS_HSCROLL WS_VSCROLL To ca s vi khung mng To ca s vi thanh tiu To ca s con, s dng to iu khin nh phm v cc ca s con MDI Lp ca s maximize v khng c ca s cha Lp ca s vi cc kiu: WS_OVERLAPPED, WS_CAPTION, WS_SYSMENU, WS_MAXIMIZEBOX, WS_MINIMIZEBOX, Ca s s b v hiu lc Ca s c vin nh dialog C s c Maximize Ca s c nt maximize trn thanh tiu Ca s c thanh trt ngang Ca s c thanh trt dc

ngha

Hin th ca s
WinMain
ng k lp ca s To lp ca s Hin th ca s Message Loop

CMainWindow
BOOL CMyApp::InitInstance () { m_pMainWnd = new CMainWindow; m_pMainWnd->ShowWindow (m_nCmdShow); m_pMainWnd->UpdateWindow (); return TRUE; }

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; }

ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd);


return TRUE; }

Vng lp v x l thng ip
while (GetMessage (&msg, NULL, 0, 0)) { ng k TranslateMessagelp ca s (&msg); To lp ca s DispatchMessage (&msg); } Hin th ca s
Message Loop LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_PAINT: hdc = BeginPaint (hwnd, &ps); Ellipse (hdc, 0, 0, 200, 100); EndPaint (hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage (0); return 0; } return DefWindowProc (hwnd, message, wParam, lParam); }

WinMain

CMainWindow

Tip nhn v x l thng ip nh th no ?

Message Map

Message Map nh x thng ip


nh x thng ip l bng tng quan gia thng ip v cc hm thnh phn qun l cc thng ip Bt k lp no suy din t CCmdTarget u c th cha nh x thng ip nh x thng ip c khai bo nh cc thnh phn lp khc trong khai bo lp
class CMainWindow : public CFrameWnd { public: CMainWindow (); protected: afx_msg void OnPaint (); DECLARE_MESSAGE_MAP () };

Message Map (tt)


Mi message s c x l bi 1 hm thnh phn tng ng
Nhng thng ip vi tin t WM_XYZ s c ci t tng ng vi hm theo qui tc
WM_ c chuyn thnh On XYZ c chuyn thnh ch thng tr k t u

V d:
WM_PAINT s c chuyn thnh OnPaint WM_MOUSEMOVE s c chuyn thnh OnMouseMove WM_LBUTTONDOWN s c chuyn thnh OnLButtonDown

Message Map (tt)


Khai bo Message Map
class CMainWindow : public CFrameWnd { public: CMainWindow (); protected: afx_msg void OnPaint (); afx_msg void OnLButtonDown(UINT, CPoint); DECLARE_MESSAGE_MAP () };

Ci t x l Message
BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd) ON_WM_PAINT () ON_WM_LBUTTONDOWN() END_MESSAGE_MAP () Void CMainWindow::OnPaint() { } Void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point) { }

Thng ip WM_XXX v hm MFC tng ng


Message WM_CHAR WM_CREATE WM_DESTROY WM_LBUTTONDOWN WM_LBUTTONUP WM_MOUSEMOVE WM_RBUTTONDOWN WM_RBUTTONUP WM_SIZE WM_PAINT Afx function
afx_msg void OnChar(UINT,UINT,UINT) afx_msg int OnCreate(LPCREATESTRUCT) afx_msg void OnDestroy() afx_msg void OnLButtonDown(UINT, CPoint) afx_msg void OnLButtonUp(UINT, CPoint) afx_msg void OnMouseMove(UINT, CPoint) afx_msg void OnRButtonDown(UINT, CPoint) afx_msg void OnRButtonUp(UINT, CPoint) afx_msg void OnSize() afx_msg void OnPaint()

X l thng ip ca s - Message Map

To ng dng MFC bng AppWizard


Chn menu File Chn Tab Projects Chn loi project MFC AppWizard (exe) t tn project trong Project name Xc nh ng dn th mc trong Location Step 1: Chn loi ng dng Single Document, b option Document/View architecture support Nhn Next n Step 4 of 6 New

To ng dng MFC bng AppWizard

Chn loi project MFC AppWizard (EXE)

To ng dng MFC bng AppWizard

Chn option Single document

To ng dng MFC bng AppWizard

Chn option None

To ng dng MFC bng AppWizard

To ng dng MFC bng AppWizard

To ng dng MFC bng AppWizard

To ng dng MFC bng AppWizard

ng dng MFC u tin File view

ng dng MFC u tin Resource view

ng dng MFC u tin Class view

ng dng MFC u tin kt qu chng trnh

NI DUNG
Gii thiu v MFC Microsoft Visual C++ 6.0 Cc loi ng dng trong windows Chng trnh MFC u tin X l chut v bn phm Message Box

X l thng ip chut
Message WM_LBUTTONDOWN WM_LBUTTONUP WM_LBUTTONBLCLK WM_RBUTTONDOWN WM_RBUTTONUP WM_RBUTTONBLCLK WM_MOUSEMOVE Afx function
afx_msg void OnLButtonDown(UINT, CPoint) afx_msg void OnLButtonUp(UINT, CPoint) afx_msg void OnLButtonDblClk(UINT, CPoint)

afx_msg void OnRButtonDown(UINT, CPoint) afx_msg void OnRButtonUp(UINT, CPoint) afx_msg void OnRButtonDblClk(UINT, CPoint) afx_msg void OnMouseMove(UINT, CPoint)

Message Map v ClassWizard

Hm x l thng ip chut tng ng

Hm x l thng ip chut tng ng

Hm x l thng ip chut tng ng

Hm x l thng ip chut tng ng


void CMouseView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CView::OnLButtonDown(nFlags, point); } void CMouseView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CView::OnLButtonUp(nFlags, point); } void CMouseView::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CView::OnMouseMove(nFlags, point); }

Hm x l thng ip chut tng ng


void CMouseView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default

m_nStartX = point.x; m_nStartY = point.y;


CView::OnLButtonDown(nFlags, point); } void CMouseView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default

CClientDC dc(this); dc.MoveTo(m_nStartX,m_nStartY); dc.LineTo(point.x,point.y);


CView::OnLButtonUp(nFlags, point); }

Kt qu x l thng ip chut

Hm x l thng ip chut tng ng


void CMouseView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default

m_nStartX = point.x; m_nStartY = point.y;


CView::OnLButtonDown(nFlags, point); } void CMouseView::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default

CClientDC dc(this); dc.MoveTo(m_nStartX,m_nStartY); dc.LineTo(point.x,point.y);


CView::OnLButtonUp(nFlags, point); }

Kt qu x l thng ip chut

NI DUNG
Gii thiu v MFC Microsoft Visual C++ 6.0 Cc loi ng dng trong windows Chng trnh MFC u tin X l chut v bn phm Message Box

Message Box - Hp thng bo


Hp thng bo c s dng thng bo, trao i gia ng dng vi ngi dng

to lp hp thng bo, ta s dng hm MessageBox


Cwnd::int MessageBox (LPCTSTR lpszText, LPCTSTR lpszCaption = NULL, UINT nType = MB_OK)

Cc kiu hp thng bo v gi tr tr v tng ng


Type MB_ABORTRETRYIGNORE MB_OK MB_OKCANCEL MB_RETRYCANCEL MB_YESNO MB_YESNOCANCEL Buttons Abort, Retry, Ignore OK OK, Cancel Retry, Cancel Yes, No Yes, No, Cancel Possible Return Codes IDABORT, IDRETRY, IDIGNORE IDOK IDOK, IDCANCEL IDRETRY, IDCANCEL IDYES, IDNO IDYES, IDNO, IDCANCEL

Message Box - Hp thng bo

to lp hp thng bo trn ta s dng hm MessageBox nh sau:


int nRes = MessageBox (The file Database Tip.doc is a read-only file. Are you sure you want to move it to the Recycle Bin ?, Confirm File Delete, MB_YESNO)
Chn Yes Chn No nRes =IDYES nRes = IDNO

Message Box - Hp thng bo

to lp hp thng bo trn ta s dng hm MessageBox nh sau:


int nRes = MessageBox (Do you want to save the changes to Document1 ?, Microsoft Office Word, MB_YESNOCANCEL)
Chn Yes Chn No nRes =IDYES nRes = IDNO nRes = IDCANCEL

Chn Cancel

Cc kiu biu tng trong hp thng bo MB_ICONQUESTION MB_ICONINFORMATION

MB_ICONSTOP

MB_ICONEXCLAMATION

Message Box - Hp thng bo

to lp hp thng bo vi Icon nh trn ta s dng hm MessageBox nh sau: int nRes = MessageBox (Do you want to continue ?, My Application, MB_YESNO |

MB_ICONQUESTION )

Message Box - Hp thng bo

to lp hp thng bo vi Icon nh trn ta s dng hm MessageBox nh sau: int nRes = MessageBox (Do you want to save the changes to Document1 ?, Microsoft Office Word, MB_YESNOCANCEL |

MB_ICONEXCLAMATION )

You might also like