문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top