Paint TProgressBar behind text and icon in TSpeedButton - delphi

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.

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.

Related

What is the ideal way to modify sub control styles in a custom firemonkey control?

I am attempting to develop my first proper custom control for the Firemonkey framework and have ran into what may possibly be an obvious (or not) solution.
Inside my Firemonkey control I have declared FPanel: TPanel; which is then created in the constructor and freed in the destructor. The panel is created along with my control when I add it to a new Multi-Device Form without any problems.
By default the TPanel has borders around the sides of the control which I do not need in my control.
So my question is, what is the ideal way to remove the borders of a TPanel which is child to my custom control? I could not see an obvious property to change, unless I am mistaking I believe we must modify the style of the panel which I assume would be done via a TStyleBook.
Am I right then in thinking that I need to add a TStyleBook to my control, and from there add the panel to the Style book and modify it this way? Unless I am missing something this seems like a lot of extra work for what should be a very quick and simple change.
Assuming this is the correct way, is there an example of modifying a TStyleBook through code?
Thanks.
Because all Firemonkey controls can be parents, one way is to not use TPanel at all and instead replace it with another Firemonkey control such as the TRectangle shape.
The TRectangle shape can then be customised directly through its properties to remove the border which can be achieved by setting the Corners and Sides to False.
Additionally if you don't require any borders whatsoever then the TLayout control behaves just like a TPanel but without the borders.

Add shortcut to a TForm or Panel.Transparent?

To thwart the nit-pickers, let me start with, I searched here with this and could not find an answer, and yes, also I did scroll through the "Similar questions."...
Adding shortcuts to a TForm
I want to drag and drop some shortcuts from the Desktop to a TForm in my application. I am using Anders Melander's brilliant Drag Drop Suite (DDS).
I tried putting a TImage on the form but the DDS does not drop to an Image so I added a TPanel with a TImage on it. I could then drop on the panel and assign the image to the TImage.Picture. Problem was the Panel has no Transparent Property so the shortcut on the form looks clunky with the visible Panel behind it.
I need to be able to drop to the TImage or make the underlying TPanel transparent.
Can anyone help with code-snippets for either of those options, or better yet, a method of dropping a Shortcut directly on to my Form.
Thanks
Coincidentally I needed to make a TWinControl (the base for every visible control with a window handle, including TPanel) transparent. I found numerous results and applied them to this answer.
It's been a while since I implemented drag and drop, but I assume you call some API and pass it the handle of the panel? That answers the question why you can't use TImage. TImage is a graphic control, a control without a handle, that relies on its parent for recieving messages and drawing itself.
It should be possible to use the form, though, since that has a handle too.
If the TImage is directly on the TForm, then let the TForm handle the drop, no TPanel needed. OLE Drag&Drop operations (which Ander's components implement) provide coordinates where dragging and dropping occurs. The TForm should be able to detect when a drag is over the area occupied by the TImage and what type of data is being dragged, and only allow dropping of supported types within that area, extracting the dropped data and updating the TImageas needed, and denying anything else that does not match that criteria.

Delphi 6 : Detect when a Form is fully painted?

Is there a simple and convenient way to tell when all the Components, Frames, and Child Controls on a TForm are fully painted? I'm basically looking for the equivalent of the onload() event found in web pages but in the context of Delphi forms. Preferably this technique would still work even if some form elements aren't currently visible and also it would fire again after a form resize occurred. I've got some form element "jiggling" going on as different form elements auto-adjust their size and I'd like to turn off repaints until it's all done to get a cleaner look.
Delphi Controls have a BeginUpdate and EndUpdate procedure. Back when I did Delphi programming - it has been a while - we would want to load a grid that took a lot of data, we would call BeginUpdate before the load and then EndUpdate after load finished. This told the control to quit receiveing messages (ie. scrolling as new records loaded) and wait to update at one time - making everything "look" faster. You may be able to set controls to BeginUpdate mode until re-paint finish then EndUpdate to sync everything up.
Take a look at DoubleBuffered property:
Determines whether the control’s image
is rendered directly to the window or
painted to an in-memory bitmap first.
Delphi syntax: property
DoubleBuffered: Boolean; Description
When DoubleBuffered is false, the
windowed control paints itself
directly to the window. When
DoubleBuffered is true, the windowed
control paints itself to an in-memory
bitmap that is then used to paint the
window. Double buffering reduces the
amount of flicker when the control
repaints, but is more memory
intensive. When a windowed control is
a dock site and has an associated dock
manager, it must be double-buffered.
Note: Some controls, such as
TRichEdit, can’t paint themselves into
a bitmap. For such controls,
DoubleBuffered must be set to false.

Avoid painting over nested controls

I am writing a toolbar-style control and use the ThemeServices unit to paint the background required by the toolbar. Like so:
ThemeServices.DrawElement(Canvas.Handle,
ThemeServices.GetElementDetails(trRebarRoot), ARect);
I then drop child controls onto the toolbar and voila - instant toolbar. Except that every now and again I notice that the nested (child) controls don't repaint as well. So my control paints its background and the child controls disappear until you move the mouse over them.
Some of the edit controls show their client area (no border) and some of them are just gone altogether.
This happens mostly when I place a window from another application over the toolbar and move it away.
I keep thinking this has to be very easy to cure, that perhaps I'm somehow preventing the paint message from reaching the child controls, or missing an override but I have been unable to figure it out.
For this to work properly so you do not end up over-painting the child controls from the WM_NCPaint, you can use the Window's GDI Region functions to create a clipping region which excludes the areas you do not want to paint.
You can do this by creating a Region using the CreateRectRgn (or similar) function with the size of your background.
Select this region in to the DC you are using via the SelectClipRgn function.
Next, loop through the top level child windows on your toolbar / panel and calling ExcludeClipRect with the appropriate coords for each child.
Finally when you paint, only the area's that have not been excluded will be painted.
Also be aware you might need to clear up your region (best check the GDI help).
That's normal for a canvas to have to repaint when covered by another windows control.
You should draw your tool bar in the OnPaint event of the container control.
OK, I found the problem myself. I was drawing the background in WM_NCPAINT because it is a gradiated background that cannot really be drawn bit by bit. I didn't realize that WM_NCPAINT is often called without the client controls being repainted. So I still paint the background in WM_NCPAINT but also in WM_PAINT. The latter is already clipped to exclude the border and to get the former to clip the client area, I called ExcludeClipRect.
This works like a treat.

How to make control invisible but responsive to mouse events?

I want to create a control (derived from TCustomControl) that is invisible but reacts to normal events (I want to use it to show a hint when moving the mouse over a custom element). I thought overriding the paint method and leaving it empty would do the job but unfortunately a rectangle is drawn where the component is.
How can I make the control completely invisible?
You can inherit from TGraphicControl instead of from TCustomControl, and leave the paint handler empty. Nothing will be drawn.
If you need a windowed control, then you should make sure that it has no border and uses the parent background. See this question for info on how to do that. You may need to override CreateParams() as well, to remove the border style bits.
If the control is not visible then process click messages in the parent, do a simple test for those being in the control's rectangle and use PostMessage to forward the message to the control. Such code may be more readable than empty paint handlers.
Bri

Resources