windows gui programming with win32 and mfc

29
Win32 & MFC 1 Windows GUI Programming Windows GUI Programming with Win32 and MFC with Win32 and MFC DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY Feb 6, 2008 by Emil Vassev by Emil Vassev

Upload: anne-tate

Post on 30-Dec-2015

142 views

Category:

Documents


18 download

DESCRIPTION

DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY. Windows GUI Programming with Win32 and MFC. by Emil Vassev. Feb 6, 2008. Windows Programming Model. :: Win32 API & MFC. Windows application? Examples? Win32 API ? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Windows GUI Programming with Win32 and MFC

Win32 & MFC 1

Windows GUI ProgrammingWindows GUI Programming

with Win32 and MFCwith Win32 and MFC

DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY

Feb 6, 2008

by Emil Vassevby Emil Vassev

Page 2: Windows GUI Programming with Win32 and MFC

Win32 & MFC 2

Windows Programming Model

Windows application? Examples?

Win32 API ? Application Programming Interface - the Windows

programming interface; Includes hundreds (over 2000) of functions that an

application can call to perform various tasks such as creating a window, drawing a line, and performing file input and output.

MFC ? Microsoft Foundation Classes – a class library to work

with Win32 API.

:: Win32 API & MFC

Page 3: Windows GUI Programming with Win32 and MFC

Win32 & MFC 3

Windows Programming Model

Windows programming is event-driven. Windows and its device drivers capture hardware events generated by user interaction and translates these events into messages that Windows programs can understand - Windows messages.

Windows stores these messages in data structures called message queues.

In a multitasking environment, messages are stored for each process in separate queues called application queues. Applications wait for messages related to user input.

:: Introduction

Page 4: Windows GUI Programming with Win32 and MFC

Win32 & MFC 4

•Win apps respond to events by processing messages sent by the OS.

•Event - a keystroke, a mouse click, or a command for a window to repaint itself, etc.

•The entry point for a Windows program is a function named WinMain() (not main()).

• WndProc - the most important function (known as window procedure). Processes messages for the

main window. WndProc

Win32 API Model

Page 5: Windows GUI Programming with Win32 and MFC

Win32 & MFC 5

Win32

A function that receives and processes all messages sent to the main window.

Every window object in Windows has a window procedure to respond to messages.

The system sends a message to a window procedure by passing the message data as arguments to the procedure.

WndProc() is the window procedure function we write to receive all input directed to our window.

Messages that we do not handle in WndProc() should be handled in a default way, i.e. we must call DefWindowProc() for them.

:: Window Procedure

Page 6: Windows GUI Programming with Win32 and MFC

Win32 & MFC 6

Win32

CALLBACK function can be called by the OS.

Proc params:hWnd - unique handle of the window; message – unique ID; wParam – data carried by the message;lParam – more data;

Windows messages:•WM_COMMAND. •WM_PAINT.•WM_KEYDOWN.•WM_DESTROY

Page 7: Windows GUI Programming with Win32 and MFC

Win32 & MFC 7

Win32

Over 140 predefined messages.WM_DESTROY - sent if the user has closed the window. You should post a message telling windows to destroy the window as it is shown.WM_COMMAND - sent when a menu item or an accelerator key is pressed.WM_PAINT – sent when the window needs redrawing – i.e. when it’s created, maximized, brought to front etc.WM_KEYDOWN – sent when a key is pressed. There is also a WM_KEYUP message. The wParam contains a virtual key code.

:: Windows Messages - I

Page 8: Windows GUI Programming with Win32 and MFC

Win32 & MFC 8

Win32

WM_MOUSEMOVESent to the window when the mouse is moved over its surface area. wParam indicates if a specific key or mouse button is held down. The low word of lParam is the x position of the mouse and the high word is the y position. So to retrieve the position and button states:

:: Windows Messages - II

Page 9: Windows GUI Programming with Win32 and MFC

Win32 & MFC 9

Win32

Like main() in C/C++.In this function: we set up our application; enter a loop that will continue until the application is closed.

