unable to FillRect with transparent brush when double buffering - c++

As the title says I'm unable to FillRect bitmap to be transparent. I know when creating the bitmap it is not monochrome as gray brush works fine but I have no way (that I'm aware of) to check if it is colored or grayscale. I also am aware that by default the bitmap is black hence why I'm trying to change it to transparent. I am also aware that I'm likely not cleaning up the dc's correctly however that is not the main issue. I'm trying to solve the black background by making it transparent.
#include<windows.h>
#include<iostream>
int main()
{
// Init DC
HWND Wnd = GetDesktopWindow();//GetConsoleWindow();
HDC ScreenDC = GetDC(Wnd);
// Init Rectangle
RECT ClientRect;
GetClientRect(Wnd, &ClientRect);
// Init Double Buffer
HDC MemDC = CreateCompatibleDC(ScreenDC);
HBITMAP MemBM = CreateCompatibleBitmap(ScreenDC, ClientRect.right - ClientRect.left, ClientRect.bottom - ClientRect.top);
HBITMAP OldBM = (HBITMAP)SelectObject(MemDC, MemBM);
// Create Brush and Pen
HPEN Pen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
HBRUSH ClearBrush = (HBRUSH)GetStockObject(GRAY_BRUSH);
// Set Brush and Pen
SelectObject(MemDC, Pen);
SelectObject(MemDC, ClearBrush);
POINT p;
while(!GetAsyncKeyState(VK_RETURN))
{
// Clear and Draw
GetCursorPos(&p);
FillRect(MemDC, &ClientRect, ClearBrush);
Rectangle(MemDC, p.x, p.y, p.x+20, p.y+20);
BitBlt(ScreenDC, 0, 0, ClientRect.right - ClientRect.left, ClientRect.bottom + ClientRect.left, MemDC, 0, 0, SRCCOPY);
}
SelectObject(MemDC, OldBM);
DeleteObject(ClearBrush);
DeleteObject(Pen);
DeleteObject(OldBM);
DeleteObject(MemBM);
DeleteDC(MemDC);
ReleaseDC(Wnd, ScreenDC);
return 0;
}
I've tried many different ways of setting transparent background to no avail. The end result is a rectangle appearing over the mouse and following it across the screen however the background shouldn't be black I should be able to see other windows.

Related

Dynamic transparency geometry in MFC

I'm creating a new-UI walkthrough for my MFC application and want to highlight certain controls as the walkthrough proceeds. Specifically, I want to darken the whole window except the control I'm emphasizing.
I tried creating a partly-transparent black overlay using SetLayeredWindowAttributes, but this doesn't let me set a sub-area completely transparent. UpdateLayeredWindow can do this, but I'm not eager to create a BMP/PNG file for every control I need to highlight.
Can I create the transparency geometry dynamically? For example, can I draw bitmap transparency from scratch then load it into UpdateLayeredWindow?
I also need to be compatible with Windows 7 (despite its support EOL).
Follow-up:
Trying to paint transparent GDI+ regions, but doesn't work:
void ApplicationDlg::Highlight(const CRect& rect)
{
CRect wndRect;
GetWindowRect(&wndRect);
Gdiplus::Rect wndRectPlus(wndRect.left, wndRect.top, wndRect.Width(), wndRect.Height());
Gdiplus::Region wndRegion(wndRectPlus);
Gdiplus::Rect controlRectPlus(rect.left, rect.top, rect.Width(), rect.Height());
Gdiplus::Region highlightRegion(controlRectPlus);
wndRegion.Exclude(&highlightRegion);
Gdiplus::SolidBrush transparentBrush(Gdiplus::Color(0, 0, 0, 0));
Gdiplus::SolidBrush darkenBrush(Gdiplus::Color(128, 0, 0, 0));
CDC* pDCScreen = m_WalkthroughDlg.GetDC();
HDC hDC = CreateCompatibleDC(pDCScreen->m_hDC);
HBITMAP hBmp = CreateCompatibleBitmap(hDC, wndRect.Width(), wndRect.Height());
HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, hBmp);
Gdiplus::Graphics graphics(hDC);
graphics.FillRegion(&darkenBrush, &wndRegion);
graphics.FillRegion(&transparentBrush, &highlightRegion);
BLENDFUNCTION blend = {0};
blend.BlendOp = AC_SRC_OVER;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = AC_SRC_ALPHA;
SIZE sizeWnd = {wndRect.Width(), wndRect.Height()};
POINT ptSrc = {0,0};
m_WalkthroughDlg.UpdateLayeredWindow(pDCScreen, NULL, &sizeWnd, CDC::FromHandle(hDC), &ptSrc, NULL, &blend, ULW_ALPHA); // TODO cleanup FromHandle refs
m_WalkthroughDlg.BringWindowToTop();
SelectObject(hDC, hBmpOld);
DeleteObject(hBmp);
DeleteDC(hDC);
}
You can dynamically create a mask by using CRgn class: https://learn.microsoft.com/en-us/cpp/mfc/reference/crgn-class?view=vs-2019
It allows you to combine regions (if you need to highlight more than one area). You could then use FillRgn function to update the hdcSrc DC used in UpdateLayeredWindow.
Alternatively, if your highlights are rectangular, you could just draw rectangles on that hdcSrc.

