GDI绘图之设置文本背景色为透明度

来源:互联网 发布:微商城 源码 编辑:程序博客网 时间:2024/05/21 10:39


SetBkMode(dc, TRANSPARENT);






#include <Windows.h>


// 窗口处理函数
HINSTANCE g_hInstance = 0;
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC dc;
PAINTSTRUCT ps;
switch (uMsg)
{
case WM_PAINT:
{
dc = BeginPaint(hwnd, &ps);
HBRUSH brush = CreateSolidBrush(RGB(0, 255, 0));
HGDIOBJ nOldBrush = SelectObject(dc, brush);
SetTextColor(dc, RGB(255, 0, 0));
SetBkColor(dc, RGB(0, 0, 0));
SetBkMode(dc, TRANSPARENT);  // 设置透明
Rectangle(dc, 10, 10, 500, 500);
TextOut(dc, 10, 10, L"HELLO,WORLD", wcslen(L"HELLO,WORLD"));
SelectObject(dc, nOldBrush);
DeleteObject(brush);
EndPaint(hwnd, &ps);


}
break;
case WM_DESTROY:
PostQuitMessage(0);
default:
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
// 窗口注册函数
void Register(LPCWSTR lpClassName, WNDPROC wndproc)
{
WNDCLASSEX wcx = { 0 };


wcx.cbClsExtra = 0;
wcx.cbSize = sizeof(wcx);
wcx.cbWndExtra = 0;
wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcx.hCursor = LoadCursor(g_hInstance, IDC_ARROW);
wcx.hIcon = NULL;
wcx.hIconSm = NULL;
wcx.hInstance = g_hInstance;
wcx.lpfnWndProc = wndproc;
wcx.lpszClassName = lpClassName;
wcx.lpszMenuName = NULL;
wcx.style = CS_HREDRAW | CS_VREDRAW;


RegisterClassEx(&wcx);
}


HWND CreateMain(LPCWSTR lpClassName)
{
HWND hwnd = CreateWindowEx(0, lpClassName, L"LEARN", WS_OVERLAPPEDWINDOW, 100, 100, 700, 500, NULL, NULL, g_hInstance, NULL);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
return hwnd;
}


int exec()
{
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
g_hInstance = hInstance;
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(nCmdShow);
TCHAR szAppName[] = L"MAIN";
Register(szAppName, WndProc);
CreateMain(szAppName);
return exec();
}
0 0