visual c++

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

Upload: keren

Post on 01-Feb-2016

73 views

Category:

Documents


1 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Visual C++

Visual C++

Lecture 11Friday, 29 Aug 2005

Page 2: Visual C++

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

Page 3: Visual C++

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

Page 4: Visual C++

A Hello MFC Programwithout the Wizard

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

virtual BOOL InitInstance();};

Page 5: Visual C++

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

Page 6: Visual C++

Hello.cpp

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

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

Page 7: Visual C++

Hello.cpp, continued (1)

BOOL CMyApp::InitInstance( ){

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

}

Page 8: Visual C++

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

Page 9: Visual C++

Hello.cpp, continued (3)

// CMainWindow constructor

CMainWindow::CMainWindow(){

Create(NULL, "The Hello Application");

}

Page 10: Visual C++

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

}

Page 11: Visual C++

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

Page 12: Visual C++

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

Page 13: Visual C++

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

Page 14: Visual C++

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

Page 15: Visual C++

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

Page 16: Visual C++

Files Generated

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

Build and run the application

Page 17: Visual C++

Modifying WinGreetDoc.h

class CWinGreetDoc: public CDocument{

protected: char *m_Message;

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

protected: // create from serialization …

Page 18: Visual C++

Modifying WinGreetDoc.cpp

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

m_Message = “Greetings!”;}

Page 19: Visual C++

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

Page 20: Visual C++

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

Page 21: Visual C++

MFC Class Hierarchy

COject

CCmdTarget Cwnd CFile CDC CMenu

CWinThread CDocument

CWinApp

CFrameWnd

CDialog

CView

CButton

CClientDC

CPaintDC

CPoint, CRect, CString

Page 22: Visual C++

The Flow of Program Control

WinMainCWinGreetApp

entry

exit

Message loop

Initialization of global objects

InitInstance

Page 23: Visual C++

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

Page 24: Visual C++

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

Page 25: Visual C++

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

Page 26: Visual C++

In MiniDrawView.cpp

CMiniDrawView::CMiniDrawView(){

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

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

Page 27: Visual C++

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

Page 28: Visual C++

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

Page 29: Visual C++

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

}

Page 30: Visual C++

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

}

Page 31: Visual C++

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