Hello,
The issue is related to Windows GDI, actually about repainting "framed" regions (HRGN framed with "FrameRgn" function)
Looking for help or an advice on how to handle regions repainting correctly.
I have created following class:
#ifndef OwnerDrawnButtonH
#define OwnerDrawnButtonH
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <jpeg.hpp>
#include <Graphics.hpp>
#include <windows.h>
class
TOwnerDrawnButton {
HRGN rgn;
HWND hWnd;
TImage *img;
TForm *form;
COLORREF rgb;
public:
TOwnerDrawnButton (TForm *, TImage *, HWND);
TOwnerDrawnButton () {};
void __fastcall drawPressed();
void __fastcall drawFocused();
void __fastcall clearFrame();
};
#endif
Implementation of some methods:
TOwnerDrawnButton::TOwnerDrawnButton (TForm *frm, TImage *image, HWND handle)
{
rgb = 0x00ffeeff;
hWnd = handle;
form = frm;
img = image;
}
void __fastcall TOwnerDrawnButton::clearFrame()
{
rgb = 0x00ff0000;
rgn = CreateRoundRectRgn(form->Left + img->Left + 1,
form->Top + 26 + img->Top + 1,
form->Left + img->Left + img->Width + 12,
form->Top + 26 + img->Top + img->Height + 12,
4 , 4);
HDC hdc = GetDC(hWnd);
HBRUSH brush = CreateSolidBrush(rgb);
FrameRgn(hdc, rgn, brush, 1, 1);
ReleaseDC(hWnd,hdc);
DeleteObject(rgn);
rgb = 0x00ffeeff;
rgn = CreateRoundRectRgn(form->Left + img->Left + 2,
form->Top + 26 + img->Top + 2,
form->Left + img->Left + img->Width + 11,
form->Top + 26 + img->Top + img->Height + 11,
4 , 4);
hdc = GetDC(hWnd);
brush = CreateSolidBrush(rgb);
FrameRgn(hdc, rgn, brush, 4, 4);
ReleaseDC(hWnd,hdc);
DeleteObject(rgn);
}
I put two bitmap images on the form (by dragging Image icon from toolbar - TImage *img1, TImage *img2). Then i load bitmap images on to their Picture properties (TBitmap * - type). In the private section of the form i have declared two attributes type TOwnerDrawnButton*:
TOwnerDrawnButton *odButton1;
TOwnerDrawnButton *odButton2;
Now, in the forms constructor I have initialized these two TOwnerDrawnButton objects next way :
odButton1 = new TOwnerDrawnButton(this, img1, hwnd);
odButton2 = new TOwnerDrawnButton(this, img2, hwnd);<br />
To always have painted frames around images on the form, i put following code in an OnPaint event of the form:
odButton1->clearFrame();
odButton2->clearFrame();
When i run application, problem is that when my forms window is not top most (i drag some other opened window above it), those region frames are being redrawed on top of the foreground window. I understand that it is normal because of the code in an OnPaint event, but don't know the way to solve this situation.
Some more information:
i am coding in C++ using Borland C++ Builder, including VCL library (creating forms with 'drag and drop' technique). Actually, I created form (for now it's just that first form initially created when you start new project) and put two image components on it (VCL library, Additional Tab, dragged and dropped Image component on form).
This way i added two IDE Managed Components of 'TImage *' type (named img1 and img2):
TImage* img1;
TImage* img2;
Each 'TImage*' object/component has its own Picture property, which is of TBitmap type. So, in Object Inspector i have assigned proper bitmap images to Picture properties.
Appreciate any help or advice.