Introduction to Windows Programming Jim Fawcett Fall 2005

Download Report

Transcript Introduction to Windows Programming Jim Fawcett Fall 2005

Introduction to Windows Programming

Jim Fawcett CSE 681 – Software Modeling and Analysis Fall 2005 Basic Windows Programming Page 1

References

    Developing Professional Applications for Windows 95 and NT Using MFC, Marshall Brain and Lance Lovett, Prentice Hall, 1997.

– Now out of print, but some copies still available on-line.

Practical Visual C++ 6, Jon Bates and Tim Tompkins, Que, 1999.

– Currently available through Barnes & Noble, Borders, and Amazon.com.

Programming Windows with MFC, Jeff Prosise, Microsoft Press, 1999.

– This is a large, effective book, used in CSE 778 – Advanced Windows Programming, but overkill for this course.

Windows Forms Programming in C#, Chris Sells, Addison-Wesley, 2004 – Comprehensive and very well written.

Basic Windows Programming Page 2

user events via keyboard or mouse

Classic Windows Processing

win32 API calls

WinMain

implements message loop messages

windows

implements message queue and message routing provides win32 API messages register windows "class" create window show window update window get message dispatch message

windows controls

messages process windows messages

WinProc

implements message handling for application application function calls application functionality Basic Windows Programming Page 3

The Simplest Classical Windows Program

 Output from the MINWIN program is shown on the next slide. The program: – Creates a frame window – Paints a fixed text message in the window – Reacts to left mouse button clicks by creating a MessageBox  You will the program code, minwin.cpp, and project workspace, minwin.dsw in the folder MINWIN.

www.ecs.syr.edu/faculty/fawcett/handouts/CoreTechnologies/WindowsP rogramming/code/basicWindows/Minwin Basic Windows Programming Page 4

Basic Windows Programming Page 5

Windows Processing Using MFC

windows Microsoft Foundation Classes (MFC)

CWinApp CStatic CEdit CFrameWnd CButton CApp single global instance starts processing user event via keyboard or mouse Basic Windows Programming process windows messages CWindow function calls application functionality Page 6

Partial MFC Hierarchy Providing Support for Windows Programming

CObject CDC CMenu CGdiObject CCmdTarget CFile database support classes utility classes CWinThread CWnd CDocument CDocItem CDocTemplate COle classes CWndApp CSingleDocTemplate CMultiDocTemplate CFrameWnd CDialog CControlBar CView CPropertyPage Basic Windows Programming CButton CComboBox CEdit CStatic CScrollBar CListBox other control classes Page 7

The Simplest MFC based Program

 Output from the MFCWIN program is shown on the next slide. The program: – Creates a frame window – Paints a fixed text message in the window – Reacts to left mouse button clicks by creating a MessageBox  You will the program code, mfcwin.cpp, and project workspace, mfcwin.dsw in the folder MINWIN.

www.ecs.syr.edu/faculty/fawcett/handouts/CoreTechnologies/WindowsP rogramming/code/basicWindows/MfcWin Basic Windows Programming Page 8

Basic Windows Programming Page 9

Dialog Based Applications

 A dialog is an opaque window, called a form, on which are embedded a collection of smaller windows called controls.

– The main dialog form is wrapped in a dialog class, derived from MFC’s CDialog class.

– Each control is a window with a self contained set of functionality that dialog designers use “as-is”, or modify in limited ways.

– Controls are placed on the dialog form by dragging from a palette in the dialog resource editor.

– Controls are hooked into the application processing by providing message handlers in the main dialog class that access the controls using CDialog::GetDlgItem(…) and CDialog::SetDlgItem(…) member functions.

Basic Windows Programming Page 10

Basic Windows Programming Page 11

Structure of MFC Dialog based Application

CWinThread CWnd CWinApp

Object stored locally in CMyApp::InitInstance()

CDialog CMyApp

Basic Windows Programming

CMyDlg

Page 12

Frame Window Applications

 A frame window application consists of a window that supports a large client area in which text and graphics are displayed, embedded in a frame that supports menus and perhaps some controls.

– All text and drawing in the client area are supported by the Graphics Device Interface Object (CGDIObject) and Device Context (CDC) classes.

