Download presentation
1
Chapter2: Drawing a Window
2
Chapter 2: Drawing a Window
The Windows GDI
3
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. Devide- Independent Applications GDI Output Hardwares (Monitor or printer) Device- Dependent
4
≒ A canvas and tools ready to draw
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) ≒ A canvas and tools ready to draw
5
How to draw in Windows Procedure (step-by-Step)
Application: Requires Device Context Windows(GDI): Creates a DC and returns its handle Application: Draws on the DC Windows(GDI): Checks the availability and displays the drawing if possible Using pointer CDC* pDC = GetDC (); // require DC // Do some drawing ReleaseDC (pDC); // release D C or Using class CDC DC(this); // require DC // Do some drawing
6
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 metafile Class Hierarchy
7
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); }
8
WM_PAINT?? WM: Windows Message WM_PAINT
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
9
WM_PAINT – Invalid Region
When it is no longer valid:
10
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);
11
Chapter 2: Drawing a Window
Drawing with the GDI
12
Member functions of the CDC
Drawing shapes Function name function Rectangle() Drawing a rectangle Ellipse() Drawing an ellipse (x1, y1) dc.Rectangle (x1, y1, x2, y2); dc.Ellipse (x1, y1, x2, y2); (x2, y2)
13
Code Practice When the left mouse button is clicked, Draw a rectangle at the position of the mouse click
14
How to add Windows Event Handler
Using CMainWindow’s Properties window: Adding Window Messages
15
Drawing Functions of CDC (1/3)
Point Same with the SetPixel, but it does not return the original color value. So it is more efficient. SetPixelV Set the given color value at the position and return the original color value at the position SetPixel Get the color value at the given position. GetPixel Description Name COLORREF color = dc.GetPixel (x,y); dc.SetPixelV(x,y, RGB(r,g,b));
16
COLORREF A data type for storing a color value 32 bit as 0x00rrggbb
Macro function easy to use: COLORREF color = RGB(r,g,b); r = GetRValue (color); g = GetGValue (color); b = GetBValue (color); 16
17
Drawing test:
18
Drawing Functions of CDC (2/3)
Drawing shapes Function name Description Rectangle() Drawing a rectangle Ellipse() Drawing an ellipse (x1, y1) dc.Rectangle (x1, y1, x2, y2); dc.Ellipse (x1, y1, x2, y2); (x2, y2)
19
void GetClientRect(CRect)
How to get the window size? CRect : a class for storing a rectangle information member variables: bottom top right left CRect rect; GetClientRect(rect); (left, top) (right, top) (left, bottom) (right, bottom)
20
void GetClientRect(CRect)
Practice: draw an ellipse fit to the client area CRect rect; GetClientRect(rect);
21
Drawing Functions of CDC (3/3)
Drawing a Line Drawing a line from the original position to the given position LineTo() Move your pen to the given position MoveTo() Description Name (x1,y1) dc.MoveTo(x1,y1); dc.LineTo(x2,y2); (x2,y2)
22
Coding practice
23
Drawing a Text by using CDC
Text-related functions Sets alignment parameters SetTextAlign() Sets the background color SetBkColor() Sets the text output color SetTextColor() Draws text in a formatting rectangle DrawText() Outputs a line of test at the position TextOut() Description Function Name dc.SetTextColor(RGB(255,0,0)); dc.SetBkColor(RGB(0,255,0)); dc.SetTextAlign(TA_CENTER); dc.TextOut(300,200,_T(“Sejong University”));
24
Coding Test CRect rect; GetClientRect(rect);
dc.SetTextColor(RGB(255,0,0)); dc.SetBkColor(RGB(255,255,0)); dc.DrawText(CString(_T(“Draw Text Test”)), &rect, DT_CENTER|DT_VCENTER|DT_SINGLELINE); dc.SetTextAlign(TA_CENTER); dc.TextOut(300,200,CString(_T("Welcome“)));
25
Coding Test CRect rect; GetClientRect(rect);
dc.SetTextColor(RGB(255,0,0)); dc.SetBkColor(RGB(255,255,0)); dc.DrawText(CString(_T(“Draw Text Test”)), &rect, DT_CENTER|DT_VCENTER|DT_SINGLELINE); dc.SetTextAlign(TA_CENTER); dc.TextOut(300,200,CString(_T("Welcome“))); Output text Text Alignment The text will be placed in the rectangle x y Output text
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.