Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

winapi

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.

like image 991
Ravi shankar Avatar asked Nov 18 '09 06:11

Ravi shankar


People also ask

How do I add an image to a dialog box?

(1) Added Images as resources ( You can see images in the "Resource Files" ). (2) Created the Dialog Box where the image would be displayed. (3) Added a Picture Control to the dialog box.

What is PictureBox in Windows Forms?

PictureBox control is used to display images in Windows Forms. In this article, I will discuss how to use a PictureBox control to display images in Windows Forms applications. PictureBox control is used to display images in Windows Forms.

What is Win32 API in C++?

Related topics The Win32 API (also called the Windows API) is the original platform for native C/C++ Windows applications that require direct access to Windows and hardware. It provides a first-class development experience without depending on a managed runtime environment like.NET and WinRT (for UWP apps for Windows 10).

How to add PictureBox to a form in Salesforce?

PictureBox class represents a PictureBox control. The following code snippet creates a PictureBox, sets its width and height and adds control to the Form by calling Controls.Add () method. Image property is used to set an image to be displayed in a PictureBox control.


1 Answers

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.

like image 174
Ferenc Deak Avatar answered Oct 09 '22 20:10

Ferenc Deak