Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows Graphics problem, HRGN region repainting problem

Status
Not open for further replies.

vvozar

Programmer
Mar 4, 2011
2
RS
Hello,

The issue is related to Windows GDI, actually about repainting "framed" regions (HRGN framed with "FrameRgn" function)
I'm making application using BCB including VCL library. I put TImage component/object on an empty form and loaded proper bitmap image to Picture property of that image.
Then i wrote form's method that paints a frame around 'img':
Code:
void __fastcall 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);
 ReleaseDC(hWnd,hdc);
 DeleteObject(rgn);
}
Wanting always to have frame around my image, i am calling this method in OnPaint event.
Code:
void __fastcall TfrmMainForm::FormPaint(TObject *Sender)
{
    drawFrame();
}
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 nature of an OnPaint event, but don't know the way to avoid this situation.
I mean, how to disable that repainting regions frame on top of foreground window - window that's not my applications 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;
        form->Canvas->Rectangle(
          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, FormStyle must be set either to fsNormal or fsStayOnTop.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top