简体   繁体   中英

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. 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).

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.

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. How can I set an image as a background (in 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. The window whose background I am trying to set has the handle hwnd.

I have tried defining the background when defining the window class, but this brings no change at all to the 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.

    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.

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.

And I use my own bmp image to test the background and static window image successfully.

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.

I suggest you try to test with other bmp images in the correct format so that you can get the correct results.

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):

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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