Visual C++ Lecture 11 Friday, 29 Aug 2005. Windows Graphic User Interface l Event driven programming...

Post on 29-Dec-2015

214 views 2 download

Tags:

Transcript of Visual C++ Lecture 11 Friday, 29 Aug 2005. Windows Graphic User Interface l Event driven programming...

Visual C++

Lecture 11Friday, 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

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

A Hello MFC Programwithout the Wizard

In hello.h (App Class)class CMyApp : public CWinApp{public:

virtual BOOL InitInstance();};

A Hello MFC Program in hello.h continued

class CMainWindow : public CFrameWnd{public:

CMainWindow(); // constructorprivate:

afx_msg void OnPaint(); // WM_PAINT // message handler

DECLARE_MESSAGE_MAP();};

Hello.cpp

#include <afxwin.h>#include "hello.h"

CMyApp myApp; // One and only // instance of CMyApp class // object, global

Hello.cpp, continued (1)

BOOL CMyApp::InitInstance( ){

m_pMainWnd = new CMainWindow;m_pMainWnd -> ShowWindow(m_nCmdShow);m_pMainWnd->UpdateWindow();return TRUE;

}

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()

Hello.cpp, continued (3)

// CMainWindow constructor

CMainWindow::CMainWindow(){

Create(NULL, "The Hello Application");

}

Hello.cpp, continued (4)

// MW_PAINT Handlervoid CMainWindow::OnPaint()

CPaintDC dc (this); CRect rect;

GetClientRect(&rect);

dc.DrawText("Hello, MFC", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

}

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

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

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

Generate an Empty Window with AppWizard

1. Choose File -> New2. Open Project tab, create project

“WinGreet”3. Choose “MFC AppWizard (exe)”4. Click OK5. Fill AppWizard dialog box steps 1

to 6.6. Finish

AppWizard Steps

1. Choose single document2. Default (database support none)3. Remove ActiveX Controls4. Choose only 3D controls5. Default and “as statically linked

library”6. Default

Files Generated

Takes a look of the files and classes generated in the “Workspace”

Build and run the application

Modifying WinGreetDoc.h

class CWinGreetDoc: public CDocument{

protected: char *m_Message;

public: char *GetMessage( ) { return m_Message; }

protected: // create from serialization …

Modifying WinGreetDoc.cpp

CWGreetDoc:: CWinGreetDoc( ){// TODO: one-time construction code

m_Message = “Greetings!”;}

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);}

Classes

DocumentDerived from CDocument, for storing program

data, save/read files View

Derived from CView, for view window display and user input

Main Frame WindowDerived from CFrameWnd, for managing main

window Application

Derived from CWinApp, general tasks

MFC Class Hierarchy

COject

CCmdTarget Cwnd CFile CDC CMenu

CWinThread CDocument

CWinApp

CFrameWnd

CDialog

CView

CButton

CClientDC

CPaintDC

CPoint, CRect, CString

The Flow of Program Control

WinMainCWinGreetApp

entry

exit

Message loop

Initialization of global objects

InitInstance

Document/View Architecture

WinAppobject

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

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

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

In MiniDrawView.cpp

CMiniDrawView::CMiniDrawView(){

// TODO: add construction code herem_Dragging = 0; // mouse not draggedm_HCross = AfxGetApp() ->

LoadStandardCursor(IDC_CROSS); // handle to cross cursor}

Add OnLButtonDown Handler with Wizard

1. View -> ClassWizard2. In ClassWizard dialog box, opne

Message Maps tab3. Select CMiniDrawView class4. Select CminiDrawView in Oject IDs: list5. Select WM_LBUTTONDOWN in

Message: list6. Click Add Function, click Edit Code

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);}

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 modeClientDC.MoveTo(m_PointOrigin); // erase oldClientDC.LineTo(m_PointOld);ClientDC.MoveTo(m_POintOrigin); // draw new lineClientDC.LineTo(point);m_PointOld = point; // update end point

}CView::OnMouseMove(nFlags, point);

}

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);

}

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