Article View: borland.public.cppbuilder.nativeapi
Article #332Draw Transparent Bitmap
From: 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
Technical Details
Message-ID:
<4819ce0a$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,c8bc454d19975d6f,start X-Google-Attributes: gid10cc67,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Reply-To: "Renald Yazmir" <renald@partmaker.com> From: "Renald Yazmir" <renald@partmaker.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Draw Transparent Bitmap Date: Thu, 1 May 2008 10:04:47 -0500 Lines: 87 Organization: IMCS Inc X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 66.106.28.123 Message-ID: <4819ce0a$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1209650698 66.106.28.123 (1 May 2008 07:04:58 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail