Using a mask for drawing non rectangular bitmaps
-
I have a problem with drawing a non rectangular bitmap using a mask bitmap. The two bitmaps are derived from an icon file in the resources. I want to create a HBITMAP object that contains the correct result of the masked drawing.
HICON hIcon5 = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON12)); GetIconInfo(hIcon5, &iinfo);//retreive the BITMAP bm; GetObject(iinfo.hbmColor,sizeof(bm),&bm); HDC hdcToolbar = GetDC(hToolbar);//hToolbar is a window HDC hdcBitmap5 = CreateCompatibleDC(hdcToolbar); HDC hdcMask = CreateCompatibleDC(hdcToolbar) ; HDC hdcColor = CreateCompatibleDC(hdcToolbar) ; HBITMAP hBitmap5 = CreateCompatibleBitmap(hdcBitmap5,bm.bmWidth,bm.bmHeight); HGDIOBJ hOldBitmap5 = SelectObject(hdcBitmap5, hBitmap5); HGDIOBJ hOldMask = SelectObject(hdcMask, iinfo.hbmMask) ; HGDIOBJ hOldColor = SelectObject(hdcColor, iinfo.hbmColor); //Now comes the difficult part where I can't find the solution BitBlt (hdcBitmap5, 0, 0, bm.bmWidth,bm.bmHeight, NULL, 0, 0, WHITENESS); BitBlt (hdcBitmap5, 0, 0, bm.bmWidth,bm.bmHeight, hdcMask, 0, 0, 0x220326);//found this code in Pedzold, chapter 14) BitBlt (hdcBitmap5, 0, 0, bm.bmWidth,bm.bmHeight, hdcColor, 0, 0, SRCPAINT) ; SelectObject(hdcMask, hOldMask) ; SelectObject(hdcColor, hOldColor); SelectObject(hdcBitmap5, hOldBitmap5); DeleteDC(hdcMask); DeleteDC(hdcColor); DeleteDC(hdcBitmap5); ReleaseDC(hToolbar, hdcToolbar);
Download the icon i used for the test here Can someone help me please? thanks in advance, Ward -
I have a problem with drawing a non rectangular bitmap using a mask bitmap. The two bitmaps are derived from an icon file in the resources. I want to create a HBITMAP object that contains the correct result of the masked drawing.
HICON hIcon5 = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON12)); GetIconInfo(hIcon5, &iinfo);//retreive the BITMAP bm; GetObject(iinfo.hbmColor,sizeof(bm),&bm); HDC hdcToolbar = GetDC(hToolbar);//hToolbar is a window HDC hdcBitmap5 = CreateCompatibleDC(hdcToolbar); HDC hdcMask = CreateCompatibleDC(hdcToolbar) ; HDC hdcColor = CreateCompatibleDC(hdcToolbar) ; HBITMAP hBitmap5 = CreateCompatibleBitmap(hdcBitmap5,bm.bmWidth,bm.bmHeight); HGDIOBJ hOldBitmap5 = SelectObject(hdcBitmap5, hBitmap5); HGDIOBJ hOldMask = SelectObject(hdcMask, iinfo.hbmMask) ; HGDIOBJ hOldColor = SelectObject(hdcColor, iinfo.hbmColor); //Now comes the difficult part where I can't find the solution BitBlt (hdcBitmap5, 0, 0, bm.bmWidth,bm.bmHeight, NULL, 0, 0, WHITENESS); BitBlt (hdcBitmap5, 0, 0, bm.bmWidth,bm.bmHeight, hdcMask, 0, 0, 0x220326);//found this code in Pedzold, chapter 14) BitBlt (hdcBitmap5, 0, 0, bm.bmWidth,bm.bmHeight, hdcColor, 0, 0, SRCPAINT) ; SelectObject(hdcMask, hOldMask) ; SelectObject(hdcColor, hOldColor); SelectObject(hdcBitmap5, hOldBitmap5); DeleteDC(hdcMask); DeleteDC(hdcColor); DeleteDC(hdcBitmap5); ReleaseDC(hToolbar, hdcToolbar);
Download the icon i used for the test here Can someone help me please? thanks in advance, WardWhy not just use
DrawIcon()
?Ward wrote:
HBITMAP hBitmap5 = CreateCompatibleBitmap(hdcBitmap5,bm.bmWidth,bm.bmHeight);
At this point in your code
hdcBitmap5
has a one by one pixel monchrome bitmap selected into it. When you create a compatible bitmap it will also be a monochrome bitmap. You should usehdcToolbar
as the HDC for creating compatible bitmaps. [blatant self-promotion] Have a look at http://www.codeproject.com/tools/imageviewer.asp[^] [/blatant self-promotion]
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!
-
Why not just use
DrawIcon()
?Ward wrote:
HBITMAP hBitmap5 = CreateCompatibleBitmap(hdcBitmap5,bm.bmWidth,bm.bmHeight);
At this point in your code
hdcBitmap5
has a one by one pixel monchrome bitmap selected into it. When you create a compatible bitmap it will also be a monochrome bitmap. You should usehdcToolbar
as the HDC for creating compatible bitmaps. [blatant self-promotion] Have a look at http://www.codeproject.com/tools/imageviewer.asp[^] [/blatant self-promotion]
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!
Well, at first I've tried DrawIcon, but when I wanted to use the bitmap hBitmap5 as the bitmap for a toolbar button (used in TB_ADDBUTTON-message) the area which was supposed to be the background color of the toolbarbuttons, were turned in white. I did found a solution last friday (at last). First I fill the hBitmap5 with the color of the toolbar buttons. The color of the toolbar buttons I was able to retreive using GetSysColor. Then I used the DrawIcon on this hBitmap5. And finally I passed this hBitmap5 to the toolbar with the TB_ADDBUTTON-message. The final result is now exact what I wanted. Anyway, thanks for your reply. kind regards, Ward -- modified at 16:09 Sunday 25th December, 2005