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.
(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.
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.
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).
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.
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
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);
Store the handle above somewhere in your application where you can access it from your WindowProcedure
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With