Recent

Author Topic: [SOLVED] Paint image within rounding  (Read 1549 times)

SaraT

  • Full Member
  • ***
  • Posts: 131
  • A little student
[SOLVED] Paint image within rounding
« on: July 25, 2023, 01:32:04 pm »
Hi, all

I am painting an image on BCButton usin AfterRender event. It works ok, but the image is painted
over the rounding, so the image looks square, not rounded.

Take a look at the attached image.
Any help will be appreciated.
« Last Edit: July 26, 2023, 11:08:17 pm by SaraT »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Paint image within rounding
« Reply #1 on: July 25, 2023, 02:41:34 pm »
If your Target is Windows, you can try my attached project that not use your component, its simply a panel with a image attached and got round corners, you can add a click event and than it act like your suggested control.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

SaraT

  • Full Member
  • ***
  • Posts: 131
  • A little student
Re: Paint image within rounding
« Reply #2 on: July 25, 2023, 07:00:47 pm »
If your Target is Windows, you can try my attached project that not use your component...

Thanks a lot, but it does not work with TBCButton control (as you said).
And I dont use the Panel control because I need the TBCButton proeprties.
Maybe someone else can help me.

Regards.

Josh

  • Hero Member
  • *****
  • Posts: 1344
Re: Paint image within rounding
« Reply #3 on: July 25, 2023, 08:54:38 pm »
Hi

just had a play.

Might not be the best solution. Not sure how cross-platform it is either

Code: Pascal  [Select][+][-]
  1. procedure TForm1.BCButton1AfterRenderBCButton(Sender: TObject;
  2.   const ABGRA: TBGRABitmap; AState: TBCButtonState; ARect: TRect);
  3. Var MyPic:TBGRABitmap;
  4.     rgn : HRGN;
  5. begin
  6.   rgn := CreateRoundRectRgn(0, 0, tbcbutton(sender).Width, tbcbutton(sender).Height,
  7.                             tbcbutton(sender).Rounding.RoundX,tbcbutton(sender).Rounding.RoundY);
  8.   MyPic:=TBGRABitmap.Create('apic2.png');  // i placed here for ease of use, this should be a fixed variable, that is not loaded everytime
  9.   SelectClipRgn(abgra.Canvas.Handle , rgn );
  10.   MyPic.Bitmap.Transparent:=true;
  11.   abgra.Canvas.StretchDraw(arect,MyPic.Bitmap);
  12.   MyPic.free;
  13. end;

you will need to add to your uses
LclIntF,LclType



The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Josh

  • Hero Member
  • *****
  • Posts: 1344
Re: Paint image within rounding
« Reply #4 on: July 26, 2023, 01:15:09 am »
further playing or hack %)

using the glyph of the tbcbutton to hold the image, you must set the glyphscale to 0; so that the glyph isnot drawn

Code: Pascal  [Select][+][-]
  1. procedure TForm1.BCButton1AfterRenderBCButton(Sender: TObject;
  2.   const ABGRA: TBGRABitmap; AState: TBCButtonState; ARect: TRect);
  3. Var MyPic:TBGRABitmap;
  4.     rgn : HRGN;
  5.     tw,th,x,y:integer;
  6. Begin
  7.   If Sender is TBCButton then    // sanity check
  8.   Begin
  9.     // check if glyph is empty
  10.     If TBitmap(TBCButton(Sender).Glyph).Empty=False then
  11.     Begin
  12.     // width and height +1 to correct issue
  13.       rgn := CreateRoundRectRgn(0, 0, TBCButton(Sender).Width+1, TBCButton(Sender).Height+1,
  14.                               TBCButton(Sender).Rounding.RoundX,TBCButton(Sender).Rounding.RoundY);
  15.       MyPic:=TBGRABitmap.Create(TBCButton(Sender).Glyph);
  16.       // you must set the button glyphscale to 0
  17.       SelectClipRgn(ABGRA.Canvas.Handle , rgn );
  18.       MyPic.Bitmap.Transparent:=True;
  19.       MyPic.AntialiasingDrawMode:=dmDrawWithTransparency;
  20.       ABGRA.Canvas.StretchDraw(ARect,MyPic.Bitmap);
  21.       // if caption exists display it... work needed
  22.       If TBCButton(Sender).Caption<>'' then
  23.       Begin
  24.         ABGRA.Canvas.Brush.Style:=bsClear;
  25.         ABGRA.Canvas.Font.Name:=AState.FontEx.Name;
  26.         ABGRA.Canvas.Font.Style:=AState.FontEx.Style;
  27.         ABGRA.Canvas.Font.Height:=AState.FontEx.Height;
  28.         ABGRA.Canvas.Font.Color:=AState.FontEx.Color;
  29.         ABGRA.Canvas.GetTextSize(TBCButton(Sender).Caption,tw,th);
  30.         x:=(ABGRA.Width-tw) div 2;
  31.         y:=(ABGRA.height-th) div 2;
  32.         ABGRA.Canvas.TextOut(x,y,TBCButton(Sender).Caption);
  33.       End;
  34.       MyPic.Free;
  35.     End;
  36.   End;
  37. End;
« Last Edit: July 26, 2023, 01:31:50 am by Josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

circular

  • Hero Member
  • *****
  • Posts: 4356
    • Personal webpage
Re: [SOLVED] Paint image within rounding
« Reply #5 on: July 27, 2023, 04:38:58 pm »
Actually with BGRABitmap you can call FillRoundRectAntialias and give an image as parameter for texture. If the image needs to be resized, you can first create a resampled version of the image and use this as the texture parameter of FillRoundRectAntialias.
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018