1 / 31

Visual C++

Visual C++. Lecture 11 Friday, 29 Aug 2005. Windows Graphic User Interface. Event driven programming environment Windows graphic libraries (X11 on Unix, Application Programming Interface on PCs) High level libraries such as Microsoft Foundation Class (MFC) library.

rmarco
Download Presentation

Visual C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  4. A Hello MFC Programwithout the Wizard • 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 <afxwin.h> #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 • No main() function – the program starts from WinMain() which is in the MFC • Programmer implements member functions – no explicit control on program flow of execution

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

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

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

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

  16. Files Generated • Takes a look of the files and classes generated in the “Workspace” • 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 • Document Derived from CDocument, for storing program data, save/read files • View Derived from CView, for view window display and user input • Main Frame Window Derived from CFrameWnd, for managing main window • Application Derived from CWinApp, general tasks

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

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

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

  24. Example, Interactive Drawing Program • A window program that draw lines by click mouse • 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 • View -> ClassWizard • In ClassWizard dialog box, opne Message Maps tab • Select CMiniDrawView class • Select CminiDrawView in Oject IDs: list • Select WM_LBUTTONDOWN in Message: list • 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 • Mastering Visual C++ 6, Michael J. Young • Programming Windows 95 with MFC, Jeff Prosise • Introduction to MFC Programming with Visual C++, Richard M. Jones

More Related