MFC Application Frameworks (AFX) - PowerPoint PPT Presentation

1 / 77
About This Presentation
Title:

MFC Application Frameworks (AFX)

Description:

Title: PowerPoint Author: 3 Last modified by: kbsuh_note Created Date: 8/9/2001 4:38:52 AM Document presentation format: – PowerPoint PPT presentation

Number of Views:447
Avg rating:3.0/5.0
Slides: 78
Provided by: 355
Category:

less

Transcript and Presenter's Notes

Title: MFC Application Frameworks (AFX)


1
MFC Application Frameworks (AFX)
2
???? ??? interface
3
Application Framework
4
AFX? Class
?? ?? MFC ???? ?? ???
??? ???? ?? ??
????? ????? ??
???? ???? ???? ??
???? ??? ?? (?? ??? ????)
???? ??? ???(??)? ???? ??
???? ???? ??? ???? ??
5
Application Wizard ? ?? ??? ?????
6
CFrameWnd
protected/private ??
?? ??
?? ??
CWinApp
CDocument
CView
CMyApp ???? ???? (?? ??)
7
CWinApp ???
  • ????? ??? ?? ??
  • ?? ?? theApp ? ???? ??
  • ??? ??? ??
  • ??? ?? (????)
  • InitInstance( )
  • Run( )
  • ExitInstance( )
  • // WM_QUIT messge ? ??? ?? loop ? ?? ??

8
CWinApp ???? ??
9
CWinApp ?? ???? ??
CWinApp
10
InitInstance ?? (CWinApp ? ?????)
  • BOOL CMyAppInitInstance()
  • ..
  • CSingleDocTemplate pDocTemplate
  • pDocTemplate new CSingleDocTemplate(
  • IDR_MAINFRAME,
  • RUNTIME_CLASS(CMyDoc), // Document class ?
    MyDoc ? ???? ??
  • RUNTIME_CLASS(CMainFrame), // main SDI
    frame window
  • RUNTIME_CLASS(CMyView))
  • AddDocTemplate(pDocTemplate) // CWinApp class
    ?? ??
  • AFX ???? ???? ???, ??? ??? ???, ????? ???? ??.

11
??? ???
12
Message ???
13
??? ??? ??
14
??? ?? ??? ???
??? ????? ???
???
????
??? ?
CWinAppRun( ) switch( ) case
??? ???
WM_ACTIVATE
WM_CREATE
OnCreate OnActivate OnMove
WM_ACTIVATE
WM_MOVE
15
CWnd ???
  • CFrameWnd ???? Cview ???? ???
  • 300 ?? member ??? ??? ??
  • ??? ??- ??? ?? , ?? , ??,??? ??
  • ??? ??- message handler ? ??.
  • Messge Handler? overriding
  • int CMainFrameOnCreate(LPCREATESTRUCT
    lpCreateStruct) // Cframwnd? ?????
  • .
  • if (CFrameWndOnCreate(lpCreateStruct) -1)
  • return -1
  • // ??? ????? ?? ??? ??? ?
  • ..

16
Message Handler ??? overriding
WM_CREATE
WM_MOVE
WM_SIZE
17
MESSAGE MAP
  • ??? ????? -gt ?? class ?? ????? ??
  • Message handler -gt ?? ??? ?? ?? ?? ?? message map
    ? ??? ???? overridng ? ???.
  • ???
  • ?? binding ? ?? 4byte ? ??? memory ? ?? ??
    messge handler ? ???? ? ??? ??
  • ?? messge handler ? ??? overriding ? ??
  • -gt Message map ? ?? (virtual ?? ???? ??)
  • ??? macro
  • ?????? message ??? ??? ??? ???? ?? ???? ??? ????,
    ?????? ??? ????? ?.
  • ? ??? map ? ??? ??? ???? ?? ???? ???? ????.

