slide1 n.
Download
Skip this Video
Download Presentation
Windows GUI Programming with Win32 and MFC

Loading in 2 Seconds...

play fullscreen
1 / 29

Windows GUI Programming with Win32 and MFC - PowerPoint PPT Presentation


  • 199 Views
  • Uploaded on

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 ?

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 'Windows GUI Programming with Win32 and MFC' - anne-tate


Download Now 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
slide1

DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING

CONCORDIA UNIVERSITY

Windows GUI Programming

with Win32 and MFC

by Emil Vassev

Win32 & MFC

Feb 6, 2008

windows programming model
Windows Programming Model

:: Win32 API & MFC

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

windows programming model1
Windows Programming Model

:: Introduction

  • Windows programming isevent-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.

Win32 & MFC

slide4

WndProc

Win32API Model

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

Win32 & MFC

win32
Win32

:: Window Procedure

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

Win32 & MFC

win321
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

Win32 & MFC

win322
Win32

:: Windows Messages - I

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.

Win32 & MFC

win323
Win32

:: Windows Messages - II

WM_MOUSEMOVE

  • Sent 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:

Win32 & MFC

win324
Win32

:: WinMain – The Application Entry Point

  • 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:
  • 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.

Win32 & MFC

win325
Win32

:: WinMain – Creating the Window

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

Win32 & MFC

win326
Win32

:: Create your First WIn32 Application

  • Use the Win32 App wizard:

Win32 & MFC

win327
Win32

:: Additional Reading Material and Code Samples

  • http://www.tenouk.com/cplusplusnmfc.html
  • theForger’s Win32 API Programming Tutorial

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

Win32 & MFC

mfc microsoft foundation classes
MFC – Microsoft Foundation Classes

:: Visual C++ Application Build Process

Win32 & MFC

mfc microsoft foundation classes1
MFC – Microsoft Foundation Classes

VC++ Windows Application

MFC Library

Win32 API

Computer Hardware

:: Introduction

The Microsoft Foundation Classes (MFC)Library is:

  • A Hierarchy of C++ classes designedto facilitate Windows programming.
  • An alternative to using Win32API functions.
  • A Visual C++ Windows application can useeither Win32 API, MFC, or both:

Win32 & MFC

mfc microsoft foundation classes2
MFC – Microsoft Foundation Classes

:: Characteristics

MFC Library:

  • Comprises about 200 MFC classes (versus more than 2000APIfunctions). MFC Hierarchy Chart.
  • Provides a framework upon which to buildWindows applications.
  • Is object oriented (arguably) - encapsulates most of the Win32 API in a setof logically organized classes.
  • Has the convenience of code reuse:
    • Many tasks common to all Windows apps areprovided by MFC.
    • Our programs can inherit and modify thisfunctionality as needed.
    • We don't need to recreate these tasks.
    • MFC handles many clerical details in Windowsprograms.

Win32 & MFC

mfc microsoft foundation classes3
MFC – Microsoft Foundation Classes

:: Creating an MFC Application

  • 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 -CWinAppand CFrameWnd, which can be used to create a window & the application.
  • CWinAppprovides 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.

Win32 & MFC

mfc microsoft foundation classes4
MFC – Microsoft Foundation Classes

:: The Window

  • To create a useful frame window we create a class that derivesthe MFC class CFrameWnd.
  • We create the window by using the CFrameWnd::Create() function.

Win32 & MFC

mfc microsoft foundation classes5
MFC – Microsoft Foundation Classes

:: The Application

  • 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 functionCWinApp::InitInstance()– compare to Win32 InitInstance().

Win32 & MFC

mfc microsoft foundation classes6
MFC – Microsoft Foundation Classes

:: Message Maps

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

  • The ON_WM_CLOSE macro associates the WM_CLOSE message with the OnClose() member function.

Win32 & MFC

mfc microsoft foundation classes7
MFC – Microsoft Foundation Classes

:: Sample - The Modified Window Class

Win32 & MFC

mfc microsoft foundation classes8
MFC – Microsoft Foundation Classes

:: Implementing the Message Map - I

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

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

Win32 & MFC

mfc microsoft foundation classes9
MFC – Microsoft Foundation Classes

:: Implementing the Message Map - II

BEGIN_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_LBUTTONDOWNandON_WM_LBUTTONDOWN.
  • It is closed byEND_MESSAGE_MAP.

Win32 & MFC

mfc microsoft foundation classes10
MFC – Microsoft Foundation Classes

:: Implementing the Message Map - III

ON_WM_LBUTTONDOWN & ON_WM_RBUTTONDOWN

  • Theseare the macros which declares that the MFCTutorialWindow is going to handle left and right button clicks messages.
  • The functionsthat 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.

Win32 & MFC

mfc microsoft foundation classes11
MFC – Microsoft Foundation Classes

The relationship between a document and its view

:: Documents and Views

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.

Win32 & MFC

mfc microsoft foundation classes12
MFC – Microsoft Foundation Classes

:: SDI and MDI

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

Win32 & MFC

mfc microsoft foundation classes13
MFC – Microsoft Foundation Classes

:: MFC Paint Brush Application - I

Win32 & MFC

mfc microsoft foundation classes14
MFC – Microsoft Foundation Classes

:: MFC Paint Brush Application - II

  • 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.
  • CClientDCis 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.

Win32 & MFC

slide28

Next Lecture

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

Win32 & MFC

slide29

References

  • C++ & MFC, http://www.tenouk.com/cplusplusnmfc.html
  • CoderSource.net, “MFC Tutorial”,http://www.codersource.net/codersource_mfc_prog.html

Win32 & MFC