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 [僅限傳統型應用程式] |
| 目標平臺 | 窗戶 |
| 頁首 | wingdi.h (包括 Windows.h) |
| 程式庫 | Gdi32.lib |
| DLL | Gdi32.dll |