Windows/C++ -- rendering icon to bitmap with transparency

I am no expert in anything GDI. But I was given some code YEARS ago which has served me decently well. But, it's getting old ... and with new windows10 dark theme, it's showing it's flaws.
I am rendering a menu (in an explorer menu plugin). Here's the snippet of code used to generate the bitmap.
My goal, to convert this code to generate a bitmap with transparency of the icon preserved.
(the result HBITMAP ends up in pItem->m_hBitmap )
HICON hIcon;
if ( (iIndex >= 0) && (ExtractIconEx(iconDLLPath, iIndex, NULL, &hIcon, 1) != 0) )
{
HDC hdc = CreateIC(L"DISPLAY", NULL, NULL, NULL);
HDC hdcMem = CreateCompatibleDC(hdc);
// XP demands 12x12, otherwise use 16x16
int cx = GetSystemMetrics((m_bUseSmallerIcons) ? SM_CXMENUCHECK : SM_CXSMICON);
int cy = GetSystemMetrics((m_bUseSmallerIcons) ? SM_CYMENUCHECK : SM_CYSMICON);
pItem->m_hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
HBITMAP hBmOld = (HBITMAP) SelectObject(hdcMem, pItem->m_hBitmap);
// DC: paint entire mem dc COLOR_MENU so icon looks transparent
// when painted into context menu having this background color
HBRUSH hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = cx;
rect.bottom = cy;
FillRect(hdcMem, &rect, hBrush);
DeleteObject(hBrush);
// Draw icon transparently, on top of the background color. Transparent
// areas will be the background color.
DrawIconEx(hdcMem, 0, 0, hIcon, cx, cy, 0, 0, DI_NORMAL);
// Cleanup
SelectObject(hdcMem, hBmOld);
DeleteDC(hdc);
DeleteDC(hdcMem);
DestroyIcon(hIcon);
}
I should remove the which draws the white background, but how do I put down a transparent background? Everything I've tried yields a black background.
* just removing the white "fill"
* SetBkMode(TRANSPARENT)
* using the theme code to get the menu color...
How do I go about making a proper bitmap with transparency?
Ultimately, the answer is that I am doing nothing wrong. This menu is rendered as part of a menu constructed in a shell plugin using IContextMenu.
The answer - implement IContextMenu2. And in doing so, do an owner draw menu item.

Double-buffering AlphaBlend rect with text overlay

