使用CreateWindow显示BMP

时间:2011-02-26 11:50:47

标签: c++ winapi bitmap

我想在屏幕顶部显示一个徽标。徽标是一个bmp文件。我做了一个示例程序来显示完整工作代码中会发生什么:

#include <windows.h>
#include <commctrl.h>
#include <tchar.h>
#include "resource.h"

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);

char szClassName[] = "BmpTestApp";

int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
    HWND hwnd;
    MSG messages;
    WNDCLASSEX wincl;

    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof(WNDCLASSEX);

    wincl.hIcon = NULL;
    wincl.hIconSm = NULL;
    wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;

    wincl.hbrBackground = (HBRUSH)COLOR_WINDOW;

    if(!RegisterClassEx(&wincl))
        return 0;

    DEVMODE display;
    EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &display);

    hwnd = CreateWindowEx(WS_OVERLAPPED, szClassName, "Bitmap Display Test", WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, HWND_DESKTOP, NULL, hThisInstance, NULL);
    ShowWindow(hwnd, nCmdShow);

    while(GetMessage(&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }

    return messages.wParam;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        HWND logo;
        case WM_CREATE:
            RECT hwndRect;
            GetWindowRect(hwnd, &hwndRect);
            int winWidth;
            int winHeight;
            winWidth = hwndRect.right - hwndRect.left;
            winHeight = hwndRect.bottom - hwndRect.top;

            HBITMAP logoImage;
            logoImage = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_LOGO), IMAGE_BITMAP, winWidth - 180, winHeight - 530, LR_DEFAULTCOLOR);
            logo = CreateWindow(WC_STATIC, NULL, SS_BITMAP | SS_CENTER | WS_CHILD | WS_VISIBLE, 0, 0, winWidth - 180, winHeight - 530, hwnd, NULL, NULL, NULL);
            SendMessage(logo, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)logoImage);
            break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hwnd, message, wParam, lParam);
    }

    return 0;
}


// resource.rc
IDB_LOGO BITMAP "Logo.bmp"

// resource.h
#define IDB_LOGO 300

当我运行程序时,只有一个图像大小的框,但里面没有图像。 Logo.bmp绝对位于我的resource.rc文件所在的文件夹中,以及所有其他项目文件。为什么不显示?感谢。

2 个答案:

答案 0 :(得分:2)

您没有为未处理的邮件调用DefWindowProc。

答案 1 :(得分:2)

删除SS_CENTER样式标志,它与SS_BITMAP冲突。