+ All Categories
Home > Documents > Chapter2: Drawing a Window. Review: Introducing MFC.

Chapter2: Drawing a Window. Review: Introducing MFC.

Date post: 13-Dec-2015
Category:
Upload: dwayne-joseph
View: 223 times
Download: 1 times
Share this document with a friend
Popular Tags:
24
Chapter2: Drawing a Window
Transcript
Page 1: Chapter2: Drawing a Window. Review: Introducing MFC.

Chapter2: Drawing a Window

Page 2: Chapter2: Drawing a Window. Review: Introducing MFC.

Review: Introducing MFC

Page 3: Chapter2: Drawing a Window. Review: Introducing MFC.

Look into the codesclasses

Page 4: Chapter2: Drawing a Window. Review: Introducing MFC.

Our Simple Code Structure

myApp(CMyApp : CWinApp)

m_pMainFrame(CMainWindow : CFrameWnd)

Program itselfRunning message loop

“Look” of the applicationTaking care of the user interactions

Page 5: Chapter2: Drawing a Window. Review: Introducing MFC.

The big picture

• The Main function is hidden in MFC.

• CMyApp is the program which initializes the windows and runs the message loop.

• CMinWindow is included in CMyApp which is the “look” of the window and processes the messages regarding to Input/Output.

• MFC simplifies the coding work.

Page 6: Chapter2: Drawing a Window. Review: Introducing MFC.

Chapter 2: Drawing a Window

Page 7: Chapter2: Drawing a Window. Review: Introducing MFC.

Before Windows…

• In 1980s, before the birth of the “windows 95”– MS-DOS (Disk Operating System)

– Graphics Card: CGA(4colors)/EGA(16colors)/VGA(256colors)

A game using CGA graphics cardCGA graphics card

DOS did not take care of graphicsDOS did not take care of graphics

A game company took care of the graphics driversA game company took care of the graphics drivers

Page 8: Chapter2: Drawing a Window. Review: Introducing MFC.

Before Windows…

• In 1980s, before the birth of the “windows 95”– MS-DOS (Disk Operating System)

– Graphics Card:

DOS does not take care of graphicsDOS does not take care of graphics

A game company took care of the graphics driversA game company took care of the graphics drivers

Too complicated, so people started to think about

“Device-Independent”

Page 9: Chapter2: Drawing a Window. Review: Introducing MFC.

9

Two big things of WindowsTM

• Changes after windows– Multi-tasking

• We can run Multiple applications at the same time• Windows controls and distributes its resources to the

application.

– Device-independent• Windows controls the input and outputs.• Applications only communicate with Windows and do

not directly access to the actual hardwares.

Page 10: Chapter2: Drawing a Window. Review: Introducing MFC.

10

Issues when drawing

• Because of the multi-tasking– An application takes just a part of the window:

the position and the size of the window can change

– Multiple applications can run at the same time:There will be overlapping. One is hiding the other

Page 11: Chapter2: Drawing a Window. Review: Introducing MFC.

Chapter 2: Drawing a Window

The Windows GDI

Page 12: Chapter2: Drawing a Window. Review: Introducing MFC.

12

The Windows GDI

• GDI(Graphics Device Interface)– A part of the Operating System. – Translate the applications’ signals into the

hardwares’ signal.– Determine whether it is shown or not.

Applications GDIOutput Hardwares(Monitor or printer)

Devide-Independent

Device-Dependent

Page 13: Chapter2: Drawing a Window. Review: Introducing MFC.

13

GDI and Device Context

• Device Context (DC)– Data structure for information about displaying– It determines how to display in Multi-tasking GUI

environment.

CDC: MFC Device Context Class

A package for displaying:(physical hardware information,Many functions for drawing)

CDC: MFC Device Context Class

A package for displaying:(physical hardware information,Many functions for drawing)

≒ A canvas and tools ready to draw

≒ A canvas and tools ready to draw

Page 14: Chapter2: Drawing a Window. Review: Introducing MFC.

14

How to draw in Windows

• Procedure (step-by-Step)1. Application: Requires Device Context

Windows(GDI): Creates a DC and returns its handle

2. Application: Draws on the DCWindows(GDI): Checks the availability and displays the drawing if possibleCDC* pDC = GetDC (); // require DC // Do some drawing ReleaseDC (pDC); // release D

C

CDC* pDC = GetDC (); // require DC // Do some drawing ReleaseDC (pDC); // release D

C

CDC DC(this); // require DC // Do some drawing CDC DC(this); // require DC // Do some drawing

or

Using pointer

Using class

Page 15: Chapter2: Drawing a Window. Review: Introducing MFC.

15

Various kinds of Device Context

Class Name Description

CPaintDC For drawing in a window's client area (OnPaint handlers only)

CClientDC For drawing in a window's client area (anywhere but OnPaint)

CWindowDC For drawing anywhere in a window, including the nonclient area

CMetaFileDC For drawing to a GDI metafileClass Hierarchy

Page 16: Chapter2: Drawing a Window. Review: Introducing MFC.

Drawing test using PaintDC

• Where you should put your drawing code?– In the WM_PAINT message handler ==

OnPaint()void CMainWindow::OnPaint() {

CPaintDC dc(this); dc.Rectangle(50,50,250,250);dc.Ellipse(100,100,200,200);

}

void CMainWindow::OnPaint() {

CPaintDC dc(this); dc.Rectangle(50,50,250,250);dc.Ellipse(100,100,200,200);

}

Page 17: Chapter2: Drawing a Window. Review: Introducing MFC.

WM_PAINT??

• WM: Windows Message – A set of the basic messages predefined by MFC– Usually regarding to the creation/changing of the

windows, mouse controlling, and keyboard typing

• WM_PAINT– A signal from windows that it is time to update

the screen: an “invalid region” happens– When:

• A windows is popping up (when creating or maximizing)• Another window which blocked the window is moving

out.• The user (or application) demands it

Page 18: Chapter2: Drawing a Window. Review: Introducing MFC.

18

WM_PAINT – Invalid Region

• When it is no longer valid:

Page 19: Chapter2: Drawing a Window. Review: Introducing MFC.

19

WM_PAINT from yourself

• You may need to update the screen when the contents change

• You can make Windows to send WM_PAINT message to your application

– Make the screen invalid == Invalidate

void CWnd::Invalidate (BOOL bErase = TRUE);void CWnd::Invalidate (BOOL bErase = TRUE);

Page 20: Chapter2: Drawing a Window. Review: Introducing MFC.

20

Member functions of the CDC

• Drawing shapes

dc.Rectangle (x1, y1, x2, y2);dc.Ellipse (x1, y1, x2, y2);

(x1, y1)

(x2, y2)

Function name function

Rectangle() Drawing a rectangle

Ellipse() Drawing an ellipse

Page 21: Chapter2: Drawing a Window. Review: Introducing MFC.

CPaintDC Practice

• Draw rectangles:

Page 22: Chapter2: Drawing a Window. Review: Introducing MFC.

Code Practice 1

1. Draw a small rectangle

2. Enlarge the size of the rectangle when the left mouse button is clicked

3. Reduce the size of the rectangle when the right mouse button is clicked

Page 23: Chapter2: Drawing a Window. Review: Introducing MFC.

How to add Windows Event Handler

• Using CMainWindow’s Properties window:

Adding Window Messages

Page 24: Chapter2: Drawing a Window. Review: Introducing MFC.

Code Practice2

1. When the left mouse button is clicked, Draw a rectangle at the position of the mouse click


Recommended