18
MESSAGE MAP ? ?
19
ClassWizardCntlW
Initinstance ?? message ? ?? initInstance ???
???? overriding ?? ??
20
ExitInstance ??? overriing ? ???.
21
ExitInstance ??? overriing ? ???.(????)
22
Exitinstance() ?? ??
??? ????? ??
23
MyApp.h ??? ????? ???
24
WM_ACTIVATE ??? message ? ???? ??? overriding
25
OnActivate ?? Overriding ?
26
MainFrm.h (???)
  • 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()
  • ???? ?? ? ??? afx_msg ? virtual ?? ???? ???,
    overriding ? ???? ???.
  • ?? ??? compile? error ??
  • define afx_msg

27
MainFrm.c (???)
  • ..
  • BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  • //AFX_MSG_MAP(CMainFrame)
  • ON_WM_CREATE()
  • ON_WM_ACTIVATE()
  • //AFX_MSG_MAP
  • END_MESSAGE_MAP()
  • ..
  • void CMainFrameOnActivate(UINT nState, CWnd
    pWndOther, BOOL bMinimized)
  • CFrameWndOnActivate(nState, pWndOther,
    bMinimized)
  • // TODO Add your message handler code here

28
CFrameWnd Class
  • ??? ??, ?? ,????, ??, ??, ???? ??
  • ltMainFrame.hgt
  • class CMainFrame public CFrameWnd
  • protected // control bar embedded members
  • CStatusBar m_wndStatusBar // CStatusBar ???
    object ? MFC class
  • CToolBar m_wndToolBar //CToolBar - ?? object
    ? MFC class
  • // 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()

29
CFrameWnd Class(MainFrame.cpp)
  • int CMainFrameOnCreate(LPCREATESTRUCT
    lpCreateStruct)
  • if (CFrameWndOnCreate(lpCreateStruct) -1)
  • return -1
  • // ??? ???? Create ? ???? ??? ??
  • if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT,
    WS_CHILD WS_VISIBLE CBRS_TOP CBRS_GRIPPER
    CBRS_TOOLTIPS CBRS_FLYBY CBRS_SIZE_DYNAMIC)
    !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  • TRACE0("Failed to create
    toolbar\n")
  • return -1 // fail to create
  • // ???? ???? Create? ???? ???? ??
  • 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
  • // TODO Delete these three lines if you don't
    want the toolbar to be dockable
  • m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY)
  • EnableDocking(CBRS_ALIGN_ANY)
  • DockControlBar(m_wndToolBar)
  • return 0

30
Cview ???
  • ???? ?????? ?? CWnd class ? ??? ??, message
    ???? ??
  • ??????? ?? Application ??? ??. ?? ?? ??? ?? ?.
  • ltMyView.hgt
  • class CMyView public CView
  • public
  • CMyDoc GetDocument() // Document Object ?
    pointer ? ?? ??
  • // Overrides
  • // ClassWizard generated virtual function
    overrides
  • //AFX_VIRTUAL(CMyView)
  • public
  • virtual void OnDraw(CDC pDC) // overridden to
    draw this view
  • virtual BOOL PreCreateWindow(CREATESTRUCT cs)
  • protected
  • virtual BOOL OnPreparePrinting(CPrintInfo
    pInfo)
  • virtual void OnBeginPrinting(CDC pDC,
    CPrintInfo pInfo)
  • virtual void OnEndPrinting(CDC pDC, CPrintInfo
    pInfo)
  • //AFX_VIRTUAL

31
?? ???? (??? message)
32
MyView.cpp
BOOL CMyViewPreCreateWindow(CREATESTRUCT
cs) // TODO Modify the Window class or styles
here by modifying // the CREATESTRUCT
cs return CViewPreCreateWindow(cs) //
CMyView drawing void CMyViewOnDraw(CDC
pDC) CMyDoc pDoc GetDocument() ASSERT_VALI
D(pDoc) // TODO add draw code for native data
here BOOL CMyViewOnPreparePrinting(CPrintInfo
pInfo) // default preparation return
DoPreparePrinting(pInfo) // chapter17? void
CMyViewOnBeginPrinting(CDC /pDC/,
CPrintInfo /pInfo/) // TODO add extra
initialization before printing void
CMyViewOnEndPrinting(CDC /pDC/, CPrintInfo
/pInfo/) // TODO add cleanup after
printing
  • // myView.cpp implementation of the CMyView
    class
  • include "stdafx.h"
  • include "my.h"
  • include "myDoc.h"
  • include "myView.h"
  • BEGIN_MESSAGE_MAP(CMyView, CView)
  • //AFX_MSG_MAP(CMyView)
  • //AFX_MSG_MAP
  • // Standard printing commands
  • ON_COMMAND(ID_FILE_PRINT,CViewOnFilePrint
    ) ON_COMMAND(ID_FILE_PRINT_DIRECT,CViewOnFileP
    rint)
  • ON_COMMAND(ID_FILE_PRINT_PREVIEW,CViewOnFilePri
    ntPreview)
  • END_MESSAGE_MAP()
  • // CMyView construction/destruction
  • CMyViewCMyView()
  • // TODO add construction code here
  • CMyViewCMyView()