Signatures:

:: WinMain – The Application Entry Point

•hInstance - the instance handle that uniquely identifies the application running in Windows;

•hPrevInstance - always NULL, so we can ignore it. •lpCmdLine – the command-line arguments. •nCmdShow – how the window should be shown to start with. For example, SW_SHOWMAXIMIZED and SW_SHOWMINIMIZED.

Page 10: Windows GUI Programming with Win32 and MFC

Win32 & MFC 10

Win32

WinMain() calls the InitInstance() function to create the main window:

:: WinMain – Creating the Window

Page 11: Windows GUI Programming with Win32 and MFC

Win32 & MFC 11

Win32

Use the Win32 App wizard:

:: Create your First WIn32 Application

Page 12: Windows GUI Programming with Win32 and MFC

Win32 & MFC 12

Win32

http://www.tenouk.com/cplusplusnmfc.htmltheForger’s Win32 API Programming Tutorial

http://www.winprog.org/tutorial/

:: Additional Reading Material and Code Samples

Page 13: Windows GUI Programming with Win32 and MFC

Win32 & MFC 13

MFC – Microsoft Foundation Classes :: Visual C++ Application Build Process

Page 14: Windows GUI Programming with Win32 and MFC

Win32 & MFC 14

MFC – Microsoft Foundation Classes

The Microsoft Foundation Classes (MFC) Library is: A Hierarchy of C++ classes designed to facilitate Windows

programming. An alternative to using Win32 API functions. A Visual C++ Windows application can use either Win32 API,

MFC, or both:

:: Introduction

VC++ Windows Application

MFC Library

Win32 API

Computer Hardware

Page 15: Windows GUI Programming with Win32 and MFC

Win32 & MFC 15

MFC – Microsoft Foundation Classes

MFC Library: Comprises about 200 MFC classes (versus more than 2000 API

functions). MFC Hierarchy Chart. Provides a framework upon which to build Windows applications. Is object oriented (arguably) - encapsulates most of the Win32

API in a set of logically organized classes. Has the convenience of code reuse:

Many tasks common to all Windows apps are provided by MFC.

Our programs can inherit and modify this functionality as needed.

We don't need to recreate these tasks. MFC handles many clerical details in Windows programs.

:: Characteristics

Page 16: Windows GUI Programming with Win32 and MFC

Win32 & MFC 16

MFC – Microsoft Foundation Classes

The first task to be done in any MFC application is to create a window and the MFC application running that window.

MFC provides two important classes - CWinApp and CFrameWnd, which can be used to create a window & the application.

CWinApp provides the application level functionalities. CFrameWnd provides the functionalities related to GUI. Both classes have their own message handling mechanisms,

screen-drawing functions etc., Both classes are derived from CCmdTarget which in turn is

derived from CObject. CCmdTarget is created with the capability to handle windows

messages, which is referred as Message Maps.

:: Creating an MFC Application

Page 17: Windows GUI Programming with Win32 and MFC

Win32 & MFC 17

MFC – Microsoft Foundation Classes

To create a useful frame window we create a class that derives the MFC class CFrameWnd.

We create the window by using the CFrameWnd::Create() function.

:: The Window

Page 18: Windows GUI Programming with Win32 and MFC

Win32 & MFC 18

MFC – Microsoft Foundation Classes

A MFC Windows application starts at the WinMain() function. This function is hidden by MFC, i.e. it is implemented by MFC

for you. The MFC program entry point is the member function

CWinApp::InitInstance() – compare to Win32 InitInstance().

:: The Application

Page 19: Windows GUI Programming with Win32 and MFC

Win32 & MFC 19

MFC – Microsoft Foundation Classes

Message Maps are the way by which MFC handles the application messages.

Any class which is derived from CCmdTarget is a candidate for handling messages.

A Message Map is a table that associates messages with functions. When an application receives a message, MFC will go through its

Message Map and search for a corresponding message handler. MFC has many predefined macros, which associate messages with

your member function.

