TVSKIN源代码阅读日记(八)--- DEVICE CONTEXT之Graphic Modes

Graphic Modes

Windows supports five graphic modes that allow an application to specify how colors are mixed, where output appears, how the output is scaled, and so on. These modes, which are stored in a DC, are described in the following table.

 

Graphics modeDescription
Background Defines how background colors are mixed with existing window or screen colors for bitmap and text operations.
Drawing Defines how foreground colors are mixed with existing window or screen colors for pen, brush, bitmap, and text operations.
Mapping
Defines how graphics output is mapped from logical (or world) space onto the window, screen, or printer paper.
Polygon-fill
Defines how the brush pattern is used to fill the interior of complex regions.
Stretching Defines how bitmap colors are mixed with existing window or screen colors when the bitmap is compressed (or scaled down).

 

As it does with graphic objects, the system initializes a DC with default graphic modes. An application can retrieve and examine these default modes by calling the following functions.

Graphics modeFunction
Background GetBkMode
Drawing GetROP2
Mapping
GetMapMode
Polygon-fill GetPolyFillMode
Stretching
GetStretchBltMode

 

An application can change the default modes by calling one of the following functions.

Graphics modeFunction
Background SetBkMode
Drawing
SetROP2
Mapping
SetMapMode
Polygon-fill SetPolyFillMode
Stretching SetStretchBltMode

 

SetBkMode

The SetBkMode function sets the background mix mode of the specified device context. The background mix mode is used with text, hatched brushes, and pen styles that are not solid lines.

int SetBkMode(
  HDC hdc,      // handle to DC
  int iBkMode  // background mode
);
Parameters
hdc
[in] Handle to the device context.
iBkMode
[in] Specifies the background mode. This parameter can be one of the following values.
ValueDescription
OPAQUE
Background is filled with the current background color before the text, hatched brush, or pen is drawn.
TRANSPARENT

Background remains untouched.

 

Return Value

If the function succeeds, the return value specifies the previous background mode.

If the function fails, the return value is zero.

 

Remarks

The SetBkMode function affects the line styles for lines drawn using a pen created by the CreatePen function. SetBkMode does not affect lines drawn using a pen created by the ExtCreatePen function.

The iBkMode parameter can also be set to driver-specific values. GDI passes such values to the device driver and otherwise ignores them.

 

Example

void CDeviceContextDlg::OnPaint()
{
 
    CDialog::OnPaint();
 

    CPaintDC dc(this); // device context for painting

    HDC hdc = ::GetDC(this->m_hWnd);
    RECT rc;
    int angle;
    HFONT hfnt, hfntPrev;
    LPSTR lpszRotate = "String to be rotated.";
    HRESULT hr;


    // Allocate memory for a LOGFONT structure.
    PLOGFONT plf = (PLOGFONT) LocalAlloc(LPTR, sizeof(LOGFONT));
    // Specify a font typeface name and weight.
    hr = StringCchCopy(plf->lfFaceName, 6, "Arial");
    if (FAILED(hr))
    {
    // TODO: write error handler
    }

    plf->lfWeight = FW_NORMAL;
    // Retrieve the client-rectangle dimensions.
    GetClientRect(&rc);
    // Set the background mode to transparent for the
    // text-output operation.
    SetBkMode(hdc, TRANSPARENT);
    // Draw the string 36 times, rotating 10 degrees
    // counter-clockwise each time.
    for (angle = 0; angle < 3600; angle += 100)
    {
        plf->lfEscapement = angle;
        hfnt = CreateFontIndirect(plf);
        hfntPrev = (HFONT)SelectObject(hdc, hfnt);
        //
        // The StringCchLength call is fitted to the lpszRotate string
        //
        size_t pcch=0;
        hr = StringCchLength(lpszRotate, 22, &pcch);
        if (FAILED(hr))
        {
        // TODO: write error handler
        }
        TextOut(hdc, rc.right / 2, rc.bottom / 2,
            lpszRotate, pcch);
        SelectObject(hdc, hfntPrev);
        DeleteObject(hfnt);
    }
    // Reset the background mode to its default.
    SetBkMode(hdc, OPAQUE);
    // Free the memory allocated for the LOGFONT structure.
    LocalFree((LOCALHANDLE) plf);


}

执行效果

SetBkModeRotateLines

如果注释掉语句:

    SetBkMode(hdc, TRANSPARENT);

执行效果图:

SetBkModeNoTransparent

再次添加语句:

COLORREF tempCor = GetBkColor(hdc);

g_Trace.printInt(GetRValue(tempCor));

g_Trace.printInt(GetRValue(tempCor));

g_Trace.printInt(GetRValue(tempCor));

R,G,B三色分别为255,255,255. 白色

 

To be continued…. waterathena 209-03-16 20:55

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值