I'm attempting to draw to an off-screen device context / bitmap and move the image to the main hdc using bitblt. Here's the result I'm currently seeing:
The blue, yellow, and green bars on the left are being drawn directly to the window's hdc. The strange-looking ones on the right were drawn to the back buffer and copied over as a single frame. They should be identical, but clearly that's not the case.
Here's the code I'm using, reduced to a minimal example:
COLORREF color_yellow = RGB (224, 224, 0);
COLORREF color_green = RGB (0, 192, 0);
COLORREF color_blue = RGB (0, 0, 192);
HBRUSH brush_yellow = CreateSolidBrush (color_yellow);
HBRUSH brush_green = CreateSolidBrush (color_green);
HBRUSH brush_blue = CreateSolidBrush (color_blue);
HDC hdc = GetDC (Window);
HDC hdc_buffer = CreateCompatibleDC (hdc);
HBITMAP bitmap_buffer = CreateCompatibleBitmap (hdc_buffer, blit.screen_width, blit.screen_height);
SelectObject (hdc_buffer, bitmap_buffer);
draw_rectangle (hdc, 0, 0, 100, 30, brush_blue);
draw_rectangle (hdc, 0, 30, 100, 60, brush_yellow);
draw_rectangle (hdc, 0, 60, 100, 90, brush_green);
draw_rectangle (hdc_buffer, 0, 0, 100, 30, brush_blue);
draw_rectangle (hdc_buffer, 0, 30, 100, 60, brush_yellow);
draw_rectangle (hdc_buffer, 0, 60, 100, 90, brush_green);
BitBlt (hdc, 120, 0, 100, 90, hdc_buffer, 0, 0, SRCCOPY);
void draw_rectangle (HDC hdc, int left, int top, int right, int bottom, HBRUSH brush)
{
RECT rect;
SetRect (&rect, left, top, right, bottom);
FillRect (hdc, &rect, brush);
}
I'm creating a new hdc (compatible with the window's), creating a compatible bitmap, selecting it, drawing the rectangles, and bit blitting over with SRCCOPY. All of this looks right to me.
I'm sure there's some small thing I'm not doing, but I can't find it.
This is explained in documentation for CreateCompatibleBitmap:
Note: When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. To create a color bitmap, use the HDC that was used to create the memory device context
Therefore, change
CreateCompatibleBitmap(hdc_buffer, width, height);//monochrome
to
CreateCompatibleBitmap(hdc, width, height);//colored bitmap
Related
I need to display an image from a sprite using MFC. A search on google led me to this link which led me to this code :
//This code is in the OnPaint function
//img is a CImage, declared as a class member
img.Load(_T("icon-sprite.png"));
HDC imgDc = img.GetDC();
int height = 24;
int width = 24;
//Sprite Icon is a CStatic
CDC* spriteDc = spriteIcon.GetDC();
HDC spriteHdc = spriteDc->GetSafeHdc();
CClientDC pDC(this);
HDC hdcWindow = pDC->GetSafeHdc();
//img.StretchBlt(imgDc, 0, 0, 600, 203, SRCCOPY);
//img.BitBlt(imgDc, width, height, 600, 203, 0, 0, SRCAND);
//img.BitBlt(imgDc, width, height, 640, 480, 0, 0, SRCPAINT);
StretchBlt(imgDc, 0, 0, 600, 203,
imgDc, 0, 0, 200, 203, SRCCOPY);
BitBlt(imgDc, width, height, 600, 203,
imgDc, 0, 0, SRCAND);
BitBlt(imgDc, width, height, 640, 480,
imgDc, 0, 0, SRCPAINT);
spriteIcon.SetBitmap((HBITMAP)img);
With this code spriteIcon only display a rectangle of the size of the sprite image.
What did I do wrong ?
Most probably CImage is local and going out of scope, thus the image (and HBITMAP) is invalid. You have two options:
Declare CImage such that it would exist even after this function returns.
Use CImage::Detach which returns a HBITMAP and relinquishes its ownership with handle (i.e. won't delete on destructor).
Please ensure that given image exists and is loaded properly.
Also, you don't need to new CClientDC, you can have it on stack.
So, I'm very new to WinAPI and I've succeeded in loading a sprite which I can move with the arrow keys. My teacher told me to be very careful with 'memory leaks' and I'm not sure how to free memory the right way because C++ has done it for me when I was doing console programming. I ran the program and it froze my computer for a minute after moving the character for a while so I guess I've done something wrong. Can you tell me how to properly free memory (if I've done it wrong) and if there's anything bad with my bitmap method that needs optimising? Thanks! Here's the code.
void LoadAndBlitBitmap(LPCSTR szFileName, HWND hwnd, HDC winDC, int xPos, int yPos)
{
//DEFINITIONS
/*TransparentBlt(destDC, destXpos, destYpos, sizeX, sizeY, srcDC, 0, 0, sizeX, sizeY, RGB(a, b, c)) = TRANSPARENT BITMAP BLIT. Ex. RGB(255, 255, 255) if background is white*/
/*BitBlt(destDC, destXpos, destYpos, sizeX, sizeY, srcDC, 0, 0, SRCCOPY); = SOLID BITMAP BLIT WITH NO TRANSPARENCY*/
//END DEFINITIONS
//-----Get the size of the window---------
RECT rect;
int win_width = 0;
int win_height = 0;
if(GetWindowRect(hwnd, &rect))
{
win_width = rect.right - rect.left; //Subtracting the right coordinate with the left gives the width
win_height = rect.bottom - rect.top; //Subtracting the bottom coordinate with the top gives the height
}
//----------------------------------------
HDC hdcMem = CreateCompatibleDC(winDC); //Create the DC that will hold the off-screen printing. (Double Buffering "Anti-Flicker" Method)
HBITMAP hbmMem = CreateCompatibleBitmap(winDC, win_width, win_height); //Create the bitmap with the size of the window
HANDLE hOld = SelectObject(hdcMem, hbmMem); //Set the paintable bitmap to the off-screen DC
//Draw to the off-screen DC
HBITMAP bitmap = (HBITMAP)LoadImage(NULL, szFileName, IMAGE_BITMAP, 69, 69, LR_LOADFROMFILE); //Load the .bmp from a file
HDC blockDC = CreateCompatibleDC(NULL); //Create a DC to hold the .bmp
SelectObject(blockDC, bitmap); //Select the .bmp to the DC
TransparentBlt(hdcMem, xPos, yPos, 69, 69, blockDC, 0, 0, 69, 69, RGB(255, 255, 255)); //Blit the .bmp to the DC*/
//Transfer off-screen DC to the screen
BitBlt(winDC, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
// Uninitialize and deallocate resources
SelectObject(hdcMem, hOld);
DeleteDC(hdcMem);
SelectObject(blockDC, hOld);
DeleteDC(blockDC);
DeleteObject(bitmap);
}
Two things are wrong:
SelectObject(blockDC, hOld);
hOld did not come from blockDC, it came from hdcMem. You are not even saving the old bitmap from blockDC. Change to:
HBITMAP hOld2 = SelectObject(blockDC, bitmap);
// and
SelectObject(blockDC, hOld2);
Secondly, you are not deleting hbmMem anywhere. At the bottom, add:
DeleteObject(hbmMem);
Actually, a third thing is wrong too - you're not checking for failure in any of the API calls you make. You should check if things like CreateCompatibleDC return NULL, and abort and cleanup if so.
In my application I'm drawing a bitmap in all fammilar and usual way using StretchBlt:
hBitmap = (HBITMAP)LoadImage(hInst, L"NewBitmapImage.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hBitmap);
StretchBlt(hdc, 100, 185, 100, 50, hdcMem, 0, 0, 100, 50, SRCCOPY);
It all works fine until WS_EX_LAYOUTRTL style is used for the main application window which results in the image width getting cropped by 1px. So, the problem only occurs when source width == destination width and when RTL style is used.
Is it a bug in StretchBlt or am I just not using it correctly?
I would like to owner-draw a red border to a EDIT or Push button in C++ win32 api. NO MFC Please.
I have gotten this far. Drawing a black border but most if not all the hButtonDC,hButtonBitmap are undeclared.
PAINTSTRUCT ps;
HDC hdc;
HBRUSH hBrush;
BeginPaint(hwndButton2, &ps);
// Create memory DC to contain hButtonBitmap
hButtonDC = CreateCompatibleDC(ps.hdc);
hButtonBitmap = SelectObject(hButtonDC, hButtonBitmap);
// Create second memory DC where the button borders will be drawn and select into this DC an empty bitmap with the
// size of the button bitmap
hMemDC = CreateCompatibleDC(ps.hdc);
hBitmap = CreateCompatibleBitmap(ps.hdc, ps.rcPaint.right, ps.rcPaint.bottom);
hBitmap = SelectObject(hMemDC, hBitmap);
// Copy hButtonDC into hMemDC
BitBlt(hMemDC, 0, 0, ps.rcPaint.right, ps.rcPaint.bottom, hButtonDC, 0, 0, SRCCOPY);
// Paint the button borders with black pixels (1 pixel width)
PatBlt(hMemDC, 0, 0, ps.rcPaint.right - 1, 1, BLACKNESS);
PatBlt(hMemDC, ps.rcPaint.right - 1, 0, 1, ps.rcPaint.bottom, BLACKNESS);
PatBlt(hMemDC, 0, ps.rcPaint.bottom - 1, ps.rcPaint.right , 1, BLACKNESS);
PatBlt(hMemDC, 0, 0, 1, ps.rcPaint.bottom - 1, BLACKNESS);
// Paint the button with drawn borders to its window DC, ps.hdc .
BitBlt(ps.hdc, 0, 0, ps.rcPaint.right, ps.rcPaint.bottom, hMemDC, 0, 0, SRCCOPY);
// Delete hBitmap e hMemDC
DeleteObject(SelectObject(hMemDC, hBitmap));
DeleteDC(hMemDC);
// Delete hButtonDC
SelectObject(hButtonDC, hButtonBitmap);
DeleteDC(hButtonDC);
EndPaint(hWnd, &ps);
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.