How to write a number on a TShape Component? - delphi

I have a TShape Component. I need to load it dynamically and I need to place a number on that TShape. If anyone knows the way - please suggest it to me.
Thanks Rakesh

You can use the Canvas property of the TShape component to draw the number, to access this protected property you must create descendent class of TShape and publish that property or just use a interposer class.
type
TShape = class(ExtCtrls.TShape); //interposer class
TForm1 = class(TForm)
Shape1: TShape;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Shape1.Canvas.Font.Name :='Arial';// set the font
Shape1.Canvas.Font.Size :=20;//set the size of the font
Shape1.Canvas.Font.Color:=clBlue;//set the color of the text
Shape1.Canvas.TextOut(10,10,'1999');
end;

Place a TLabel above it and make its background transparent (Transparent = True). Edit text alignment if needed (Alignment := taCenter)

Related

TeeChart not redrawing after call of Canvas.TextExtent of child Label

I use
Embarcadero® Delphi 10.3 Version 26.0.36039.7899
TeeChart Standard v2020.29.200113 32bit VCL
My OS is Windows 7 Ultimate ServicePack1
I put a TChart on form and then i put a label on TChart.
After first call of Label1.Canvas.TextExtent the Chart stops redrawing. For example after minimizing and maximizing form instead of Chart contents i see the part of background window.
Is it allowed to put a label on TChart?
type
TForm1 = class(TForm)
Chart1: TChart;
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ASize: TSize;
begin
ASize := Label1.Canvas.TextExtent(Label1.Caption);
end;
After click on Button1 the chart stops redrawing.
UPD:
We migrate from Delphi7 to RAD Studio 10.3. In our project we use custom labels. The paint method is overriden. We draw on canvas of label, and we use Label.Canvas.TextExtent to calculate the size of text.
The parent component of labels is TChart.
The example above works correct in Delphi7, but not in Delphi 10.3.

Disable scrolling possibility of TWebBrowser

Does anyone know, how to disable scrolling possibility of TWebBrowser control in a Firemonkey iOS / Android application? My goal is to get absolutely static element that will not react on touches and so on.
There is no single setting or action that disables all user actions of the Fmx.TWebBrowser. But there is a feature you can use for your purpose.
The feature I refer to, is Fmx.WebBrowser.TCustomWebBrowser.CaptureBitmap documented here
Description
Captures the currently visible Web page as a bitmap.
This method returns a TBitmap object, which allows you to create,
manipulate and store images in memory or as files on a disk. The
scenario of the use of this method could be as follows:
1. Call this method to capture a visible Web page as a bitmap.
2. Hide a TWebBrowser control.
3. Display the bitmap and overlay other components (such as buttons or popups) on top of the bitmap.
In your case you would just hide the WB and display the bitmap.
Tested with the following code:
type
TForm25 = class(TForm)
Button1: TButton;
WebBrowser1: TWebBrowser;
Edit1: TEdit;
Image1: TImage;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure WebBrowser1DidFinishLoad(ASender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
bmp: TBitmap;
public
{ Public declarations }
end;
implementation
procedure TForm25.Button1Click(Sender: TObject);
begin
WebBrowser1.URL := Edit1.Text;
end;
procedure TForm25.Timer1Timer(Sender: TObject);
begin
bmp := WebBrowser1.CaptureBitmap;
Image1.Bitmap := bmp;
end;
procedure TForm25.WebBrowser1DidFinishLoad(ASender: TObject);
begin
Timer1.Enabled := True;
end;
With the WB hidden, it cannot be operated on.
The timer (1000 ms) I added because my attempts to capture the image already in the OnDidFinishLoad() event did nöt succeed.

Setfocus to the searchbox in a tlistview

I am working in Seattle, writing a FM application for windows only.
I have A tlistview on my form and have it populated with data.
I have the search option turned on.
How do I programmatically set focus to the search box?
How do I increase the size and font size of the search box?
thanks
The searchbox is not intended to be accessed programmatically except for setting it visible and to fire event when changed. Otherwise it is intended to be accessed only by the user.
Therefore, access is a little bit involved. However, the example of the OnSearchChange event inspired the following answer:
uses ..., FMX.SearchBox;
type
TForm17 = class(TForm)
ListView1: TListView;
Button1: TButton;
Label1: TLabel;
...
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
sb: TSearchBox; // a local reference
...
end;
implementation
procedure TForm17.Button1Click(Sender: TObject);
begin
if Assigned(sb) then
sb.SetFocus;
end;
procedure TForm17.FormCreate(Sender: TObject);
var
i: integer;
begin
ListView1.SearchVisible := True; // or set in the Object Inspector at design time
for i := 0 to ListView1.Controls.Count-1 do
if ListView1.Controls[I].ClassType = TSearchBox then
begin
sb := TSearchBox(ListView1.Controls[i]);
Break;
end;
end;
procedure TForm17.ListView1SearchChange(Sender: TObject);
begin
if Assigned(sb) then
Label1.Text := sb.Text;
end;
At form creation we search the SearchBox control and if found we store a reference to it in the sb: TSearchBox; field. Then access is quite straightforward.

Mouse hovering(similar to hint) delphi

Is there any event that determines, if the mouse is hovering above an edit box? Basically, I want to show a hint/help for the user, but I want to display an image and simple instructions. What would be the best way to proceed?
Thanks for any help
Use the OnMouseEnter and OnMouseLeave events. In the event handlers, you can set the visibility of a Label or simliar control with the hint text. In the example, I took an empty VCL form and inserted a TEdit and a TLabel. I implemented the OnMouseMEnter and the OnMouseLeave events:
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
procedure Edit1MouseEnter(Sender: TObject);
procedure Edit1MouseLeave(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Edit1MouseEnter(Sender: TObject);
begin
Label1.Visible:=True;
end;
procedure TForm1.Edit1MouseLeave(Sender: TObject);
begin
Label1.Visible:=False;
end;
Another solution could be to use the OnMouseEnter and OnMouseLeave events.
This is a sample found on Embarcadero:
type
TForm1 = class(TForm)
Button1: TButton;
StatusBar1: TStatusBar;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
procedure DisplayHint(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ Here is the implementation of the OnHint event handler }
{ It displays the application’s current hint in the status bar }
procedure TForm1.DisplayHint(Sender: TObject);
begin
StatusBar1.SimpleText := GetLongHint(Application.Hint);
end;
{ Here is the form’s OnCreate event handler. }
{ It assign’s the application’s OnHint event handler at runtime }
{ because the Application is not available in the Object Inspector }
{ at design time }
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnHint := DisplayHint;
end;
You can use special tag on HINT property of TLabel, then manage the output as you need.

Delphi 2010 - How to Copy and Clear the TShape

Ok, after working with TShape, I need to clean my "Shape1" from Lines and Text.
And also how to copy everything in "Shape1" into "Shape2" ?
Thanks B4 ^o^
type
TShape = class(ExtCtrls.TShape); //interposer class
TForm1 = class(TForm)
Shape1: TShape;
Shape2: TShape;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
// Draw some text on Shape1 := TShape
Shape1.Canvas.Font.Name :='Arial';// set the font
Shape1.Canvas.Font.Size :=20;//set the size of the font
Shape1.Canvas.Font.Color:=clBlue;//set the color of the text
Shape1.Canvas.TextOut(10,10,'1999');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
// Copy everything from Shape1 to Shape2 (make a duplication)
// How to do it ?
showmessage('copy Shape1 into Shape2');
end;
End.
Following pseudo-code makes a copy of SourceShape canvas content to the TargetShape canvas, but only until the TargetShape is refreshed:
procedure TForm1.Button1Click(Sender: TObject);
begin
TargetShape.Canvas.CopyRect(Rect(0, 0, TargetShape.ClientWidth,
TargetShape.ClientHeight), SourceShape.Canvas, Rect(0, 0,
SourceShape.ClientWidth, SourceShape.ClientHeight));
end;
To clear the previously copied content, you can use the following:
procedure TForm1.Button2Click(Sender: TObject);
begin
TargetShape.Invalidate;
end;
To keep your drawing persistent you need to implement your own OnPaint event, in which whenever it fires, copy the current canvas content from the source to target using the CopyRect method like shown above.
But the question is then, why to use a TShape control at all. It would be better to use TPaintBox and draw your stuff by yourself including the shapes that are drawn by TShape control.

Resources