33
Cdocument ??? (??? ?? ????)
include "stdafx.h" include "my.h" include
"myDoc.h" CMyDocCMyDoc() // TODO add one-time
construction code here CMyDocCMyDoc() BOOL
CMyDocOnNewDocument() if (!CDocumentOnNewDo
cument()) return FALSE // TODO add
reinitialization code here // (SDI documents will
reuse this document) return TRUE void
CMyDocSerialize(CArchive ar) if
(ar.IsStoring()) // TODO add storing code
here else // TODO add loading code here
  • ????? ???? ???? ??
  • ??? ???? ???? ??
  • ??? ???? ??? ??
  • ???? ???? ????
  • ???? ??? ??? View Object ? ??? ??
  • class CMyDoc public CDocument
  • protected // create from serialization only
  • CMyDoc()
  • // Overrides
  • // ClassWizard generated virtual function
    overrides
  • //AFX_VIRTUAL(CMyDoc)
  • public
  • virtual BOOL OnNewDocument()
  • virtual void Serialize(CArchive ar)
  • //AFX_VIRTUAL
  • // Implementation

34
Serialize ??(Cdocument ? ??Class- CMyDoc)
Carchive Cdocument class ? CFILE? CMemfile ??
CSocketFile class ? ????? ?? ???
void CMyDocSerialize(CArchive ar) if
(ar.IsStoring()) // TODO add storing code
here else // TODO add loading code
here // ?? Cobect ?? ?? ??
35
???? ???
  • lt???? ???gt
  • ??? ?? ???
  • ?????(?????? ???? ? ? index? ???? ??? ??? ?????
    ???)
  • ?? ??? ???
  • ????
  • ???? ???
  • CPoint 2 ?? ???? ?? ???? ???(int x,y)
  • CSize ?? ?? ??? ???? ???(int cx, cy)
  • CRect ???? ????? ????? ??? ???? ?? ???
  • (left,top,right,bottom)
  • CString ???? ???? ?? ???, ???? ??? ?? ??? ???
    ??? ???? ???? ??
  • CTime ??? ???? ?? ???
  • CTimeSpan ??? ???? ?????? ????? ?? ???? ?? ???

36
???? class
  • lt ??? ?????? ?? ???gt
  • Cfile fopen, fclose, fread,_open, _close,
    _create,_write,_read?? ??? ?? ??? ???
  • ?????? ??? database ??? ??? ??? ???(16?)
  • lt??? ?? ???gt
  • ???? ??? ?????? ????? ??? ??? ???? ?? ??.
    ???????? ?????? ??? ??? ?? ??? ????? ??.
    (chatting program)
  • CAsycSocekt, CSocket
  • ISAPI ????? NT?? ???? Information Server ? ????
    Web Server? ???? CGI ? ??? ?? Web Server?? ???
    ??? ???? ???? Web Server? ???? ??.
  • CInternetSession
  • CInternetConnection
  • CHttpConnection http
  • CFtpConnection ftp service
  • CGopherConnection gopher service
  • ltOLE ?? ???gt
  • OLE(Object Linking and Embedding)?? ??
  • ?? ?????? ?? ???? ?????? ?? ?? ??? ????? ??
    ?????.

