2.5. Sample Code: ShowImage.g

This tutorial demonstrates how to add a BMP image using the GStatic control.

Code

require ("Application");
require ("WindowsSupport");

class ShowImage Application
{
	window;
	imageFile = "c:/tmp/Capture.bmp";
	imageHandles;
}

/*
 * This creates a GStatic and populates it with an image from a file.
 * The image must be in Windows BMP format.  You can specify a width
 * and height to force the image to be scaled to fit that size.  If
 * you specify 0 for both width and height then the image will be
 * rendered in its natural size. Notice that we need to store the
 * image handle and then free it during the destructor.
 */
method ShowImage.createImage (parent, x, y, width, height, imageFile)
{
	local imageObj = parent.CreateControl(GStatic, x, y, width,
					      height, "", SS_BITMAP);
	local handle = LoadImage(0, imageFile, IMAGE_BITMAP,
				 width, height, LR_LOADFROMFILE);
	.imageHandles = cons(handle, .imageHandles);
	imageObj.SendMessage(STM_SETIMAGE, IMAGE_BITMAP, handle);
	imageObj;
}

/* Write the 'main line' of the program here. */
method ShowImage.constructor ()
{
	local rect = CreateRect(10, 10, 400, 400);
	local win;
	
	win = new GWindow();
	win.Create(0, rect, "Test Window", WS_OVERLAPPEDWINDOW, 0);
	win.MessageHandler (WM_DESTROY,
			    `(!destroyed_p(@self) ?
			      destroy(@self) : nil));
	
	.createImage(win, 10, 10, 0, 0, .imageFile);
	
	.window = win;
	win.ShowWindow(SW_SHOW);
}

/* Any code to be run when the program gets shut down.  We need to
   delete the image handles. */
method ShowImage.destructor ()
{
	with handle in .imageHandles do
		DeleteObject(handle);
	if (instance_p(.window))
		destroy(.window);
}

/* Start the program by instantiating the class. */
ApplicationSingleton (ShowImage);