Best way to Hide and Restore an Application - delphi

I would like to hide my entire Application and then later restore it back to the state it was (Kinda like minimize to Tray).
This includes all opened forms and an included Modal Form. It should also hide each form's taskbar visibility. I can hide the MainForm, but what about the other forms and the modal form? What would be the easiest way to hide dynamically all forms and restore them back to the state how they were?

Call Application.Minimize and Application.Restore to perform these actions.
To remove a form from the taskbar, hide it. Assuming that you have Application.MainFormOnTaskbar set to True, and only the main form associated with the taskbar, you can use Application.MainForm.Visible := False. Reverse this when you call Application.Restore.
So, in summary, to go dark:
Application.Minimize;
Application.MainForm.Visible := False;
And to reappear:
Application.MainForm.Visible := True;
Application.Restore;
If you have more than one form associated with the taskbar, you'd need to hide those forms too to remove the button from the task bar.

I don't understand your question. If I remember, subforms aren't visible in taskbar. Try change forms visibility in your project options.
function HideFromTaskbar(hWnd: HWND): Boolean;
begin
if SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_TOOLWINDOW) = 0 then
Result := False
else
Result := True;
end;
function HideFromTaskList(dwProcessId : DWORD) : Boolean;
const
RSP_SIMPLE_SERVICE: Integer = 1;
begin
Result := RegisterServiceProcess(dwProcessId, RSP_SIMPLE_SERVICE);
end;
ShowWindow(Application.Handle, SW_HIDE);

Related

Displaying a disabled modal form

I'm trying to disable a TForm's descendant and showing it as a modal form.
procedure TForm1.Button1Click(Sender: TObject);
var
Frm : TMyForm;
begin
Frm := TMyForm.Create(nil);
try
Frm.Enabled := False;
Frm.ShowModal();
finally
Frm.Free;
end;
end;
At runtime, it raises the following error message:
Cannot make a visible window modal.
The OP wants to display a disabled form modally when the form should be displayed for read-only purposes.
Disabling the form is the wrong thing to do.
How do you display the information? If you are using TEdit, TMemo, or TRichEdit controls, you should simply set them to read only. Otherwise, if you have some combinations of various controls like radio buttons, you should disable each and every such control, not the form itself. I mean, surely you still want the Cancel button to be enabled?
In addition, disabling the form instead of the actual controls will make the controls look enabled, which is very confusing! That's an important point.
So what you need to do is to display the form normally (not disabled!) and then set its controls to their appropriate states when the dialog is shown.
Just to emphasise my point about disabling the form vs its controls, consider this dialog box:
If I do
procedure TCustomViewFrm.FormShow(Sender: TObject);
begin
Enabled := False;
end;
then it looks like this when shown:
As you can see, every control looks very enabled indeed, but no control responds to mouse or keyboard input. This is very confusing and a horribly bad UX.
In fact, you cannot even close the dialog box using its title-bar Close button or Alt+F4. You cannot close it using its system menu, either. In fact, you cannot close it at all, because to close a window, it must respond to user input, and a disabled window doesn't do that. (You cannot move the window, either.)
Instead, if we disable all controls (except the Cancel button),
procedure DisableControl(AControl: TWinControl);
begin
for var i := 0 to AControl.ControlCount - 1 do
begin
if
(AControl.Controls[i] is TCustomButton)
and
(TCustomButton(AControl.Controls[i]).ModalResult = mrCancel)
then
Continue;
if AControl.Controls[i] is TWinControl then
DisableControl(TWinControl(AControl.Controls[i]));
AControl.Controls[i].Enabled := False;
end;
end;
procedure TCustomViewFrm.FormShow(Sender: TObject);
begin
DisableControl(Self);
end;
you get this nice UI:
Not only is it very clear that all controls are disabled, the user can also close the dialog box without killing your application using the Task Manager.

TEdit with clear button [duplicate]

When use TEdit control on the right side stay small icon 'x'. How after click on icon clear TEdit box.
Tnx all!
Delphi provide TClearEditButton to clear the TEdit content. It can be added by right clicking and selecting AddItem - TClearEditButton from the popup menu. It also has a Click procedure overriden in FMX.Edit unit like:
procedure TClearEditButton.Click;
var
EditTmp: TCustomEdit;
begin
inherited Click;
EditTmp := GetEdit;
if EditTmp <> nil then
begin
if EditTmp.Observers.IsObserving(TObserverMapping.EditLinkID) then
if not TLinkObservers.EditLinkEdit(EditTmp.Observers) then
Exit; // Can't change
EditTmp.Text := string.Empty;
if EditTmp.Observers.IsObserving(TObserverMapping.EditLinkID) then
TLinkObservers.EditLinkModified(EditTmp.Observers);
if EditTmp.Observers.IsObserving(TObserverMapping.ControlValueID) then
TLinkObservers.ControlValueModified(EditTmp.Observers);
end;
end;
Which make you don't need to write OnClick event handler for the TClearEditButton unless you want to do some other job along side with clearing the edit.
If you are using a TEditButton then you should write the OnClick event handler like:
procedure TForm1.EditButton1Click(Sender: TObject);
begin
Edit1.Text:= EmptyStr;
end;

Controls in a modal Form doesn't get focus when called from onActivate of another form. Why?

In certain cases my application try to open a certain Form (Form2) automatically after another one (Form1) is shown. I'm using onFormActivate to call ShowModal, but after the second form is shown, it's controls are losing their focus.
To reproduce:
Create a new VCL Applicattion;
Create a second Form and drop a TEdit in it;
On Main Form add an onFormActivate listener;
'
procedure TForm1.FormActivate(Sender: TObject);
begin
Form2.ShowModal;
end;
After run you will see Form2 being shown. But the edit doesn't get the focus.
It works if I comment the MainFormOnTaskbar in the project file.
// Application.MainFormOnTaskbar := True;
But that is not what I'm supposed to change. I would like to understand: Why the TEdit is losing the focus?
OnActivate is triggered while focus is in progress of being shifting around. Interrupting that process is a really bad idea.
If you want the OnActivate event to trigger a ShowModal() call, you should delay it using PostMessage() (or a short TTimer) so the message loop can finish processing the focus shift that is already in progress, and then can perform the ShowModal() when it is safe to do so. For example:
const
WM_SHOWMODAL_FORM2 = WM_APP + 1;
procedure TForm1.FormActivate(Sender: TObject);
begin
PostMessage(Handle, WM_SHOWMODAL_FORM2, 0, 0);
end;
procedure TForm1.WndProc(var Message: TMessage);
begin
if Message.Msg = WM_SHOWMODAL_FORM2 then
Form2.ShowModal
else
inherited;
end;

Display TPanel as Modal

I have a main form, with multiple panels, some of which are hidden. As the user interacts with the main form, I need to make some of the hidden panels visible and display them in a modal fashion so the user can't interact with the other parts of the main form until they finish with the modal panel.
Is there a way to display an existing panel on a form in a modal fashion?
I would prefer to not cycle through the main forms controls and disable/hide everything except for the one panel, which is the common answer developers have given when others have asked this same question.
My goal is to simply display an existing panel on the main form in a modal fashion without having to manipulate the other controls on the main form.
Since a TForm has .ShowModal(), we can easily create a temporary form, move the TPanel to the form, display the form as modal, wait for the user to dismiss the form, then move the TPanel back to its original parent before destroying the TForm.
If you create a form with a hidden TPanel called pnl and a button on that panel called btnCloseModalPanel, then the following code displays pnl as modal until the user clicks the button.
begin
DisplayModalPanel(pnl);
// do something with 'pnl.data...'
end;
procedure TForm1.DisplayModalPanel(Panel: TPanel);
var
frm: TForm;
old_top, old_left: Integer;
old_parent: TWinControl;
old_visible: Boolean;
begin
frm := TForm.Create(Panel.Parent);
try
frm.BorderStyle := bsNone;
frm.Position := poOwnerFormCenter;
frm.Tag := 12921; // test in close button click, so we don't close the wrong form
// Rememer properties we can change and then restore them
old_top := Panel.Top;
old_left := Panel.Left;
old_parent := Panel.Parent;
old_visible := Panel.Visible;
// Move the panel to the modal form
Panel.Parent := frm;
Panel.Top := 0;
Panel.Left := 0;
Panel.Visible := True;
// Display the modal form
frm.AutoSize := True;
frm.ShowModal;
// Restore everything
Panel.Visible := old_visible;
Panel.Parent := old_Parent;
Panel.Left := old_left;
Panel.Top := old_top;
finally
FreeAndNil(frm);
end;
end;
procedure TForm1.btnCloseModalPanelClick(Sender: TObject);
var
frm: TForm;
begin
if pnl.Parent is TForm then
begin
frm := pnl.Parent as TForm;
if frm.Tag = 12921 then // don't close the wrong form
frm.Close;
end;
end;

Disabling the form still allow childs controls to receive input

