Chapter2: Drawing a Window
Review: Introducing MFC
Look into the codesclasses
Our Simple Code Structure
myApp(CMyApp : CWinApp)
m_pMainFrame(CMainWindow : CFrameWnd)
Program itselfRunning message loop
“Look” of the applicationTaking care of the user interactions
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.
Chapter 2: Drawing a Window
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
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”
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.
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
Chapter 2: Drawing a Window
The Windows GDI
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
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
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
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
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);
}
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
18
WM_PAINT – Invalid Region
• When it is no longer valid:
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);
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
CPaintDC Practice
• Draw rectangles:
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
How to add Windows Event Handler
• Using CMainWindow’s Properties window:
Adding Window Messages
Code Practice2
1. When the left mouse button is clicked, Draw a rectangle at the position of the mouse click