2012-06-27 147 views
1

我討厭我之前做過,我覺得我錯過了一些東西,我不知道它是什麼。所以,我試圖在C++ WinAPI中設置一個窗口的背景圖像。C++ WINAPI設置窗口背景圖片不起作用

我Resources.rc

#include "Resources.h" 

BACKGROUND BITMAP "background.bmp" 

我Resources.h

#define BACKGROUND 200 

我Main.cpp的

.... 
WNDCLASSEX wcls; 
wcls.cbSize   = sizeof(WNDCLASSEX); 
wcls.style   = CS_PARENTDC|CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS; 
wcls.lpfnWndProc = WndProc; 
wcls.cbClsExtra  = 0; 
wcls.cbWndExtra  = 0; 
wcls.hInstance  = hInstance; 
wcls.hIcon   = LoadIcon(hInstance, MAKEINTRESOURCE(HICON)); 
wcls.hCursor  = LoadCursor(NULL, IDC_ARROW); 
wcls.hbrBackground = HBRUSH(COLOR_WINDOW + 1); 
wcls.lpszClassName = L"WndClass"; 
wcls.hIconSm  = LoadIcon(hInstance, MAKEINTRESOURCE(HICON)); 

HWND Window = CreateWindowEx (
          WS_EX_LAYERED|WS_EX_TOOLWINDOW, 
          wcls.lpszClassName, 
          L"Window Title", 
          WS_VISIBLE|WS_POPUP|WS_CLIPCHILDREN, 
          CW_USEDEFAULT, 
          CW_USEDEFAULT, 
          384, 
          128, 
          NULL, 
          NULL, 
          hInstance, 
          NULL 
          ); 

if (!Window) return 0; 

SetLayeredWindowAttributes(
          Window, 
          RGB(35, 35, 35), 
          85, 
          LWA_ALPHA|LWA_COLORKEY 
          ); 

SetWindowPos(
      Window, 
      HWND_TOPMOST, 
      0, 
      0, 
      0, 
      0, 
      SWP_NOMOVE|SWP_NOREDRAW|SWP_NOSIZE 
      ); 

ShowWindow(Window, SW_SHOWNORMAL); 
UpdateWindow(Window); 


LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM Wpar, LPARAM Lpar) { 
HBITMAP Background = NULL; 
BITMAP BgImg; 
HDC DeviceContext, BgContext; 
PAINTSTRUCT PaintStruct; 

switch (Msg) { 
    case WM_PAINT: 
     ContextLansator = BeginPaint(hWnd, &PaintStruct); 
     Fundal = LoadBitmap(hInstance, MAKEINTRESOURCE(BACKGROUND)); 
     FundalLansator = CreateCompatibleDC(DeviceContext); 
     SelectObject(DeviceContext, Background); 
     GetObject(Background, sizeof(BgImg), &BgImg); 
     BitBlt(DeviceContext, 0, 0, 384, 128, BgContext, 0, 0, SRCAND); 
     DeleteObject(Background); 
     DeleteDC(BgContext); 
     EndPaint(hWnd, &PaintStruct); 
    break; 
..... 

我使用的代碼::塊。資源嵌入正確,窗口開始,但只是一個白色的背景,圖像不繪。 WM_PAINT消息在開始時被調用一次。我填充像BgImg有問題但我不知道是什麼。需要一些幫助!謝謝!

+0

嗯,你正在使用很多未初始化的變量。 –

+0

LoadBitmap返回NULL – ali

+0

然後位圖並不存在於由hInstance指定的模塊中。確保你的hInstance是正確的,並且資源真的是一個整數資源。 –

回答

1

在註冊類之前創建一個PatternBrush並將其設置爲背景。例如:

ATOM MyRegisterClass(HINSTANCE hInstance) 
{ 
    WNDCLASSEX wcex; 

    wcex.cbSize = sizeof(WNDCLASSEX); 

    wcex.style   = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc = WndProc; 
    wcex.cbClsExtra  = 0; 
    wcex.cbWndExtra  = 0; 
    wcex.hInstance  = hInstance; 
    wcex.hIcon   = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINAPIBACKGROUND)); 
    wcex.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground = CreatePatternBrush(LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1))); 
    wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WINAPIBACKGROUND); 
    wcex.lpszClassName = szWindowClass; 
    wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); 

    return RegisterClassEx(&wcex); 
} 

它看起來像這樣: enter image description here

位圖的深度不應該的問題。但看看你的實現中的樣式。

enter image description here

+0

我只是得到一個黑色的背景。我的位圖是24位的,這可能是問題嗎? – ali

+0

LoadBitmap返回NULL – ali

+0

這意味着您沒有使用來自資源文件的位圖的正確標識符。 – Indy9000

1

我用這個代碼,它爲我工作的罰款。 我也使用代碼塊4.7。

case WM_PAINT: 
{ 
    PAINTSTRUCT  ps; 
    HDC    hdc; 
    BITMAP   bitmap; 
    HDC    hdcMem; 
    HGDIOBJ   oldBitmap; 
    hBitmap01 = (HBITMAP)LoadImage(NULL, "c:\\energy.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); 


    hdc = BeginPaint(wnd01, &ps); 

    hdcMem = CreateCompatibleDC(hdc); 
    oldBitmap = SelectObject(hdcMem, hBitmap01); 

    GetObject(hBitmap01, sizeof(bitmap), &bitmap); 
    BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY); 

    SelectObject(hdcMem, oldBitmap); 
    DeleteDC(hdcMem); 

    EndPaint(wnd01, &ps); 
    }