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

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

Using Macros and Visual Basic for Applications (VBA) with Excel
MFC Workshop: Intro to the Document/View Architecture.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Introduction to MFC. Motivation Abstract the Windows API Provides additional GUI options Insert Open Inventor into existing MFC application Document /
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Introduction to Microsoft Windows MFC Programming: the Application/Window Approach Lecture 4.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
MFC Workshop: Intro to MFC. What is MFC? Microsoft Foundation Classes C++ wrappers to the Windows SDK An application framework A useful set of extensions.
1 ENG236: ENG236: C++ Programming Environment (2) Rocky K. C. Chang THE HONG KONG POLYTECHNIC UNIVERSITY.
9/3/2015Department of IT1 The Microsoft Foundation Class Library Application Framework.
INTRODUCTION TO VC++ As the Microsoft Windows 3.X and then 5.5 operating system was becoming popular, many programmers were interested in creating graphical.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Bertrand Bellenot ROOT Users Workshop Mar ROOT GUI Builder Status & Plans ROOT & External GUI World MFC, FOX, Qt, PVSS… Snapshot of the Future.
1 Chapter 15 Drawing in a Window. 2 The Window Client Area  A coordinate system that is local to the window.  It always uses the upper-left corner of.
Building an MFC Application Using the Wizard. Terms Solution – A container for related projects – An executable or DLL – May be constructed from multiple.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
IE 411/511: Visual Programming for Industrial Applications
C++ MFCs CS 123/CS 231. MFC: Writing Applications for Windows zClasses in MFC make up an application framework zFramework defines the skeleton of an application.
BZUPAGES.COM Visual Programming Lecture – 5 Miss. SADAF MAJEED SIAL Computer Science Department Bahauddin Zakariya University Multan.
Getting Started The structure of a simple wxWidgets program, Look at where and how a wxWidgets application starts and ends, how to show the main window,
Object Oriented Programming Dr. Ennis-Cole CECS 5100.
Chapter 1: Hello, MFC Your first MFC Application Department of Digital Contents Sang Il Park.
MFC Windows Programming: Document/View Approach More detailed notes at: 360/notes-html/class15.htm.
Program Design and Coding
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2010 CHAPTER THREE Program Design and Coding.
Overview of Previous Lesson(s) Over View  Microsoft Foundation Classes (MFC)  A set of predefined classes upon which Windows programming with Visual.
Classic Controls Trần Anh Tuấn A. Week 1 How to create a MFC project in VS 6.0 How to create a MFC project in VS 6.0 Introduction to Classic Controls.
OBJECTIVE  After completing this Lab, students will upgrade their knowledge in the field of VC++.  Students will also get the clear view about the concepts.
Create a Simple MFC Application Automatically How to use the application wizard.
1 Pointer A pointer is a variable that stores an address of another variable of a particular type. A pointer has a variable name just like any other variable.
Microsoft Foundation Classes. What is MFC? Set of C++ classes written by MS Simplifies writing complex programs Covers many areas: – GUI – I/O – O/S interfaces.
Chapter 3 The mouse and the Keyboard. Getting input from the Mouse.
BZUPAGES.COM Visual Programming Lecture – 6- 7 Miss. SADAF MAJEED SIAL Computer Science Department Bahauddin Zakariya University Multan.
ActiveX Controls ActiveX Controls vs Ordinary Windows Controls Installing ActiveX Controls The Calendar Control ActiveX Control Container Programming Create.
CNS 1410 Graphical User Interfaces. Obectives Students should understand the difference between a procedural program and an Event Driven Program. Students.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Microsoft Foundation Classes
Bitmap (Chapter 15).
Chapter2: Drawing a Window. Review: Introducing MFC.
Chapter2: Drawing a Window
Chapter 8 Dialog Boxes and Property Sheet
Chapter 6: FILE I/O and Serialize CFile class. FILE I/O Serialization and the CArchive Class.
Programming with Visual Studio MFC and OpenGL. Outline Creating a project Adding OpenGL initialization code and libraries Creating a mouse event Drawing.
Microsoft Foundation Classes
Overview of Previous Lesson(s) Over View  Windows Programming  WinMain()  Where execution of the program begins and basic program initialization is.
Introduction to MFC Microsoft Foundation Classes.
Problem: Take Two Numbers, Add Them Together And Display The Results. Now To Build The Flowchart… We Probably Need One Like This… Let’s Add The Routines…
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Part II Document/View Architecture. Chapter 9 Documents, Views, and the Single Document Interface.
Drawing in Windows. To help with drawing on the Windows operating system, Microsoft created the Graphical Device Interface, abbreviated as GDI. – It is.
Creating ActiveX Controls at runtime If you need to create an ActiveX Control at runtime without a resource template entry, follow the programming steps.
Visual Info Processing Programming Guide
Building an MFC Application
Chapter 14 Windows Programming with the Microsoft Foundation Classes
Introduction to Windows Programming
MULTIPLE DOCUMENTS AND MULTIPLE VIEWS Prosise - Chapter 11
Document/View Architecture
Message Handling in MFC
Microsoft Foundation Classes MFC
Steps to Build Frame Window Recipe Application
Chapter 17 Creating the Document and Improving the View
Chapter 1 Hello, MFC.
Working with Dialogs and Controls
Chapter 1: An Introduction to Visual Basic 2015
MFC Dialog Application
Introduction to Windows Programming with Microsoft Framework Classes (MFC) Jim Fawcett Summer 2004 Syracuse University.
Steps to Build Frame Window Recipe Application
Chapter 17 Creating the Document and Improving the View
Presentation transcript:

Visual C++ Lecture 11 Friday, 29 Aug 2005

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

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

A Hello MFC Program without the Wizard l 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(); // constructor private: afx_msg void OnPaint(); // WM_PAINT // message handler DECLARE_MESSAGE_MAP(); };

Hello.cpp #include #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 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 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

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

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

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 Finish

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

Files Generated l Takes a look of the files and classes generated in the “Workspace” l 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 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

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 WinMain CWinGreetApp entry exit Message loop Initialization of global objects InitInstance

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

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

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 here m_Dragging = 0; // mouse not dragged m_HCross = AfxGetApp() -> LoadStandardCursor(IDC_CROSS); // handle to cross cursor }

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

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