0

So I had this issue for a while now....and I can't seem to get around it...I managed to setup the start background which is relatively easy but I just can't figure out how to change it inside my application.

I have 2x Bitmaps imported with IDs: IDI_BITMAP1 IDI_BITMAP2

I use bitmap1 for start background and I wanted to change the background when the user clicks on a button case ID_MENUBUTTON1: //change background could any1 help me out with this? I really tried to understand it but I just can't. Here's how I created the start background:

case WM_CREATE:

    /* Create start background image */
    hBMP[0] = LoadBitmap(hInst, MAKEINTRESOURCE(IDI_BITMAP1));

    hBitmap[0] = CreateWindowEx(0,
        L"Static",
        L"",
        WS_CHILD | WS_VISIBLE | SS_BITMAP,
        -10,
        0,
        0,
        0,
        hwnd,
        (HMENU)IDI_BITMAP2,
        hInst,
        NULL);

    SendMessage(hBitmap[0], STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBMP[0]);

I understand it's possible to do it using WM_PAINT but I'm not really sure how it would work with what I want, that's that the background only changes if the user clicks the button.

Code is in C++, using VS13

Survaf93
  • 162
  • 1
  • 11

2 Answers2

1

If I'm understanding your question correctly, you've already got the background set up correctly at startup, but you want to be able to change it at runtime.

If that's the case, you're more than halfway there. The way you set the background initially

SendMessage(hBitmap[0], STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBMP[0]);

is the same way that you change the background in the future. Just pass a different bitmap handle for the last parameter.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • I tried that, added SendMessage(hBitMap[1], STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBMP[1]); and I created & loaded them before on WM_CREATE but nothing changes when I click the button (p.s. yes the button is working I checked ) – Survaf93 May 13 '14 at 23:22
  • @Survaf93, `hBitMap[1]`? I thought you wanted to change the one you showed us. – chris May 13 '14 at 23:30
  • I'm sorry I'm acting all noobish around here but I did try `SendMessage(hBitmap[0], STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBMP[1]);` and the result is the same :( – Survaf93 May 13 '14 at 23:32
  • At first I thought I understand how it works and as Cody Gray said I understand that but I don't know why the damn thing won't update my background even after SendMessage – Survaf93 May 13 '14 at 23:36
  • @Survaf93 The control may not be repainting itself. I forget if this is standard behavior or not with the `STM_SETIMAGE` message. Try calling `InvalidateRect(hBitmap[0], NULL, FALSE);` – Cody Gray - on strike May 13 '14 at 23:40
  • No change, I even tried with WM_ERASEBKGND, changing and updating and still doesn't wanna listen to me, but if I just load a different img for starting background then it creates it normally so I know my bitmaps IDs are working fine... any chance you guys got a reference to some simple example of this, might be able to find out what's up with this thing. – Survaf93 May 13 '14 at 23:48
  • 1
    @Survaf93 Looking at your code (sorry, didn't see you posted that), it appears to me that you're creating two different static controls, with the two different calls to `CreateWindowEx`. That is likely the problem. You only want *one* static control, then you just swap out which bitmap it is displaying. – Cody Gray - on strike May 13 '14 at 23:52
  • I did delete hBitmap[1] and only left hBitmap[0], still loaded hBMP[1] tho and used Sendmessage(hBitmap[0], x, x, (LPARAM)hBMP[1]); and it didn't do any good, however I am playing with it a bit now and if I do this: DeleteObject(hBitmap[0]); DeleteObject(hBMP[0]); and after that define hBitmap[1] & hBMP[1] load the different bitmap (the one I want) and then send the message with hBitmap[1] and hBMP[1] I managed to change the background but I need to update all of my windows since then I get my background over all of my current active windows :( – Survaf93 May 13 '14 at 23:56
  • For some reason it decided to work from the start this morning, thx for the help Cody :) – Survaf93 May 14 '14 at 07:08
1

in this application you can change the background by a click.

compile the code below.

write your file name in textbox and press change background.

i use mingw 4.7.

you can only use .bmp images, if you want to use other images type you have to use GdiPluse.

#include <windows.h>

//variables
HWND hwnd01, label01;
HBITMAP hBitmap01 = NULL;

//functions
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);



int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                LPSTR lpCmdLine, int nCmdShow )
{
MSG  msg ;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT( "GUI01" );
wc.hInstance     = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc   = WndProc ;
wc.hCursor       = LoadCursor(0, IDC_ARROW);


RegisterClass(&wc);
hwnd01 = CreateWindow( wc.lpszClassName, TEXT("GUI01 Headline"),
            WS_OVERLAPPEDWINDOW | WS_VISIBLE,
            150, 150, 330, 150, 0, 0, hInstance, 0);

while( GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch(msg)
{
case WM_CREATE:
{
    CreateWindow(TEXT("button"), TEXT("Change Background"),
                 WS_VISIBLE | WS_CHILD ,
                 20, 50, 140, 25,
                 hwnd, (HMENU) 1, NULL, NULL);

    CreateWindow(TEXT("button"), TEXT("Quit"),
                 WS_VISIBLE | WS_CHILD ,
                 190, 50, 80, 25,
                 hwnd, (HMENU) 2, NULL, NULL);
    label01 = CreateWindow(TEXT("Edit"), TEXT("Label"),
                 WS_VISIBLE | WS_CHILD,
                 20, 10, 280, 25,
                 hwnd, (HMENU) 3, NULL, NULL);
    break;
}

  case WM_COMMAND:
  {
   if (LOWORD(wParam) == 1) {
       TCHAR* string01 = new TCHAR[300];

        GetWindowText(label01, string01, 300);
        hBitmap01 = (HBITMAP)LoadImage(NULL, string01, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

        if (hBitmap01==NULL) 
        MessageBox(NULL, "Error Loading Image.", "ERROR", MB_ICONWARNING | MB_DEFBUTTON2);
        else
        InvalidateRect(hwnd01, NULL, TRUE);
   }

   if (LOWORD(wParam) == 2) {
          PostQuitMessage(0);
   }

   break;
   }
case WM_PAINT:
{
    PAINTSTRUCT     ps01;
    HDC             hdc01;
    BITMAP          bitmap01;
    HDC             hdcMem01;
    HGDIOBJ         oldBitmap01;

    hdc01 = BeginPaint(hwnd01, &ps01);

    hdcMem01 = CreateCompatibleDC(hdc01);
    oldBitmap01 = SelectObject(hdcMem01, hBitmap01);

    GetObject(hBitmap01, sizeof(bitmap01), &bitmap01);
    BitBlt(hdc01, 0, 0, bitmap01.bmWidth, bitmap01.bmHeight, hdcMem01, 0, 0, SRCCOPY);

    SelectObject(hdcMem01, oldBitmap01);
    DeleteDC(hdcMem01);

    EndPaint(hwnd01, &ps01);

    break;
}

  case WM_DESTROY:
  {
     PostQuitMessage(0);
     break;
  }
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
Amir
  • 1,638
  • 19
  • 26