Example:Example: The ON_WM_CLOSE macro associates the WM_CLOSE message

with the OnClose() member function.

:: Message Maps

Page 20: Windows GUI Programming with Win32 and MFC

Win32 & MFC 20

MFC – Microsoft Foundation Classes :: Sample - The Modified Window Class

Page 21: Windows GUI Programming with Win32 and MFC

Win32 & MFC 21

MFC – Microsoft Foundation Classes

We use only 5 additional macros for defining the Message Map.

DECLARE_MESSAGE_MAPDECLARE_MESSAGE_MAP()()

This tells the application that the class in which this is called is going to have a message map and handle messages.

A class can have only one message map. A class will be eligible to execute a message map if it is derived

from CCmdTarget or a class which is derived from CCmdTarget. 

:: Implementing the Message Map - I

Page 22: Windows GUI Programming with Win32 and MFC

Win32 & MFC 22

MFC – Microsoft Foundation Classes

BEGIN_MESSAGE_MAP & END_MESSAGE_MAPBEGIN_MESSAGE_MAP & END_MESSAGE_MAP

The first macro takes two parameters - the class name which implements the message map and the base class for it.

It then is followed by the macros which represent messages - ON_WM_LBUTTONDOWN and ON_WM_LBUTTONDOWN.

It is closed by END_MESSAGE_MAP.

:: Implementing the Message Map - II

Page 23: Windows GUI Programming with Win32 and MFC

Win32 & MFC 23

MFC – Microsoft Foundation Classes

ON_WM_LBUTTONDOWNON_WM_LBUTTONDOWN & & ON_WM_ON_WM_RRBUTTONDOWNBUTTONDOWN

These are the macros which declares that the MFCTutorialWindow is going to handle left and right button clicks messages.

The functions that will handle those messages are OnLButtonDown() and OnRButtonDown().

When there is any related click, the mentioned functions will be called automatically with the specific parameters.

:: Implementing the Message Map - III

Page 24: Windows GUI Programming with Win32 and MFC

Win32 & MFC 24

MFC – Microsoft Foundation Classes

Usually, the MFC applications are far more complex. They contain application and frame classes plus two other classes

that represent the document (CDocument) and the view (CView). The document-view architecture is the core of the MFC

application framework and is based on the Model-View-Controller design pattern.

:: Documents and Views

The relationship between a document and its view

Page 25: Windows GUI Programming with Win32 and MFC

Win32 & MFC 25

MFC – Microsoft Foundation Classes

Single Document Interface (SDI) applications support just one open document at a time.

Multiple Document Interface (MDI) applications permit two or more documents to be open concurrently and also support multiple views of a given document.

Dialog-Based MFC applications.

:: SDI and MDI

Page 26: Windows GUI Programming with Win32 and MFC

Win32 & MFC 26

MFC – Microsoft Foundation Classes :: MFC Paint Brush Application - I

Page 27: Windows GUI Programming with Win32 and MFC

Win32 & MFC 27

MFC – Microsoft Foundation Classes

When the left mouse button is pressed down, the application stores the mouse pointer’s coordinates in the variable called m_StartPoint of type CPoint.

When the mouse button is released the mouse pointer’s coordinates are stored in the variable m_EndPoint of the same CPoint type. 

The function CClientDC::MoveTo() is used for moving to a particular co-ordinate and CClientDC::LineTo() is used for drawing the line.

CClientDC is the device context that directs the outputs to the screen. There is a concept in windows programming, called Device Context in windows. This is used in conjunction with the outputs.

:: MFC Paint Brush Application - II

Page 28: Windows GUI Programming with Win32 and MFC

Win32 & MFC 28

Next Lecture

Creating MFC Applications; Dialogs; Drawing - a sample application how to draw hexagons.

Page 29: Windows GUI Programming with Win32 and MFC

Win32 & MFC 29

• C++ & MFC, http://www.tenouk.com/cplusplusnmfc.html

• CoderSource.net, “MFC Tutorial”, http://www.codersource.net/codersource_mfc_prog.html

References