1. Technology

"The Big Brother" Delphi Code Toolkit - Part 1

Hiding from the Task List, Disabling Task-Switch, Remove From Task Bar, ...


By default, every Windows (Delphi) application you run has a corresponding button on the TaskBar (the Start button lies on it). You can use the Alt-Tab key combination when you want to switch to or from the running applications. When you press the Ctrl-Alt-Delete you get the "Close Program" window used to "End task" or to "Shut down" the system.

Have you ever wondered how to make your application invisible to the "Close Program" dialog that lists all the applications running in Windows xx? Suppose you're writing a utility that you want to run in the background so that the user doesn't notice it. How about disabling all task-switching/starting or stopping operations; coding "The Big Brother" Delphi application to prevent the user to work with any other (and I mean AnyOther) application but yours?

Code snippets will show you how to utilise the power of the Windows API to add special features to your Delphi applications.

Disable Task Switching

Using the SystemParametersInfo API function you can trick Windows into thinking that the screen saver is running. Doing so disables the Ctrl-Alt-Delete key sequence from displaying the "Close Program" dialog and rebooting the computer and Alt-Tab from switching to another application. It also stops Ctrl-Esc from opening the Start Menu.

If you wish to disable those keys while your application is running call the following SystemKeys function (place it in the Implementation section of your unit's code - and call from any procedure in your application - where needed). When you call SystemKeys, if Disable is True, the keys will be disabled, False otherwise.

 procedure SystemKeys(Disable: Boolean) ;
   OldVal : LongInt;
  SystemParametersInfo(SPI_SCREENSAVERRUNNING, Word(Disable), @OldVal, 0) ;
After a call to SystemKeys(True) the program runs, but you are unable to Alt-Tab to it nor switch to it in the task list. You can't invoke the Start button, either.

Disable or Hide the Task Bar and Tray

Another thing we can do to prevent the user from working with anything else but our application is to disable or even hide the TaskBar. Since the Tray (down-right where the clock is) is on the TaskBar disabling the TaskBar will disable the Tray.
The ShowTaskBar procedure displays or hides the TaskBar depending on the value of the bShow parameter passed to it.
 procedure ShowTaskBar(bShow: Boolean) ;
 if bShow = True then
  ShowWindow(FindWindow('Shell_TrayWnd', nil), SW_SHOWNA)
  ShowWindow(FindWindow('Shell_TrayWnd', nil), SW_HIDE) ;
Next, the EnableTaskBar procedure enables or disables the TaskBar (with ALL the buttons-applications on it) depending on the value of the bEnable parameter passed to it.
 procedure EnableTaskBar(bShow: Boolean) ;
  if bShow = True then
    EnableWindow(FindWindow('Shell_TrayWnd', nil), TRUE)
    EnableWindow(FindWindow('Shell_TrayWnd', nil), FALSE) ;
Note: if all the buttons on the TaskBar are from your applications then disabling/hiding the TaskBar is not a good idea. Why not just Disable the Start button and Hide/Show the Tray Icons. Of course Disabling Desktop Icons, will do the rest of the work. You can even Hide all the Icons on the Desktop.

Hiding the Application's TaskBar Button

Suppose you want to remove/hide the button on the TaskBar that represents your application. The trick is simple: in the OnCreate handler for your main form, insert the following piece of code:
 procedure TMainForm.FormCreate(Sender: TObject) ;
   ShowWindow(Application.Handle, SW_HIDE) ;
   SetWindowLong(Application.Handle, GWL_EXSTYLE, GetWindowLong(Application.Handle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW ) ;
   ShowWindow(Application.Handle, SW_SHOW) ;
Your application is now "invisible". Unfortunately pressing Ctrl-Alt-Del displays (if enabled) your applications Title allowing anyone to use "End Task" to terminate it or even "Shut Down" to exit from Windows. Of course, Delphi has the means of Detecting and Preventing Windows Shut Down.

Hide from "Close Program"

When you press the Ctrl-Alt-Del key combination the "Close Program" dialog box pops up. One simple way to hide your program from that dialog (the Task Manager dialog) is to clear the Application object's Title. If a program's main window does not have a title, Windows XX does not put the program in the "Close Program" dialog window. Remember that in Delphi the so called "main" window is not the Application window. The best place to clear the Title property is inside the Project's source code. To see the Project source select Project|View Source in the IDE. After the Application.Initialize add the bolded line:
   Application.Title := '';
   Application.CreateForm(TForm1, Form1) ;

I got the power...

As always, the beauty of Delphi and the ease of using the Win API has made "impossible" things to happen. All the API functions are available directly from the Windows unit which is automatically added to every new Delphi form unit - to call all the API function given in this article you do not need to do anything extra - just use them as described.

If you know of any other way of "hiding a Delphi application from Windows" please, go to the Forum and gives us your solutions.

If you are interested in a Delphi component that does all the dirty work for you, check the: "The Big Brother" Delphi code toolkit - PART 2.

©2014 About.com. All rights reserved.