Tài liệu này dành cho sinh viên, giáo viên khối ngành công nghệ thông tin tham khảo và có những bài học bổ ích hơn, bổ trợ cho việc tìm kiếm tài liệu, giáo án, giáo trình, bài giảng các môn học khối ngành công nghệ thông tin
Trang 6± Windows, title bar, toolbar, menu bar, status bar
± Thanh ዠ ¯ዓ: File, Edit, Tool, Helpǥ
± ዒ : ዛዕ ዜ các ¯ chung: Edit
Control, Button Control, CheckboxǥǤ
6
ኼ¿
Trang 8д ЦН¿-driven
8
ኼ¿
Trang 99
دЪȋȌ
ዉ ዎ (Windows) ghi ዑ ዠ ዉ ra
(event) trong ዒ thông ¯ዉ (message) và ¯
trong hàng ¯ዘ thông ¯ዉ (messages queue)
Trang 1313
Windows Data Types (1)
0ዋ Â ዕ ዝ khóa typedef trong các ኼ tin (windef.h;
winbase.h, winuser.h ¯ዛዘ include trong windows.h).
Ví ዙ:
± typedef unsigned int UINT;
Trang 14LONG long 32-bit.
BOOL .LʀX logic True/False
Trang 18Microsoft style (4) - Calling convention
0ዋÂዌዖ
#define CALLBACK stdcall
#define WINAPI stdcall
#define APIENTRY WINAPI
#define APIPRIVATE stdcall
#define PASCAL stdcall
Thêm vào ዛዔ tên hàm ¯ ዊ ra cho
compiler ኾ hàm này có cách ዐ ዜ ዙ
ዙ cho ዉ ዌ ¯ዅ ዛ ዅ nào.
18
ኼ¿
Trang 20± Tell Windows what kind of window the program requires
± Create the program window
± Initialize the program window
± Retrieve Windows messages intended for the program
WinProc(): ዞ lý các thông ¯ዉ liên quan ¯ዅ ዞ
ዐ ዚ nó
20
ኼ¿
Trang 2121
1.2.1 Hàm WinMain()
Hàm chính ዚ ዜ ዙ trên Windows
Program entry point
hInstance, is a handle to the instance of the program you
are writing.
hPrevInstance, is used if your program had any previous
instance If not, this argument can be ignored.
szCmdLine, is a string that represents all items used on the
command line to compile the application.
nCmdShow, controls how the window you are building will
Trang 22Hàm WinMain (cont.)
Tìm hàm WinMain ኻ:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
wndclass.hInstance = hInstance; // ç͔QK danh ͲQJ GͮQJ
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCusor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL; // Không có menu
22
Trang 23CW_USEDEFAULT, // &KL͊X U͢QJ
CW_USEDEFAULT, // &KL͊X dài
NULL, // &ͶD V͞ cha
NULL, // Không có menu
Trang 25hIcon ĈʈQKGDQKFͧDELʀXWɉͣQJ Dùng hàm LoadIcon
hCursor ĈʈQKGDQKFͧDFRQWU͏FKX͙W Dùng hàm LoadCursor
WɪSWLQWjLQJX\rQ
lpszClassName 7rQO͛S
25
Trang 27int nWidth, // ç͢ U͢QJ ban ÿ̰X
int nHeight, // ç͢ cao ban ÿ̰X
HWND hWndParent, // ç͔QK danh FͰD cͶa V͞ cha
MENU hMenu, // ç͔QK dang FͰD thͺc ÿ˱Q
INSTANCE hInstance, // ç͔QK danh WK͌ KL͐Q Ͳng GͮQJ
PVOID lpParam // Các tham V͚ ban ÿ̰X
) ;
27
ኼ¿
Trang 28Ш Ь п б
0 ኸ ዉ ዞ ዐ ዌ hàm ShowWindow
BOOL ShowWindow(HWND hWnd, // ç͔QK danh cͰD cͶD V͞ F̰Q WK͌ KL͐Q
int iCmdShow // 7U̪QJ thái KL͌Q WK͔
Trang 29 Giao ዅ ዔ ዉ ዎ (Windows), toàn ዒ các
thông¯ዉ ዞ ዔ ¯ዛዘ ዞ lý qua hàm này
ዛዕ khai báo ዛ sau:
Trong ¯× tham ዎ ¯ኹ tiên là ¯ዋ danh ዚ ዞ ዐ,
thamዎ ዜ 2 là ¯ዋ danh thông ¯ዉ, và ዎ cùng
tin kèm theo thông ¯ዉ
LRESULT CALLBACK WinProc (HWND, UINT, WPARAM, LPARAM);
29
ኼ¿
Trang 30MSG msg; // Windows message structure
Trang 3131
Message Loop
Trong ¯× msg là ዒ ዅ ኸ trúc MSG ¯ዛዘ
¯ዋ Â trong winuser.h
typedef struct tagMSG {
POINT là ዌ ¯ዒ ¯, ¯ዋ Â trong windef.h
typedef struct tagPOINT {
LONG x;
LONG y;
Trang 34 Tìm ዒ hàm ዞ lý ዞ ዐ:
LRESULT CALLBACK WinProc (HWND hwnd, UINT msg, WPARAM
wParam, LPARAM lParam)
Trang 3535
Thông ዛዕ ዊ ¯ ዞ lý các thông ¯ዉ ኹ ዅ, còn
giao cho hàm ዞ lý ¯ዋDefWindowProc
DrawText(hdc, "Hello" , -1, &rect, DT_SINGLELINE| DT_CENTER| DT_VCENTER);
EndPaint(hwnd, &ps);
return 0;
case WM_SIZE:
/*9L͈W ÿR̪Q mã khi kích th˳ͤF cͶD V͞ thay ÿ͞L*/
Trang 36×О
36
ኼ¿
Trang 38Win32 Application in Visual C++ Developer Studio
38
ኼ¿
Trang 39LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR
szCmdLine, int iCmdShow)
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
Trang 40HelloWorld (2)
hwnd = CreateWindow(szAppName, // Tên OͤS FͶD V͞
TEXT ( " The Hello World Program" ), //
Trang 41DrawText(hdc, TEXT( "Hello World" ), - 1 , &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
Trang 42HelloWorld (run)
42
ኼ¿
Trang 441.3 Microsoft Foundation Classes (MFC)
MFC = Microsoft Foundation Classes
Trang 45± Cho phép phát ዜ ዙ Win32 platforms.
± Program low-level, tiêu ዎ ዕ gian
Trang 4848
ኼ¿
Trang 5353
The Solution
Solution Explorer: allows you to view and
navigate all the files that make up your
application.
Class View allows you to navigate and
manipulate your source code on a C++ class
level.
Resource View: allows you to find and edit
each of the various resources in your
application, including dialog window
designs, icons, and menus.
53
ኼ¿
Trang 54Output Pane & Editor Area
Output Pane:
± Cung ኸ thông tin cho ዛዕ ኼ trình
± Compiler progress statements
± Warnings, error messages
± Thông tin debugger:
Trang 5555
Menu Bar + Rearranging IDE
Menu bar + Tool bars
Trang 56 КнкШеΪΪ
56
Trang 60± Code view, design view
± Compile, debug, run
60
ኼ¿
Trang 64Add code to greeting (1)
DzdzǣȀ
ዌ ዠ ዉ, tên hàm ዞ lý
64
ኼ¿
Trang 66Run without Debug (Ctrl+F5)
Set Breakpoint (F9) and Run Debug
Trang 72¿пýدЪ
72
ኼ¿
Trang 80Џк Ȁ
80
Trang 82Й trúc Document/View З SDI
82
ኼ¿
Trang 86е
ዒዎዛዓዜ ዌǣ
virtual BOOL OnNewDocument();
virtual BOOL OnOpenDocument(LPCTSTR
lpszPathName);
void SetModifiedFlag(BOOL bModified =
ȌǢȋ0ኸǣ ȀȌ
BOOL IsModified();
virtual void OnCloseDocument();
virtual BOOL OnSaveDocument(LPCTSTR
lpszPathName);
CDocument::DeleteContents
86
ኼ¿
Trang 92± Select MFC standard as the project style and Windows
Native/Default as the Visual style and colors option
± Keep the Use Unicode libraries option.
± ƤƤȗǤ
ƤǤ
± Change the Generated Classes set of options at their
default settings so that the base class for the
CSketcherView class is CView.
92
ኼ¿
Trang 9595
Menu bar: bao ዏ drop-down menu và
Trang 98Add Handler for menu messages
98
ኼ¿
Trang 9999
п lý Ъ л Menu item
ዠ trên message WM_COMMAND
0ዋ Â message map
Trang 100 Thao tác trên Menu:
± DeleteMenu: ዒ¯ዎዛዘ²
± TrackPopupMenu: ዋ
100
ኼ¿
Trang 104З Н menu cho dialog
ዒ tài nguyên menu cho ዜ ዙ
ዌ menu ኹ ኽ vào dialog trong ዒ
Trang 112пýtrong hàm OnCreate Ȃ Эͳ
112
ኼ¿
Trang 114Toolbar Style
114
ኼ¿
Trang 116Toolbar docking style
116
ኼ¿
Trang 117 ዅ không có menu item ዛዓ ዜ, thêm ዅ hàm ዞ lý
thông ¯ዉ (vào MESSAGE_MAP) Ví ዙ ID_BUTTON_BOLD
117
Trang 120Àкпlý Ш ȋͳȌ
120
ኼ¿
Trang 122Àкпlý ШȋͳȌ
122
ኼ¿
Trang 124Enable/Disable button
124
ኼ¿
Trang 126126
ኼ¿
Trang 127127
127
ኼ¿
Trang 136CPoint MoveTo(int x, int y);
// Move to position x,y
CPoint MoveTo(POINT aPoint);
// Move to position defined by aPoint
Trang 137BOOL LineTo(int x, int y);
// Draw a line to position x,y
BOOL LineTo(POINT aPoint);
// Draw a line to position defined by aPoint
CDC *pDC;
pDC->MoveTo(50,50); // Set the current position
pDC->LineTo(50,200); // Draw a vertical line down 150 units
pDC->LineTo(150,200); // Draw a horizontal line right 100 units
pDC->LineTo(150,50); // Draw a vertical line up 150 units
pDC->LineTo(50,50); // Draw a horizontal line left 100 units
Trang 138BOOL Ellipse(int x1, int y1, int x2, int y2);
BOOL Ellipse(LPCRECT lpRect);
BOOL Arc(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);
BOOL Arc(LPCRECT lpRect, POINT startPt, POINT endPt);
CDC *pDC;
pDC->Ellipse(50,50,150,150); // Draw the 1st (large) circle
ȀȀƤ ʹȋȌ
CRect rect(250,50,300,100);
CPoint start(275,100); // Arc start point
CPoint end(250,75); // Arc end point
pDC->Arc(&rect, start, end); // Draw the second circle
Trang 139LPRECT lpRect, UINT nFormat );
int DrawText(
const CString& str, LPRECT lpRect, UINT nFormat );
Trang 142int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0,
const DWORD* lpStyle = NULL );
Trang 144// Declare a pen object and initialize it as
// a red solid pen drawing a line 2 pixels wide
CPen aPen;
aPen.CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
CPen* pOldPen = pDC->SelectObject(&aPen); // Select aPen as the pen
pDC->Ellipse(50,50,150,150); // Draw the 1st (large) circle
// Define the bounding rectangle for the 2nd (smaller) circle
CRect rect(250,50,300,100);
CPoint start(275,100); // Arc start point
CPoint end(250,75); // Arc end point
pDC->Arc(&rect,start, end); // Draw the second circle
pDC->SelectObject(pOldPen); // Restore the old pen
Trang 145int nIndex, COLORREF crColor );
explicit CBrush(
CBitmap* pBitmap );
Trang 160afx_msg void OnMsgName(UINT nChar, UINT nRepCnt, UINT nFlags)
void CSketcherView::OnKeyDown(UINT nChar, UINT
nRepCnt, UINT nFlags)
Trang 162162
ኼ¿
Trang 171CPenDialog aDlg; // Create a local dialog object
aDlg.DoModal(); // Display the dialog as modal
}
Trang 177LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL );
Trang 184± virtual int DoModal()
± COLORREF GetColor() const
± static COLORREF* PASCAL GetSavedCustomColors
± protected virtual BOOL OnColorOK()
± void SetCurrentColor( COLORREF clr )
184
ኼ¿
Trang 186CFontDialog
Ȁ ǣ
± CFontDialog( const CHARFORMAT& charformat,
DWORD dwFlags = CF_SCREENFONTS, CDC* pdcPrinter = NULL,
CWnd* pParentWnd = NULL );
186
ኼ¿
Trang 187187
CFontDialog
Parameters:
± lplfInitial: A pointer to a LOGFONTdata structure that allows you to
set some of the font's characteristics.
± charFormat: A pointer to a CHARFORMATdata structure that
allows you to set some of the font's characteristics in a rich edit
control.
± dwFlagsSpecifies one or more choose-font flags One or more
preset values can be combined using the bitwise OR operator If you
modify the m_cf.Flags structure member, be sure to use a bitwise
OR operator in your changes to keep the default behavior intact
For details on each of these flags, see the description of the
CHOOSEFONTstructure in the Platform SDK.
± pdcPrinter: A pointer to a printer-device context If supplied, this
parameter points to a printer-device context for the printer on
which the fonts are to be selected.
± pParentWnd: A pointer to the font dialog box's parent or owner
window.
187
ኼ¿
Trang 203 ȀȀ0ዋዒኼ
);
Trang 204± void SetModify(BOOL bModified = TRUE);
± BOOL SetReadOnly( BOOL bReadOnly = TRUE );
± void SetPasswordChar (TCHAR ch);
Trang 207// dynamically create an edit control
CEdit* pEdit = new CEdit;
pEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE
| WS_TABSTOP | WS_BORDER, CRect(10, 10, 100, 100),
Trang 209± HBITMAP SetBitmap(HBITMAP hBitmap)
± HICON SetIcon(HICON hIcon);
± virtual void DrawItem( LPDRAWITEMSTRUCT
lpDrawItemStruct );
± int GetCheck() const;
± void SetCheck(int nCheck);
209
ኼ¿
Trang 211// Create an auto 3-state button
͵Ǥȋ̴ȋDz͵-state button"), WS_CHILD|WS_VISIBLE|BS_AUTO3STATE,
Trang 215WS_CHILD | WS_VISIBLE | WS_BORDER |
LBS_NOTIFY | WS_VSCROLL, CRect( 200, 10, 280, 80 ), this,
̴ ȌǢȀȀ ¯ዋÂ Ǥ
m_listbox- >AddString( "Ong A" );
m_listbox- >AddString( "Ba B" );
m_listbox- >AddString( "Co C" );
Trang 220int iSel = m_Combobox.GetCurSel( );
if (iSel == LB_ERR) // no selection
iSel = 0;
m_pComboBox- >GetLBText( iSel, info );
m_edit->SetWindowText( info );
}
Trang 221CWnd* pParentWnd,
UINT nID );
Trang 223m_editAge.Create ( WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER,
CRect( 135, 45, 190, 65 ), this, IDC_EDIT);
static CSpinButtonCtrl m_spinAge;
m_spinAge.Create ( WS_CHILD | WS_VISIBLE |
UDS_ARROWKEYS | UDS_SETBUDDYINT | UDS_ALIGNRIGHT,
CRect(0,0,1,1), this, IDC_SPIN);
Trang 226virtual BOOL Create( DWORD dwStyle,
const RECT& rect, CWnd* pParentWnd,
UINT nID );
Trang 228228
ኼ¿
static CProgressCtrl myCtrl;
// Create a smooth child progress control.
// Offset the position by one-fourth of the total range
int nLower, nUpper;
myCtrl.GetRange(nLower, nUpper);
myCtrl.OffsetPos((nUpper-nLower)/4);
// Advance the position to the next step
myCtrl.StepIt();
Trang 229± void SetScrollRange ( int nLower, int nUpper);
± void GetScrollRange ( int &nLower, int &nUpper);
± int SetScrollPos( int nPos );
± int GetScrollPos( int &nPos);
± ȋαȌǢȀȀኼȀኽ
ዒ
229
ኼ¿
virtual BOOL Create( DWORD dwStyle,
const RECT& rect, CWnd* pParentWnd,
UINT nID );
Trang 252ЭЦ
252
ኼ¿
Trang 255//TODO: If this DLL is dynamically linked against the MFC DLLs,
// any functions exported from this DLL which call into
Trang 264264
ኼ¿
Trang 271271
е
Data members
± m_hThread: The current thread handle
± m_nThreadID: The current thread ID
CWinThread
CWinApp
Trang 272CWinThread* AfxBeginThread( AFX_THREADPROC ThreadProc,
LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL,
UINT nStackSize = 0, DWORD dwCreateFlags = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );
Trang 273ThreadProc controlling function, cannot be NULL This function must be declared
as follows: UINT MyControllingFunction( LPVOID pParam );
pParam Parameter to be passed to the controlling function as shown in the
parameter to the function declaration in pfnThreadProc
nPriority Thread priority
nStackSize Specifies the size in bytes of the stack for the new thread If 0, the
stack size defaults to the same size stack as the creating thread.
dwCreateFlags
additional flag and can be any of these two CREATE_SUSPENDED or
0 CREATE_SUSPENDED starts the thread with a suspend count of one The thread will not execute until ResumeThread is called 0 Start the thread immediately after creation.
lpSecurityAttrs Points to a SECURITY_ATTRIBUTES structure See
SECURITY_ATTRIBUTES for more details.
Trang 275AfxBeginThread( ThreadFunction, &data);
UINT ThreadFunction(LPVOID param) {
UINT nIterations = (UINT) pParam;
for (int i=0; i<nIterations; i++);
ȀȀǥ
return 0;
}
Trang 277THREAD_PRIORITY_TIME_CRITICAL Normal for HIGH, NORMAL, or
IDLE Double for REALTIME.
THREAD_PRIORITY_HIGHEST = Process Priority + 2.
THREAD_PRIORITY_ABOVE_NORMAL = Process Priority +1.
THREAD_PRIORITY_NORMAL = Process Priority.
THREAD_PRIORITY_BELOW_NORMAL = Process Priority -1.
THREAD_PRIORITY_LOWEST = Process Priority -2.
THREAD_PRIORITY_IDLE Normal for REALTIME, 1 for
HIGH, NORMAL,
Trang 279Function name Purpose
InitInstance Perform thread instance initialization Must be overridden.
Run Controlling function for the thread Contains the message pump
Rarely overridden.
OnIdle Perform thread-specific idle-time processing Not usually overridden.
PreTranslateMessage Filter messages before they are dispatched to TranslateMessage and
DispatchMessage Not usually overridden.
... class="page_container" data-page="92">± Select MFC standard as the project style and Windows
Native/Default as the Visual style and colors option
±