OnPaint Region Handling

I'm making application using BCB including VCL library. I put TImage component/object on an empty form, assigned proper bitmap image to Picture property of that image: [code]TImage* img1;[/code]

Then i wrote form's method that paints a frame around 'img'.
[code]void __fastcall TfrmMainForm::drawFrame()
rgb = 0x00ffeeff; // light gray RGB color code

// creating region on image area, 26 is width of caption in pixels
// frame will be 4 pixels width around image

rgn = CreateRoundRectRgn(form->Left + img1->Left + 2,
form->Top + 26 + img1->Top + 2,
form->Left + img1->Left + img1->Width + 11,
form->Top + 26 + img1->Top + img1->Height + 11,
4 , 4);

hdc = GetDC(hWnd);
brush = CreateSolidBrush(rgb);
FrameRgn(hdc, rgn, brush, 4, 4);
Wanting always to have frame around my image, i am calling this method in OnPaint event. When OnPaint event occurs everything is repainted fine, except in one situation.
When my forms window is not top most (i drag some other opened window above it - like windows explorer), those region frame around image is 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.
I mean, how to disable that repainting region frame on top of foreground windows other than my application window?

Appreciate any help or advice.


  • Instead of using regions, device contexts and brushes, i will draw directly on the form.
    I've changed code in drawFrame() method to:
    [code]void __fastcall TfrmMainForm::drawFrame()
    form->Canvas->Pen->Style = psSolid;
    form->Canvas->Pen->Width = 4;
    form->Canvas->Pen->Color = clLtGray;
    form->Canvas->Brush->Style = bsClear;
    img->Left - 2,
    img->Top - 2,
    img->Left + img->Width + 2,
    img->Top + img->Height + 2
    This way it draws directly on forms canvas, and requires less memory management (allocating and freeing) comparing to using regions and brushes.
    To be able to draw directly on a form, [b]FormStyle[/b] must be set either to [b]fsNormal[/b] or [b]fsStayOnTop[/b].
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion