开发者

How to draw image on a window?

I have created a window with createwindow() api using VS2005 in C++ on Windows Vista

My requirement is开发者_开发问答 to draw an image (of any format) on that window. I am not using any MFC in this application.


not exactly sure what is your problem: draw a bitmap on the form, or you would like know how to work with various image formats, or both. Anyways below is an example of how you could load a bitmap and draw it on the form:

HBITMAP hBitmap = NULL;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;

    switch (message)
    {
<...>

    case WM_CREATE:
        hBitmap = (HBITMAP)LoadImage(hInst, L"c:\\test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
        break;
    case WM_PAINT:
        PAINTSTRUCT     ps;
        HDC             hdc;
        BITMAP          bitmap;
        HDC             hdcMem;
        HGDIOBJ         oldBitmap;

        hdc = BeginPaint(hWnd, &ps);

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

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

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

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        DeleteObject(hBitmap);
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

LoadImage loads an icon, cursor, animated cursor, or bitmap. Details here

For working with various images formats you can use Windows Imaging Component (see IWICBitmapDecoder) or code from here Loading JPEG and GIF pictures or 3rd party tools like FreeImage or LeadTools

hope this helps, regards


void LoadScreen(HWND hWnd) {
    RECT rect;
    HDC hdc = GetDC(hWnd);
    HBRUSH brush = CreatePatternBrush((HBITMAP)LoadImage(NULL, L"file.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE));
    GetWindowRect(hWnd, &rect);
    FillRect(hdc, &rect, brush);
    DeleteObject(brush);
    ReleaseDC(hWnd, hdc);
}


#include <windows.h>
#include <string.h>

HBITMAP hBitmap, hOldBitmap;
HDC hdc, hdcMem;
BITMAP bm;
HINSTANCE hI;
PAINTSTRUCT ps;
RECT rect;
RECT rc;

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
    {
    case WM_CREATE:
    hBitmap = (HBITMAP)LoadImage(hI, "1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    GetObject(hBitmap, sizeof(BITMAP), &bm);
    hdc = GetDC(hWnd);
    hdcMem = CreateCompatibleDC(hdc);
    hOldBitmap = SelectBitmap(hdcMem, hBitmap);
    ReleaseDC(hWnd, hdc);
    return 0;

    case WM_LBUTTONDOWN:
    //for dragging not only by the title, but also by any part of the window 
    ReleaseCapture();
    SendMessage(hWnd, 0xA1, 2, 0);
    break;
    case WM_PAINT:
    hdc=BeginPaint(hWnd,&ps);
    
    //overlay image with stretching to fit the window 
    GetClientRect(hWnd,&rect);
    SetStretchBltMode(hdc, STRETCH_HALFTONE);
    StretchBlt(hdc,0,0,rect.right,rect.bottom,
    hdcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
    
    EndPaint(hWnd,&ps);
    break;      


    case WM_DESTROY:
    PostQuitMessage(0);
      
    DeleteDC(hdcMem);
    DeleteObject(hBitmap);
    DeleteObject(hOldBitmap);
    break;
    }
return DefWindowProc(hWnd, msg, wParam, lParam);
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPInst, LPSTR lpCmdLine, int nCmdShow)
{
//copying a pointer to a running application instance (module)
hI=hInstance;

WNDCLASS wc;

wc.style         = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc   = WindowProcedure;
wc.hInstance     = hInstance;
wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
wc.lpszClassName = "test_class";
wc.lpszMenuName  = NULL;
wc.cbClsExtra    = 0;
wc.cbWndExtra    = 0;

RegisterClass(&wc);

HWND hWnd = CreateWindow(wc.lpszClassName, "Image Window", 
//window with title (overlapping window) 
WS_OVERLAPPEDWINDOW,
//window without title
//WS_VISIBLE | WS_POPUP | WS_SYSMENU | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 500, NULL, NULL, hInstance, NULL);

ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

MSG msg;
while(GetMessage (&msg, NULL, 0, 0))
    {
    DispatchMessage (&msg);
    TranslateMessage (&msg);
    }
UnregisterClass(wc.lpszClassName, hInstance);
return (int) msg.wParam;
}

How to draw image on a window?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