37
?? ??? ???? ?? ???
  • Debug /Release mode
  • CDumpContext, CMemoryState memory ? ??? ???? ??
    class
  • Cexception Class ???? ???
  • 1. ??? ??
  • ???? ???
  • int m_NumberOfStudent, m_Grade
  • 2. ???? ??? ???? ?? ???? ??? ????? ?? ??? ????
    ???? ??? ???? ???? ?? ???? ?????
  • a ??
  • b ?? f Bool ? ??? ?? flag
  • by Byte(unsigned char)
  • c counter ? ???? ??
  • ch char ? ??
  • cx, cy x, y ??? ???? ?? ???? ??
  • d ??? ??
  • dbl double ???
  • h handle ? ?? (???????? ???? ??? ????, ?????
    ????? ?? ?? ?? ??? ???? ? ?? ?? ??? ?????.)

38
???? ???
  • N ?? I int ? ??
  • L long ? ??
  • P pointer ??
  • LP long(far) ??? ?? ( 32 ?? ???????? ?? ????
    ??)
  • S ???
  • SZ ? ??? ??? ???
  • U usigned int ? ??
  • W Word( unsigned short) ? ??
  • DW DWORD(unsigned long) ? ??
  • Str Cstring ??
  • ?) m_lpszFilename
  • ??? m_(member ??) lp( long pointer) sz(? ???
    ??? ???)
  • Filename(?? ???)

39
??? ??????? ??? ????
  • BOOL True or False
  • BYTE unsigned char ( 8 bit)
  • DWORD unsigned long(32bit)
  • DWORDLONG unsigned double ( 64 bit)
  • FLOAT float
  • LONG signed long(32bit)
  • LONGLONG signed double(64bit)
  • LPARAM 32bit ??? ????
  • LPCSTR ???? ??? ??? ??? ??? ???
  • LPCTSTR ???? ??? ????? ??? ??? ??? ???
  • LPSTR ???? ??? ???? ???
  • LPTSTR ???? ??? ????? ??? ???? ???
  • TCHAR ????? ??? ??
  • UNIT unsigned int(32bit)
  • WORD unsigned short(16bit)
  • WPARAM 16bit ??? ????

40
GDI/DC/GDI Object
  • GDI(Graphic Device Interface)
  • ??? ????? ??? ????? ???? ??? ?? ??? ??????? ????
    ????? ??? ??? ?????? ??.
  • DC(Device Context)
  • GDI ????(GDI Object)

41
GDI(Graphic Deviced interface) Object
  • DC(???? ????)-???? ??? ?? ??? ??? ??? ???
  • GDI Object -DC ? graphic option? ???? ????? ? ???
    ??? Option? GDI Object? ??.

42
??? ?? MFC???
  • MFC??? DC ? GDI Object?? ?? ???? ??
  • ??? ???? ???
  • CDC ???(???? ??? ???? ???? ???? ???)
  • ???? DC ??
  • CDC pDC GetDC() // ???? ??. -gt CClientDC
    dc(this)
  • pDC-gtRectangle(10,10,100,100)
    dc.Rectangle(10,10,100,100)
  • ReleaseDC(pDC) // ??
    // ????? ReleaseDC? ?
  • this pointer ?? ???? ??????? ?????? ????? ???
    ???

43
This pointer ? ???
  • Class Where
  • public
  • int data
  • void PrintPointer()
  • Void WherePrintPointer()
  • printf(????? ??? x?????.\n,this)
  • Void Main()
  • Where a, b ,c
  • a.PrintPointer()
  • b.PrintPointer()
  • c.PrintPointer()
  • ?? ????? ??? 0xffe0?????
  • ????? ??? 0xffe4?????
  • ????? ??? 0xffe8?????

