How To Create a Tray Icon Controlled Application in Delphi

Problem:

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.

Solution:

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.Initialize;
Application.ShowMainForm := False;
Application.MainFormOnTaskbar := True;
:
Application.Run;

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);
begin
  MyOtherForm.Show;
end;

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);
begin
  Application.Terminate;
end;

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);
begin
Show;
end;

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);
begin
  // Minimize application instead of closing
  CanClose := False;
  Application.Minimize;
  // Remove the main form from the Task bar
  Self.Hide;
end;

2. Clicking on the tray icon restores the application

procedure TMainForm.TrayIcon1Click(Sender: TObject);
begin
  // Restore application from minimized state
  Application.Restore;
  Application.BringToFront;
  // 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
  Self.Show;
end;

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);
begin
  Application.Terminate;
end;

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

Problem:

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.

Solution:

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

Problem:

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

Solution:

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

Problem:

You want to migrate a target environment other than development.

Solution:

For production:

>rake environment RAILS_ENV=production migrate

For test:

>rake environment RAILS_ENV=test migrate

Redirect Pages in WordPress

Problem

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.

Solution

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=”http://wordpress.org/extend/plugins/exclude-pages/”>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

Problem:

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

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

Lösning:

Solution from: http://blog.codahale.com/2006/06/19/time-for-a-grown-up-server-rails-mongrel-apache-capistrano-and-you/:

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 http://mydomain.com/svn/repos/my_app

Force a www Prefix

Problem:

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 http://hans-eric.com be redirected to http://www.hans-eric.com

Solution:

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} !^www.alistercameron.com$ [NC]
RewriteRule ^(.*)$ http://www.alistercameron.com/$1 [R=301,L]RewriteCond %{REQUEST_URI} ^/[^.]+[^/]$

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

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