I'm a little confused on how to double buffer this. I'm
not sure if I need to create another CreateCompatibleBitmap or CreateCompatibleDC and how to link it all.
This works as is but I don't think its double buffered right.
void __OnPaint(HWND hWnd, HDC _hdc = nullptr)
{
HDC hdc = _hdc;
PAINTSTRUCT paint;
RECT& rcClient = paint.rcPaint;
if (!_hdc)
hdc = BeginPaint(hWnd, &paint);
else
GetClientRect(hWnd, &rcClient);
if (hdc)
{
int width = rcClient.right - rcClient.left;
int height = rcClient.bottom - rcClient.top;
HDC hDCMem = CreateCompatibleDC(_hdc);
HBITMAP hBitmapMem = CreateCompatibleBitmap(hDCMem, width, height);
SelectObject(hDCMem, hBitmapMem);
Rectangle(hDCMem, 0, 0, width, height);
BLENDFUNCTION bfn;
bfn.BlendOp = AC_SRC_OVER;
bfn.BlendFlags = 0;
bfn.AlphaFormat = 0;
bfn.SourceConstantAlpha = 0x50;
AlphaBlend(hdc, 0, 0, width, height, hDCMem, 0, 0, width, height, bfn);
SetTextColor(hdc, RGB(255, 0, 0));
SetBkMode(hdc, TRANSPARENT);
DrawText(hdc, "Your text here", -1, &rcClient, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
//BitBlt(hdc, 0, 0, width, height, hDCMem, 0, 0, SRCCOPY);
DeleteDC(hDCMem);
DeleteObject(hBitmapMem);
}
if (!_hdc)
EndPaint(hWnd, &paint);
}
Also i found i have another problem with this.
i move my window in WM_TIMER, i call my __onpaint, problem im having is that it does not redraw it has something todo with the alphaBlend, it keeps what ever was under the window at the time of 1st draw, since it worked before i was using that
double buffering is to do all your printing and drawing on a temporary bitmap, that should be stored somewhere. the drawings on that bitmap can happen outside of WM_PAINT event (eg: on Adding items or selection change).
then on WM_PAINT event, the only thing you have to do is to project that bitmap to the window via BitBlt function or similar functions.
the way you are using AlphaBlend is wrong. AlphaBlend is used to draw images that have an AlphaChanel over an existing image as an overlay.

C++ Win32 Draw to a DC and Keeping It

I am trying to draw a simple few rectangles and store the result, I only need to draw it once. So, keeping the HDC (hdcBackround) at the top "globaly."
void drawBackground(HWND hwnd) { // hwnd is the main windows handle
// dimensions
RECT rect;
GetWindowRect(hwnd, &rect);
HDC hWinDC = GetDC(hwnd);
hdcBackground = ::CreateCompatibleDC(hWinDC); // "global"
HBITMAP hbm = ::CreateCompatibleBitmap(hWinDC, rect.right, rect.bottom);
::SelectObject(hdcBackground, hbm);
SetBkMode(hdcBackground, TRANSPARENT);
SelectObject(hdcBackground, hFont[HF_DEFAULT]);
SelectObject(hdcBackground, hBrush[HB_TOPBG]);
SelectObject(hdcBackground, hPen[HP_THINBORDER]);
// draw
Rectangle(hdcBackground, 0, 0, rect.right, 20);
SelectObject(hdcBackground, hBrush[HB_LOWBG]);
Rectangle(hdcBackground, 50, 20, rect.right, 40);
// ??? clean up after it works
ReleaseDC(hwnd, hWinDC);
}
I call that function once, and in a timer I BitBlt() hdcBackground to the screens HDC. When I test it out, it draws both the rectangles, with a 1px border (as the pen is set as,) but there is no color, it is just black and white.
The brushes and such are all fine, it is just that I am not getting color. The RGB on the brushes are (25,25,25) and (65,65,65), dark grey.
Any ideas?
Thanks.

How to draw text with transparent background using c++/WinAPI?

How to draw text with transparent color using WinAPI?
In usual way I used SetBkMode(hDC, TRANSPARENT), but now I need to use double buffer.
In this way images draws correct, but text draws not correct (with black background).
case WM_PAINT:
{
hDC = BeginPaint(hWnd, &paintStruct);
SetBkMode(hDC, TRANSPARENT);
HDC cDC = CreateCompatibleDC(hDC);
HBITMAP hBmp = CreateCompatibleBitmap(hDC, width, height);
HANDLE hOld = SelectObject(cDC, hBmp);
HFONT hFont = (HFONT)SelectObject(hDC, font);
SetTextColor(cDC, color);
SetBkMode(cDC, TRANSPARENT);
TextOut(cDC, 0, 0, text, wcslen(text));
SelectObject(cDC, hFont);
BitBlt(hDC, 0, 0, width, height, cDC, 0, 0, SRCCOPY);
SelectObject(cDC, hOld);
DeleteObject(hBmp);
DeleteDC(cDC);
EndPaint(hWnd, &paintStruct);
return 0;
}
SetBkMode(dc, TRANSPARENT) should work fine still. Make sure you're using the correct DC handle when drawing to your back buffer.
When you create a bitmap, the color isn't specified. The documentation doesn't state how it's initialized, but solid black (all zeros) seems likely. Since you're drawing the text on the bitmap, the background of the bitmap remains black. You then copy the entire bitmap to the DC and all the pixels come along, the background along with the text.
To fix this you must copy the desired background into the bitmap before you draw the text.