Chapter2: Drawing a Window
Chapter 2: Drawing a Window The Windows GDI
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
≒ 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
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
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
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); }
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
WM_PAINT – Invalid Region When it is no longer valid:
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);
Chapter 2: Drawing a Window Drawing with the GDI
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)
Code Practice When the left mouse button is clicked, Draw a rectangle at the position of the mouse click
How to add Windows Event Handler Using CMainWindow’s Properties window: Adding Window Messages
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));
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
Drawing test:
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)
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) http://msdn2.microsoft.com/ko-kr/library/zzs00fs6(VS.80).aspx
void GetClientRect(CRect) Practice: draw an ellipse fit to the client area CRect rect; GetClientRect(rect);
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)
Coding practice
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”)); http://msdn2.microsoft.com/ko-kr/library/e37h9k5s(VS.80).aspx
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“)));
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