1

我讨厌我以前做过,我觉得我错过了一些东西,我不知道它是什么。所以,我正在尝试在 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 出了点问题,但我不知道是什么。需要一些帮助!谢谢!

4

3 回答 3

1

在注册类之前创建一个 PatternBrush 并将其设置为背景。例如:

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINAPIBACKGROUND));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = CreatePatternBrush(LoadBitmap( hInstance, MAKEINTRESOURCE( IDB_BITMAP1 ) ) );
    wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_WINAPIBACKGROUND);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

它看起来像这样: 在此处输入图像描述

位图深度无关紧要。但是看看你的实现中的样式。

在此处输入图像描述

于 2012-06-27T11:55:28.790 回答
1

在处理我认为类似的症状 20 小时后,我找到了解决方案。

我的问题是bitmap没有加载。我验证了该文件是真实的bitmap并且代码正确。我更正并重新编译但Code::Blocks不会加载它(return = 0)。尝试删除两个文件夹BINOBJ两个文件xxx.dependxxx.layout. 然后重新编译运行。

于 2019-12-16T11:40:11.170 回答
1

我使用这段代码,它对我来说很好。我也使用代码块 4.7。

case WM_PAINT:
{
    PAINTSTRUCT     ps;
    HDC             hdc;
    BITMAP          bitmap;
    HDC             hdcMem;
    HGDIOBJ         oldBitmap;
    hBitmap01 = (HBITMAP)LoadImage(NULL, "c:\\energy.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);


    hdc = BeginPaint(wnd01, &ps);

    hdcMem = CreateCompatibleDC(hdc);
    oldBitmap = SelectObject(hdcMem, hBitmap01);

    GetObject(hBitmap01, sizeof(bitmap), &bitmap);
    BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, oldBitmap);
    DeleteDC(hdcMem);

    EndPaint(wnd01, &ps);
    }
于 2015-08-17T22:59:18.590 回答