如何在win32中更改窗口的背景图像?

时间:2011-04-26 10:55:59

标签: c++ winapi visual-c++

如何在运行时在WIN32中更改窗口的背景图像?

我使用CreateWindow()创建了窗口。然后我有一个LoadImage按钮打开一个模型对话框(打开文件对话框)...现在我想使用此对话框返回的文件名作为窗口的图像。

4 个答案:

答案 0 :(得分:5)

窗口背景没有原生概念,您必须手动绘制图像,通常在WM_ERASEBKGND消息中。如果您使用的是MFC,可以查看以下文章:http://www.codeproject.com/KB/graphics/picturewindow.aspx

答案 1 :(得分:2)

编译下面的代码。

在文本框中输入您的文件名,然后按更改背景。

我使用mingw 4.7。

你只能使用.bmp图像,如果你想使用其他图像你必须使用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);
}

答案 2 :(得分:1)

CoreyStup在窗口回调中解释的一段示例代码:

switch (message)
{
   case WM_CREATE:
   {
       HBITMAP hImage = (HBITMAP)LoadImage(NULL, L"guibg.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
       HWND hImageView = CreateWindowEx(NULL, L"STATIC", NULL, SS_BITMAP | WS_VISIBLE | WS_CHILD, 0, 00, 500, 600, hWnd, (HMENU)IMAGE_VIEW, GetModuleHandle(NULL), NULL);
       SendMessage(hImageView, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImage);
...

还向fritzone致信,他们在这个类似的主题上回答了这个问题:How to add picture box in win32 API using visual c++

注意:使用paint生成的位图,否则LoadImage可能会返回NULL而不会出现错误,并且图像将不可见。

答案 3 :(得分:0)

static control可以有背景图片(虽然它仅限于位图)。您可以将一个子静态控件放在窗口内(边缘到边缘的大小)并将其设置为背景。

如果您想要像透明度这样的精美图像处理,则需要手动执行此操作。