Download presentation
Presentation is loading. Please wait.
Published byDwayne Joseph Modified over 9 years ago
1
Chapter2: Drawing a Window
2
Review: Introducing MFC
3
Look into the codes classes
4
Our Simple Code Structure myApp (CMyApp : CWinApp) m_pMainFrame (CMainWindow : CFrameWnd) Program itself Running message loop “Look” of the application Taking care of the user interactions
5
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.
6
Chapter 2: Drawing a Window
7
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 graphics A game company took care of the graphics drivers
8
Before Windows… In 1980s, before the birth of the “windows 95” –MS-DOS (Disk Operating System) –Graphics Card: DOS does not take care of graphics A game company took care of the graphics drivers Too complicated, so people started to think about “Device-Independent”
9
9 Two big things of Windows TM 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
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
11
Chapter 2: Drawing a Window The Windows GDI
12
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. ApplicationsGDI Output Hardwares (Monitor or printer) Devide- Independent Device- Dependent
13
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
14
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 DC Windows(GDI): Checks the availability and displays the drawing if possible CDC* pDC = GetDC (); // require DC // Do some drawing ReleaseDC (pDC); // release DC CDC* pDC = GetDC (); // require DC // Do some drawing ReleaseDC (pDC); // release DC CDC DC(this); // require DC // Do some drawing CDC DC(this); // require DC // Do some drawing or Using pointer Using class
15
15 Various kinds of Device Context Class NameDescription CPaintDCFor drawing in a window's client area (OnPaint handlers only) CClientDCFor drawing in a window's client area (anywhere but OnPaint) CWindowDCFor drawing anywhere in a window, including the nonclient area CMetaFileDCFor drawing to a GDI metafile Class Hierarchy
16
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); }
17
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
18 WM_PAINT – Invalid Region When it is no longer valid:
19
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);
20
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 namefunction Rectangle()Drawing a rectangle Ellipse()Drawing an ellipse
21
CPaintDC Practice Draw rectangles:
22
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
23
How to add Windows Event Handler Using CMainWindow’s Properties window: Adding Window Messages
24
Code Practice2 1.When the left mouse button is clicked, Draw a rectangle at the position of the mouse click
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.