<!-- Article image -->
<!-- Add the rest of your HTML here -->
Aug 23, 2003 - Fixed transparent drawing bug
After I wrote my CFileEditCtrl edit control, I got a request to place an image on the browse button, and then another request to make the control flat and hot to the mouse. That was becoming quite a task, searching on this site and www.codeguru.com for drawing code that would draw images, either bitmaps or icons, either normally or transparently, grey-scaled or disabled, possibly stretched or at least centered on the button. So after finding some code that solved one bit or another (but not quite the way I wanted), I put them together, filled in the pieces that were missing and came up with the
CPJAImage class. While the reason I wrote the class was for the browse button, I thought it was a shame not to share it with you as yet another separate image rendering class.
Jean-Edouard Lachand-Robert for his
Paul Reynolds for the "True Mask" transparency function.
Zafir Anjum and Guy Gascoigne for their greyscale algorithm.
Using the CPJAImage class
The first step is to include the PJAImage.h and PJAImage.cpp files in your project. Then after you have loaded or created your bitmap or icon object in the normal fashion, select it into the
CPJAImage object using the
Then when you are ready to draw the image, use the
DrawImage() function to draw the image onto the given device context.
PJAImage.DrawImage(pDC, left, top, width, height, DrawFlags);
The drawing flags you specify in the
DrawImage() function determine how the image is drawn.
The class constructor creates an empty
The class destructor destroys the image handle
void CPJAImage::DrawImage(CDC *pToDC, int x, int y, int w, int h, DWORD DrawFlags = 0)
DrawImage() does the actual drawing.
|A pointer to the device context the image is to be drawn on.|
|The left side of the bounding rectangle where the image is to be drawn|
|The top of the bounding rectangle|
|The width of the bounding rectangle|
|The height of the bounding rectangle|
|The flags used to control the drawing|
Flags used in the DrawFlags parameter
|Draws the image centered on the supplied rectangle. If the image is larger than the rectangle, it will be clipped. This flag can not be used with the |
|Draws the image so that it fills the supplied rectangle. Bitmaps are stretched using the |
StretchBlt() API function. Icons are stretched using the
DrawIconEx() API function. This flag can not be used with the
|Draws the image as disabled (3D Monochrome). This flag can not be used with the |
|Draws the image in grayscale. This flag can not be used with the |
|Draws the image transparently. If the transparent color is not specified with the |
SetTransparentColor() function, the color at the top left corner (GetPixel(0, 0)) of the image is used as the transparent color. This flag has no effect if the image is an icon, as icons are transparent by default.
CSize object that contains the size of the image currently contained in the
BOOL CPJAImage::SetImage(HANDLE hImage, DWORD Flags)
Selects the supplied image into the
CPJAImage object. Returns
TRUE if the supplied image was successfully selected,
FALSE if not.
HANDLE of the image, can be a
HBITMAP (DIB or DDB) or a
|Specifies the image flags.|
Flags used by the SetImage() function
|The given handle is an |
|The given handle is a |
|The given handle will be deleted and the memory freed when a new image is set or the |
CPJAImage object is deleted. If this flag is not set, the user of this class is responsible for freeing the image handle when it is no longer needed
COLORREF CPJAImage::SetTransparentColor(COLORREF clr = CLR_DEFAULT)
Sets the color to be used as the transparent color for the bitmap image. Returns the previous transparent color. If
CLR_DEFAULT (0xFF000000) is set as the transparent color, the color of the pixel at the top left corner (
GetPixel (0, 0) ) is used as the transparent color.
Be sure to check out the demo app to see the
CPJAImage class in action. While the demo has a preloaded image in it, you can drag 'n drop your favorite picture files ( *.ico, *.bmp, *.jpg, or *.gif ) onto the demo app.
To change the transparent color, click the Set Color button, and then use the mouse to select the color from the picture displayed.