简体   繁体   English

如何将BMP图像设置为Windows API ZF6F87C9FDCF8B3C3F07F93F1EE8中的window的背景?

[英]How to set a BMP image as the background of a window in Windows API C++?

LATER EDIT:后期编辑:

I realised that what was wrong was not my code, but the image itself.我意识到出了问题的不是我的代码,而是图像本身。 Do not use online tools for converting from jpg to bmp, as they don't provide usable images.不要使用在线工具从 jpg 转换为 bmp,因为它们不提供可用的图像。 What I did instead was open the jpg in Paint and then save it as a 24-bit Bitmap (the only one which kept my original colours).我所做的是在 Paint 中打开 jpg,然后将其保存为 24 位 Bitmap(唯一保留我原始颜色的)。

ORIGINAL POST:原帖:

I am trying to make a pretty home window as part of a game, but I don't understand many things in WIN 32.我正在尝试制作一个漂亮的 window 作为游戏的一部分,但我不了解 WIN 32 中的很多东西。

I want to create a window which will also have some buttons and I also want to set its background to a.bmp image, not a solid colour.我想创建一个 window 也将有一些按钮,我还想将其背景设置为 a.bmp 图像,而不是纯色。 How can I set an image as a background (in C++)?如何将图像设置为背景(在 C++ 中)?

The image I am talking about is saved as "bg1.bmp", both in the first folder of my project (along with the source code and the.cbp file) and in the bin/Debug/ folder, where the.exe is.我正在谈论的图像保存为“bg1.bmp”,在我的项目的第一个文件夹(连同源代码和.cbp 文件)和.exe 所在的bin/Debug/ 文件夹中。 The window whose background I am trying to set has the handle hwnd.我试图设置其背景的 window 具有句柄 hwnd。

I have tried defining the background when defining the window class, but this brings no change at all to the window:我在定义 window class 时尝试定义背景,但这对 window 没有任何改变:

wincl.hbrBackground=CreatePatternBrush((HBITMAP) LoadImage(0,_T("bg1.bmp"),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE));

and also having another static window of the same size overlapping the main window, which gives me a black window instead of a white one.并且还有另一个相同大小的 static window 与主 window 重叠,这给了我一个黑色 Z05B8C7425702FBF24DE4C1A 而不是白色

    background=CreateWindow("STATIC","background",SS_BITMAP|WS_CHILD | WS_VISIBLE,0,0,300,300,hwnd,NULL,NULL,NULL);
    HBITMAP hBmp = (HBITMAP)LoadImage(NULL, "seamless_background1.bmp", IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    SendMessage(background, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);

but this one just gives me a black window.但是这个只是给了我一个黑色的 window。

Could you tell me what's wrong with the code?你能告诉我代码有什么问题吗? Also, is there any neater way of doing this?另外,有没有更简洁的方法来做到这一点?

I used the same code and created the simplest Windows Desktop Application.我使用相同的代码并创建了最简单的 Windows 桌面应用程序。

And I use my own bmp image to test the background and static window image successfully.并且我使用自己的bmp图像成功测试了背景和static window图像。

I think the problem lies in your image format, if you just changed the suffix of an other image to.bmp, then your LoadImage will fail but GetLasterror will return 0.我认为问题在于您的图像格式,如果您只是将其他图像的后缀更改为.bmp,那么您的 LoadImage 将失败但GetLasterror将返回 0。

I suggest you try to test with other bmp images in the correct format so that you can get the correct results.我建议您尝试使用其他格式正确的 bmp 图像进行测试,以便获得正确的结果。

This is my sample:这是我的样本:

#include <Windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("test windows");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = CreatePatternBrush((HBITMAP)LoadImage(NULL, L"D:\\VS_test_projects\\win_api\\bitmap.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE));;
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;
    if (!RegisterClass(&wndclass))
    {
        MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
    }

    hwnd = CreateWindow(szAppName,
        TEXT(""test windows""),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL);
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);
    HWND background = CreateWindow(L"STATIC", L"background", SS_BITMAP | WS_CHILD | WS_VISIBLE, 0, 0, 300, 300, hwnd, NULL, NULL, NULL);
    HBITMAP hBmp = (HBITMAP)LoadImage(NULL, L"test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    int e = GetLastError();
    SendMessage(background, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);
    while (GetMessageW(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;
    switch (message)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

And it works like this(Ignore the pictures I use that are not pretty):它的工作原理是这样的(忽略我使用的不漂亮的图片):

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM