I have created an animation which works fine, but it flicks. I need help with double-buffering since I don't know anything about it.
This is the code in my onPaint():
VOID onPaint(HDC hdc)
{
Graphics graphics(hdc);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawEllipse(&pen, sf , 0, 10, 10);
}
It works fine but with flicker. I tried this code but it didn't work:
VOID onPaint(HDC hdc,HWND hWnd)
{
HDC hDC=GetDC(hWnd);;
HDC memDC = CreateCompatibleDC(hDC);
HBITMAP hMemBmp = CreateCompatibleBitmap(hDC,10,10);
HBITMAP hOldBmp = (HBITMAP)SelectObject(memDC,hMemBmp);
BitBlt(hDC, 0, 0, 10, 10, memDC, 0, 0, SRCCOPY);
Graphics graphics(memDC);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawEllipse(&pen, sf , 0, 10, 10);
// Always select the old bitmap back into the device context
SelectObject(memDC, hOldBmp);
DeleteObject(hMemBmp);
DeleteDC(memDC);
}
It looks like you're just prematurely copying the offscreen DC to the display. Try moving the call to BitBlt down four lines, to make it the last line before you start the clean-up, like so:
VOID onPaint(HDC hdc,HWND hWnd)
{
// this line looks a little odd :
HDC hDC = GetDC(hWnd);
// .. usually the hdc parameter passed to onPaint would already refer to
// the on-screen DC that windows wants updated. Also worth noting is that
// when you use GetDC(), you should have a matching ReleaseDC()
// As a quick test, you might just replace the above line with
// HDC hDC = hdc;
HDC memDC = CreateCompatibleDC(hDC);
HBITMAP hMemBmp = CreateCompatibleBitmap(hDC,10,10);
HBITMAP hOldBmp = (HBITMAP)SelectObject(memDC,hMemBmp);
// draw to the off-screen map ..
Graphics graphics(memDC);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawEllipse(&pen, sf , 0, 10, 10);
// now that you've drawn on the offscreen map, go ahead
// and put it on screen.
BitBlt(hDC, 0, 0, 10, 10, memDC, 0, 0, SRCCOPY);
// Always select the old bitmap back into the device context
SelectObject(memDC, hOldBmp);
DeleteObject(hMemBmp);
DeleteDC(memDC);
}
Another thing about this code, you've passed the constant '10' as the width and height of your off-screen bitmap, as well as using it for the width and height params to the BitBlt() that does the copy. Chances are the window client area being updated is very much larger than that. The 'black square' is a consequence of blitting the 10x10 off-screen map onto the window client area. Instead of hard-coding 10 there, you might try using another GDI function to obtain the dimensions of the on-screen bitmap, or at the very least you could #define width and height values, and use these in the params.
The other thing killing you is probably the 'sf' in the line "graphics.DrawEllipse(&pen, sf , 0, 10, 10)" -- since you've created an incredibly tiny 10x10 map, if the value of 'sf' is anything outside of 0..10, the DrawEllipse() call will place the ellipse entirely outside of the available pixels in your offscreen map.
So, bottom line, you probably want to make the offscreen map the same size as the window client area, and be sure to move the BitBlt() call down so that it happens after all the drawing ops on the off-screen map.
Related
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.
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.
1. Problem
I have two buffers. Primary buffer, which is displayed on the screen and a secondary buffer where everything is drawn and then passed to the primary.
The Graphics object is created from the secondary buffer, which is associated with a bitmap of size 800x600. Naturally when you resize the window, the size of the bitmap has to change in order to prevent clipping issues.
The secondary HDC gets updated, and the bitmap is copied to the primary.
The issue is that there is something left in the Graphics object associated with secondary HDC that generates clipping. The drawing region still stays 800x600 despite already being updated to something larger (1000x1000).
My question is what should I update inside the Graphics object (Without having to explicitly recreate it from the existing HDC) in order to make it's drawing region fit the bitmap size.
2. What I tried
The first thing I tried was recreating the Graphics object from the updated HDC. This method works and the drawing region fits the size of the bitmap. It does not meet the design standard however. Graphics should be reusable.
I also tried updating the clipping region of the graphics object using the SetClip method. Although that did not seem to be the problem.
This is how I create the buffers.
HDC CoreWindowFrame::InitPaint(HWND hWnd)
{
windowHdc = GetDC(hWnd);
HDC secondaryBuffer = CreateCompatibleDC(windowHdc);
HBITMAP map = CreateCompatibleBitmap(windowHdc, width, height);
SelectObject(secondaryBuffer, map);
return secondaryBuffer;
}
This function is called on resize
void CoreWindowFrame::UpdateBitmap(int width, int height)
{
HBITMAP map = CreateCompatibleBitmap(windowHdc, width, height);
SelectObject(secondaryBuffer, map);
}
And this is message processing:
case WM_SIZE:
ConsoleWrite("WM_SIZE RECIEVED");
width = *((unsigned short*)&lParam);
height = ((unsigned short*)&lParam)[1];
UpdateBitmap(width, height);
break;
case WM_PAINT:
ConsoleWrite("WM_PAINT RECIEVE");
windowGraphics->Clear(Color(255, 255, 255));
Pen* testPen = new Pen(Color(255, 0, 0), 1.0F);
windowGraphics->DrawLine(testPen, 0, 0, calls*3, 100);
BitBlt(windowHdc, 0, 0, updatedWidth, updatedHeight, secondaryBuffer, 0, 0, MERGECOPY);
3. Expected Result
The graphics object should be reusable and it should not be tossed away and replaced with a new one each time something is refreshed. Therefore it has to be updated accordingly in case anything is resized or changed. I expect the region to fit the size of the currently updated bitmap in the secondary HDC. The bitmap, as seen in the code, is updated properly. It is the Graphics object that does not. It acts like it still remembers some of the value from the previous BitMap which results in this behavior.
windowHdc = GetDC(hWnd);
HDC secondaryBuffer = CreateCompatibleDC(windowHdc);
HBITMAP map = CreateCompatibleBitmap(windowHdc, width, height);
SelectObject(secondaryBuffer, map);
return secondaryBuffer;
HDC obtained from GetDC or BeginPaint cannot be reused, as noted in comments.
You can however reuse memory bitmap (from CreateCompatibleBitmap) and reuse memory dc (obtained from CreateCompatibleDC), although there is usually no point in reusing memory dc.
Moreover, cleanup is required to avoid resource leak. Call ReleaseDC when you are finished with GetDC. See documentation for relevant release/delete functions.
Do this for a simple paint routine:
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
Gdiplus::Graphics gr(hdc);
Gdiplus::Pen testPen(Gdiplus::Color(255, 0, 0), 1.0F);
gr.Clear(Gdiplus::Color::White);
gr.DrawLine(&testPen, 0, 0, 100, 100);
EndPaint(hwnd, &ps);
return 0;
}
Usually you don't need to do anything more. Just call InvalidateRect in response to WM_SIZE to update paint.
For double-buffering, or drawing on a canvas, you can create a memory bitmap and reuse it. If window size changes, then you have to call DeleteObject for the old bitmap, and create a new bitmap based on the new size. Or you can create a bitmap which matches the largest window size, then use this bitmap for all window sizes.
See the example below for double-buffer paint (however reusing hbitmap is not necessary in this case)
HBITMAP hbitmap = NULL;
void InitPaint(HWND hwnd)
{
HDC hdc = GetDC(hwnd);
//create a bitmap with max size:
int w = GetSystemMetrics(SM_CXFULLSCREEN);
int h = GetSystemMetrics(SM_CYFULLSCREEN);
hbitmap = CreateCompatibleBitmap(hdc, w, h);
ReleaseDC(hwnd, hdc);
}
void cleanup()
{
if (hbitmap)
DeleteObject(hbitmap);
}
...
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT rc; GetClientRect(hwnd, &rc);
int w = rc.right;
int h = rc.bottom;
HDC memdc = CreateCompatibleDC(hdc);
HBITMAP oldbmp = (HBITMAP)SelectObject(memdc, hbitmap);
Gdiplus::Graphics gr(memdc);
Gdiplus::Pen testPen(Gdiplus::Color(255, 0, 0), 1.0F);
gr.Clear(Gdiplus::Color(255, 255, 255));
gr.DrawLine(&testPen, 0, 0, w, h);
BitBlt(hdc, 0, 0, w, h, memdc, 0, 0, SRCCOPY);
//cleanup:
SelectObject(memdc, oldbmp);
DeleteDC(memdc);
EndPaint(hwnd, &ps);
return 0;
}
Summary:
GetDC doesn't create anything. It only returns a reference to an existing handle which is used by the system. When you are finished with this handle, release it. There is no way to optimize this any further.
Other GDI objects, like pen or bitmap, can be created for your program and they can be reused. It only takes a few nano-seconds to create/destroy a pen, so it's usually not worth the added complexity to store these objects and keeping track.
The main bottle-neck is when you draw on the screen, for example drawing a line. You have to talk to the graphics card and talk to the monitor which takes a while. You can use double-buffering to draw on a bitmap, then BitBlt on the screen. BitBlt can be slow too, depending on the system.
For animation, use double-buffering if you see flicker.
For better performance you can use newer technologies like Direct2D
If animation is still too slow, consider using a second thread to run any math type calculations (the second thread should not reference any window handle, such as HDC from GetDC or BeginPaint)
Now I am working on a legacy product which use GDI to draw text in screen. Now I try to use DirectWrite to draw text for better appearance and accurancy of font. I am very curious that has anyone done this before?
I meet a problem that when I use DirectWrite to draw text on a GDI hdc, the background color is always white, I need a transparent background, is it possible? it seems that the SetBkMode is useless
The sample code is as below,
SetBkMode(hdc, TRANSPARENT); //hDC is the target GDI dc
SIZE size = {};
HDC memoryHdc = NULL;
memoryHdc = g_pBitmapRenderTarget->GetMemoryDC();
SetBkMode(memoryHdc, TRANSPARENT);
hr = g_pBitmapRenderTarget->GetSize(&size);
Rectangle(memoryHdc, 0, 0, size1.cx , size1.cy );
if (SUCCEEDED(hr)) {
hr = g_pTextLayout->Draw(NULL, g_pGdiTextRenderer, 0, 0);
}
BitBlt(hdc, x, y, width + 1, height + 1, memoryHdc, 0, 0, SRCCOPY | NOMIRRORBITMAP);
Default (Stock) brush for freshly created GDI device context is white solid brush, which is why you have white rectangle in output. See GetStockObject
GDI doesn't work with transparent images, BitBlt will replace all pixels inside destination rectangle in target DC. You have to copy contents of target DC destination rectangle in memory DC, then draw text and copy result back to achieve desired effect.
SetBkMode(hdc, TRANSPARENT); //hDC is the target GDI dc
SIZE size = {};
HDC memoryHdc = g_pBitmapRenderTarget->GetMemoryDC();
BitBlt(memoryHdc, 0, 0, width+1, height+1, hdc, x, y, SRCCOPY);
if (SUCCEEDED(hr)) {
hr = g_pTextLayout->Draw(NULL, g_pGdiTextRenderer, 0, 0);
}
BitBlt(hdc, x, y, width + 1, height + 1, memoryHdc, 0, 0, SRCCOPY | NOMIRRORBITMAP);
Make sure you use smallest possible update region, since moving large chunks of bitmaps in memory will surely dwindle performance.
If application uses back buffer to draw windows, memory DC of IDWriteBitmapRenderTarget could be used instead of allocating another one - in that case you solve problem of transparent text background automatically.
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.