Title: Visual C
1Visual C
20. C Review
3Virtual Function
- ?? ???(dynamic binding)
- ????? ?? ?? ?? ??? ??? ??
- Virtual function table(hidden pointer)
- vs. ?? ???(static binding)
- DrawObj p_obj
- p_obj new Point
- p_obj-gtDraw()
- p_obj new Line
- p_obj-gtDraw()
- p_obj new PolyLine
- p_obj-gtDraw()
4Virtual Function - ??? ??
p_obj new PolyLine p_obj-gtDraw()
p_obj new DrawObj p_obj-gtDraw()
50
250
100
350 430
vtbl 50
vtbl 180
virtual function table
int a
int a
180
550 430
int x
int y
p_obj new Line p_obj-gtDraw()
Point start
250
DrawObjDraw
Point end
350
pointer of virtual function table
vtbl 100
Point0 pt
LineDraw
int a
Point1 pt
430
LineCalc
int x
Point2 pt
int y
Point3 pt
Point start
Point4 pt
550
PolyLineDraw
Point end
Code ??
5Why MFC?
- ??? ??
- ??? ??? ???? ???? ???? ??????
- ??? ???? ??
- ????? ??? ???
- ?????? ?????(application framework) ??
- ?? ?? ??
- ?? ??? ???
??????
MFC
SDK API
????
61. Hello, MFC
7Introducing MFC
- MFC Design Philosophy
- MFC should provide an object-oriented interface
to the Windows operating system that supports
reusability, self-containment, and other tenets
of OOP. - It should do so without imposing undue overhead
on the system or unnecessarily adding to an
application's memory requirements. - Document/View Architecture
- MFC Class Hierarchy
- Serialization support
- Run-time class information support
- Diagnostic and debugging support
- AFX Functions
8Commonly Used AFX Functions
- AfxAbort Unconditionally terminates an
application usually called when an unrecoverable
error occurs - AfxBeginThread Creates a new thread and begins
executing it - AfxEndThread Terminates the thread that is
currently executing - AfxMessageBox Displays a Windows message box
- AfxGetApp Returns a pointer to the application
object - AfxGetAppName Returns the name of the application
- AfxGetMainWnd Returns a pointer to the
application's main window - AfxGetInstanceHandle Returns a handle identifying
the current application instance - AfxRegisterWndClass Registers a custom WNDCLASS
for an MFC application
9AFX Classes
- ?????? ????? ???? ???? ??? ??
- ??? ????? ???? ??? ??
- ??? ???? ?? ??
- ????? ?? ??
10AFX ???? ?? ??
11??? ????
- ??? ?? ???
- ?? ?? ???
- ?? ? ?????? ?? ???
- ??? ?? ???
- OLE ?? ???
- ?? ?? ? ???? ?? ???
12Hello, MFC
13Hello.h
class CMyApp public CWinApp public
virtual BOOL InitInstance () class
CMainWindow public CFrameWnd public
CMainWindow () protected afx_msg void
OnPaint () DECLARE_MESSAGE_MAP ()
14Hello.cpp
include ltafxwin.hgt include "Hello.h" CMyApp
myApp // CMyApp member functions BOOL
CMyAppInitInstance () m_pMainWnd new
CMainWindow m_pMainWnd-gtShowWindow
(m_nCmdShow) m_pMainWnd-gtUpdateWindow ()
return TRUE BEGIN_MESSAGE_MAP (CMainWindow,
CFrameWnd) ON_WM_PAINT () END_MESSAGE_MAP
() CMainWindowCMainWindow () Create
(NULL, _T ("The Hello Application")) void
CMainWindowOnPaint () CPaintDC dc
(this) CRect rect GetClientRect
(rect) dc.DrawText (_T ("Hello, MFC"), -1,
rect, DT_SINGLELINE DT_CENTER
DT_VCENTER)
15????? ?? ???
- AppWiard? ? ?? ?
- MFC? AFX ????? ??? ?? ?? 1? ?? ? ?? ???? ??
- ?? ?? ????? MFC? ??? ???? ??
16CWinApp ???
- CWinApp ???? ??
- ????? ??? ??? ??
- ????? ??? ?, ?? ??? ???? ??
- ????? ??? ???? ???
- WM_QUIT ???? ??? ????? ?? ??
- ????? ??? ???? ???? ??
- CMyApp ???? ????? ???? ????? ??
17CWinApp ???
- ????? ????? ?? ?? ?? ??? ??? ??
- AfxGetApp ???? ?? ??? ??
- AfxWinInit hInstance, nCmdShow ?? ????? ??
- InitApplication, InitInstance ???? ???
- Run ??? ??
- ExitInstance WinMain?? ???
- AfxWinTerm ???? ??
18CWinApp ???
- stdafx.h? Standard Application Frameworks? ??
- AFX ???? ???? ???, ??? ??? ???, ? ???? ??? ???? ??
class CMyApp public CWinApp // //
Overrides // ClassWizard generated virtual
function overrides //AFX_VIRTUAL(CMyApp) pub
lic virtual BOOL InitInstance() virtual
int ExitInstance() //AFX_VIRTUAL //
19CWinApp ???
- ???? ???, ??? ??? ???, ? ???? ?? CMyDoc,
CMainFrame, CMyView ???? ??? ??? ?? - ? ??? CSingleDocTemplate ???? ??? ? CWinApp ????
?? ??? AddDocTemplate ??? ???? CWinApp ???? ??
CMyApp theApp BOOL CMyAppInitInstance()
CSingleDocTemplate pDocTemplate
pDocTemplate new CSingleDocTemplate( IDR_MAINFR
AME, RUNTIME_CLASS(CMyDoc), RUNTIME_CLASS(CMainF
rame), RUNTIME_CLASS(CMyView))
AddDocTemplate(pDocTemplate) return TRUE
20CWnd ???
- ??? ??? ??
- ???? ??, ??, ??, ?? ?? ???? ?? ??? ??
- ??? ???-????? ???? ???? ??
21??? ??? ??? ??
- ???? ??? ??? ????? ??? ????? ??? ?? ?? ? ??? ???
??
22??? ???? ??
int CMyWndOnCreate(LPCREATESTRUCT
lpCreateStruct) CWndOnCreate(lpCreateStruct)
// ??? ?? ??? ?? ??? ??? ???.
23??? ?
- ?? ??
- ??? ? ??? ??? ??? ???? ?? 4??? ?? ???? ??
- CWnd ???? ??? ??? ??? ? 200? ? ?? ?? ?? ??? ????
? 800???? ???? ? ?? - ??? ???,? ???, ??, ???, ????? ??, ?? ??? ? ?? ??
???? ?? - ??? ?
- ?? ???? ??? ??? ??? ??? ???? ?? ???? ??? ????, ??
???? ??? ???? ???
24CFrameWnd ???
- ???? ?????? ??
- ??? ???? ??? ???
- ??? ?????? ??? ??
- ???? ??, ??, ?? ?? ??? ?? ?? ?? ?????? ??
- ??? ???? ?? ?? CFrameWnd ????? ??? ?? ?? ????? ??
- OnCreate ??? ????? ??, ? ?? ???? ??? ???? ????
??? ??? ?? - ????? ? OnCreate ???? ?? ???? OnCreate ??? ??? ??
int CMyWndOnCreate(LPCREATESTRUCT
lpCreateStruct) CWndOnCreate(lpCreateStruc
t) // ??? ?? ??? ?? ??? ??? ???.
25CFrameWnd ???
- afx_msg virtual ?? ????? ? ???? ???? ?? ??
class CMainFrame public CFrameWnd // //
Generated message map functions protected
//AFX_MSG(CMainFrame) afx_msg int
OnCreate(LPCREATESTRUCT lpCreateStruct)
afx_msg void OnActivate(UINT nState, CWnd
pWndOther, BOOL bMinimized) //AFX_MSG
DECLARE_MESSAGE_MAP()
26AFX ????
27CView ???
- ???? ?????? ??
- CView ???? ??? ???? ????? ?? ??? ?? ?? ???? ???
????? ???? ??? ? - ? ?????? ??? ??
- ?????? ??? ???? ???
class CMyView public CView public
CMyDoc GetDocument( ) // Overrides //
ClassWizard generated virtual function overrides
//AFX_VIRTUAL(CMyView) public virtual
void OnDraw(CDC pDC) // overridden to draw
this view protected virtual BOOL
OnPreparePrinting(CPrintInfo pInfo) virtual
void OnBeginPrinting(CDC pDC, CPrintInfo
pInfo) virtual void OnEndPrinting(CDC pDC,
CPrintInfo pInfo) //AFX_VIRTUAL
28CView ???
- GetDocument()
- ???? ????? ???? ?? ??
- OnDraw()
- ?? ?????? ???? ??? ??? ??? ???? ??
- OnXXXPrinting()
- ??? ??? ?? ??
29CDocument ???
- CDocument ???? ??
- ????? ???? ???? ?? (?? ??? ?? ??)
- ??? ???? ???? ?? (?? ??? ?? ??)
- ??? ???? ??? ?? (?? ??? ? ?? ??)
- ?? ?? ???? ?? ?? (?? ??? ?? ??)
- ???? ??? ??? ? ????? ??? ??
// MyDoc.h class CMyDoc public
CDocument protected // create from
serialization only CMyDoc() // Overrides
public virtual BOOL OnNewDocument() //
Implementation public virtual CMyDoc()
30Framework ???? ????
31Framework ???? ????
- ????
- CWinApp AfxGetApp( )
- CWinApp ?? ???? ???? ???? ??
- ???? ?? ????? ?? ??
- CWnd AfxGetMainWnd( )
- ?? ??? ???? ???? ???? ??
- ???? ?? ????? ?? ??
- ??? ??? ????? ????/? ??
- virtual CDocument GetActiveDocument( )
- ??? ??? ????? ???? ???? ??
- CView GetActiveView( ) const
- ?? ??? ???? ??? ? ???? ???? ???? ??
32Framework ???? ????
- ??? ??? ????? ????/? ??
- virtual CDocument GetActiveDocument( )
- ?? ??? ???? ??? ???? ???? ???? ???? ??
- CView GetActiveView( ) const
- ??? ?? ??? ???? ??? ? ???? ???? ???? ??
- ? ????? ??? ???/???? ??
- CFrameWnd GetParentFrame( ) const
- ? ????? ? ?? ???? ?? ??? ???? ??
- CDocument GetDocument( ) const
- ? ????? ? ?? ??? ???? ???? ??
33Framework ???? ????
- ???? ????? ? ??
- CDocumentGetFirstViewPosition, GetNextView
- ??? ????? ?? ?? ?? ??
- ?????? ?? ??? ?? ?? ??? ??? ??
- ???? ????? ??? ??? ??
- SDI AfxGetMainWnd ??? ??
- MDI ?? ??? ??
POSITION pos GetFirstViewPosition() while
(pos ! NULL) CView pView
GetNextView(pos) pView-gtUpdateWindow()
34???? ??? ??
- CWndSendMessage
- ???? ??? ?? ??? ??
- CWndPostMessage
- ???? ??? ?? ???
- CWinApp ???? CDocument ?? ?????? XxxxMessage ??
?? ??? - AfxGetMainWnd( )-gtXxxxMessage( , , )
35??
- Hello MFC ???? ??
- Project-gtWin32 Application
- Project menu-gtsettings(altF7)-gtgeneral-gtUse MFC
in a Shared DLL - Menu ??
- System ?????
- Minimize,Maximize ?? ???
- ?? ??? ??, ?? ???
362. Menu
37Menu ??
- ?? ??? ????, ID? Caption? ??
38Loading and Displaying a Menu
- ??? ???? ???? ???.
- CFrameWndCreate ??? ??? ID? ??
- Create (NULL, _T ("My Application"),
WS_OVERLAPPEDWINDOW, rectDefault, NULL,
MAKEINTRESOURCE (IDR_MAINFRAME)) - CFrameWndLoadFrame ?? ??
- LoadFrame (IDR_MAINFRAME, WS_OVERLAPPEDWINDOW,
NULL, NULL) - CMenu ?? ??? LoadMenu ??
- CMenu menu
- menu.LoadMenu (IDR_MAINFRAME)
- SetMenu (menu) menu.Detach ()
39Menu ???
- CWndDrawMenuBar()
- ???? ??? ??? ??? ?? ???
Create (NULL, _T ("My Application"))
m_menuLong.LoadMenu (IDR_LONGMENU) m_menuShort.L
oadMenu (IDR_SHORTMENU) SetMenu (m_bShortMenu ?
m_menuShort m_menuLong)
m_bShortMenu TRUE SetMenu (m_menuShort) DrawM
enuBar ()
m_bShortMenu FALSE SetMenu (m_menuLong) DrawM
enuBar ()
40Responding to Menu Commands
- WM_COMMAND Message
- wParam? ?? ID ??
- SDK??? switch-case ??
- MFC??? ON_COMMAND ??? ???? ??? ? ??? ??
ON_COMMAND (ID_FILE_NEW, OnFileNew) ON_COMMAND
(ID_FILE_OPEN, OnFileOpen) ON_COMMAND
(ID_FILE_SAVE, OnFileSave) ON_COMMAND
(ID_FILE_SAVE_AS, OnFileSaveAs) ON_COMMAND
(ID_FILE_EXIT, OnFileExit) void
CMainWindowOnFileExit () PostMessage
(WM_CLOSE, 0, 0)
41Command Message
- ???? ?? ?? ??? ????, WM_COMMAND ?? ??? ???? ??
- ?? ??? ????? ???? ?? ??? ID? ???? ??? ??
- ?? OnCommand?? ??? ????? ??? ?? ? ?? ?? ???
42Command Message ?? ??
- ???? ??? ???? ???? ??? ? CWnd ???
- CWnd ??? ?? ???? ??? ???? ?? ??? ??
- ???? ?? ??? ???? ????? ??? ??? WM_COMMAND ???? ??
AFX ???? ??
43Command Message ??
- ????? ???? ???? ?? ? CDocument ?? ???
- ???? ??? ???? ?? ? CView ?? ???
- ????? ??? ??? ??? ??? ?CWinApp ?? ???
- ??? ???? ??? ??? ???? ?? ? CFrameWnd ?? ???
44Command Ranges
- // In CMainWindow's message map
- ON_COMMAND (ID_COLOR_RED, OnColorRed)
- ON_COMMAND (ID_COLOR_GREEN, OnColorGreen)
- ON_COMMAND (ID_COLOR_BLUE, OnColorBlue)
-
- void CMainWindowOnColorRed ()
-
- m_nCurrentColor 0
-
- void CMainWindowOnColorGreen ()
-
- m_nCurrentColor 1
-
- void CMainWindowOnColorBlue ()
-
- m_nCurrentColor 2
45Command Ranges
- // In CMainWindow's message map
- ON_COMMAND (ID_COLOR_RED, OnColor)
- ON_COMMAND (ID_COLOR_GREEN, OnColor)
- ON_COMMAND (ID_COLOR_BLUE, OnColor)
- void CMainWindowOnColor ()
-
- UINT nID (UINT) LOWORD (GetCurrentMessage
()-gtwParam) //wParam ? ?? - m_nCurrentColor nID
// In CMainWindow's message map ON_COMMAND_RANGE
(ID_COLOR_RED, ID_COLOR_BLUE, OnColor) void
CMainWindowOnColor (UINT nID)
m_nCurrentColor nID
463. Graphic Device Interface
47GDI
- ??? ???? ????? (GDI Graphic Device Interface)
- ??? ??? ???? ??? ????? ?????? ????? ???? ?? ??
- ???? ???? (DC Device Context)
- ???? ??? ?? ??? ??? ??? ???
- GDI ???? (GDI Object)
- ????? ???? ? ??? ??? ??
48??? ?? MFC ???
- CDC ???
- GDI ????? ?? ???? ??? ??? ??
- ?? ??? ???? CDC ???? ?? ??
- ?? ???
- CClientDC
- CWindowDC
- CPaintDC
- CMetaFileDC
CDC pDC GetDC() pDC-gtRectangle(10, 10, 100,
100) ReleaseDC(pDC)
49CClientDC
- CDC ????? ??
- ??? ???? GetDC? ??
- ??? ???? ReleaseDC? ??
CClientDC dc(this) dc.Rectangle(10, 10, 100,
100)
50GDI ???? ???
- ??? ??? ?? ??? ??? ??? ?? GDI ???? ???? ??? ??
??? DC? ??, ? DC? ???? ??? ??
51?? ???
- ?? ???? ??? ???
- ?? DC? ???? ?? ?? ???? ???? ??
- ???? ?? ??? ?? DC? ???? ?? ???? ??
52??? ?? ???
CPen pen pen.CreatePen(PS_SOLID, 3, RGB(255, 0,
0)) CClinetDC dc(this) dc.SelectObject(pen) dc
.Rectangle(10, 10, 100, 100)
?? ??? ???
53??? ?? ???
- ??? ??? ???? ???? ??? ??? ???? ??? DC? ???? ?? ?
????? ?? - SelectObject ??? ????? ???? GDI ???? ??
CPen pen pen.CreatePen(PS_SOLID, 3, RGB(255, 0,
0)) CClinetDC dc(this) CPen pOldPen (CPen
)dc.SelectObject(pen) dc.Rectangle(10, 10,
100, 100) dc.SelectObject(pOldPen)
54?? (Stock) GDI ????
- ?? ??? ???? GDI ????? ??? ????? ??
- GetStockObject ?? ??
- ?? ????? ?? ???? ??
CClientDC dc dc.SelectObject(
GetStockObject(WHITE_PEN) )
55??
56??? ?????
- ?? ?? ??? ??? ??? ?? ??? ??
- int SetROP2( int nDrawMode )
- R2_COPYPEN
- ??? ???? ???? ??? ?? ??
- R2_XORPEN
- ??? ???? ??, ?? ??? ???? ??? ?? ? ??
- ??? ??? ? ? ???? ?? ?? ???? ??
57??
- ???? ??? ?? ?? ?? ? ?? ????
584. Dialog Box and Controls
59????? ??
- ???? ?? ?? ???? ??? ??? ? ????? ?? ??
- ????? ??? ?????? ?? ?? ???? ?? ??? ??? ??
- ????? ???
- ????? ??? ??? ? ??? ????? ??? ??? ?? ???
- ??? ??? ??
60????? ?? ?? ??
- Caption ???? ????? ??? ???
- Font ??????? ???? ?? ??
- Styles ????? ??? ??? ??
61????? ?? ???
- CWnd ??? ??
- CWnd ???? ?? ???? ?? ?? ??? ??? ??? ??? ??? ?? ??
- CDialog ???? ??? ??? ??
- cf) ???? MFC ???? ??? ??
62????? ?? ??? (1/2)
class CNameDlg public CDialog //
enum IDD IDD_DIALOG1 //
63????? ?? ??? (2/2)
- CDialog ?? ???? ???? ??? ????
- ???? ??? ?? ?? ????? ??? ??
include NameDlg.h void CDlgTestViewOnDialog
( ) CNameDlg dlg // CDialog ?? ???? ?????
?? dlg.DoModal( ) // CDialog ???? ?? ??? ??
// ????? ???? ?? ?? ?? ???? ??
64CDialog ???? ??
- ????? ??? ???? ??
- ??? ??? ??
- ??? ??? ??? ??
- ????? ??? ?? (OK) ??? ?? (Cancel) ??? ??? ???
???? ??? ??
include NameDlg.h void CDlgTestViewOnDialog
( ) CNameDlg dlg if( dlg.DoModal( )
IDOK) // OK ??? ??? ?? ???? ??
// ????? ???? ?? ?? ?? ???? ??
65CDialog ?? ???? ???? ?? ??
- ???? ???? ??? ??? ??
- ???? ?? ??? ?????? ??
- ??? ??? ??? ???? ?? ??
- Show/Hide
- ???/????
66Value? ????
- ???? ??? ?? ??? CDialog ?? ???? ?? ??
- MFC? ???? ??? ???? ???? ??? ?? ??? ??
- ??? ??? ?? ???? ???? ?? ???? ??
67???? Value? ?? ?? ??
- ????? ????? Value? ?? ??? ?????? ???? ???
Ctrl?? ?? ???? ??? ??? ?? ?? - ???? ??
68???? Value? ?? ?? ??
CNameDlgCNameDlg(CWnd pParent /NULL/)
CDialog(CNameDlgIDD, pParent) //AFX_DATA_IN
IT(CNameDlg) m_strName _T("") m_nAge
0 //AFX_DATA_INIT void CNameDlgDoDataExcha
nge(CDataExchange pDX) CDialogDoDataExchange
(pDX) //AFX_DATA_MAP(CNameDlg) DDX_Text(pDX,
IDC_EDIT1, m_strName) DDX_Text(pDX, IDC_EDIT2,
m_nAge) //AFX_DATA_MAP
class CNameDlg public CDialog //
//AFX_DATA(CNameDlg) enum IDD
IDD_DIALOG1 CString m_strName
int m_nAge //AFX_DATA //
69???? Value? ?? ?? ??? ??? ??
- UpdateData ??? ??? ??? ??
- CDialog ????? ????? ???? ?? UpdateData ??
- ????? ?? ?? ? UpdateData(FALSE) ??
- ????? ?? ?? ? UpdateData(TRUE) ??
- ?? ?? (OK) ??? ???? ??? Value? ?? ??? ??
70??? ?? ??? ??
- ??? ??? ??? ??? ???
- ??? ??? ???
void CNameDlgDoDataExchange(CDataExchange
pDX) CDialogDoDataExchange(pDX) //AFX_DAT
A_MAP(CNameDlg) DDX_Text(pDX, IDC_EDIT1,
m_strName) DDV_MaxChars(pDX, m_strName,
10) DDX_Text(pDX, IDC_EDIT2, m_nAge) DDV_MinMa
xInt(pDX, m_nAge, 0, 100) //AFX_DATA_MAP
71?? ???? ?? ??? ??
72??? ??? ?? ??
- ??? ??? ??? ???(Maximum Value)? ???(Minimum
Value)?? ??
73UpdateData ??? ?? ??
- UpdateData ??? ????? DoDataExchange ??? ??
void CNameDlgDoDataExchange(CDataExchange
pDX) CDialogDoDataExchange(pDX)
//AFX_DATA_MAP(CNameDlg) DDX_Text(pDX,
IDC_EDIT1, m_strName) DDV_MaxChars(pDX,
m_strName, 10) DDX_Text(pDX, IDC_EDIT2,
m_nAge) DDV_MinMaxInt(pDX, m_nAge, 0, 100)
//AFX_DATA_MAP
74UpdateData ??? ?? ??
- UpdateData(TRUE)
- DoDataExchang ??? ??
- DDV_? ???? ???? ??
- DDX_? ???? ???? ??
- UpdateData(FALSE)
- DoDataExchang ??? ??
- DDX_? ???? ???? ??
75???? ?? Value? ?? ??
- ?? ?? ???CStiring,int,UINT,long,DWORD,float,double
,BYTE,short,BOOL,COleDateTime,COleCurrency - ??? ?? ???? ?? ?? ???
- CString
- ?? ?? ???
- BOOL
76???? ?? Value? ?? ??
- ??? ?? ???
- Group ??? ?? ??? ?? ??????? ?? Group ??? ?? ???
?? ?????? ??? ?? - Group ??? ?? ???? ??? Value? ?? ??? ??
- int
77Control? ?? ??
- MFC?? ?? ????? ???? ??
- ???? Control? ?? ??? ???? ??
- ?? ??? ???? ?? ???? CWnd
78CWnd ???
- Create
- ???? ??
- ShowWindow
- SW_SHOW ,SW_HIDE
- EnableWindow
- ???? ??? ???? ???? ??
- SetWindowPos
- ???? ??? ??? ??
- SetWindowText
- ???? ?? ???? ??
79CEdit ???
- GetSel
- ??? ??? ???? ???
- SetSel
- ?? ??? ?? ???? ??
- ReplaceSel
- ??? ??? ???? ??? ???? ??
- GetLineCount
- ?? ??? ???? ? ? ??
- GetLine
- ??? ??? ???? ???
80CButton ???
- ?? ??, ?? ?? ??, ??? ??
- GetCheck
- ??? ??? ??? ???
- SetCheck
- ??? ??? ??? ??
81CScrollBar ???
- GetScrollRange
- ???? ???? ???? ??
- SetScrollRange
- ???? ???? ???? ??
- GetScrollPos
- ???? ???? ?? ??? ? ??
- SetScrollPos
- ???? ???? ? ??
82CListBox ???? CComboBox ???
- GetCount
- ???? ???? ? ???? ?? ??
- GetCurSel, SetCurSel
- ??? ???? ??? ????? ??
- ???? 0?? ??
- AddString, InsertString
- ???? ??
- DeleteString,ResetContent
- ???? ??
- Dir
83???? ??
- WM_INITDIALOG ??? (OnInitDialog ??? ??? ??)
- ???? ??? ??? ?? ????.
- ?? (NOTIFICATION) ???
- ?? ???? ???? ??? ??? ???? ?? ? ???? ?? ?? ?? ????
????? ??? ???? ?? - ?? ???? ??
- ?? ???? ?????? ??? ??.
84Notification Message
- Button
- BN_CLICKED, BN_DBLCLK
- Edit Box
- EN_CHANGE, EN_SETFOCUS, EN_KILLFOCUS
- List Box
- LBN_DBLCLK, LBN_SELCHANGE, LBN_SETFOCUS,
LBN_KILLFOCUS - Combo Box
- CBN_DBLCLK, CBN_SELCHANGE, CBN_SETFOCUS,
CBN_KILLFOCUS, CBN_EDITCHAGE
85???? ????? ??
- ?? ????? ??
- ????? ??? ???? ?? ???? ? ????? ??? ????? ?? ????
?? - ???? ????? ??
- ????? ??? ????? ???? ???? ??
86?? ??
- Modal Dialog
- DoModal ??? ??
- ????? ??? ???? ?? IDOK ?? IDCANCEL
- Modaless Dialog
- Create ?? ??? ???? ??
- ???? ????? ??? ???? ? ?? ??
87?? ??
- Modal Dialog
- EndDialog ?? ??
- OnOK ?? ????? EndDialog(IDOK)? ??
- OnCancel ?? ????? EndDialog(IDCANCEL)? ??
- Modaless Dialog
- EndDialog ??? ?? ??
- DestroyWindow ??? ???? ??
- OnOK ??? OnCancel ??? ?????? CDialog ???? OnOK
??? OnCancel ??? ???? ??? ??
88???? ??
class CModalessDlg // CModalessDlg ???? ??? ???
?? class CMainFrame public CFrameWnd // CMo
dalessDlg m_pDlg // include
ModalessDlg.h void CMainFrameOnModaless()
m_pDlg new CModalessDlg m_pDlg-gtCreate(thi
s)
- include ModalDlg.h
-
- void CMainFrameOnModal()
-
- CModalDlg dlg
- dlg.DoModal()
89class CModalessDlg public CDialog BOOL
Create(CWnd pParentWnd) BOOL
CModalessDlgCreate(CWnd pParentWnd) return
CDialogCreate(IDD, pParentWnd) void
CModalessDlgOnClose() DestroyWindow() vo
id CModalessDlgPostNcDestroy() ((CMainFrame
)AfxGetMainWnd())-gtm_pDlg NULL delete
this
90class CMainFrame public CFrameWnd CModalessDl
g m_pDlg void CMainFrameOnModaless()
if(m_pDlg ! NULL) // ????? ??? ?? ????
?? ?? m_pDlg-gtSetFocus() else // ?????
??? ???? ?? ?? ?? m_pDlg new
CModalessDlg m_pDlg-gtCreate(this) m_pDlg-gtSh
owWindow(SW_SHOW)
91????? ?? ?? ??????
92????? ?? ?? ??????
- CWinApp ?? ???? CDialog ?? ??? ??? ??
BOOL CDlgAppAppInitInstance() // CDlgAppDlg
dlg m_pMainWnd dlg int nResponse
dlg.DoModal() // return FALSE
935. ??? ?????
94?? ??? ???? ?? ??
- BOOL CCmdUIAppInitInstance( )
-
- //
- CSingleDocTemplate pDocTemplate
- pDocTemplate new CSingleDocTemplate(
- IDR_MAINFRAME,
- RUNTIME_CLASS(CCmdUIDoc),
- RUNTIME_CLASS(CCmdUIView),
- RUNTIME_CLASS(CMainFrame))
- AddDocTemplate(pDocTemplate)
- //
-
95????? ??? ?? ??
96???? ??
- void CContextMenuViewOnContextMenu(CWnd pWnd,
CPoint point) -
- CMenu muTemp, pContextMenu
-
- muTemp.LoadMenu(IDR_MAINFRAME)
- pContextMenu muTemp.GetSubMenu(0)
- pContextMenu-gtTrackPopupMenu(TPM_LEFTALIGN,
point.x, point.y, AfxGetMainWnd()) -
- BOOL TrackPopupMenu(UINT nFlags, int x,int y
,CWnd pWnd ,LPCRECT lpRect NULL) - nFlag - TPM_CENTERALIGN,TPM_LEFTALIGN,TPM_RIGHTALI
GN , TPM_LEFTBUTTON , TPM_RIGHTBUTTON - x , y ???? ??? ??? ??
- pWnd ??? ??? ???
- lpRect ???? ???? ??? ???? ? ??
97??? ??
98???? ??? ???? ?? ??
- ? ?? (N)\tCtrlN
- , \t ?? ??? ???? ??? ??? ??
- N ? ???? alt?? ???? ?????? ???
99??? ????? ?? ???
- ON_UPDATE_COMMAND_UI? ??? ?????? ??? ??? ??? ????
?? ??? - BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //AFX_MSG_MAP(CMainFrame)
- ON_UPDATE_COMMAND_UI (ID_RED, OnUpdateColor)
- //AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- void CMainFrameOnUpdateColor(CCmdUI pCmdUI)
-
-
-
100ON_UPDATE_COMMAND_UI_RANGE ???
- ID_RED?? ID_YELLOW?? ??? ID? ?? ??? ?????? ???
? ? ?? ??? OnUpdateColor ???? ?? ?? - ON_UPDATE_COMMAND_UI_RANGE? ClassWizard?? ???? ??
- BEGIN_MESSAGE_MAP(CMainFrame,CFrameWnd)
- //AFX_MSG_MAP(CMainFrame)
- //AFX_MSG_MAP??? ??
- ON_UPDATE_COMMAND_UI_RANGE(ID_RED, ID_YELLOW,
- OnUpdateColor)
- END_MESSAGE_MAP()
-
- void CMainFrameOnUpdateColor(CCmdUI pCmdUI)
-
-
-
101CCmdUI ???
- CCmdUI ???? ??? ????? ??? ??? ??? ???? ???
- m_nID command ID
102CCmdUIEnable ??
- ??? ???/???? ??
- BEGIN_MESSAGE_MAP(CCmdUIView, CView)
-
- ON_UPDATE_COMMAND_UI(ID_BEGIN, OnUpdateBegin)
- ON_UPDATE_COMMAND_UI(ID_END, OnUpdateEnd)
- END_MESSAGE_MAP()
- void CCmdUIViewOnUpdateBegin(CCmdUI pCmdUI)
-
- pCmdUI-gtEnable(!m_bStart)
-
- void CCmdUIViewOnUpdateEnd(CCmdUI pCmdUI)
-
- pCmdUI-gtEnable(m_bStart)
103CCmdUISetText ??
- ??? ?? ???
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //AFX_MSG_MAP(CMainFrame)
- ON_COMMAND(IDM_BEGINEND, OnBeginend)
- ON_UPDATE_COMMAND_UI(IDM_BEGINEND,
- OnUpdateBeginend)
- //AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- void CMainFrameOnUpdateBeginend(CCmdUI pCmdUI)
-
- if(m_bStart) pCmdUI-gtSetText("?? ??(E)")
- else pCmdUI-gtSetText("?? ??(S)")
-
104CCmdUISetCheck ??
- void CMainFrameOnColorRed()
-
- m_nColorCheck LOWORD(GetCurrentMessage()-gtwPara
m) -
- void CMainFrameOnColorBlue()
-
- m_nColorCheck LOWORD(GetCurrentMessage()-gtwPara
m) -
- void CMainFrameOnUpdateColorRed(CCmdUI pCmdUI)
-
- if (pCmdUI-gtm_nID m_nColorCheck)
- pCmdUI-gtSetCheck(TRUE)
- else
- pCmdUI-gtSetCheck(FALSE)
-
- void CMainFrameOnUpdateColorBlue(CCmdUI
pCmdUI) -
- if (pCmdUI-gtm_nID m_nColorCheck)
105CCmdUISetCheck ??
- ??? ????
- class CMainFrame public CFrameWnd
-
- //
- UINT m_nColorCheck
- //
-
- CMainFrameCMainFrame()
-
- m_nColorCheck IDM_REDCHECK
-
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //AFX_MSG_MAP(CMainFrame)
- ON_COMMAND_RANGE(IDM_REDCHECK, IDM_BLUECHECK,
OnColorCheck) - //AFX_MSG_MAP
- END_MESSAGE_MAP()
- void CMainFrameOnColorCheck(UINT nID)
-
106CCmdUISetCheck ??
- ?? ??
- ON_UPDATE_COMMAND_UI_RANGE ???? ??
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //AFX_MSG_MAP(CMainFrame)
- ON_COMMAND_RANGE(IDM_REDCHECK,
- ON_UPDATE_COMMAND_UI_RANGE(IDM_REDCHECK,IDM_BLUEC
HECK, OnUpdateColorCheck) - //AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- void CMainFrameOnUpdateColorCheck(CCmdUI
pCmdUI) -
- pCmdUI-gtSetCheck(m_nColorCheck
pCmdUI-gtm_nID) -
107CCmdUISetRadio ??
- SetCheck ? ??
- void CMainFrameOnUpdateColorRadio(CCmdUI
pCmdUI) -
- pCmdUI-gtSetRadio(m_nColorRadio
pCmdUI-gtm_nID) -
108?? ??
- CToolBar ??? ???? ??
- Create, LoadToolBar ?? ??
- class CMainFrame public CFrameWnd
-
- //
- CToolBar m_wndToolBar
- //
-
- int CMainFrameOnCreate(LPCREATESTRUCT
lpCreateStruct) -
- if (CFrameWndOnCreate(lpCreateStruct) -1)
- return -1
-
- m_wndToolBar.Create(this)
- m_wndToolBar.LoadToolBar(IDR_MAINFRAME)
- return 0
-
109?? ??? ??
- ??? ??? ? SetBarStyle() ?? ??? ??
- m_wndToolBar.GetBarStyle( )? ?? ???? ?? ???? ??
?? - m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle
() - CBRS_TOOLTIPS CBRS_FLYBY CBRS_SIZE_DYNAMIC)
- CBRS_FLOAT_MULTI ???? ??? ??
- CBRS_SIZE_DYNAMIC ??? ????
- CBRS_TOOLTIPS ?? ??? ??
- CBRS_FLYBY ???? ??? ??
- CBRS_GRIPPER Gripper ??
110??? ?? ???
- ??? ????? ??? ? ?? ??
- ??? ??? ??? ???? ?? EnableDocking ??? ??
- EnableDocking ??? ?? ???? ??
- int CMainFrameOnCreate(LPCREATESTRUCT
lpCreateStruct) -
- //
- m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY)
- EnableDocking(CBRS_ALIGN_ANY)
- DockControlBar(m_wndToolBar)
- return 0
-
- ?? ???
- CBRS_ALIGN_TOP, CBRS_ALIGN_BOTTOM,
CBRS_ALIGN_LEFT , CBRS_ALIGN_RIGHT ,
CBRS_ALIGN_ANY
111???? ??? ?? ???
- SetButtonText
- int CMainFrameOnCreate(LPCREATESTRUCT
lpCreateStruct) -
- m_wndToolBar.SetButtonText(0, "???")
- m_wndToolBar.SetSizes(CSize(48, 32), CSize(16,
15)) - return 0
-
- CreateEx ??? ??? ?????? ???? ??
- m_wndToolBar.CreateEx(this, TBSTYLE_FLAT,
WS_CHILD WS_VISIBLE CBRS_TOP CBRS_GRIPPER
CBRS_TOOLTIPS CBRS_FLYBY CBRS_SIZE_DYNAMIC)
112????? ?
- ?? ?? ?? ????? ?? ???? ??? ???? ????? ?? ??
- ???? ???? ???? ????? ? ???
- Project Add to project Components and
controls
113???
- ??? ???? ??
- class CMainFrame public CFrameWnd
-
- //
- CStatusBar m_wndStatusBar
- //
-
- Create? ??? SetIndicators ??? ???? ??? ??
114???
- static UINT indicators ID_SEPARATOR,
// status line indicator - ID_INDICATOR_CAPS, ID_INDICATOR_NUM,
ID_INDICATOR_SCRL, - int CMainFrameOnCreate(LPCREATESTRUCT
lpCreateStruct) -
- if (CFrameWndOnCreate(lpCreateStruct) -1)
- return -1
- if (!m_wndStatusBar.Create(this)
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
-
- TRACE0("Failed to create status bar\n")
- return -1 // fail to create
-
- m_wndStatusBar.SetPaneInfo(1, ID_SEPARATOR,
SBPS_NOBORDERS, 200) - return 0
115??? ????
- ? ??
- indicators ??? ID_SEPARATOR? ?? ? ??
- ? ?? ??
- m_wndStatusBar.SetPaneInfo(1, ID_SEPARATOR,SBPS_NO
RMAL, 200) - Index- ? ?? ?? ?? ??? ??? ???? ??
- nID - ?? ?? ??? ??? ?? ??
- nStyle - ?? ???? ??
- SBPS_NOBORDERS,SBPS_POPOUT,SBPS_DISABLED,SBPS_STRE
TCH,SBPS_NORMAL - cxWidth - ?? ??? ??
- ???? ??? ??
- BOOL SetPaneText( int nIndex, LPCTSTR
lpszNewText, BOOL bUpdate TRUE )
116??
117??? ??
- int AfxMessageBox( LPCTSTR lpszText, UINT nType
MB_OK, - UINT nIDHelp 0 )
- ??? ?? ???
- MB_ABORTRETRYIGNORE , MB_OK,MB_OKCANCEL,MB_RETRYCA
NCEL,MB_YESNO,MB_YESNOCANCEL - ??? ?? ???
118??? ??
- ??? ?? ??
- MB_DEFBUTTON1, MB_DEFBUTTON2, MB_DEFBUTTON3
- ???? ??
- MB_APPLMODAL
- MB_SYSTEMMODAL
- ???
- IDABORT
- IDCANCEL
- IDIGNORE
- IDNO
- IDOK
- IDRETRY
- IDYES
119?????
- WM_SETCURSOR ???
- ??? ??? ??? ??? ??? ??? ??
- ?? ??
- HCURSOR CWinAppLoadStandardCursor( LPCTSTR
lpszCursorName ) - HCURSOR CWinAppLoadCursor( LPCTSTR
lpszResourceName ) const - HCURSOR CWinAppLoadCursor( UINT nIDResource )
const - ?? ??
- SetCursor ??? ??
- BOOL CCursorViewOnSetCursor(CWnd pWnd, UINT
nHitTest, UINT message) -
- HCURSOR hCursor
- hCursor AfxGetApp()-gtLoadStandardCursor(IDC_CRO
SS) - SetCursor(hCursor)
- return TRUE
-
- ??!) ?????? SetCursor ?? ?? ??!
120?????
- ?? ?? ???
- BOOL CCursorViewOnSetCursor(CWnd pWnd, UINT
nHitTest, UINT message) -
- HCURSOR hCursor
- hCursor AfxGetApp()-gtLoadStandardCursor(IDC_CRO
SS) - SetCursor(hCursor)
- return TRUE
-
- ???? ??
- void Function( )
-
- BeginWaitCursor( )
- // ??? ??? ??? ??? ????.
- EndWaitCursor( )
-
1216. Control
122?? ???
- Control Type WNDCLASS MFC Class
- Buttons "BUTTON CButton
- List boxes "LISTBOX" CListBox
- Edit controls "EDIT" CEdit
- Combo boxes "COMBOBOX" CComboBox
- Scroll bars SCROLLBAR CScrollBar
- Static controls "STATIC" CStatic
123Button
- ??? ??
- CButton m_wndPushButton
- m_wndPushButton.Create (_T ("Start"), WS_CHILD
WS_VISIBLE BS_PUSHBUTTON, rect, this,
IDC_BUTTON)
124Button Style
- BS_PUSHBUTTON - Creates a standard push button
control - BS_DEFPUSHBUTTON - Creates a default push button
used in dialog boxes to identify the push button
that's clicked if Enter is pressed - BS_CHECKBOX - Creates a check box control
- BS_AUTOCHECKBOX - Creates a check box control
that checks and unchecks itself when clicked - BS_3STATE - Creates a three-state check box
control - BS_AUTO3STATE - Creates a three-state check box
control that cycles through three stateschecked,
unchecked, and indeterminatewhen clicked - BS_RADIOBUTTON - Creates a radio button control
- BS_AUTORADIOBUTTON - Creates a radio button
control that, when clicked, checks itself and
unchecks other radio buttons in the group - BS_GROUPBOX - Creates a group box control
125Push Button
- BS_PUSHBUTTON ?? ??
- ?? ??? WM_COMMAND ??? ???? ??????? ??
- Notificatin ?? BN_CLICKED ? ???
- BN_CLICKED ?? ????
- ON_BN_CLICKED (IDC_BUTTON, OnButtonClicked)
- void CMainWindowOnButtonClicked ()
-
- MessageBox (_T ("I've been clicked!"))
-
126Check Box
- BS_CHECKBOX, BS_AUTOCHECKBOX, BS_3STATE,
BS_AUTO3STATE ? ??? ???? ??
m_wndCheckBox.SetCheck (BST_CHECKED) //
Check m_wndCheckBox.SetCheck (BST_UNCHECKED) //
Uncheck //---------------------------------------
------------------------------------------- void
CMainWindowOnCheckBoxClicked ()
m_wndCheckBox.SetCheck (m_wndCheckBox.GetCheck ()
BST_CHECKED ? BST_UNCHECKED
BST_CHECKED) //--------------------------------
--------------------------------------------------
m_wndCheckBox.SetCheck (BST_INDETERMINATE)
127Radio Buttons
- BS_RADIOBUTTON ?? BS_AUTORADIOBUTTON ???? ??
- BS_AUTORADIOBUTTON ? ???? ??
- BS_RADIOBUTTON ? ?? ??? ???? ??(BN_CLICKED?? ??)
- SetCheck() , GetCheck() ??
- ????? ???? ??
void CMainWindowOnRadioButton1Clicked () //1?
??? ???? ??? ??? Unchecked ??? ??
m_wndRadioButton1.SetCheck (BST_CHECKED)
m_wndRadioButton2.SetCheck (BST_UNCHECKED)
m_wndRadioButton3.SetCheck (BST_UNCHECKED)
m_wndRadioButton4.SetCheck (BST_UNCHECKED)
128Radio button ???
- ??? ???? ??? ??
- ??? ??? WS_GROUP ?? ??
- ??? WS_GROUP? ?? ??? ????? ????? ??? ??
m_wndRadioButton1.Create (_T ("COM1"), WS_CHILD
WS_VISIBLE WS_GROUP BS_AUTORADIOBUTTON,
rect1, this, IDC_COM1) m_wndRadioButton2.Create
(_T ("COM2"), WS_CHILD WS_VISIBLE
BS_AUTORADIOBUTTON, rect2, this,
IDC_COM2) m_wndRadioButton1.SetCheck
(BST_CHECKED) m_wndCheckBox.Create (_T ("Save
settings on exit"), WS_CHILD WS_VISIBLE
WS_GROUP BS_AUTOCHECKBOX, rectCheckBox,
this, IDC_SAVESETTINGS)
129Group Box
- BS_GROUPBOX? ??
- ???? ?? ???? ??? ??
- ?? ??? ??? ??
130CListBox
- ????? ??
- m_wndListBox.Create (WS_CHILD WS_VISIBLE
LBS_STANDARD, rect, this, IDC_LISTBOX) - LBS_STANDARD WS_BORDER WS_VSCROLL
LBS_NOTIFY LBS_SORT - ?? ??
- m_wndListBox.AddString (string)
- m_wndListBox.InsertString (3, string)
- ?? ?? ??
- CListBoxGetCount
- ?? ??
- CListBoxDeleteString
131CListBox
- ?? ??
- ?? ?? ??
- GetCaretIndex
// In CMainWindow's message map ON_LBN_DBLCLK
(IDC_LISTBOX, OnItemDoubleClicked) void
CMainWindowOnItemDoubleClicked () CString
string int nIndex m_wndListBox.GetCurSel
() m_wndListBox.GetText (nIndex, string)
MessageBox (string)
132CStatic
- Static ??? ??
- m_wndStatic.Create (_T ("Name"), WS_CHILD
WS_VISIBLE SS_LEFT, rect, this, IDC_STATIC) - SS_LEFT rect ????? ?? ??
- ????? ??? ?? ?? ?? ? ??? ???SS_CENTERIMAGE
- Bitmap ??
m_ctrlStatic.Create (_T (""), WS_CHILD
WS_VISIBLE SS_BITMAP SS_SUNKEN, rect , this,
IDC_STATIC1) HINSTANCE hInstance
AfxGetInstanceHandle() HBITMAP hBitmap
LoadBitmap(hInstance ,MAKEINTRESOURCE(IDB_BITMAP1)
) m_ctrlStatic.SetBitmap(hBitmap)
133CEdit
- ??? ??? ??
- ??, ????, ???? ?? ??
- Edit ??? ??
- m_wndEdit.Create (WS_CHILD WS_VISIBLE
WS_BORDER ES_AUTOHSCROLL, rect, this,
IDC_EDIT) - WS_BORDER ??? ??? ??? ??
- ES_AUTOHSCROLL ?????? ?? ??
- Multiline Edit ??? ??
- m_wndEdit.Create (WS_CHILD WS_VISIBLE
WS_BORDER WS_HSCROLL WS_VSCROLL
ES_MULTILINE, rect, this, IDC_EDIT)
1347. MDI (Multiple Document Interface)
135?????
- ?? ?? ???(Dynamic Split Window)
- ????? ?? ?? ???? ?? ??
- ?? ???? ?? ???? ????, ????? ???? ?? ?? ??? ?????
- ?? ?? ???(Static Split Window)
- ???? ??? ??? ????? ????, ???? ?? ?? ??? ??? ?????
- ???? ?? ????? ??? ???? ????
136?? ?? ??? ??
- Step 1 CSplitterWnd ???? ???? ??
- CSplitterWnd m_wndSplitter
- Step 2 OnCreateClient ?? ???
- Step 3 ?? View ? ??
- View ????? UpdateAllViews(NULL) ??
BOOL CMainFrameOnCreateClient(LPCREATESTRUCT
lpcs, CCreateContext pContext) if
(!m_wndSplitter.Create(this, 2, 2, CSize(10, 10),
pContext)) TRACE0("?? ??? ??? ??
?????.\n") return FALSE // failed to
create return TRUE
137SDI vs. MDI
138SDI vs. MDI
- CMDIFrameWnd? CMDIChildWnd ???? CFrameWnd ?????
?? ? ??????? ?? ?? - CMDIFrameWnd ????? CMDIChildWnd ???? ??
- CMDIChildWnd ????? CMDIFrameWnd? ??? ???? ????
??? ??
139SDI ? MDI ? ??
- ?? ??? ??? ??
- CMDIChildWnd?? ?? ?? CChildFrame ???? ??
- ?? ??? ???? ?? ??? ??
- ????, ??? ???, ? ??
- CSingleDocTemplate-gtCMultiDocTemplate ?? ??
- RUNTIME_CLASS(CMainFrame)-gt RUNTIME_CLASS(CChildFr
ame)?? ?? - AddDocTemplate ????? ?? ??? ??
- ?? ??? ???? ??? ??? public?? ??
CMainFrame pMainFrame new CMainFrame if
(!pMainFrame-gtLoadFrame(IDR_MAINFRAME))
return FALSE m_pMainWnd pMainFrame
140MDI ? SDI ? ??
- ?? ??? ??? ??
- ?? ??? ???? ?? ??? ??
- ????, ??? ???, ? ??
- ?? ??? ??
- ?? ??? ???? ?? ???? ??
- MainFrame? ?? DECLARE_DYNAMIC/IMPLEMENT_DYNAMIC ?
DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE? ??
141?? ?? ?? ??
void CMyClassOnUpdateAllViews( ) CWinApp
pApp AfxGetApp( ) POSITION posTemplate
pApp-gt GetFirstDocTemplatePosition()
while(posTemplate) CDocTemplate
pTemplate pApp-gtGetNextDocTemplate(posTemplate)
POSITION posDoc pTemplate-gtGetFirstDo
cPosition( ) while(posDoc)
CDocument pDoc pTemplate-gtGetNextDoc(po
sDoc) POSITION posView
pDoc-gtGetFirstViewPosition( )
while(posView) CView pView
pDoc-gtGetNextView(posView) pView-gtInvalidate(
)
142?? ?? ??
143?? ?? ??
144Serialize
- void CMyDocSerialize(CArchive ar)
-
- if (ar.IsStoring())
-
- // ????
- ar ltlt n
-
- else
-
- // ????
- ar gtgt n
-
145?? ???? ?? MDI
146Document Template
- BOOL CDrawAppInitInstance()
-
- //
- CMultiDocTemplate pDocTemplate
- pDocTemplate new CMultiDocTemplate(
- IDR_DRAWTYPE,
- RUNTIME_CLASS(CDrawDoc),
- RUNTIME_CLASS(CChildFrame),
- RUNTIME_CLASS(CDrawView))
- AddDocTemplate(pDocTemplate)
- pDocTemplate new CMultiDocTemplate(
- IDR_TEXTTYPE,
- RUNTIME_CLASS(CTextDoc),
- RUNTIME_CLASS(CTextFrame),
- RUNTIME_CLASS(CTextView))
- AddDocTemplate(pDocTemplate)
- //
147Document Template
148???? ???? ??
149OnFileNew
- void CDrawAppOnFileNew()
-
- POSITION pos GetFirstDocTemplatePosition( )
- CDocTemplate pTemplate
- for(int i0 iltN i)
- pTemplate GetNextDocTemplate(pos)
- pTemplate-gtOpenDocumentFile(NULL)
150OnFileOpen
151OnFileOpen
- void CDrawAppOnFileOpen()
-
- char szFilter "Draw??(.drw), Text??(.txt)
.drw.txt - ????(.)."
- CFileDialog dlg(TRUE, NULL, NULL,
OFN_HIDEREADONLY, szFilter) - if(dlg.DoModal( ) IDOK)
- OpenDocumentFile(dlg.GetPathName())
-
- if(dlg.DoModal( ) IDOK)
- for(POSITION pos dlg.GetStartPosition( ) pos
! NULL ) - OpenDocumentFile(dlg.GetNextPathName(pos))
152Additional Function of CDocument
- OnCloseDocument Called to close the document.
- OnNewDocument Called to create a new document.
- OnOpenDocument Called to open an existing
document. - OnSaveDocument Called to save the document to
disk.
153Additional Function of CDocument
- IsModified Indicates whether the document has
been modified since it was last saved. - SetModifiedFlag Sets a flag indicating that you
have modified the document since it was last
saved. - SetPathName Sets the path of the data file used
by the document. - SetTitle Sets the document's title.
154Additional Function of CView
- OnActivateView Called when a view is activated.
- OnActivateFrame Called when the frame window
containing the view is activated or deactivated. - OnDraw Called to render an image of the document
for screen display, printing, or print preview.
Implementation required. - OnBeginPrinting, OnEndPrinting,
OnEndPrintPreview, OnPreparePrinting, OnPrint
155CDocument CView Interaction
- GetFirstViewPosition, GetNextView
- GetDocument
POSITION pos GetFirstViewPosition() while (pos
! NULL) CView pView
GetNextView(pos) pView-gtInvalidate()
156CDocument CView Interaction
157??? ? ??? ?? ?? ??
- BOOL CDrawAppInitInstance()
-
- //
- CMainFrame pMainFrame new CMainFrame
- if (!pMainFrame-gtLoadFrame(IDR_MAINFRAME))
- return FALSE
- m_pMainWnd pMainFrame
-
- // Enable drag/drop open
- m_pMainWnd-gtDragAcceptFiles()
- //
-
1588. Serialization
159??? ?? ? ????
- CObject ????? ???? ???? ???? Serialize ??? ??
- CObject ? ???? ???? Serialize ??? ???? ????? ??
?? ???? - Serialize ??? ?????? ??
160Serialize ??? ??
- CDocument ?? ???? CObject ???? Serialize ???
??????? ?? - CDocument ?? ????? Serialize ??? ????? ?? ?? ???
???? ????? ??? ? ??
161?????? ??? ??? ?? ?? ?? ??
- ID_FILE_NEW, ID_FILE_OPEN, ID_FILE_SAVE,
ID_FILE_SAVE_AS ? ???? ?? - CWinApp?? ???? ?? CDocument ???? ???.
162?? ?? ??
163Serialize
- CDocument ?? ????? Serialize ??? ?? ????? ??.
- void CDrawCrossDocSerialize(CArchive ar)
-
- if (ar.IsStoring())
-
- // TODO add storing code here
- ar ltlt n1 ltlt n2
-
- else
-
- // TODO add loading code here
- ar gtgt n1 gtgt n2
-
-
164CArchive ???
- CDocument ???? CFile ???? ??? ?? ?? ???
- CFile ? ??? ???? ???
- OnOpenDocument ??? OnSaveDocument ??? CArchive
????? CFile ????? ?? - IsStoring() ???? ?? ????? ??
165??? ?? ? ????
- ??
- CDocument ???? OnOpenDocument ??? OnSaveDocument
??? ??????? ?? ???? ???? ??? ???? ??? ?? - CDocument ???? Serialize ??? ????? ?? ???? ????
??? ???? ??? ?? ??
166OnOpenDocument ? OnSaveDocument ? ??
- BOOL CDrawCrossDocOnOpenDocument(LPCTSTR
lpszPathName) -
- if (!CDocumentOnOpenDocument(lpszPathName))
- return FALSE
-
- // TODO Add your specialized creation code here
-
- return TRUE
-
-
- BOOL CDrawCrossDocOnSaveDocument(LPCTSTR
lpszPathName) -
- // TODO Add your specialized code here and/or
call the base class -
- return CDocumentOnSaveDocument(lpszPathName)
-
167Serialize ??? ??
- void CDrawCrossDocSerialize(CArchive ar)
-
- int i
- if (ar.IsStoring())
-
- ar ltlt m_nCount
- for(i 0 i lt m_nCount i)
- ar ltlt m_ptDatai
-
-
- else
-
- ar gtgt m_nCount
- for(i 0 i lt m_nCount i)
- ar gtgt m_ptDatai
-
-
-
1689. Collection Class
169CObList Construction and Tail Access
- CObListCObList( int nBlockSize 10 )
- nBlockSize - The memory-allocation granularity
for extending the list. - POSITION AddTail( CObject newElement )
- Adds a new element or list of elements to the
tail of this list. The list can be empty before
the operation.
class CAge public CObject int
m_years public CAge( int age0 ) m_years
age
CObList list list.AddTail( new CAge( 21 )
) list.AddTail( new CAge( 40 ) ) // List now
contains (21, 40).
170CObList Iteration
- POSITION CObListGetHeadPosition()
- Gets the position of the head element of this
list. - CObject CObListGetNext( POSITION rPosition )
- Gets the list element identified by rPosition,
then sets rPosition to the POSITION value of the
next entry in the list. - CObject GetAt( POSITION position ) const
- GetAt retrieves the CObject pointer associated
with a given position
for( pos list.GetHeadPosition() pos ! NULL
) list.GetNext( pos )
171CObList Removal
- void CObListRemoveAll()
- Removes all the elements from this list and frees
the associated CObList memory. It is your
responsibility to delete the objects themselves.
CObList list CAge pa1 CAge pa2 ASSERT(
list.IsEmpty()) // Yes it is. list.AddHead( pa1
new CAge( 21 ) ) list.AddHead( pa2 new CAge(
40 ) ) // List now contains (40, 21). ASSERT(
!list.IsEmpty()) // No it isn't. list.RemoveAll()
// CAge's aren't destroyed. ASSERT(
list.IsEmpty()) // Yes it is. delete pa1 //
Now delete the CAge objects. delete pa2
17210. ?????
173??? ???
- ??? ??? vs ?? ???
- ???
- ?? ??? 2?? ??? ???? ????? ??
- ?? ???
- ?, ?, ?, ?, ??, ??? ? ????? ????? ?? ??? ????
????? ??
174Bitmap GDI Object
- ??? GDI ????
- ?????? ???? ??? GDI ????? ??
- CBitmap ??? cf) CPen , CBrush
- ??? ???? GDI ????? ???? ??? ?? ??
- ??? GDI ????? ???? ??? ??
- BitBlt
- Bit Block Transfer? ??
- ??? DC?? ?? DC? ??? ??? ??
- StretchBlt
- ??? DC?? ?? DC? ??? ??? ????, ?? ?? ??? ?? ??
- cf) ?? ?????? ?? DC? 2?? ??
175???? ????? ?? ??
176?? DC? ??? DC ???
- ???? ??? ?? ???? DC
- CClientDC WinDC(this)
- ???? ???? DC
- CDC MemDC
- ??? ?? ???? MemDC??? ?? ?????? ??? WinDC? ??? ???
??
177?? DC? ???? ?? ??? DC ???
- ??? ?? ??? ???? DC ?? ???? ??? ??.
- MemDC.CreateCompatibleDC(WinDC)
- MemDC? WinDC? ???? ?? ?? ?? ??? ?? ??? ??
178??? ????
- ??? ???? ????, ???? ??? CBitmap ???? ??
- ???? ???? ?? ??? ??? ??
- CBitmap bitmap
- bitmap.LoadBitmap(IDB_BITMAP1)
- ???? DC? ??
- CBitmap pOldBitmap (CBitmap )MemDC.SelectObject
(bitmap)
179??? ?? ??
- BOOL CDCBitBlt(int x, int y, int nWidth, int
nHeight, CDC pSrcDC, int xSrc, int ySrc, DWORD
dwRop ) - Ex) WinDC.BitBlt(100, 200, 20, 30, MemDC, 30,
40, SRCCOPY)
180Bitmap ??
- void CMyWndDrawBitmap( )
-
- CClientDC WinDC(this) // ??? DC? ??
- CDC MemDC // ??? DC? ??
-
- // MemDC? WinDC? ??? ?? ??? ?
- MemDC.CreateCompatibleDC(WinDC)
-
- // ????? ???? ???
- CBitmap bitmap
- bitmap.LoadBitmap(IDB_BITMAP1)
-
- // ???? DC? ??
- CBitmap pOldBitmap (CBitmap
)MemDC.SelectObject(bitmap) -
- // ??? ??? ??
- WinDC.BitBlt(100, 200, 20, 30, MemDC, 30, 40,
SRCCOPY) -
- // DC ??
181???? ??/??
- void CBMPViewOnDraw(CDC pDC)
-
- // ??? ?? ??? DC? ??
- CDC MemDC
- MemDC.CreateCompatibleDC(pDC)
- // ????? ???? ?? ?? DC? ??
- CBitmap bitmap
- bitmap. LoadBitmap(IDB_MAN)
- CBitmap pOldBitmap (CBitmap
)MemDC.SelectObject(bitmap) - // ??? ?? ??
- pDC-gtBitBlt(50, 50, 32, 32, MemDC, 0, 0,
SRCCOPY) - // ????? ??? ??
- pDC-gtStretchBlt(100, 50, 32, 32, MemDC, 32, 32,
-32, -32, SRCCOPY) - // 2? ???? ??
- pDC-gtStretchBlt(150, 50, 64, 64, MemDC, 0, 0,
32, 32, SRCCOPY) - // DC ??
- MemDC.SelectObject(pOldBitmap)
-
182??? ??
??????? ?????? AND ??
???? OR ??
183??? ??
- static int nAnimate
- CClientDC dc(this)
-
- CDC MemDC // ??? ?? ??? DC? ??
- MemDC.CreateCompatibleDC(dc)
- CBitmap bmpMask, bmpMan, bmpBack // ????? ??? ??
- bmpMask.LoadBitmap(IDB_MASK)
- bmpMan.LoadBitmap(IDB_MAN)
- bmpBack.LoadBitmap(IDB_BACK)
-
- CBitmap pOldBitmap (CBitmap )MemDC.SelectObjec
t(bmpBack) // ?? ?? ??? - dc.BitBlt(50, 50, 32, 32, MemDC, 50, 50,
SRCCOPY) -
- MemDC.SelectObject(bmpMask) // ??? ???
- dc.BitBlt(50, 50, 32, 32, MemDC, 32nAnimate, 0,
SRCAND) -
- MemDC.SelectObject(bmpMan) // ??? ?? ???
- dc.BitBlt(50, 50, 32, 32, MemDC, 32nAnimate, 0,
SRCPAINT)
184??? ???
- ???? ??? ??, ??? ??? ?? ?? ???
- ?? ??? ??? ?? ?? ? ?? (?????)
- ?? ?? ??? ???? ???? ??? ????? ??? ???? ??
- ???? ?? ??? ???? ?? ?? ???? ??
185??? ???? ?? ?? ??
- // ?? DC? ??? ?? ??? DC? ??
- CDC BufferDC
- BufferDC.CreateCompatibleDC(pDC)
- // ?? DC? ??? ?? ??? ???? ??
- CBitmap bmpBuffer
- bmpBuffer.CreateCompatibleBitmap(pDC, 200, 200)
-
- // ??? DC? ??? ???? ??
- CBitmap pOldBitmap (CBitmap )BufferDC.SelectOb
ject(bmpBuffer) - // ??? DC? ??? ??
- BufferDC.Rectangle(0, 0, 200, 200)
- BufferDC.Rectangle(10, 10, 100, 100)
- BufferDC.Ellipse(70, 70, 180, 180)
- // ??? ???? ??? ??? ???? ??
- pDC-gtBitBlt(0, 0, 200, 200, BufferDC, 0, 0,
SRCCOPY)
18611. ?? ??? ??
187??? ?????
- ?? ???? ???? ???? ? ???? ?? ?? ?? ????? ???? ????
????? ??? - ?????? ?? ?? ??
- ???? ?? ? ????? ?? ??? ??? ?????? ??
- ???? ?????
- ????? ???? ???? ???? ?? ??? ???? ??? ??, ? ????
?? ???? ???? ???? ??? ?? - BOOL SubclassDlgItem(UINT nID, CWnd pWnd)
- nID ???????? ?? ???? ????? ?????? ID
- pWnd ???? ?? ???? ???
- ?? ?????? ? ?? SubclassDlgItem ??? ?? ????? ??? ?
188??? ?????
189??? ?????
190??? ?????
- WM_CHAR ???? ??
- ? ???? ?? ???? ??? ??? ??? ??? ??
- ?? ?? ??? ?? ???? ??
- void CNumEditOnChar(UINT nChar, UINT nRepCnt,
UINT nFlags) -
- if((nChargt'0' nCharlt'9')
(nCharVK_BACK)) - CEditOnChar(nChar, nRepCnt, nFlags)
- else
- MessageBeep(MB_ICONASTERISK)