How to draw a border to a win32 control in C++ - c++

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);

Related

unable to FillRect with transparent brush when double buffering

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.

GDI objects leak

I encounter a problem with GDI objects increasing. How can I solve this problem?
The DeleteObject() function doesn`t help.
// Other Events
GetClientRect(hAnimationStars, &Dimensions);
AnimationStarsDC = BeginPaint(hAnimationStars, &ps);
MemoryDC = CreateCompatibleDC(AnimationStarsDC);
HBITMAP Bitmap = CreateCompatibleBitmap(AnimationStarsDC, Dimensions.right, Dimensions.bottom);
SelectObject(MemoryDC, Bitmap);
SetBkMode(MemoryDC, TRANSPARENT);
FillRect(MemoryDC, &Dimensions, CreateSolidBrush(BackgroundColor));
// Draw Operations
BitBlt(AnimationStarsDC, 0, 0, Dimensions.right, Dimensions.bottom, MemoryDC, 0, 0, SRCCOPY);
while (!DeleteDC(MemoryDC));
while (!DeleteObject(Bitmap));
EndPaint(hAnimationStars, &ps);
// Other Events
You need to restore any object you replace with SelectObject() before destroying the HDC. You also need to destroy the HBRUSH you are creating.
GetClientRect(hAnimationStars, &Dimensions);
HDC AnimationStarsDC = BeginPaint(hAnimationStars, &ps);
HDC MemoryDC = CreateCompatibleDC(AnimationStarsDC);
HBITMAP Bitmap = CreateCompatibleBitmap(AnimationStarsDC, Dimensions.right, Dimensions.bottom);
HBITMAP oldBmp = (HBITMAP) SelectObject(MemoryDC, Bitmap); // <-- REMEMBER THE OLD BITMAP!
SetBkMode(MemoryDC, TRANSPARENT);
HBRUSH Brush = CreateSolidBrush(BackgroundColor); // <-- REMEMBER THE BRUSH YOU CREATE!
FillRect(MemoryDC, &Dimensions, Brush);
DeleteObject(CreateSolidBrush); // <-- DESTROY THE BRUSH!
// Draw Operations
BitBlt(AnimationStarsDC, 0, 0, Dimensions.right, Dimensions.bottom, MemoryDC, 0, 0, SRCCOPY);
SelectObject(MemoryDC, oldBmp); // <-- RESTORE THE OLD BITMAP!
DeleteObject(Bitmap);
DeleteDC(MemoryDC);
EndPaint(hAnimationStars, &ps);

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.

Converting ICON to BITMAP -- side-effect

How do I make it so that this code strictly does the conversion from a Windows ICON to a CBitmap?
The code is incorrectly displaying the new bitmap on screen. :(
This code was acquired from 'someone' on the web. And though it achieves it's goal of converting the ICON, it also displays the icon on screen (upper left hand corner) which it should not be doing.
D'oh!
void CUIHelper::ConvertIconToBitmap2(CBitmap& bmpObj, HICON hIcon)
{
CClientDC clientDC(NULL);
CDC memDC;
memDC.CreateCompatibleDC(&clientDC);
ASSERT(hIcon);
ICONINFO info;
VERIFY(GetIconInfo(hIcon, &info));
BITMAP bmp;
GetObject(info.hbmColor, sizeof(bmp), &bmp);
HBITMAP hBitmap = (HBITMAP)CopyImage(info.hbmColor, IMAGE_BITMAP, 0, 0, 0);
ASSERT(hBitmap);
ASSERT(memDC.GetSafeHdc());
HBITMAP hOldBmp = (HBITMAP)memDC.SelectObject(hBitmap);
clientDC.BitBlt(0, 0, bmp.bmWidth, bmp.bmHeight, &memDC, 0, 0, SRCCOPY);
memDC.SelectObject(hOldBmp);
VERIFY( bmpObj.Attach(hBitmap) );
DeleteObject(info.hbmColor);
DeleteObject(info.hbmMask);
}
I am dumb when it comes to GDI.
clientDC.BitBlt(0, 0, bmp.bmWidth, bmp.bmHeight, &memDC, 0, 0, SRCCOPY);
should be
memDC.BitBlt(0, 0, bmp.bmWidth, bmp.bmHeight, &memDC, 0, 0, SRCCOPY);

Win32 C++ BitBlt Transparency

UPDATED LOOK AT BOTTOM OF THIS POST
What I am doing is trying to use one Black and White bitmap, to lay a background bitmap on the white, and the tile overlay on the black. The problem I am having is adding the overlay.
and this is my BitBlt() code, this code produces #5.
hOldBitmap = (HBITMAP)SelectObject(hdcMem, bitmap.hbmBackground); // #2
BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCCOPY);
hOldBitmap = (HBITMAP)SelectObject(hdcMem, bitmap.hbmMap); // #1
BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCAND);
hOldBitmap = (HBITMAP)SelectObject(hdcMem, bitmap.hbmMapOverlay); // #4
BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCAND);
I am unsure about using the same "hOldBitmp", but it seems to do the same thing either way.
The transparent blt function would not fully suffice here, either.
Thanks.
NEW
I have been having trouble combining and rastoring. I can somewhat handle DC's and bitmaps, but this is one thing I cant figure out how to do... Creating memory dcs, and dcs to hold a bitmap, dc for another bitmap, then bitblt to the mem. I think...
Heres my redundant code I have at the moment. Really I am needing help with pseudo code, how to combine the bitmaps... how many DC's are necessary, etc..
buffer.getBufferDC() is the main DC that displays on the screen.
HDC hdc = GetDC(hWnd);
HDC hdcMem = CreateCompatibleDC(hdc);
HDC hdcMem2 = CreateCompatibleDC(hdc);
HDC hdcMem3 = CreateCompatibleDC(hdc);
HDC hdcMem4 = CreateCompatibleDC(hdc);
HBITMAP hbmMem3 = CreateCompatibleBitmap(hdc, WINDOW_WIDTH, WINDOW_HEIGHT);
HBITMAP hbmMem4 = CreateCompatibleBitmap(hdc, WINDOW_WIDTH, WINDOW_HEIGHT);
ReleaseDC(hWnd, hdc);
// Copy the map and clean the hdcMem
HBITMAP hbmOld;
hbmOld = (HBITMAP)SelectObject(hdcMem, bitmap.hbmMap);
BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmOld);
hbmOld = (HBITMAP)SelectObject(hdcMem2, bitmap.hbmBackground);
BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem2, 0, 0, SRCAND);
SelectObject(hdcMem2, hbmOld);
hbmOld = (HBITMAP)SelectObject(hdcMem3, bitmap.hbmMapOverlay);
hbmOld = (HBITMAP)SelectObject(hdcMem4, bitmap.hbmMap);
BitBlt(hdcMem3, 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem4, 0, 0, SRCINVERT);
BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem3, 0, 0, SRCPAINT);
//hbmOld = (HBITMAP)SelectObject(hdcMem, bitmap.hbmMap);
//BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCPAINT);
//SelectObject(hdcMem, hbmOld);
//hbmOld = (HBITMAP)SelectObject(hdcMem, bitmap.hbmMapOverlay);
//BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCAND);
//SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
DeleteDC(hdcMem2);
DeleteDC(hdcMem3);
DeleteDC(hdcMem4);
Combine hbmpBackground with hbmMap using SRCAND, as you've done in 3.
Combine hbmMapOverlay with an inverted hbmMap (SRCINVERT should do it).
Combine those two results using OR (SRCPAINT)
Although this can be done with BitBlt, it's usually quite a bit easier to use PlgBlt.
Start by BitBlting the background bitmap to the destination. Then call PlgBlt, passing it both the foreground bitmap and the mask.