我讨厌我以前做过,我觉得我错过了一些东西,我不知道它是什么。所以,我正在尝试在 C++ WinAPI 中设置窗口的背景图像。
我的资源.rc
#include "Resources.h"
BACKGROUND BITMAP "background.bmp"
我的资源.h
#define BACKGROUND 200
我的 Main.cpp
....
WNDCLASSEX wcls;
wcls.cbSize = sizeof(WNDCLASSEX);
wcls.style = CS_PARENTDC|CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
wcls.lpfnWndProc = WndProc;
wcls.cbClsExtra = 0;
wcls.cbWndExtra = 0;
wcls.hInstance = hInstance;
wcls.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(HICON));
wcls.hCursor = LoadCursor(NULL, IDC_ARROW);
wcls.hbrBackground = HBRUSH(COLOR_WINDOW + 1);
wcls.lpszClassName = L"WndClass";
wcls.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(HICON));
HWND Window = CreateWindowEx (
WS_EX_LAYERED|WS_EX_TOOLWINDOW,
wcls.lpszClassName,
L"Window Title",
WS_VISIBLE|WS_POPUP|WS_CLIPCHILDREN,
CW_USEDEFAULT,
CW_USEDEFAULT,
384,
128,
NULL,
NULL,
hInstance,
NULL
);
if (!Window ) return 0;
SetLayeredWindowAttributes(
Window,
RGB(35, 35, 35),
85,
LWA_ALPHA|LWA_COLORKEY
);
SetWindowPos(
Window,
HWND_TOPMOST,
0,
0,
0,
0,
SWP_NOMOVE|SWP_NOREDRAW|SWP_NOSIZE
);
ShowWindow(Window, SW_SHOWNORMAL);
UpdateWindow(Window);
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM Wpar, LPARAM Lpar) {
HBITMAP Background = NULL;
BITMAP BgImg;
HDC DeviceContext, BgContext;
PAINTSTRUCT PaintStruct;
switch (Msg) {
case WM_PAINT:
ContextLansator = BeginPaint(hWnd, &PaintStruct);
Fundal = LoadBitmap(hInstance, MAKEINTRESOURCE(BACKGROUND));
FundalLansator = CreateCompatibleDC(DeviceContext);
SelectObject(DeviceContext, Background);
GetObject(Background, sizeof(BgImg), &BgImg);
BitBlt(DeviceContext, 0, 0, 384, 128, BgContext, 0, 0, SRCAND);
DeleteObject(Background);
DeleteDC(BgContext);
EndPaint(hWnd, &PaintStruct);
break;
.....
我正在使用代码::块。资源已正确嵌入,窗口启动但只是一个白色背景,图像未绘制。WM_PAINT 消息在开始时被调用一次。我觉得 BgImg 出了点问题,但我不知道是什么。需要一些帮助!谢谢!