SetBkMode 函数 (wingdi.h)

SetBkMode 函数设置指定设备上下文的背景混合模式。 背景混合模式与文本、阴影画笔和笔样式一起使用,这些样式不是实线。

语法

int SetBkMode(
  [in] HDC hdc,
  [in] int mode
);

参数

[in] hdc

设备上下文的句柄。

[in] mode

后台模式。 此参数可以是下列值之一。

价值 含义
OPAQUE 在绘制文本、阴影画笔或笔之前,背景填充当前背景色。
TRANSPARENT 背景不受影响。

返回值

如果函数成功,则返回值指定以前的后台模式。

如果函数失败,则返回值为零。

注解

SetBkMode 函数影响使用 CreatePen 函数创建的笔绘制的线条的线条样式。 SetBkMode 不会影响使用 ExtCreatePen 函数创建的笔绘制的线条。

例子

若要了解如何使阴影画笔的背景透明或不透明,请参阅 CreateHatchBrush 主题中显示的示例。

下一个示例绘制一个字符串 36 次,每次旋转 10 度反时针。 它还将背景模式设置为透明,以使文本可见。

#include "strsafe.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    
    case WM_PAINT:
        {
        hdc = BeginPaint(hWnd, &ps);
        RECT rc; 
        int angle; 
        HGDIOBJ hfnt, hfntPrev; 
        WCHAR lpszRotate[22] = TEXT("String to be rotated.");
        HRESULT hr; 
        size_t pcch = 22;
 
// 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, TEXT("Arial"));
if (FAILED(hr))
{
// TODO: write error handler
}

plf->lfWeight = FW_NORMAL; 
 
// Retrieve the client-rectangle dimensions. 
 
GetClientRect(hWnd, &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 = SelectObject(hdc, hfnt);
    
    //
    // The StringCchLength call is fitted to the lpszRotate string
    //
    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); 
        EndPaint(hWnd, &ps);
        break;
        }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

要求

要求 价值
最低支持的客户端 Windows 2000 Professional [仅限桌面应用]
支持的最低服务器 Windows 2000 Server [仅限桌面应用]
目标平台 Windows操作系统
标头 wingdi.h (包括 Windows.h)
图书馆 Gdi32.lib
DLL Gdi32.dll

另请参阅

CreatePen

ExtCreatePen

GetBkMode

绘制和绘制函数

绘画和绘图概述