How To Create a Tray Icon Controlled Application in Delphi


We want to create a System Tray Icon featured application that doesn’t shut down when the user closes the main window. Instead, the form simply hides and is reactivated when the user clicks the associated tray icon.


Tested in Delphi 2009.

1. Hide the main form.

Since closing the main form automatically terminates the application we need to hide it from the user. This is done by setting Application.ShowMainForm to False in the project settings.

Application.ShowMainForm := False;
Application.MainFormOnTaskbar := True;

2. Add a TTrayIcon to Main Form

Set the Icon property and add an OnClick event handler that displays your real main form (the one that the user will see).

procedure TMainForm.TrayIcon1Click(Sender: TObject);

3. Drop a popup menu on the main form and connect it to the tray icon.

Add a close menu item that calls Application.Terminate to kill the application.

procedure TMainForm.Close1Click(Sender: TObject);

4. Make the real main form show on start of application

Simply add a OnCreate event handler to the form and invoke Show.

procedure TMyOtherForm.FormCreate(Sender: TObject);

That’s it!

Alternative solution 1.

OBSERVE! This solution has the rather big flaw that it stops the computer from shutting down.

1. Intercept the automatic termination mechanism when the main form is closing

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  // Minimize application instead of closing
  CanClose := False;
  // Remove the main form from the Task bar

2. Clicking on the tray icon restores the application

procedure TMainForm.TrayIcon1Click(Sender: TObject);
  // Restore application from minimized state
  // We must invoke Show on the main form, otherwise
  // it won't be able to minimize properly a second time
  // Tested on D2009 Upd 3, and Windows XP

3. Add a popup menu with a close action to the tray icon, and use Application.Terminate in the event handler to kill the application

procedure TMainForm.CloseActionExecute(Sender: TObject);

Alternative solution 2

If you want to do it without the TTrayIcon component, this article has what you need: System Tray Delphi Application – Quick and Easy

Rounding off floating point numbers in Ruby


If you want to round a floating point number off to two decimals, there is no standard method in Ruby that does it for you.


The general solution is

(f * 10**d).round.to_f / 10**d

where f is the floating number and d is the number of decimals.

See my code sample for adding this functionality to the Float class.

Although the above works you should concider the reason you’re performing the rounding (or equivalent) operation. If it’s for presentation reasons only a better way might be to use a format string instead, and leave the original data intact.
Here’s an example where a representation (rounded_str) of a floating point number (f) is rounded to two decimals.

rounded_str = sprintf('%.2f', f)

Turn On ArcSDE Logging


You want to log the traffic between the client (i.e. ArcMap) and an ArcSDE database.


In the Command Window, write:

>set SDEINTERCEPTLOC = c:\tempclient
>c:\program files\arcgis\bin\arcmap.exe

It’s important to start the client (ArcMap in the above example) from the command prompt. ArcMap will intercept the traffic and log it to temp files, named like this:

c:\tempclient.001, c:\tempclient.002, and so on.

Migrations and Environments in Ruby on Rails


You want to migrate a target environment other than development.


For production:

>rake environment RAILS_ENV=production migrate

For test:

>rake environment RAILS_ENV=test migrate

Redirect Pages in WordPress


I want to move a page in a WordPress site to a different location, but I still want the old address to be active and instead redirect to the new address.


Unfortunatelly as per WordPress 2.7 there isn’t any standard functionallity to accomplish this, so the easiest way to go is to use plugins.

I use Paul Bains EasyRedirect which works for WordPress 2.7 although the description states “compatible up to: 2.2.0″. Just install it and add a tag of the form [redirect url time] to your redirecting page.

And we have to make sure the redirecting page is not showing up in the automatically generated page menu. I use <a href=””>Simon Wheatley’s Exclude Pages plugin</a> for this. This excellent piece of code adds a check box named “Include this page in user menus” to your page administration page. Very convenient.

Capistrano: Subversion asks for password


When deploying a Ruby on Rails application using Capistrano and the command rake deploy, you get an error message similar to this:

** [out ::] Authentication realm: dev
** [out ::] Password for ‘hasse’:
** [out ::] subversion is asking for a password
** [out ::] Authentication realm: dev
** [out ::] Username:


Solution from:

This is a Capistrano gotcha. One needs to make sure the account can access the Subversion repository.
Log into the application server and check out the repository into a temp directory. For example:

$> cd ~/some/temp/dir
$> svn co

Force a www Prefix


You want to force the World Wide Web prefix of your domain name, even though a page was requested without it. For example, if you would like all requests to be redirected to


Solution from Alister Cameron. It works for Linux/Unix and requires .htaccess file access.
In the web root directory .htaccess file, add the following:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]RewriteCond %{REQUEST_URI} ^/[^.]+[^/]$

RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

If you are using WordPress, those rules should come before the standard WordPress rules.