0

Is it possible to paint a TProgressBar on a TSpeedButton, behind text and icon ?

I have no idea how to get started on this (assuming it's possible). How would I go about it ?

In this particular case I use the button to start and stop a process, and it would be nice to display the process in that button as well.

Peter
  • 1,334
  • 12
  • 28
  • 1
    If that button became disabled then by default so would the TProgressBar, also, you would have to put it under or either side of the text or glyph. Would that button ever be resized? I am not trying to tell you not to do this, however, there might be better options such as sub classing a TPanel to include both a TSpeedButton and TProgressBar into a reusable component. – Ross Bush Feb 11 '16 at 00:59
  • @lrb - Thanks for your comment - Food for thought ! – Peter Feb 11 '16 at 01:29
  • See: [Is there a `ProgressButton`?](http://stackoverflow.com/q/8491559/757830) – NGLN Feb 11 '16 at 05:45

1 Answers1

6

No, this is not possible with the standard TSpeedButton without creating your own descendant.

TSpeedButton does all of its drawing in response to the WM_PAINT message, and there is no way for you to inject another control behind the content that is drawn, because the drawing would erase the area where your control is drawing itself. You can see this yourself; you have the source code for TSpeedButton in almost every Delphi and C++ Builder version.

In addition, a TSpeedButton is a graphical control, not a windowed control (it derives from TGraphicControl instead of TWinControl), so it does not have a window handle to be used as the parent for other controls.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 1
    Well, you *could* always subclass the button's `WindowProc` property and handle the `WM_PAINT` message directly, copying the implementation code from `TSpeedButton.Paint()` into your subclass procedure and then inserting the extra code needed to manually draw a progress bar in between the drawing of the background and text/glyph. – Remy Lebeau Feb 11 '16 at 04:52
  • 1
    @RemyLebeau: Or you could much more easily create your own control that does this without having to resort to kludging around with TSpeedButton. – Ken White Feb 11 '16 at 13:48
  • @KenWhite Exactly, this is what writing your own custom controls is for and every developer should learn to write custom controls, so rather than copy parts here and there you build the control yourself and implement your requirements yourself. – Craig Feb 11 '16 at 14:03
  • @KenWhite: while I agree in general, for small one-offs it is overkill to implement and install a full component package, unless it is instantiated in code at runtime and you don't mind not being able to drop it on a Form at design-time. – Remy Lebeau Feb 11 '16 at 23:14
  • @Remy: For small one-offs, it's overkill to subclass the WindowProc and handle WM_PAINT instead of just dropping the idea and putting a progress bar on the form that you can show, update, and then hide when you're done. :-) – Ken White Feb 11 '16 at 23:16