TVSKIN源代碼閱讀日記(七)--- DEVICE CONTEXT之Graphic objects

A device context is a structure that defines a set of graphic objects and their associated attributes, as well as the graphic modes that affect output. The graphic objects include a pen for line drawing, a brush for painting and filling, a bitmap for copying or scrolling parts of the screen, a palette for defining the set of available colors, a region for clipping and other operations, and a path for painting and drawing operations. The remainder of this section is divided into the following three areas.

 

About Device Contexts

Device independence is one of the chief features of Microsoft windows application can draw and print ouput on a variety of devices. The software that supports this device independence is contained in two dynamic-link libraries. The first, gdi.dll , is referred to as the graphics device interface (GDI); the second is referred to as a device driver. The name of the second depends on the device where the application draws output. For example, if the application draws output in the client area of its window on a VGA display, this library is VGA.DLL; if the application prints output on an Epson FX-80 printer, this library is Epson9.dll.

 

An application must inform GDI to load a particular device driver and, once the driver is loaded, to prepare the device for drawing operations (such as selecting a line color and width, a brush pattern and color, a font typeface, a clipping region, and so on). These tasks are accomplished by creating and maintaining a device context(DC). A DC is a structure that defines a set of graphic objects and their associated attributes, and the graphic modes that affect output. The graphic objects includes a pen for drawing, a brush for painting and filling, a bitmap for copying or scrolling parts of the screen, a palette for defining the set of available colors, a region for clipping and other operations, and a path for painting and drawing operations. Unlike most of the structures, an application never has direct access to the DC, instead, it operations on the structures indirectly by calling various functions.

 

This overview provides information on the following topics:

1.Graphic objects

2.Graphic modes

3.Device Context Types

4.Device Context Operations

5.ICM-Enabled Device Context Functions

 

Topic One: Graphic Objects

The Pen, brush, bitmap, palette, region, and path associated with a DC are referred to as its graphic objects. The following attributes are associated with each of these objects.

 

Graphic object Associated attributes
Bitmap Size, in bytes; dimensions, in pixels; color-format; compression scheme; and so on.
Brush Style, color, pattern, and origin.
Palette Colors and size (or number of colors).
Font Typeface name, width, height, weight, character set, and so on.
Path Shape.
Pen Style, width, and color.
Region Location and dimensions.

 

When an application creates a DC, the system automatically stores a set of default objects in it. (There is no default bitmap or path.) An application can examine the attributes of the default objects by calling the GetCurrentObject and GetObject functions. The application can change these defaults by creating a new object and selecting it into the DC. An object is selected into a DC by calling the SelectObject function.

An application can set the current brush color to a specified color value with SetDCBrushColor.

The GetDCBrushColor function returns the DC brush color. The SetDCPenColor function sets the pen color to a specified color value. The GetDCPenColor function returns the DC pen color.

 

GetCurrentObject

The GetCurrentObject function retrieves a handle to an object of the specified type that has been selected into the specified device context (DC).

HGDIOBJ GetCurrentObject(
  HDC hdc,           // handle to DC
  UINT uObjectType  // object type
);
 

Paraments

hdc
[in] Handle to the DC.
uObjectType
[in] Specifies the object type to be queried. This parameter can be one of the following values.
Value      Meaning
OBJ_BITMAP   Returns the current selected bitmap.
OBJ_BRUSH   Returns the current selected brush.
OBJ_COLORSPACE Returns the current color space.
OBJ_FONT Returns the current selected font.
OBJ_PAL Returns the current selected palette.
OBJ_PEN Returns the current selected pen.

 

GetObject

The GetObject function retrieves information for the specified graphics object.

int GetObject(
  HGDIOBJ hgdiobj,  // handle to graphics object
  int cbBuffer,     // size of buffer for object information
  LPVOID lpvObject  // buffer for object information
);

Parameters

hgdiobj
[in] Handle to the graphics object of interest. This can be a handle to one of the following: a logical bitmap, a brush, a font, a palette, a pen, or a device independent bitmap created by calling the CreateDIBSection function.
cbBuffer
[in] Specifies the number of bytes of information to be written to the buffer.
lpvObject
[out] Pointer to a buffer that receives the information about the specified graphics object.

The following table shows the type of information the buffer receives for each type of graphics object you can specify with hgdiobj.

Object type
Data written to buffer
HBITMAP
BITMAP
HBITMAP returned from a call to CreateDIBSection DIBSECTION, if cbBuffer is set to sizeof(DIBSECTION), or BITMAP, if cbBuffer is set to sizeof(BITMAP)
HPALETTE A WORD count of the number of entries in the logical palette
HPEN returned from a call to ExtCreatePen EXTLOGPEN
HPEN LOGPEN
HBRUSH LOGBRUSH
HFONT LOGFONT

 

If the lpvObject parameter is NULL, the function return value is the number of bytes required to store the information it writes to the buffer for the specified graphics object.

 

Retrieving Graphic-Object Attributes and Selecting New Graphic Objects

An application can retrieve the attributes for a pen, brush, palette, font, or bitmap by calling the GetCurrentObject and GetObject functions. The GetCurrentObject function returns a handle identifying the object currently selected into the DC; the GetObject function returns a structure that describes the attributes of the object.

