Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual C++ Lecture 11 Friday, 29 Aug 2005. Windows Graphic User Interface l Event driven programming environment l Windows graphic libraries (X11 on Unix,

Similar presentations


Presentation on theme: "Visual C++ Lecture 11 Friday, 29 Aug 2005. Windows Graphic User Interface l Event driven programming environment l Windows graphic libraries (X11 on Unix,"— Presentation transcript:

1 Visual C++ Lecture 11 Friday, 29 Aug 2005

2 Windows Graphic User Interface l Event driven programming environment l Windows graphic libraries (X11 on Unix, Application Programming Interface on PCs) l High level libraries such as Microsoft Foundation Class (MFC) library

3 Build Window Applications with MFC l MFC : Microsoft Foundation Classes l MFC AppWizard considerably simplify coding l AppWizard generates an compile-able source code, programmer fills the details

4 A Hello MFC Program without the Wizard l In hello.h (App Class) class CMyApp : public CWinApp { public: virtual BOOL InitInstance(); };

5 A Hello MFC Program in hello.h continued class CMainWindow : public CFrameWnd { public: CMainWindow(); // constructor private: afx_msg void OnPaint(); // WM_PAINT // message handler DECLARE_MESSAGE_MAP(); };

6 Hello.cpp #include #include "hello.h" CMyApp myApp; // One and only // instance of CMyApp class // object, global

7 Hello.cpp, continued (1) BOOL CMyApp::InitInstance( ) { m_pMainWnd = new CMainWindow; m_pMainWnd -> ShowWindow(m_nCmdShow); m_pMainWnd->UpdateWindow(); return TRUE; }

8 Hello.cpp, continued (2) // CMainWindow message map // this will associate the message // WM_PAINT with the OnPaint() // handler BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd) ON_WM_PAINT() END_MESSAGE_MAP()

9 Hello.cpp, continued (3) // CMainWindow constructor CMainWindow::CMainWindow() { Create(NULL, "The Hello Application"); }

10 Hello.cpp, continued (4) // MW_PAINT Handler void CMainWindow::OnPaint() CPaintDC dc (this); CRect rect; GetClientRect(&rect); dc.DrawText("Hello, MFC", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); }

11 Some Observations l No main() function – the program starts from WinMain() which is in the MFC l Programmer implements member functions – no explicit control on program flow of execution

12 Event-Driven Programming l User of the program generates events l The WinMain dispatches events to the event handlers l The programmer writes event handlers l The order of program execution is determined by the sequence of relevant events

13 Window Events (Messages) l WM_PAINT: draw/redraw window l WM_CHAR: character typed l WM_LBUTTONDOWN: Left button l pressed l WM_MOUSEMOVE: mouse moved l WM_QUIT: about to terminate l WM_SIZE: a window is resized

14 Generate an Empty Window with AppWizard 1. Choose File -> New 2. Open Project tab, create project “WinGreet” 3. Choose “MFC AppWizard (exe)” 4. Click OK 5. Fill AppWizard dialog box steps 1 to 6. 6. Finish

15 AppWizard Steps 1. Choose single document 2. Default (database support none) 3. Remove ActiveX Controls 4. Choose only 3D controls 5. Default and “as statically linked library” 6. Default

16 Files Generated l Takes a look of the files and classes generated in the “Workspace” l Build and run the application

17 Modifying WinGreetDoc.h class CWinGreetDoc: public CDocument { protected: char *m_Message; public: char *GetMessage( ) { return m_Message; } protected: // create from serialization …

18 Modifying WinGreetDoc.cpp CWGreetDoc:: CWinGreetDoc( ) { // TODO: one-time construction code m_Message = “Greetings!”; }

19 Modifying WinGreetView.cpp Void CWinGreetView::OnDraw(CDC* pDC) { CWinGreetDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for … RECT ClientRect; GetClientRect(&ClientRect); pDC -> DrawText(pDoc->GetMessage(), -1, &ClientRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); }

20 Classes l Document Derived from CDocument, for storing program data, save/read files l View Derived from CView, for view window display and user input l Main Frame Window Derived from CFrameWnd, for managing main window l Application Derived from CWinApp, general tasks

21 MFC Class Hierarchy COject CCmdTarget Cwnd CFile CDC CMenu CWinThread CDocument CWinApp CFrameWnd CDialog CView CButton CClientDC CPaintDC CPoint, CRect, CString

22 The Flow of Program Control WinMain CWinGreetApp entry exit Message loop Initialization of global objects InitInstance

23 Document/View Architecture WinApp object View object FrameWnd object Document object WinApp handles File New, Open, and Exit View handles OnDraw, receives mouse and keyboard messages Store and process data Frame shows hidden menu bars, status bar

24 Example, Interactive Drawing Program l A window program that draw lines by click mouse l First click starts the line, press and drag mouse to the end point of the line, release mouse draw a permanent line

25 In MiniDrawView.h class CMiniDrawView : public CView { protected: CString m_ClassName; int m_Dragging; HCURSOR m_HCross; CPoint m_PointOld; CPoint m_PointOrigin; protected: // create from serialization

26 In MiniDrawView.cpp CMiniDrawView::CMiniDrawView() { // TODO: add construction code here m_Dragging = 0; // mouse not dragged m_HCross = AfxGetApp() -> LoadStandardCursor(IDC_CROSS); // handle to cross cursor }

27 Add OnLButtonDown Handler with Wizard 1. View -> ClassWizard 2. In ClassWizard dialog box, opne Message Maps tab 3. Select CMiniDrawView class 4. Select CminiDrawView in Oject IDs: list 5. Select WM_LBUTTONDOWN in Message: list 6. Click Add Function, click Edit Code

28 OnLButtonDrown Handler Code void CMiniDrawView::OnLButtonDown(UINT nFlags, CPoint point) {m_PointOrigin = point; m_PointOld = point; SetCapture(); m_Dragging = 1; RECT Rect; // confine the mouse cursor GetClientRect(&Rect); ClientToScreen(&Rect); ::ClipCursor(&Rect); CView::OnLButtonDown(nFlags, point); }

29 Mouse Move Handler void CMiniDrawView::OnMouseMove(UINT nFlags, CPoint point) { ::SetCursor(m_HCross); if(m_Dragging) {CClientDC ClientDC(this); // create device context ClientDC.SetROP2(R2_NOT); // drawing mode ClientDC.MoveTo(m_PointOrigin); // erase old ClientDC.LineTo(m_PointOld); ClientDC.MoveTo(m_POintOrigin); // draw new line ClientDC.LineTo(point); m_PointOld = point; // update end point } CView::OnMouseMove(nFlags, point); }

30 Button Up Handler void CMiniDrawView::OnLButtonUp(UINT nFlags, CPoint point) {if(m_Dragging) { m_Dragging = 0; // no longer dragging ::RelaseCapture(); // mouse can go anywhere ::ClipCursor(NULL); CClientDC ClientDC(this); ClientDC.SetROP2(R2_NOT); ClientDC.MoveTo(m_PointOrigin); ClientDC.LineTo(m_PointOld); // temporary line ClientDC.SetROP2(R2_COPYPEN); ClientDC.MoveTo(m_PointOrigin); ClientDC.LineTo(point); // permanent line draw } CView::OnLButtonUp(nFlags, point); }

31 References l Mastering Visual C++ 6, Michael J. Young l Programming Windows 95 with MFC, Jeff Prosise l Introduction to MFC Programming with Visual C++, Richard M. Jones


Download ppt "Visual C++ Lecture 11 Friday, 29 Aug 2005. Windows Graphic User Interface l Event driven programming environment l Windows graphic libraries (X11 on Unix,"

Similar presentations


Ads by Google