Windows GDI_1

download Windows GDI_1

of 35

Transcript of Windows GDI_1

  • 7/31/2019 Windows GDI_1

    1/35

    S A Nabeel

  • 7/31/2019 Windows GDI_1

    2/35

    Windows GDI(Graphics Device Interface) Device Context/MFC Device Context Classes Modes and Coordinate systems

    Drawing with the GDI Lines, Curves Ellipses Brushes(CBrush) and Pens(CPen) Fonts(CFont)

    The DRAWCLI Sample Application

  • 7/31/2019 Windows GDI_1

    3/35

    GDI - Graphics Device Interface Provides a single programming interface regardless of

    the graphics device being used.

    Program to a display the same as a printer or othergraphics device.

    Manufacturer provides the driver Windows uses to

    interface with a graphics device instead of programmershaving to write code for every device they wish tosupport.

  • 7/31/2019 Windows GDI_1

    4/35

  • 7/31/2019 Windows GDI_1

    5/35

    The DC contains information that tells Windowshow to do the job you request of it. In order forWindows to draw an object it needs some

    information. How thick should the line be? What color should the object be?

    What font should be used for the text and what is its size?

    These questions are all answered by you

    configuring the DC before requesting an objectto be drawn.

  • 7/31/2019 Windows GDI_1

    6/35

    To acquire a DC pointer in an MFC applicationoutside its OnPaint method, use CWnd::GetDC.

    Any DC pointer acquired in this fashion must bereleased with a call to CWnd::ReleaseDC.

    CDC* pDC = GetDC();

    // Do some drawing

    ReleaseDC(pDC);

  • 7/31/2019 Windows GDI_1

    7/35

    To respond to a WM_PAINT message in anOnPaint method, MFC provides functions:

    CWnd::BeginPaint and CWnd::EndPaint

    // Use BeginPaint and EndPaint when responding to WM_PAINTmessage

    // An alternative method is to use CPaintDC in place of

    // BeginPaint and EndPaint

    PAINTSTRUCT ps;

    CDC* pDC = BeginPaint(&ps);

    // Do some drawing

    EndPaint(&ps);

  • 7/31/2019 Windows GDI_1

    8/35

    So you dont have to remember procedures foracquiring and releasing the DC, MFCencapsulates them in 4 classes. CPaintDC - For drawing in a windows client area in an OnPaint method.

    CClientDC - For drawing in a windows client area outside of an OnPaintmethod.

    CWindowDC - For drawing anywhere in the Window, including thenonclient area.

    CMetaFileDC - For drawing to a GDI metafile

  • 7/31/2019 Windows GDI_1

    9/35

    Using CPaintDC makes the example from beforeeven easier and safer.

    Before: After:

    Void CMainWindow::OnPaint(){

    PAINTSTRUCT ps;

    CDC* pDC = BeginPaint(&ps);// Do some drawingEndPaint(&ps);

    }

    Void CMainWindow::OnPaint(){

    CPaintDC dc (this);

    //Do some drawing}

  • 7/31/2019 Windows GDI_1

    10/35

    The Attributes of the Device Context suppliesWindows with the information it needs to draw aline or text

    The LineTo function uses current pen to determine line color, width andstyle.

    Rectangle uses current pen to draw its border and current brush to fill itsarea.

  • 7/31/2019 Windows GDI_1

    11/35

    The function used more than any other is theSelectObject function which changes current Pen, Brushor Font of the DC. The DC is initialized with defaultvalues but you can customize the behavior of functions

    like LineTo by replacing the current CPen and CBrushwith your own.

    //Assume pPen and pBrush are pointers toCPen and CBrush objects.

    dc.SelectObject (pPen);

    dc.SelectObject (pBrush);

    dc.Ellipse(0, 0, 100, 100);

  • 7/31/2019 Windows GDI_1

    12/35

    CPen*SelectObject(CPen*pPen);

    CBrush*SelectObject(CBrush*pBrush);

    virtualCFont*SelectObject(CFont*pFont);

    CBitmap*SelectObject(CBitmap*pBitmap);

    intSelectObject(CRgn*pRgn);

  • 7/31/2019 Windows GDI_1

    13/35

    The drawing mode determines how Windows will display pixels thatrepresent an object being drawn. By default, Windows will just copypixels to the display surface. The other options combine pixel colors,based on Boolean expressions. For example, you can draw a linejust by NOTing the pixels required to draw the line, which inverts thecurrent pixel color. The drawing mode is R2_NOT.

    SetROP2 means Set Raster Operation to

    dc.SetROP2(R2_NOT);

    dc.MoveTo(0,0);

    dcLineTo(100,100);

  • 7/31/2019 Windows GDI_1

    14/35

    The mapping mode is the attribute of the device context thatindicates how logical coordinates are translated into devicecoordinates.

    Logical Coordinatesare the coordinates you pass to CDC outputfunctions. Logical coordinates are in some unit of measurementdetermined by the mapping mode.

    Device Coordinatesare the corresponding pixel positions withina window. Device Coordinates always speak in pixels.

  • 7/31/2019 Windows GDI_1

    15/35

    The default mapping mode is MM_TEXT with units in pixels. Thisdoesnt have to be the case.

    MM_LOENGLISH is a mapping mode whos units are in inches. Oneunit = 1/100 of an inch. One way to ensure something you draw is

    exactly 1 inch for instance.

    Non-MM_TEXT mapping modes allow for consistent sizes anddistances regardless of a devices physical resolution.

    dc.SetMapMode(MM_LOMETRIC)

    dc.Ellipse(0, 0, 500, -300)

  • 7/31/2019 Windows GDI_1

    16/35

    The GDI supplies a long list of output functions todraw all sorts of graphics.

    The simplest objects are lines and curves and a few ofthe supporting functions follow. MoveTo - sets current position LineTo - draws a line from current pos to new pos and updates

    current pos Polyline - Connects a set of pts with line segments. PolylineTo -PolyLine but updates current pos with last pt. Arc - Draws an arc ArcTo - Arc but updates current pos to equal the end of arc

  • 7/31/2019 Windows GDI_1

    17/35

    More advanced shapes are also supported by GDI functions.

    Chord - Draws a closed figure bounded by the intersection of anellipse and a line.

    Ellipse - Draws a circle or ellipse.

    Pie - Draws a pie-shaped wedge

    Polygon - Connects a set of points for form a polygon

    Rectangle - Draws a rectangle with square corners

    RoundRect - Draws a rectangle with rounded corners

  • 7/31/2019 Windows GDI_1

    18/35

    The Device Context has an Attribute referred to as a Pen.Windows uses the Pen to draw lines and curves and also toborder figures drawn with Rectangle, Ellipse and others.

    The default pen creates a black, solid line that is 1 pixel wide.

    Users can customize a pen by creating a CPen object andspecifying its color, width and line style then selecting it into theDevice Context with the SelectObject member function.

    Cpen pen;pen.CreatePen(PS_DASH, 1, RGB(255, 0, 0));dc.SelectObject(&pen);

  • 7/31/2019 Windows GDI_1

    19/35

    The current Brush is an attribute of the Device Context.The current brush is how Windows determines how tofill objects drawn with functions like Rectangle, Ellipseand others. Brush indicates both color and style (solid

    or Hatch)

    //Solid Brush

    CBrush brush (RGB(255,0,0));//Hatch BrushCBrush brush (HS_DIAGCROSS, RGB(255,0,0));

  • 7/31/2019 Windows GDI_1

    20/35

    As with drawing objects, the GDI offers supportingfunctions for drawing text.

    DrawText - Draws text in a formatting rectangle TextOut - Outputs a line of text at the current or

    specified position.

    TabbedTextOut - Outputs a line of text that includestabs

    ExtTextOut - Outputs a line of text and optionally fills arectangle, varies intercharacter spacing

  • 7/31/2019 Windows GDI_1

    21/35

    Drawing text and getting things to line up space properly can be alittle cumbersome. The following functions are available to supplyneeded information:

    GetTextExtent - Computes width of a string in the current font.

    GetTabbedTextExtent - Width including tabs GetTextMetrics - Font metrics(character height, average char width )

    SetTextAlign - Alignment parameters for TextOut and others

    SetTextJustification - specifies the added width needed to justify a string

    SetTextColor - Sets the DC text output color

    SetBkColor - Sets the DC background color for text

  • 7/31/2019 Windows GDI_1

    22/35

    MFC represents a Font with the CFont class. Like Pensand Brushes, you can change the default Font bycreating an instance of the CFont class, configuring itthe way you wish, and selecting it into the DC with

    SelectObject.

    //12 pt Font (pt parameter passed is 10 * desired_pt_size)

    CFont font;fond.CreatePointFont(120, _T(Times New Roman));

  • 7/31/2019 Windows GDI_1

    23/35

    Raster Fonts - fonts that are stored as Bitmapsand look best when theyre displayed in theirnative sizes.

    TrueType Fonts - fonts that are stored asmathematical formulas which allows them to scalewell.

  • 7/31/2019 Windows GDI_1

    24/35

    Windows predefines a handful of pens, brushes,fonts and other GDI objects that can be usedwithout being explicitly created and are notdeleted.

    dc.SelectStockObject(LTGRAY_BRUSH);dc.Ellipse(0,0,100,100);

  • 7/31/2019 Windows GDI_1

    25/35

    //Example Without Stock ObjectsCPen pen (PS_NULL, 0, (RGB(0,0,0)));dc.SelectObject(&pen);CBrush brush (RGB(192,192,192));

    dc.SelectObject(&brush);dc.Ellipse(0, 0, 100, 100);

    Drawing a light gray circle with no border:

  • 7/31/2019 Windows GDI_1

    26/35

    Drawing a light gray circle with no border:

    //Example With Stock Objectsdc.SelectStockObject(NULL_PEN);dc.SelectStockObject(LTGRAY_BRUSH);dc.Ellipse(0 ,0, 100, 100);

  • 7/31/2019 Windows GDI_1

    27/35

    GDI objects are resources and consume memory.This makes it important to ensure an object isdeleted when you are finished. The best way is toalways create instances of CPens, CBrushs and

    CFonts on the stack, as local objects, so theirdestructors are called as they go out of scope.

    Sometimes, newing an object is required. Itsimportant to make sure that all GDI objects createdwith the new operator are deleted with the deleteoperator when they are no longer needed.

    Stock Objects should NEVER be deleted.

  • 7/31/2019 Windows GDI_1

    28/35

    Ensuring GDI objects are deleted is important.But, it is also extremely important to avoiddeleting an object while its selected into adevice context.

    An easy and good habit to get into is to store a

    pointer to the default object that was installedwhen you retrieved the device context and thenre-select it back into the context before deletingthe object you created.

  • 7/31/2019 Windows GDI_1

    29/35

    //Option 1CPen pen (PS_SOLID, 1, RGB(255, 0, 0));CPen* pOldPen = dc.SelectObject(&pen);

    :dc.SelectObject(pOldPen);

    //Option 2CPen pen (PS_SOLID, 1, RGB(255,0,0));

    dc.SelectObject(&pen);:

    dc.SelectStockObject(BLACK_PEN);

  • 7/31/2019 Windows GDI_1

    30/35

    // Get the Black Brush.HGDIOBJ hGdiObj = GetStockObject( BLACK_BRUSH );// Check the object type with handle.if( OBJ_BRUSH == GetObjectType( hGdiObj )){// Yes! The handle points to Gdi Brush.}

  • 7/31/2019 Windows GDI_1

    31/35

    You can monitor the number of GDI objects an application has from the "task manager"

  • 7/31/2019 Windows GDI_1

    32/35

    //To programmatically find the number of GDI objects, use//the statement

    DWORD gdiCount = GetGuiResources(hProcess,

    GR_GDIOBJECTS);

  • 7/31/2019 Windows GDI_1

    33/35

  • 7/31/2019 Windows GDI_1

    34/35

  • 7/31/2019 Windows GDI_1

    35/35