开发者

How to add picture box in win32 API using visual c++

I have a Window (win32 API) Application in visual c++. I am not using MFC. I h开发者_JS百科ave 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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