How to make control invisible but responsive to mouse events? - delphi

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

Related

Paint TProgressBar behind text and icon in TSpeedButton

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.

Delphi 7, How can a child react to the parent form Moving?

I'm writing a TFrame descendant that can host any sort of controls at runtime.
Among its features, under specific conditions, it should show a sort of visual dimmed "mask".
I achieved this effect overlaying a separate TCustomForm descendant class with AlphaBlend,
and for this to work the form must have no parent.
Thus, I need some extra code to keep this window anchored to the Client area of the frame, whether it's been resized or moved.
Not a big deal for resizing: I can override TMyFrame's Resize method.
But what about MOVING?
Let's say the frame is client-aligned to the main form: its Left and Top values don't change if I move the main window, so no WM_MOVE message is sent to the frame.
And I need somethig to be incapsulated INSIDE the TMyFrame unit, in order to keep it reusable.
Is there any other message I can handle in such a situation?
Thank you
Hook the parent form's WindowProc property, or subclass the parent form's window using SetWindowSubclass(), to intercept WM_WINDOWPOSCHANING and WM_WINDOWPOSCHANGED messages.

How to prevent a firemonkey form to draw itself (for calling an own drawing function)?

I try to convert an existing OpenGL-Application from VCL-based forms to FMX-based forms in C++ Builder XE3.
For this purpose I use the handle of a Firemonkey-form to create the OpenGL device context and make this form a child of another form to mimic the panel inside the form which I had in my VCL-based application.
The problem is now that after a resize-event or in general after calling Invalidate() the form is rendered with its background. This causes a flickering or even worse: sometimes the background is shown instead of my OpenGL rendered scene.
How can I prevent the repaint of the region at the position of my Firemonkey-OpenGL-form. Alternatively, can I define an Ownerdraw-function or something else?
Please note, that it is no option for me to use a Firemonkey-3d-Form.
Have tried giving the form a transparent background? Then you can paint whatever you want 'over the top'.
The quick way is to set the forms Transparent property to True, but this creates issues with ComboBoxes.
Another way is to place a TRectangle on the form. Set it's StyleName to 'backgroundstyle'. Set it's Fill.Color to claNull. Set Stroke.Thickness to 0. This will replace the default background styling for the form.
If you want to paint your own borders too you'll need to investigate non-client area styling but I don't have a pointer to a good resource at present.
After hours of experimenting I found the solution: The "TCommonCustomForm" has the needed properties: (1) it does not draw itself, (2) it has a handle which can be used for OpenGL initialization.
I hope this information will help others.

how is it possible to clear what was painted before?

I'm using Delphi and I'm building my own label component with class TControl.
Before I paint the text according to the properties (such as caption, font, etc.) I want to clear paint rect like there is nothing at the place of component. I mean I want to make it like a glass so that the other components behind it will be displayed; and then paint the text. What should I do to paint other components that are placed behind my label to it?
To do that, you need to do nothing. :-)
When you make a transparent label-like component, you best use the TGraphicControl base class. This is actually little more than a canvas to paint on. Whenever the content should be changed, you call the Invalidate method to repaint your control. This will call the Paint method that you can override. With every repaint, your control will be clear and transparent, except for the parts where you draw stuff in your Paint method.
Unless you override and disable the background painting, then you dont need to do anything. It depends on what base-class you go for. Although you can simply use (in the Paint() method):
Canvas.Brush.Style:=bsSolid;
Canvas.Brush.Color:=self.Color; //If you have a public color property
Canvas.FillRect(ClientRect);
You should also read up on TControlCanvas. Here is a website that deals with this topic more in depth: http://www.delphidabbler.com/tips/75

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.

Resources