44
??? ?? ????? ????.
45
MyView ?? DrawCross ?? ??
46
DrawCross ?? ??
pDC CDC class ? pointer point X ? ??? ??
47
DrawCross ? ??? ??
48
??? ?? Member ??? ???? ??.
define SIZE 10 void CMyViewDrawCross(CDC pDC,
CPoint point) pDC-gtMoveTo(point.x-SIZE,point
.y-SIZE) pDC-gtLineTo(point.xSIZE,
point.ySIZE) pDC-gtMoveTo(point.x-SIZE,
point.ySIZE) pDC-gtLineTo(point.xSIZE,
point.y-SIZE)
49
?? ???? ????? X ?? ??? ?? ?? (Class Wizard ??)
50
OnLButtonDown ?? ??
51
OnLButtonDown ??? ?? ??
52
??? ??
BEGIN_MESSAGE_MAP(CMyView, CView) //AFX_MSG_MAP
(CMyView) ON_WM_LBUTTONDOWN() // ???? overriding
? //AFX_MSG_MAP END_MESSAGE_MAP()
53
?? ??
54
??? ?? ??? ?? ??
55
??? ??? ??(WM_PAINT ??) -gtOnPaint ?? ??
WM_PAINT message window ? ?????? ???? ??? ???
message - ???? OnDraw??? ??? ??. Cview class
??? OnPaint ????? OnDraw? ??? ??. ?? ????, ?
point ? ???? ??? ?.-gt MyDoc ? ?? ???? ?.
56
CMyDoc ? ?? (m_ptData100) ??
?? ??? ??
57
??? Member ??? ???
58
void CMyViewOnLButtonDown ??? ??
59
?? ?? ??(OnDraw)??? ?????
60
??? message ??
?? menu ???? ALTENTER ??
61
??? message ? ??
?? ?? MFC ???? ?? ???
??? ???? ?? ??
????? ????? ??
???? ???? ???? ??
???? ??? ?? (?? ??? ????)
???? ??? ???(??)? ???? ??
???? ???? ??? ???? ??
62
??? ????? ? command ID
63
?????
64
Menu ???
65
??menu ? ?? ??? ??? ??? ?? ??
66
OnMagnify ?? ?? ?
67
OnOrginalize ?? ???? ??? ?? ??
?? class ?? WM_PAINT message ? ??????
UpdateAllVIews(NULL)? ??
68
???? ??? ?????? ?? ???
69
?? ??? routine ??? ??
70
???? ??? ????
71
Cdocument ???? OnOpenDocument OnSaveDocument
72
Serialize ??
  • OnOpenDocument ???? Serialize ? ?????? IsStoring
    ? FALSE??
  • OnSaveDocument ???? Serialize ? ?????? IsStoring
    ? TRUE ??

73
Serialize ?? ??
  • void CMyDocSerialize(CArchive ar)
  • if (ar.IsStoring())
  • // TODO add storing code here
  • ar ltlt m_nCount
  • for( i 0 i lt m_nCount i )
  • ar ltlt m_ptDatai
  • else
  • // TODO add loading code here
  • ar gtgt m_nCount
  • for( i 0 i lt m_nCount i )
  • ar gtgt m_ptDatai
  • Carhive ???? ???? Read, Write( binary ?????) ????
  • Readstring, WriteString( text ?????)

74
AFX ???? ?? ?? ??
75
?? ???
  • AfxGetApp ??
  • CWinApp ?????? ???? ???? ????? ??
  • CWinApp AfxGetApp()
  • ????? ?????? ?? ??
  • AfxGetMainWnd ??
  • ?? ??? ???? ???? ???? ?? ??? ??
  • CWnd AfxGetMainWnd()
  • ????? ?????? ?? ??

76
??? ??? ????? Document/View ??
  • CFrameWndGetActiveDocument ??
  • - ??? ??? ????? Document class ??
  • virtual CDocument GetActiveDocument()
  • - ?? ??? ???? ??? document class ? instance
    pointer ??
  • CFrameWndGetActiveView ??
  • - ??? ??? ????? View class ??
  • CView GetActiveView() const
  • - ?? ??? ???? ??? view class ? instance
    pointer ??

77
GDI Object ???
?? ???? ??? ???
GDI Object MFC Class
? Cpen
???(?) Cbrush
?? Cfont
??? Cbitmap
??? CPalette
?? CRgn
?? ??? ??
MoveTo, LineTo ?
Rectangle, FillRect, FrameRect,Draw3dRect ???
Ellipse ??
Pie ??
Arc,ArcTo,AngleARC, Chord ?
Polygon ???
PolyDraw,PolyBezier, PolyBezierTo ??? ??
FrameRgn ??? ???
GDI object? ??? MFC class
Write a Comment
User Comments (0)
About PowerShow.com