我討厭我之前做過,我覺得我錯過了一些東西,我不知道它是什麼。所以,我試圖在C++ WinAPI中設置一個窗口的背景圖像。C++ WINAPI設置窗口背景圖片不起作用
我Resources.rc
#include "Resources.h"
BACKGROUND BITMAP "background.bmp"
我Resources.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有問題但我不知道是什麼。需要一些幫助!謝謝!
嗯,你正在使用很多未初始化的變量。 –
LoadBitmap返回NULL – ali
然後位圖並不存在於由hInstance指定的模塊中。確保你的hInstance是正確的,並且資源真的是一個整數資源。 –