1. Technology

Change the Default Application Icon for a Console Mode Delphi Application

Have a Different Icon for a Console Mode Application in Windows Explorer


Delphi Console Mode Application - Custom Icon

Delphi Console Mode Application - Custom Icon

Delphi console mode applications are pure Windows programs that run without a graphical interface. When a console application is started, Windows creates a text-mode console window through which the user can interact with the application.

By design, all console mode applications have the same icon (depends on the Windows version).

For a normal GUI application you can change the application icon using Project - Options - Application - Application Settings - Icon. For console mode applications this section is disabled - and thus all console mode application have a default icon.

Windows by design uses the first icon it locates in an application's resources for the application icon.

For console mode applications there's no resource file created - no "{$R *.res}" line in the DPR - thus no icon to use.

Specify an Icon for a Console Mode Delphi Application

To have a custom icon for a console mode application you need to create a resource file hosting, at least, an icon for the first icon-type resource.

Here's what to do:

  1. Open Notepad (or any application that can handle ASCII files). Have the next line in the document:
     1 icon "myicon.ico"
  2. Save this file as "conicon.rc" in the project's folder. You can use any name BUT the extension must be "RC". Of course, ensure that you actually have an icon named "conicon.ico" in the project's folder.
  3. Go to Project Manager and add this file to your console mode project.
  4. Compile.
  5. You should see a new line added to your project's source code file:
     {$R 'conicon.res' 'conicon.rc'}
    The top of your console mode program file should look like:
     program conapp;
     {$R 'conicon.res' 'conicon.rc'}
     uses SysUtils;
  6. Note that your compiled application *has* a different icon, specified by "myicon.ico"
What we've done is that we have create a resource file for the application hosting our icon as the first icon-type resource - and Windows is using this icon for our console mode application.

Well, that's it :)

Delphi tips navigator:
» Rename a Directory / Folder Using Delphi Code
« Implementing the On-AFTER-Create And On-BEFORE-Create Events for Delphi Forms

©2014 About.com. All rights reserved.