开发者

Display BMP using CreateWindow

I want to display a logo at the top of the screen. The logo is a bmp file. I made an example program to show what happens in full working code:

#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(GetModul开发者_StackOverfloweHandle(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

When I run my program, there is just a box the size of the image, but no image inside. Logo.bmp is definately in the folder where my resource.rc file is and all the other project files. Why is it not displaying? Thanks.


You aren't calling DefWindowProc for the messages that you don't handle.


Drop the SS_CENTER style flag, it conflicts with SS_BITMAP.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