Visual c
Download
1 / 31

Visual C++ - PowerPoint PPT Presentation


  • 201 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Visual C++' - keren


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Visual c

Visual C++

Lecture 11

Friday, 29 Aug 2005


Windows graphic user interface
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
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 program without the wizard
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
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();

};


Hello cpp
Hello.cpp

#include <afxwin.h>

#include "hello.h"

CMyApp myApp; // One and only

// instance of CMyApp class

// object, global


Hello cpp continued 1
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
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
Hello.cpp, continued (3)

// CMainWindow constructor

CMainWindow::CMainWindow()

{

Create(NULL,

"The Hello Application");

}


Hello cpp continued 4
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);

}


Some observations
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
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
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
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


Appwizard steps
AppWizard Steps

  • Choose single document

  • Default (database support none)

  • Remove ActiveX Controls

  • Choose only 3D controls

  • Default and “as statically linked library”

  • Default


Files generated
Files Generated

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

  • Build and run the application


Modifying wingreetdoc h
Modifying WinGreetDoc.h

class CWinGreetDoc: public CDocument

{

protected:

char *m_Message;

public:

char *GetMessage( )

{ return m_Message; }

protected: // create from serialization …


Modifying wingreetdoc cpp
Modifying WinGreetDoc.cpp

CWGreetDoc:: CWinGreetDoc( )

{

// TODO: one-time construction code

m_Message = “Greetings!”;

}


Modifying wingreetview cpp
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
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


Mfc class hierarchy
MFC Class Hierarchy

COject

CCmdTarget Cwnd CFile CDC CMenu

CWinThread CDocument

CFrameWnd

CDialog

CView

CButton

CClientDC

CPaintDC

CWinApp

CPoint, CRect, CString


The flow of program control
The Flow of Program Control

Initialization of global objects

CWinGreetApp

WinMain

InitInstance

entry

Message loop

exit


Document view architecture
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


Example interactive drawing program
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
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
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

}


Add onlbuttondown handler with wizard
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


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

}


Button up handler
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
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