18

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Vinayaka Karjigi
  • 1,070
  • 5
  • 13
  • 37

3 Answers3

33

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

serge_gubenko
  • 20,186
  • 2
  • 61
  • 64
  • Hi Serge, it solved my problem of drawing image on window. thanks for the help. but as this takes only bmp,cur and ico files, i need to work on converting png to bmp and then i will pass that bmp to this function – Vinayaka Karjigi Nov 19 '09 at 04:40
  • can anybody guide me in putting png image on window without using any MFC – Vinayaka Karjigi Nov 23 '09 at 05:42
  • 2
    If I can save someone a few minutes of searching around, I tried using this answer but it didn't work. After a while of poking I tried to change the sizeof(bitmap) to sizeof(BITMAP) and the code worked. I'm using Visual Studio 2015 on Windows 10. Heres the full line : GetObject(hBitmap, sizeof(BITMAP), &bitmap); – Tristan Dubé Aug 02 '16 at 01:34
  • Why bother saving the old HBITMAP and calling SelectObject again if you delete the DC immediately after? – flarn2006 Oct 09 '20 at 16:02
9
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);
}
Bja
  • 180
  • 1
  • 9
Ahmed Hussein
  • 715
  • 1
  • 15
  • 38
  • 3
    And the leaked HDC that was never released upon function exit? Windows GDI rules 101: If you Get it, then Release it; if you Create it, then Delete it. You did this correctly with your brush (Create/Delete), but failed to do so with your client DC (Get/Release). – WhozCraig May 01 '17 at 22:46
  • 2
    Ah, thanks :) I resolved it. Just added one line before closing the curly bracket. ReleaseDC(hWnd, hdc); – Ahmed Hussein May 01 '17 at 23:36
  • Functionality wise, how does this compare to [serge_gubenko's answer](https://stackoverflow.com/a/1760571/3357935)? – Stevoisiak Aug 02 '18 at 14:46
  • 1
    I don't know, but I see that my solution is much simpler, clean and straight-forward – Ahmed Hussein Dec 19 '19 at 16:55
2
#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;
}

enter image description here