380 likes | 517 Views
Chapter2: Drawing a Window. Chapter 2: Drawing a Window. Drawing with the GDI. function. Description. GetPixel. Get the color value at the given position. SetPixel. Set the given color value at the position and return the original color value at the position. SetPixelV.
E N D
Chapter 2: Drawing a Window Drawing with the GDI
function Description GetPixel Get the color value at the given position. SetPixel Set the given color value at the position and return the original color value at the position SetPixelV Same with the SetPixel, but it does not return the original color value. So it is more efficient. Drawing Functions of CDC(1/3) • Point 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); 4
Drawing Functions of CDC(2/3) • Drawing shapes (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); (right, top) (left, top) (left, bottom) (right, bottom) http://msdn2.microsoft.com/ko-kr/library/zzs00fs6(VS.80).aspx
function Description MoveTo() Move your pen to the given position LineTo() Drawing a line from the original position to the given position Drawing Functions of CDC(3/3) • Drawing a Line (x1,y1) dc.MoveTo(x1,y1); dc.LineTo(x2,y2); (x2,y2)
Function Name Description TextOut() Outputs a line of test at the position DrawText() Draws text in a formatting rectangle SetTextColor() Sets the text output color SetBkColor() Sets the background color SetTextAlign() Sets alignment parameters Drawing a Text by using CDC • Text-related functions 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“))); Text Alignment Output text The text will be placed in the rectangle x y Output text
Text Color related functions Attribute Default Get with Set with Text color black GetTextColor() SetTextColor() BackgroundColor white GetBkColor() SetBkColor() Background color mode OPAQUE TRANSPARENT GetBkMode() SetBkMode()
Drawing with GDI • An Initial Rectangle: • How to change the color and attributes? • Changing the Pen: LINE • Changing the Brush : FACE
tool usage Class name pen When drawing a line CPen brush When filling a region CBrush font When printing out a text CFont bitmap When filling a region with an image CBitmap region When defining a polygonal region CRgn GDI Objects (1/3) • GDI Objects (class) • Tools for drawing with the GDI • Changing the attributes of the shapes
GDI Objects(2/3) • Class hierarchy
GDI Objects(3/3) • When you draw an image (in real world): • Choosing a pen • Grabbing the pen • Drawing an image • Releasing the pen
GDI Objects(3/3) • When you draw an image (by using GDI) • Defining a tool (pen) • Assigning the tool to the DC(CDC::SelectObject()) • Drawing an image • (Releasing the tool from the DC)
CPen (1/2) Style width color • How to use: • Pen Style // method 1 CPen pen(PS_SOLID, 2, RGB(255, 0, 0)); // using constructor // or method 2 CPen pen; pen.CreatePen (PS_SOLID, 2, RGB (255, 0, 0)); // using initializer
CPen(2/2) • Coding Example CPaintDC dc(this); CPen pen(PS_SOLID, 1, RGB(0, 0, 255)); dc.SelectObject(&pen); dc.Rectangle(100, 100, 200, 200);
Various Brushes Example Solid brush: filling with a color CBrush brush(RGB(255, 0, 0)); Hatch brush: filling with a pattern CBrush brush(HS_DIAGCROSS, RGB(255, 0, 0)); Bitmap brush: filling with an image CBitmap bitmap; bitmap.LoadBitmap(IDB_BITMAP1); CBrush brush(&bitmap); CBrush (1/2) • A Brush defines how to fill a region • Different kinds of the brush(by using different constructor)
CBrush (2/2) • Coding Example CPaintDC dc(this); CBrush brush(RGB(255, 0, 0)); dc.SelectObject(&brush); dc.Ellipse(100, 100, 200, 200); CPaintDC dc(this); CBrush brush(HS_DIAGCROSS, RGB(255, 0, 0)); dc.SelectObject(&brush); dc.Ellipse(100, 100, 200, 200);
CFont (1/2) • How to use • Create a CFont variable • CallCreateFont() function CFont font; font.CreateFont(...); // font.CreateFontIndirect(...); // font.CreatePointFont(...); // font.CreatePointFontIndirect(...);
CFont (2/2) • Coding Example Size Font name CPaintDC dc(this); CFont font; font.CreatePointFont(400, _T("Arial")); dc.SelectObject(&font); dc.TextOut(10, 10, _T("Hello"));
name meaning BLACK_PEN black pen with 1 pixel width WHITE_PEN white pen with 1 pixel width NULL_PEN transparent pen with 1 pixel width BLACK_BRUSH black solid brush DKGRAY_BRUSH dark gray brush GRAY_BRUSH gray brush LTGRAY_BRUSH light gray brush HOLLOW_BRUSH or NULL_BRUSH transparent brush SYSTEM_FONT default font used by windows system ex) menu, dialog box Predefined Objects (stock objects) • Call CDC::SelectStockObject(…)fucntion
Client-Area Mouse Messages • Client-Area Mouse Messages Message Sent When WM_LBUTTONDOWN The left mouse button is pressed WM_LBUTTONUP The left mouse button is released WM_LBUTTONDBLCLK The left mouse button is double-clicked WM_MBUTTONDOWN The middle mouse button is pressed WM_MBUTTONUP The middle mouse button is released WM_MBUTTONDBLCLK The middle mouse button is double-clicked WM_RBUTTONDOWN The right mouse button is pressed WM_RBUTTONUP The right mouse button is released WM_RBUTTONDBLCLK The right mouse button is double-clicked WM_MOUSEMOVE The cursor is moved over the client area
Client-Area Mouse Messages • Messages for Mouse Events When clicking Left button Clicking and Dragging When moving Double clicking WM_MOUSEMOVE WM_LBUTTONDOWN WM_LBUTTONDOWN WM_LBUTTONDOWN WM_MOUSEMOVE WM_LBUTTONUP WM_MOUSEMOVE WM_LBUTTONUP WM_MOUSEMOVE WM_MOUSEMOVE WM_LBUTTONDBLCLK WM_MOUSEMOVE WM_MOUSEMOVE WM_LBUTTONUP WM_MOUSEMOVE WM_LBUTTONUP
Message Message-Map Macro Handling function WM_LBUTTONDOWN ON_WM_LBUTTONDOWN() OnLButtonDown WM_LBUTTONUP ON_WM_LBUTTONUP() OnLButtonUp WM_LBUTTONDBLCLK ON_WM_LBUTTONDBLCLK() OnLButtonDblClk WM_MBUTTONDOWN ON_WM_MBUTTONDOWN() OnMButtonDown WM_MBUTTONUP ON_WM_MBUTTONUP() OnMButtonUp WM_MBUTTONDBLCLK ON_WM_MBUTTONDBLCLK() OnMButtonDblClk WM_RBUTTONDOWN ON_WM_RBUTTONDOWN() OnRButtonDown WM_RBUTTONUP ON_WM_RBUTTONUP() OnRButtonUp WM_RBUTTONDBLCLK ON_WM_RBUTTONDBLCLK() OnRButtonDblClk WM_MOUSEMOVE ON_WM_MOUSEMOVE() OnMouseMove Client-Area Mouse Messages • Message-map macros and Message handlers
How to add the event handler • Using “Properties” window of CMainWindow
Bit Mask Meaning MK_CONTROL Ctrl key is pressed MK_SHIFT Shift key is pressed MK_LBUTTON Mouse left button is pressed MK_MBUTTON Mouse middle button is pressed MK_RBUTTON Mouse right button is pressed Mouse Message Handler • Prototype of the Handler function: • nFlags • Status of the mouse buttons and the Shift and Ct기 keyat the time when the message was generated afx_msg void On##### (UINT nFlags, CPoint point) ;
Mouse Message Handler • Prototype of the Handler function: • nFlags afx_msg void On##### (UINT nFlags, CPoint point) ; void CChildView::OnLButtonDown(UINT nFlags, CPoint point) { if(nFlags & MK_SHIFT){ // if shift key is pressed } if(nFlags & MK_RBUTTON){ // if right button is pressed at the same time } CWnd ::OnLButtonDown(nFlags, point); }
Mouse Message Handler • Prototype of the Handler function: • point • Location of the cursor afx_msg void On##### (UINT nFlags, CPoint point) ; void CChildView::OnLButtonDown(UINT nFlags, CPoint point) { CClientDC dc(this); CPoint pt = point; dc.Rectangle(pt.x-100, pt.y+100, pt.x+100, pt.y-100); CWnd ::OnLButtonDown(nFlags, point); }
Coding practice: Line drawing • Draw lines by using mouse • Key Idea: • Remembering the beginning and ending position • When the mouse left button is down • Set the position as the beginning point • When the mouse left button is released • Set the position as the ending point • Drawing the line
연습: line 긋는 프로그램2 • 화면에 마우스를 이용하여 line을 긋는다. • 마우스가 움직일 때 중간 과정을 보여준다 • Line 을 그리기 위해서는 • 시작점, 끝점을 기억한다. • 마우스 버튼 Down 시 • 시작점을 입력 • 마우스 Move 시 • 끝점을 입력 • Line 긋기 • 마우스 버튼 Up 시 • 끝점을 입력 • Line 긋기
Any problem? • What happen when moving the mouse outside the window’s client area while dragging?
Function Meaning SetCapture() Starting Mouse Capturing ReleaseCapture() Ending mouse Capturing GetCapture() Returns CWnd pointer that owns the capture Capturing the mouse • Mouse Capture: • Receiving mouse messages no matter where the mouse goes on the screen while dragging • Related function: