How to get Login Dialogs Appearing on the TaskBar

  

Most of the applications I have worked on in Delphi are database apps that may present a splash form quickly followed by a login dialog.  If the user fails to authenticate, the application needs to terminate gracefully.  The only way to do so cleanly is to modify the DPR code with some conditional logic. I’ve seen scenarios where after the main form was created the login dialog was invoked and if authentication failed everything was torn down. This complicates the shutdown logic, and often didn’t work well, encouraging a call to Halt() and sometimes leaving the process in memory.
Any long time Delphi user knows that messing with the generated DPR code in Delphi can cause all sorts of grief later when Delphi tries to auto create forms and add units to the uses clause.  That is out of scope for this post, suffice to say that it is possible to write something like this:

var
User :TUser;
begin
Screen.Cursor := crAppStart;
try
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.Title := ‘My Secure App’;
Application.CreateForm(TMainDm, MainDm);
finally
Screen.Cursor := crDefault;
end;
User := TfrmLogin.Login
(
function (const UserName,Password :string) :TObject
begin
Result := MainDm.Session.FindWhere<TUser>(
Restrictions.&And(
Restrictions.Eq(‘UserName’,UserName),
Restrictions.Eq(‘Password’,Password)
)
).FirstOrDefault;
end,
{
UserName can be passed as first parameter so don’t
have to type it in all the time
}
ParamStr(1),
3 {credential retries available }
) as TUser;
if User <> nil then
begin
Application.CreateForm(TfrmMain, frmMain);
frmMain.CurrentUser := User;
Application.Run;
end;
end.

The problem is that the Login Dialog does not appear on the Windows Taskbar. If it is hidden behind other windows, the user may think the application has not been launched and attempt to start another instance. There is no easy way for the user to bring the login dialog to the foreground short of closing other windows that may be in front of it. Putting the form on the taskbar solves this. As a quick solution I looked at the SetMainForm method in the Vcl.Forms unit, and decided to extract the ChangeAppWindow() procedure since it is not available outside of the Forms unit. Then I simply called it from this event, and voila! a taskbar button showing the Login form.

procedure TfrmLogin.FormCreate(Sender: TObject);
begin
ChangeAppWindow(handle,True,True);
end;

I’m sure there are reasons why this method is not exposed as a public TApplication class procedure, but perhaps it could be with a usage caveat.

Comments are closed.