The following example shows how an application can retrieve the current brush attributes and use the retrieved data to determine whether it is necessary to select a new brush.

    HDC hdc;                     // display DC handle 
    HBRUSH hbrushNew, hbrushOld; // brush handles 
    HBRUSH hbrush;               // brush handle 
    LOGBRUSH lb;                 // logical-brush structure 
 
    // Retrieve a handle identifying the current brush. 
 
    hbrush = GetCurrentObject(hdc, OBJ_BRUSH); 
 
    // Retrieve a LOGBRUSH structure that contains the 
    // current brush attributes. 
 
    GetObject(hbrush, sizeof(LOGBRUSH), &lb); 
 
    // If the current brush is not a solid-black brush, 
    // replace it with the solid-black stock brush. 
 
    if ((lb.lbStyle != BS_SOLID) 
           || (lb.lbColor != 0x000000)) 
    { 
        hbrushNew = GetStockObject(BLACK_BRUSH); 
        hbrushOld = SelectObject(hdc, hbrushNew); 
    } 
 
    // Perform painting operations with the white brush. 
 
 
    // After completing the last painting operation with the new 
    // brush, the application should select the original brush back 
    // into the device context and delete the new brush. 
    // In this example, hbrushNew contains a handle to a stock object. 
    // It is not necessary (but it is not harmful) to call 
    // DeleteObject on a stock object. If hbrushNew contained a handle 
    // to a brush created by a function such as CreateBrushIndirect, 
    // it would be necessary to call DeleteObject. 
 
    SelectObject(hdc, hbrushOld); 
    DeleteObject(hbrushNew); 

Note  The application saved the original brush handle when calling the SelectObject function the first time. This handle is saved so that the original brush can be selected back into the DC after the last painting operation has been completed with the new brush. After the original brush is selected back into the DC, the new brush is deleted, freeing memory in the GDI heap.

 

自己編寫的例子得到默認的GDI OBJECT

BOOL CDeviceContextDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon

    // TODO: Add extra initialization here

    HDC hdc = ::GetDC(this->m_hWnd);

    g_Trace.on();  // 開啓DEBUG輸出開關

 

    HBRUSH hbrush;               // brush handle
    LOGBRUSH lb;                 // logical-brush structure

    // Retrieve a handle identifying the current brush.
    hbrush = (HBRUSH)GetCurrentObject(hdc, OBJ_BRUSH);
    // Retrieve a LOGBRUSH structure that contains the current brush attributes.
    GetObject(hbrush, sizeof(LOGBRUSH), &lb);

    g_Trace.print(L"Default Brush R,G,B color:");
    g_Trace.printInt(GetRValue(lb.lbColor));// 255
    g_Trace.printInt(GetGValue(lb.lbColor));// 255
    g_Trace.printInt(GetBValue(lb.lbColor));// 255

    if(lb.lbStyle == BS_SOLID)// default style
        g_Trace.print(L"Default Brush style: BS_SOLID");

    // If lbStyle is BS_SOLID or BS_HOLLOW, lbHatch is ignored

 

    HFONT hfont;               // font handle
    LOGFONT lf;                 // logical-font structure
    // Retrieve a handle identifying the current font.
    hfont = (HFONT)GetCurrentObject(hdc, OBJ_FONT);
    // Retrieve a LOGFONT structure that contains the
    // current font attributes.
    GetObject(hfont, sizeof(LOGFONT), &lf);

    //default font object
    g_Trace.print(L"Default font width:");
    g_Trace.printLong(lf.lfWidth); // default value 7
    g_Trace.print(L"Default font Height:");
    g_Trace.printLong(lf.lfHeight); // default value 16
    g_Trace.print(L"Default font Escapement:");
    g_Trace.printLong(lf.lfEscapement);// 0
    g_Trace.print(L"Default font Orientation:");
    g_Trace.printLong(lf.lfOrientation);// 0
    if(lf.lfWeight == FW_BOLD )//700 FW_BOLD
        g_Trace.print(L"Default font Weight: FW_BOLD");
    g_Trace.printInt(lf.lfItalic);// 0 no italic
    g_Trace.printInt(lf.lfUnderline);// 0 no underline
    g_Trace.printInt(lf.lfStrikeOut);// 0 no strikeout
    g_Trace.printInt(lf.lfCharSet);// 134 GB2312_CHARSET
    g_Trace.printInt(lf.lfOutPrecision);// 1 OUT_STRING_PRECIS
    g_Trace.printInt(lf.lfClipPrecision);// 2 CLIP_STROKE_PRECIS
    g_Trace.printInt(lf.lfQuality);// 2 PROOF_QUALITY)
    g_Trace.printInt(lf.lfPitchAndFamily);// 34=00100010 VARIABLE_PITCH|FF_SWISS

 

 

    HPEN hpen;
    LOGPEN lp;
    hpen = (HPEN)GetCurrentObject(hdc,OBJ_PEN);
    GetObject(hpen,sizeof(LOGPEN),&lp);

    g_Trace.print(L"Defalut Pen width:");
    g_Trace.printInt(lp.lopnWidth.x); // 0
    g_Trace.print(L"Defalut Pen style: PS_SOLID");   
    g_Trace.printInt(lp.lopnStyle); // PS_SOLID
    g_Trace.print(L"Defalut Pen Color R,G,B:");
    g_Trace.printInt(GetRValue(lp.lopnColor));// 0
    g_Trace.printInt(GetGValue(lp.lopnColor));// 0
    g_Trace.printInt(GetBValue(lp.lopnColor));// 0

 

    HPALETTE hpalette;
    WORD lpalette;
    hpalette = (HPALETTE)GetCurrentObject(hdc,OBJ_PAL);
    GetObject(hpalette,sizeof(WORD),&lpalette);
    g_Trace.print(L"Default palette value:");
    g_Trace.printLong(lpalette); // 20L

    return TRUE;  // return TRUE  unless you set the focus to a control
}

 

DefaultGDIObjectOutput

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章