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:

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

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);

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.