Free Educational Resource Center for teachers and students. Includes Interviews,
Sourcecode, Free Software, Research Papers, Articles, Tutorials and much more..
     R E S E A R C H A C T I V I T Y . C O M
Our Fellow Research Center for Ph.D Schollars
Home About Submit & Earn Archives: C And C + + Programming » Dev Packages » Interviews » Php Mysql Programming » Windows Programming
search
Windows Programming > using static control as image display box in winapi

Using Static Control As Image Display Box



This is a very short tutorial (a kick start), to give you idea of loading adding images to your projects.

It is very simple to load image at runtime either from external image file or resources, or even from another dll or ocx file.



NOTE: this article to specific to windows BMP files only



In following example I will be using a 'STATIC' as our image container, to get rid of painting routine we have to write if we go from scratch. Once you have loaded an image that is kept in a variable of type HBITMAP, it is your responsibility to draw it on surface (client area) when window is repainted by system, that is the reason we are using 'STATIC' type of control that is capable of displaying image over it, and handles the bitmap painting itself.



I hope you are familiar with functions CreateWindow and CreateWindowEx, if not then consult msdn, one of these is being used in the supplied complete example. Besides here is the code to create the static that can hold a bitmap in it:



HWND myBox = CreateWindowEx(0, "STATIC","",

SS_CENTERIMAGE | SS_REALSIZEIMAGE | SS_BITMAP | WS_CHILD | WS_VISIBLE,

10,10,380,380,

myDialog,

(HMENU)-1,

NULL,

NULL

);






Ok what we have to do first, is

1. load the image, we are now loading image from external file

HBITMAP bitmap = (HBITMAP)LoadImage(NULL,"image.bmp", IMAGE_BITMAP,0,0,LR_LOADFROMFILE);




check if the image was really loaded or not, the HBITMAP variable will be NULL if the imageis NOT loaded.

if(bitmap == NULL)  { //this means image is not load

//take appropriate action

}




2. Then if loading image from resource of your application, at runtime:

Two conditions basically,

a.if you have named the bitmap resource as a string in resources

b.if you have assigned an integeral id to your bitmap resurce



Assume it was a string "ABC"



HBITMAP bitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL),"ABC", IMAGE_BITMAP,0,0,0);




Assume it was an integer id 123



HBITMAP bitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(123), IMAGE_BITMAP,0,0,0);




Now it is time to assign that bitmap to a static control:, where myBox is the HWND type handle of the static control.

SendMessage(myBox, STM_SETIMAGE,  (WPARAM)IMAGE_BITMAP, (LPARAM)bitmap);






Troubleshooting:

You might face unexpected drawn lines over your static control, when you resize it at runtime, to revent this kind of default behavior, change the class style of the static control:

SetClassLong(myBox ,GCL_STYLE,CS_HREDRAW | CS_VREDRAW);



which will not let the contained bitmap distrubed when resized.



And here goes the complete example:



#include <windows.h>





bool WINAPI myProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {

if(message == WM_CLOSE)

PostQuitMessage(0);



return false;

}



int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {

//create the main dialog

HWND myDialog = CreateWindowEx(0,WC_DIALOG,"apitalk.com",

WS_VISBLE|WS_OVERLAPPEDWINDOW,

400,100,405,450,NULL,NULL,NULL,NULL

);

//attach a dialog procedure to it

SetWindowLong(myDialog, DWL_DLGPROC, (long)myProc);



//create the static text that will hold image

HWND myBox = CreateWindowEx(0, "STATIC","",

SS_CENTERIMAGE | SS_REALSIZEIMAGE | SS_BITMAP | WS_CHILD | WS_VISIBLE,

10,10,380,380,myDialog,(HMENU)-1,NULL,NULL

);

SetClassLong(myBox ,GCL_STYLE,CS_HREDRAW | CS_VREDRAW);



//load from file first

HBITMAP bitmap = (HBITMAP)LoadImage(NULL,"image.bmp", IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

//put bitmap on static control

SendMessage(myBox,STM_SETIMAGE, (WPARAM)IMAGE_BITMAP,(LPARAM)bitmap);



MSG msg;

while(GetMessage(&msg,NULL,0,0)) {

TranslateMessage(&msg);

DispatchMessage(&msg);

}



return 0;

}






loadimage_winapi.cpp:1245:8