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

后端 未结 1 518
[愿得一人]
[愿得一人] 2021-02-20 06:46

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. C

1条回答
  •  走了就别回头了
    2021-02-20 07:05

    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 讨论(0)
提交回复
热议问题