11

I have a Window (win32 API) Application in visual c++. I am not using MFC. I have to add a picutre box to my application and Change the image of this picture box periodically. Can any one help me out in achieving the above task? Thanks in advance.

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Ravi shankar
  • 2,450
  • 1
  • 25
  • 34

1 Answers1

8

This is quite a complex task to post full code here, but I will try to give a few guidelines on how to do it:

First method is to load the image and paint it

  1. Load your image (unfortunately the plain Win32 API has support for quite a few image formats BMP, ICO ...).

    HBITMAP hImage = (HBITMAP)LoadImage(NULL, (LPCSTR)file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_LOADTRANSPARENT);
    
  2. Store the handle above somewhere in your application where you can access it from your WindowProcedure

  3. In the WinProc on the WM_PAINT message you will need to paint the image. The code is something like:

    HDC hdcMem = CreateCompatibleDC(hDC); // hDC is a DC structure supplied by Win32API
    SelectObject(hdcMem, hImage);
    StretchBlt(
        hDC,         // destination DC
        left,        // x upper left
        top,         // y upper left
        width,       // destination width
        height,      // destination height
        hdcMem,      // you just created this above
        0,
        0,          // x and y upper left
        w,          // source bitmap width
        h,          // source bitmap height
        SRCCOPY);   // raster operation
    

Should work.

Now, the second way of doing it is to create a static control, with type being SS_BITMAP and set its image as:

hImage = LoadImage(NULL, file, IMAGE_BITMAP, w, h, LR_LOADFROMFILE);
SendMessage(hwnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImage);

where hwnd is the handle of your static control.

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167