Thread View: borland.public.cppbuilder.nativeapi
2 messages
2 total messages
Started by Renald Yazmir
Thu, 01 May 2008 15:04
Draw Transparent Bitmap
Author: Renald Yazmir
Date: Thu, 01 May 2008 15:04
Date: Thu, 01 May 2008 15:04
85 lines
3178 bytes
3178 bytes
Hello,
I am using a function that I found on line to draw a transparent bitmap in
the window.
This function causing a software crash when I resize a window or move a
dialog box over the picture.
LoadBitmap returns NULL.
What is wrong with this function?
Does anyone has a complete example similar to this function?
void draw_transparent_picture(
HWND hwnd,
int pictid,
RECT *rct,
COLORREF transparent_color
)
{
BITMAP bitmap;
COLORREF crOldBack = 0;
HDC dcImage = NULL;
HDC dcTrans = NULL;
HBITMAP pOldBitmapImage = NULL;
HBITMAP bitmapTrans = NULL;
HBITMAP pOldBitmapTrans = NULL;
HBITMAP hBitmap = NULL;
HDC hDC = NULL;
HDC hMemDC = NULL;
hDC = GetDC(hwnd);
if ((hMemDC = CreateCompatibleDC(hDC)) == (HDC) NULL)
return;
hBitmap = LoadBitmap(res_lib, MAKEINTRESOURCE(pictid));// load from
resource dll
if (hBitmap == NULL)
return;
crOldBack = SetBkColor(hDC, RGB(255, 255, 255));
// Create two memory dcs for the image and the mask
dcImage=CreateCompatibleDC(hDC);
dcTrans=CreateCompatibleDC(hDC);
// Select the image into the appropriate dc
pOldBitmapImage = (HBITMAP)SelectObject(dcImage, hBitmap);
// Create the mask bitmap
GetObject(hBitmap, sizeof(BITMAP), &bitmap);
bitmapTrans=CreateBitmap(bitmap.bmWidth, bitmap.bmHeight, 1, 1, NULL);
// Select the mask bitmap into the appropriate dc
pOldBitmapTrans = (HBITMAP)SelectObject(dcTrans, bitmapTrans);
// Build mask based on transparent colour
SetBkColor(dcImage, transparent_color);
BitBlt(dcTrans, 0, 0, bitmap.bmWidth, bitmap.bmHeight, dcImage, 0, 0,
SRCCOPY);
// Do the work - True Mask method - cool if not actual display
BitBlt(hDC, rct->left, rct->top, bitmap.bmWidth, bitmap.bmHeight, dcImage,
0, 0, SRCINVERT);
BitBlt(hDC, rct->left, rct->top, bitmap.bmWidth, bitmap.bmHeight, dcTrans,
0, 0, SRCAND);
BitBlt(hDC, rct->left, rct->top, bitmap.bmWidth, bitmap.bmHeight, dcImage,
0, 0, SRCINVERT);
// Restore settings
SelectObject(dcImage, pOldBitmapImage);
SelectObject(dcTrans, pOldBitmapTrans);
SetBkColor(hDC, crOldBack);
DeleteDC (hMemDC);
DeleteDC (dcImage);
DeleteDC (dcTrans);
ReleaseDC(hwnd, hDC);
}
Thank you
Rony
Re: Draw Transparent Bitmap
Author: Bob Gonder
Date: Thu, 01 May 2008 16:47
Date: Thu, 01 May 2008 16:47
57 lines
2216 bytes
2216 bytes
Renald Yazmir wrote:
>This function causing a software crash when I resize a window or move a
>dialog box over the picture.
>LoadBitmap returns NULL.
>
>What is wrong with this function?
Not sure, could be a problem with the caller,
but will highlight some points...
>void draw_transparent_picture(
> HWND hwnd,
> int pictid,
> RECT *rct,
> COLORREF transparent_color
> )
>{
> BITMAP bitmap;
> COLORREF crOldBack = 0;
0 is never used.
> HDC dcImage = NULL;
> HDC dcTrans = NULL;
> HBITMAP pOldBitmapImage = NULL;
> HBITMAP bitmapTrans = NULL;
> HBITMAP pOldBitmapTrans = NULL;
> HBITMAP hBitmap = NULL;
> HDC hDC = NULL;
> HDC hMemDC = NULL;
All those NULLs are never used.
> hDC = GetDC(hwnd);
>
> if ((hMemDC = CreateCompatibleDC(hDC)) == (HDC) NULL)
> return;
You didn't release the hDC.
> hBitmap = LoadBitmap(res_lib, MAKEINTRESOURCE(pictid));// load from
>resource dll
>
> if (hBitmap == NULL)
> return;
You didn't delete the hMemDC
You didn't release the hDC.
> crOldBack = SetBkColor(hDC, RGB(255, 255, 255));
>
> // Create two memory dcs for the image and the mask
> dcImage=CreateCompatibleDC(hDC);
> dcTrans=CreateCompatibleDC(hDC);
You don't check those HDCs.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads