Firemonkey do stuff in background Form Delphi 10 Seattle - delphi

I've created a pop-up loadscreen Form that I want to show above any other form in a Firmonkey Multi device project. Now i've run into the problem that the loadscreen doesn't get updated with the things I do in the background Form. How can I solve this?
In the code below is an example of what i've tried:
procedure TForm1.Button1Click(Sender: TObject);
var
loadScreen:TfrmLoadScreen;
begin
loadScreen := TfrmLoadScreen.Create(nil);
loadScreen.ShowModal(
procedure(ModalResult: TModalResult)
var
i:Integer;
begin
for i := 0 to 200 do
begin
loadScreen.CurrentItem := i;
loadScreen.TextMessage := 'Item:' + loadScreen.CurrentItem.ToString;
Sleep(100);
end;
ModalResult := mrCancel;
end);
end;
I guess I have to do some multi-threading, but I don't have any experience doing this! How should I do this for my loadscreen?
I've also tried the following, but the form doesn't get shown:
procedure TForm1.Button1Click(Sender: TObject);
var
loadScreen:TfrmLoadScreen;
begin
loadScreen := TfrmLoadScreen.Create(nil);
loadScreen.OnShow := FormShowLoadScreen;
loadScreen.Show;
end;
procedure TForm1.FormShowLoadScreen(Sender: TObject);
var
i:Integer;
loadScreen:TfrmLoadScreen;
begin
loadScreen := TfrmLoadScreen(Sender);
for i := 0 to 200 do
begin
loadScreen.CurrentItem := i;
Sleep(100);
end;
loadScreen.Close;
end;

In your first code block, the annonymous method is only called after loadscreen.modalresult is set to something other than 0. This never happens (that we can see)
In your second block, you have 2 different loadscreen instances. They are not the same one. The FormShowLoadScreen handler is called after the firstly loadscreen.show, but it creates a 2nd loadscreen, with it's own displays. In fact, this might happen so fast, you wouldn't see it happen.
You really need to learn more about Delphi multi-threading. To display a "progress" form, you will have to put it's processing (display updates) inside the synchronise event of a separate thread that is started just after the loadscreen form is shown.
Actually... It's actually much easier in FMX to show an animation indicator before starting an annonymous thread, and then hide it again in the thread terminate block.
See Marco Cantu's blog post here Background Operations on Delphi Android, with Threads and Timers

Related

Avoid mouse right click for edit box

I want to avoid mouse right click on the edit boxes of my application which I am doing in BDS 2006.
I googled about it and i found a code as follows.
noPopUp := TPopupMenu.create(Edit1);
Edit1.PopupMenu := noPopup;
This is written on form activate. It works fine for edit1, but there are many edit boxes on the form so i wrote a for loop,
for i := 0 to Self.ControlCount-1 do
begin
if Self.Controls[i].ClassName = 'TEdit' then
begin
noPopUp := TPopupMenu.create(Self.Controls[i]);
TEdit(Self.Controls[i]).PopupMenu := noPopup;
end;
end;
This works fine for the edit boxes whose parent is Form. But if there are edit boxes on groupboxes or panels then, these panels and groupboxes in turn children of the form.
So my question is how to disable mouse right click on the edit boxes when the parent is not the form?
This accepted answer allocate unnecessary memory . You can think then it causes memory leaks too, because the created TPopupMenu are never released. But the Create( AOwner) of each TPopupMenu prevent this, releasing this memory on TEdit's Free.
To avoid unnecessary memory alloc, try this:
procedure TForm1.MyContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
begin
Handled := True;
end;
and in the loop:
for i := 0 to Self.ComponentCount-1 do
if Self.Components[i] is TEdit then
TEdit(Self.Components[i]).OnContextPopUp := MyContextPopup;
This is enought to do what you want!
Best regards!
The solution in not that far: substitute control with component, like this
for i := 0 to Self.ComponentCount-1 do
begin
if Self.Components[i].ClassName = 'TEdit' then
begin
noPopUp := TPopupMenu.create(Self.Components[i]);
TEdit(Self.Components[i]).PopupMenu := noPopup;
end;
end;

Abbrevia ProgressBar

I am using the Open Source Abbrevia Components to Archive some Files into a single Zip, whilst this is happening I am using the TAbMeter Gauge to display the progress.
I would prefer to use the TProgressBar for this purpose instead though (maintaining a standard interface with Windows).
How may I use a TProgressBar instead of the TAbMeter? I know I could code the progress myself, but seeing as the Abbrevia Components already have this done in the TAbMeter, I see no sense in rewriting it.
If I could even access the Position property of the TAbMeter I could simulate the TProgressBar's progress by synchronizing with the TAbMeter.
Here is a snippet, FileNames is a TStringList containing the Filenames to archive..
procedure ArchiveFiles(SaveAs: string; ProgressBar: TAbMeter);
var
AZipper: TAbZipper;
i: Integer;
begin
AZipper := TAbZipper.Create(nil);
try
AZipper.AutoSave := False;
AZipper.BaseDirectory := ExtractFilePath(SaveAs);
AZipper.ArchiveSaveProgressMeter := ProgressBar;
AZipper.FileName := SaveAs;
AZipper.StoreOptions := AZipper.StoreOptions + [soStripDrive, soRemoveDots]
- [soStripPath];
AZipper.TempDirectory := GetTempDirectory;
try
Screen.Cursor := crHourGlass;
ProgressBar.Visible := True;
for i := 0 to FileList.Count - 1 do
begin
AZipper.AddFiles(FileList.Strings[i], 0);
end;
finally
AZipper.Save;
AZipper.CloseArchive;
ProgressBar.Visible := False;
Screen.Cursor := crDefault;
end;
finally
AZipper.Free;
end;
end;
You are presumably setting the ArchiveSaveProgressMeter somewhere in your code. You can simply stop doing this and instead set the OnArchiveSaveProgress event. Then you need to supply an event with this signature:
procedure(Sender: TObject; Progress: Byte; var Abort: Boolean) of object;
You would respond to receipt of such an event by updating the Position value of the progress bar in your UI.
The method that surfaces this progress event also handles the progress meter version:
procedure TAbCustomZipper.DoArchiveSaveProgress(
Sender: TObject; Progress: Byte; var Abort : Boolean);
begin
Abort := False;
if Assigned(FArchiveSaveProgressMeter) then
FArchiveSaveProgressMeter.DoProgress(Progress);
if Assigned(FOnArchiveSaveProgress) then
FOnArchiveSaveProgress(Self, Progress, Abort);
end;
So the designers of the component have simply provided two alternative routes to receiving progress: the meter or a callback.
In order to handle progress from a callback you need to write a method like this:
procedure TMyMainForm.OnArchiveSaveProgress(
Sender: TObject; Progress: Byte; var Abort: Boolean);
begin
FProgressBar.Position := Progress;
end;
You then assign this event handler to OnArchiveSaveProgress, most likely in the IDE. It's an identical procedure to assigning an OnClick event to a button.
Note: I've never used Abbrevia so I may have picked out a different component from the one you are using. However, all the components that interact with meters, also offer progress via a callback so this basic approach will work no matter which component you use.
In case it helps anyone else, I've created a new TAbProgressBar component that can be used instead of TAbMeter. They both implement the same interface, so it works with the same Archive*ProgressMeter properties. Just update Abbrevia from Subversion and recompile the AbbreviaVCL and AbbreviaVCLDesign packages.

Get foreground CHILD window

Whenever Skype is in Default View, the TConversationWindow's become children of the tSkMainForm Window.
I am having problems finding out which TConversationWindow is active - however it's not like an MDI interface - only one TConversationWindow is visible, like if it was a Tab/Page.
When I do GetForegroundWindow, Skype's MainForm handle is returned (tSkMainForm). Is there any way that I can get the foreground TConversationWindow within Skype?
This question of mine has screenshots of Skype's Default View, if you need it. :)
EDIT: Here is a screenshot of the Winspector Hierachy:
EDIT2: I tried going thru the windows like this:
procedure TForm1.Button1Click(Sender: TObject);
function GetClassName(Handle: HWND): String;
var
Buffer: array[0..MAX_PATH] of Char;
begin
Windows.GetClassName(Handle, #Buffer, MAX_PATH);
Result := String(Buffer);
end;
Var
Wnd: HWND;
SkypeWnd: HWND;
begin
SkypeWnd := FindWindow('tSkMainForm',nil);
Wnd := GetTopWindow(SkypeWnd);
while (GetClassName(Wnd) <> 'TConversationForm') and (Wnd <> 0) and (not IsWindowVisible(Wnd)) do
begin
Wnd := GetNextWindow(Wnd,GW_HWNDNEXT);
end;
Label1.Caption := GetClassName(Wnd)+' - '+GetHandleText(wnd);
end;
The above is supposed to find the visible window, however when I debug it, it never enters the Begin End within the While loop, and Label1 displays "TChromeMenu - ChromeToolbar". When I remove the IsWindowVisible check, it atleast finds a TConversationForm. What am I doing wrong?
EDIT3: By placing the IsWindowVisible and getClassName check inside the loop, and break when true, I managed to do it. :)
By placing the IsWindowVisible and getClassName check inside the loop, and break when true, I managed to do it. :)

How to get TextWidth of string (without Canvas)?

I would like to get text width of a string before an application starts. Everything works fine until Application.MainForm canvas present. The problem is, when I try dynamically create TOrdinarium in the OnCreate event of the app. main form, "Canvas does not allow drawing" error occurs. (Application.MainForm is nil....). I tried several ways to create Canvas dynamically (one of them is written below), but it can not measure text sizes without being attached to parented control.
Is there way how to make it work somehow?
Thanx
I tried this:
TOrdinarium = class (TCustomControl)
private
function GetVirtualWidth:integer;
end;
constructor TOrdinarium.Create(AOwner:TComponent);
begin
inherited;
Width:=GetVirtualWidth;
end;
function TOrdinarium.GetVirtualWidth:integer;
var ACanvas : TControlCanvas;
begin
ACanvas := TControlCanvas.Create;
TControlCanvas(ACanvas).Control := Application.MainForm;
ACanvas.Font.Assign(Font);
result:=ACanvas.TextWidth('0');
ACanvas.Free;
end;
This works:
procedure TForm1.FormCreate(Sender: TObject);
var
c: TBitmap;
begin
c := TBitmap.Create;
try
c.Canvas.Font.Assign(self.Font);
Caption := IntToStr(c.Canvas.TextWidth('My String'));
finally
c.Free;
end;
end;
I'm not sure if this can be done, but if by "before the app starts" you mean "before the main form is displayed", you could always put your canvas-related code in the main form's OnCreate event. You'll have a valid canvas by that point.

How do do things during Delphi form startup

I have a form one which I want to show a file open dialog box before the full form opens.
I already found that I can't do UI related stuff in FormShow, but it seems that I can in FormActivate (which I protect from being called a second time...)
However, if the user cancels out of the file open dialog, I want to close the form without proceeding.
But, a form close in the activate event handler generates an error that I can't change the visibility of the form.
So how does one do some UI related operation during form start up and then perhaps abort the form (or am I trying to stuff a function into the form that should be in another form?)
TIA
It would be best (i think) to show the file open dialog BEFORE you create and show the form. If you want to keep all code together you might add a public class procedure OpenForm() or something:
class procedure TForm1.OpenForm( ... );
var
O: TOpenDialog;
F: TForm1;
begin
O := TOpenDialog.Create();
try
// set O properties.
if not O.Execute then Exit
F := TForm1.Create( nil );
try
F.Filename := O.FIlename;
F.ShowModal();
finally
F.Free();
end;
finally
O.Free();
end;
end;
Set a variable as a condition of the opendialog and close the form on the formshow event if the flag is not set correctly.
procedure TForm1.FormCreate(Sender: TObject);
begin
ToClose := not OpenDialog1.Execute;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
if ToClose then Close();
end;
or even more simply
procedure TForm1.FormShow(Sender: TObject);
begin
if not OpenDialog1.Execute then Close();
end;
If you want to keep the logic conditioning the opening self-contained in the Form, you can put a TOpenDialog in your Form and use a code like this in your OnShow event:
procedure TForm2.FormShow(Sender: TObject);
begin
if OpenDialog1.Execute(Handle) then
Color := clBlue
else
PostMessage(Handle, WM_CLOSE, 0, 0); // NB: to avoid any visual glitch use AlpaBlend
end;
If you don't need this encapsulation, a better alternative can be to check the condition before trying to show the form, for instance by embedding the Form2.Show call in a function that tests all the required conditions first.
Two Ways....
1. using oncreate and onactivate
create a global flag or even 2
var
aInitialized:boolean;
Set the flag to false in the oncreate handler.
aInitialized := false; //we have not performed our special code yet.
Inside onActivate have something like this
if not aInitialized then
begin
//our one time init code. special stuff or whatever
If successful
then set aInitialized := true
else aInitialized := false
end;
And how to close it without showing anything just add your terminate to the formshow. of course you need to test for some reason to close.. :)
Procedure Tmaindlg.FormShow(Sender: TObject);
Begin
If (shareware1.Sharestatus = ssExpired) or (shareware1.Sharestatus = ssTampered) Then
application.Terminate;
End;
In your DPR you will need to add a splash screen type effect. In my case I am showing progress as the application starts. You could also just show the form and get some data.
Code from the splash.pas
Procedure tsplashform.bumpit(str: string);
Begin
label2.Caption := str;
gauge1.progress := gauge1.progress + trunc(100 / items);
update;
If gauge1.progress >= items * (trunc(100 / items)) Then Close;
End;
Program Billing;
uses
Forms,
main in 'main.pas' {maindlg},
Splash in 'splash.pas' {splashform};
{$R *.RES}
Begin
Application.Initialize;
Application.Title := 'Billing Manager';
SplashForm := TSplashForm.Create(Application);
SplashForm.Show;
SplashForm.Update;
splash.items := 5;
SplashForm.bumpit('Loading Main...');
Application.CreateForm(Tmaindlg, maindlg);
SplashForm.bumpit('Loading Datamodule...');
Application.CreateForm(TfrmSingleWorkorder, frmSingleWorkorder);
SplashForm.bumpit('Loading SQL Builder...');
Application.CreateForm(TDm, Dm);
SplashForm.bumpit('Loading Security...');
Application.CreateForm(TSQLForm, SQLForm);
SplashForm.bumpit('Loading Reports...');
Application.CreateForm(Tpickrptdlg, pickrptdlg);
Application.Run;
End.

Resources