– Most frame window applications use the Single Document Interface (SDI) or Multiple Document Interface (MDI) architectures. These follow document/view architecture structure: • Documents provide all the support for managing the program’s data, including reading and writing from persistent storage.

• Views provide all the support for displaying one or more views of the data and most user interactions with the program.

Basic Windows Programming Page 13

Basic Windows Programming Page 14

CDialog CAboutDlg CWinApp

Developer adds message map entries to associate events like button clicks with event handler functions

CGdiDemoApp

local object defined in OnAboutDlg( ) local object in InitInstance( )

CView CGdiDemoView Attribute: CPoint pentagon[5] CPoint center float angle Operation: OnDraw( ) OnLButtonDown(...)

Developer adds functionality to OnDraw( ) and adds event handlers like OnLButtonDown( )

CPoint CFrameWnd CSingleDocTemplate CDocument CMainFrame

Developer can add menus to this object, connected through event map and handlers to application code in Doc or View classes Basic Windows Programming Developer can add code to document class to manage window's data, i.e., read and write to file.

GdiDemoDoc

GdiDemo Logical Structure

generated by appwizard, modified by developer

Page 15

resource.h

afxres.rc

GdiDemo.rc

afxres.h

GdiDemo.rc2

GdiDemo.h

GdiDemo.cpp

GdiDemoDoc.h

GdiDemoDoc.cpp

MainFrm.h

stdafx.h

stdafx.cpp

MainFrm.cpp

GdiDemoView.h

GdiDemoView.cpp

Basic Windows Programming

GdiDemo Physical Structure

generated by appwizard

Page 16

WinForms

   WinForms were introduced with .Net and are very similar to Visual Basic RAD Forms in Visual Studio, version 6.0.

Forms are used for both Frame Window and Dialog Applications.

– You get a dialog application by pulling lot’s of controls onto a form and setting the Form properties to have dialog behavior.

– You get a Frame Window by painting the Form your self, with the help of the .Net Graphics Device Interface (GDI++) Forms are quite easy to build with the Visual Studio Designers – Lots of controls are available in the toolbox – Intellisense and the Object Browser make learning very fast.

Basic Windows Programming Page 17

Simple Text Editing Demo

 It is relatively easy to build Forms that: – open and save files using a FileDialog.

– Read files from Streams and edit text in a TextBox.

• The Stream and TextBox do almost all the work.

– Uses menus to interact with files or set program behaviours.

 www.ecs.syr.edu/faculty/fawcett/handouts/CSE681/code/project2HelperF05 Basic Windows Programming Page 18

Basic Windows Programming Page 19

Programs with Communicating Forms

 Often we want to use more than one Form in a program.

– That means that we have to provide a way for them to communicate.

– Each Form that needs to contact another must have a reference to it.

– That happens automatically for the parent. The parent creates a Child Form attached to a reference it holds onto for later use.

– For the Child to talk back, the Parent must pass it a reference to itself, usually in the Childs constructor.

– For these conversations to be useful, both Parent and Child must provide functions for the other to call.

 www.ecs.syr.edu/faculty/fawcett/handouts/CSE681/code/project2HelperF05 Basic Windows Programming Page 20

Basic Windows Programming Page 21

An Example – Form with Browse Control

 If our program needs more than a simple FileDialog provides, we may wish to build a more sophisticated or specialized control.

 On the next page you will see an example of a Form, written in managed C++ invoking the services of a Browse Control, written in C#.

 www.ecs.syr.edu/faculty/fawcett/handouts/CoreTechnologies/WindowsP rogramming/code/demoCppWinForm Basic Windows Programming Page 22

Basic Windows Programming Page 23

Frame Window based on Form

 The next slide illustrates a WinForm application that behaves like a Frame Window: – It paints is client area with: • An image • A drawn text caption • A line – But it also has inserted a control, showing mouse coordinates. This illustrates that controls can be used on Frame Windows: • They paint their own surfaces • They can react to event messages from their parent window.

 www.ecs.syr.edu/faculty/fawcett/handouts/CoreTechnologies/WindowsP rogramming/code/CSharpWinForm Basic Windows Programming Page 24

Basic Windows Programming Page 25

End of Presentation

Basic Windows Programming Page 26