Recent

Author Topic: Two paintbox, how to set one of them transparent?  (Read 720 times)

krolikbest

  • Full Member
  • ***
  • Posts: 245
Two paintbox, how to set one of them transparent?
« on: December 02, 2023, 09:36:14 pm »
Hi,
years ago in Delphi I've done a project based on few TImage to paint a sketch (vector graphic) for my milling machine. I used few Image components, e.g. one for main drawing, second for guiding lines (ruler) and so on. In this project I didn't use some TBitmap as a buffer hence drawing something directly on the Image flickered terribly :)
Now I try to port this project into Lazarus but using only standard component  TPaintBox rather than TImage however I'm stuck on transparency. In the attached example I can draw on the canvas of paintbox1, but I don't see canvas of paintbox2 which is located under paintbox1 but should be all time visible. So question is where (or how) to make paintbox1 transparent? Any suggestion welcome.

VisualLab

  • Sr. Member
  • ****
  • Posts: 285
Re: Two paintbox, how to set one of them transparent?
« Reply #1 on: December 02, 2023, 10:16:26 pm »
The TPaintBox component does not have a transparency property. To draw on PaintBox, you use its Canvas property (TCanvas class). If you do not paint the entire canvas of the PaintBox object with some background color, those components (or their fragments) lying under the PaintBox that are not obscured by the displayed drawing should be visible. When using TPaintBox, you must also take into account the set copy mode (CopyMode). Additionally, TPaintBox does not support graduated transparency. For this you need to use something else, e.g. TBGRABitmap.

May I know why you are using 2 PaintBoxes?

jamie

  • Hero Member
  • *****
  • Posts: 6041
Re: Two paintbox, how to set one of them transparent?
« Reply #2 on: December 02, 2023, 10:21:21 pm »
You could try setting the PaintBox.Color := clNone;

That should stop one from wiping out the other at least. :o
The only true wisdom is knowing you know nothing

krolikbest

  • Full Member
  • ***
  • Posts: 245
Re: Two paintbox, how to set one of them transparent?
« Reply #3 on: December 02, 2023, 10:30:40 pm »

May I know why you are using 2 PaintBoxes?

To avoid using TImage, but on the other hand Image was not so bad, all that was needed was to implement buffering. I'd like to use only standard components. If not PaintBox so what else (apart from Image)?

VisualLab

  • Sr. Member
  • ****
  • Posts: 285
Re: Two paintbox, how to set one of them transparent?
« Reply #4 on: December 02, 2023, 10:54:37 pm »
Hmm. I would create my own class derived from TCustomControl, let's say: TMyDrawEdit. In this class, I would draw on a bitmap (in memory), the content of which would then be applied to the canvas of the TMyDrawEdit component. The thing is, I don't know what you're displaying (or whether you're just displaying it). I needed to display and edit elements of simple vector graphics, the content of which changed in response to events generated by TMyDrawEdit and flags set in the program by menus, buttons, etc. The actual data was stored in a container (say: TMyContainer). The TMyDrawEdit component had a pointer to the container (TMyContainer) from which it retrieved data and, based on it, drew content on a bitmap and applied it to its canvas (meaning: TMyDrawEdit). Mouse and keyboard events generated by TMyDrawEdit were captured by the controller component (say: TMyController). The controller performed operations on data (depending on events and flags currently set in the program) and placed the results in a container (TMyContainer). The container stored data and wrote it to and read from the file. The file was XML-based. It's just a lot of work to do.

krolikbest

  • Full Member
  • ***
  • Posts: 245
Re: Two paintbox, how to set one of them transparent?
« Reply #5 on: December 03, 2023, 06:38:39 pm »
Ok, done. I got rid of some bitmap buffer. I paint directly on PaintBox in OnPaint event. In OnMouseMove event there is PaintBox.Refresh only. Second PaintBox as a background. No flickering. So far so good..
« Last Edit: December 03, 2023, 06:41:52 pm by krolikbest »

 

TinyPortal © 2005-2018