SetBkMode 関数 (wingdi.h)

SetBkMode 関数は、指定されたデバイス コンテキストのバックグラウンド ミックス モードを設定します。 背景ミックス モードは、テキスト、ハッチブラシ、および実線ではないペンスタイルで使用されます。

構文

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

パラメーター

[in] hdc

デバイス コンテキストへのハンドル。

[in] mode

背景モード。 このパラメーターには、次の値のいずれかを指定できます。

説明
不透明
背景は、テキスト、ハッチブラシ、またはペンが描画される前に、現在の背景色で塗りつぶされます。
透明
背景は変更されません。

戻り値

関数が成功した場合、戻り値は前のバックグラウンド モードを指定します。

関数が失敗した場合は、0 を返します。

解説

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 を含む)
Library Gdi32.lib
[DLL] Gdi32.dll

関連項目

Createpen

ExtCreatePen

GetBkMode

描画関数と描画関数

描画と描画の概要