I'm having a lot of headache in the last days with delphi, what im trying to do is a lot simple, block the interface at somepoint and enable after some other point.
But as simply as it sound i couldn't figure out why somethings are allowed by design, so to clarify:
1) create a project
2) in the form put a edit and a button, tab order of the edit must be first
3) configure the OnExit event of the edit and write:
Enabled := False;
4) configure the OnClick event of the button and write:
ShowMessage('this is right?');
basically this is it, now compile, the focus it will be at the edit, press tab and the form will be disabled as we demanded, so accordingly to the tab order the next control to gain focus is the button (but we disabled the form), now press space and the message should come up.
so the question is: is this right? whats the logical explanation to this behaviour?
thx in advance.
Both TButton and TEdit are TWinControl descendents - this means that they are windowed controls. When they are created they are allocated their own HWND and the operating system posts messages to them directly when they have focus. Disabling their containing form prevents the main form from receiving input messages or from receiving focus but it does not disable any other windowed control if it already has input focus.
If these controls do not have input focus, it is responsibility of the containing form to transfer input focus to them when user input (click, tab key, etc) dictates. If the form is disabled and these controls are not focused then the form will not receive the input messages that would allow it to transfer focus. If focus is transferred to a windowed control, however, then all user input goes directly to that control, even if their parent control's window is disabled - they are in fact their own separate windows.
I'm not sure the behaviour you have observed is a bug - it is perhaps not expected, but it is standard behaviour. There is generally no expectation that disabling one window will also disable others within the same application.
The problem is that there are two separate hierarchies in play. On the VCL level, the Button is a child control and has a parent (the form). On the OS level, however, both are separate windows and the (component level) parent/child relationship is not known to the OS. This would be a similar situation :
procedure TForm1.Button1Click(Sender: TObject);
var
form2 : TForm1;
begin
self.Enabled := false;
form2 := TForm1.Create(self);
try
form2.ShowModal;
finally
form2.Free;
end;
end;
Would you really expect form2 to be disabled when it was shown, simply because its TComponent owner is Form1? Surely not. Windowed controls are much the same.
Windows themselves can also have a parent/child relationship, but this is separate from component ownership (VCL parent/child) and does not necessarily behave in the same way. From MSDN:
The system passes a child window's input messages directly to the
child window; the messages are not passed through the parent window.
The only exception is if the child window has been disabled by the
EnableWindow function. In this case, the system passes any input
messages that would have gone to the child window to the parent window
instead. This permits the parent window to examine the input messages
and enable the child window, if necessary.
Emphasis mine - if you disable a child window then its messages will be routed to the parent for an opportunity to inspect and act upon them. The reverse is not true - a disabled parent will not prevent a child from receiving messages.
A rather tedious workaround could be to make your own set of TWinControls that behave like this :
TSafeButton = class(TButton)
protected
procedure WndProc(var Msg : TMessage); override;
end;
{...}
procedure TSafeButton.WndProc(var Msg : TMessage);
function ParentForm(AControl : TWinControl) : TWinControl;
begin
if Assigned(AControl) and (AControl is TForm) then
result := AControl
else
if Assigned(AControl.Parent) then
result := ParentForm(AControl.Parent)
else result := nil;
end;
begin
if Assigned(ParentForm(self)) and (not ParentForm(self).Enabled) then
Msg.Result := 0
else
inherited;
end;
This walks up the VCL parent tree until it finds a form - if it does and the form is disabled then it rejects input to the windowed control as well. Messy, and probably could be more selective (maybe some messages should not be ignored...) but it would be the start of something that could work.
Digging further, this does seem to be at odds with the documentation :
Only one window at a time can receive keyboard input; that window is
said to have the keyboard focus. If an application uses the
EnableWindow function to disable a keyboard-focus window, the window
loses the keyboard focus in addition to being disabled. EnableWindow
then sets the keyboard focus to NULL, meaning no window has the focus.
If a child window, or other descendant window, has the keyboard focus,
the descendant window loses the focus when the parent window is
disabled. For more information, see Keyboard Input.
This does not seem to happen, even explicitly setting the button's window to be a child with :
oldParent := WinAPI.Windows.SetParent(Button1.Handle, Form1.Handle);
// here, in fact, oldParent = Form1.Handle, so parent/child HWND
// relationship is correct by default.
A bit more (for repro) - same scenario Edit tabs focus to button, exit handler enables TTimer. Here the form is disabled, but the button retains focus even though this seems to confirm that Form1's HWND is indeed the parent window of the button and it should lose focus.
procedure TForm1.Timer1Timer(Sender: TObject);
var
h1, h2, h3 : cardinal;
begin
h1 := GetFocus; // h1 = Button1.Handle
h2 := GetParent(h1); // h2 = Form1.Handle
self.Enabled := false;
h3 := GetFocus; // h3 = Button1.Handle
end;
In the case where we move the button into a panel, everything seems to work (mostly) as expected. The panel is disabled and the button loses focus, but focus then moves to the parent form (WinAPI suggests it should be NULL).
procedure TForm1.Timer1Timer(Sender: TObject);
var
h1, h2, h3 : cardinal;
begin
h1 := GetFocus; // h1 = Button1.Handle
h2 := GetParent(h1); // h2 = Panel1.Handle
Panel1.Enabled := false;
h3 := GetFocus; // h3 = Form1.Handle
end;
Part of the problem seems to be here - it looks like the top form itself is taking responsibility for defocusing controls. This works except when the form itself is the one being disabled :
procedure TWinControl.CMEnabledChanged(var Message: TMessage);
begin
if not Enabled and (Parent <> nil) then RemoveFocus(False);
// ^^ False if form itself is being disabled!
if HandleAllocated and not (csDesigning in ComponentState) then
EnableWindow(WindowHandle, Enabled);
end;
procedure TWinControl.RemoveFocus(Removing: Boolean);
var
Form: TCustomForm;
begin
Form := GetParentForm(Self);
if Form <> nil then Form.DefocusControl(Self, Removing);
end
Where
procedure TCustomForm.DefocusControl(Control: TWinControl; Removing: Boolean);
begin
if Removing and Control.ContainsControl(FFocusedControl) then
FFocusedControl := Control.Parent;
if Control.ContainsControl(FActiveControl) then SetActiveControl(nil);
end;
This partially explains the above observed behaviour - focus moves to the parent control and the active control loses focus. It still doesn't explain why the 'EnableWindow` fails to kill focus to the button's child window. This does start to seem like a WinAPI problem...

Resources