1. Computing & Technology

Discuss in my forum

A Custom Message Dialog with Standard Windows Icons

By , About.com Guide

Tip submitted by Peter Bernard

I needed to create my own customized "MessageDlg" message box with custom buttons, but I still wanted to have access to those spiffy icons that appear when you specify "mtInformation, mtConfirmation..." as the MessageDlg message type. After searching all over for icons on the web (and not finding anything suitable), I dug around in the Windows SDK help and cam across the LoadIcon API function.

LoadIcon - Get Standard Window Message Dialog Icons

LoadIcon loads the icon resource only if it has not been loaded; otherwise, it retrieves a handle to the existing resource. The function searches the icon resource for the icon most appropriate for the current display.
 uses ShellAPI;
 
 //drop a timage (image1) on the form
 var
   icon : TIcon;
 begin
   Icon := TIcon.Create;
   try
    icon.Handle := LoadIcon(icon.Handle, PChar(IDI_QUESTION)) ;
    Image1.Picture.Icon := Icon;
   finally
     icon.Free;
   end;
 end;
 
Note that you might see variations of the type of icon used for the various purposes, depending on the version of Windows and skin you might be running, BUT they will be in keeping with Windows standard for those dialog types.

Note: to use one of the predefined icons, set the lpIconName parameter to one of the following values:

  • IDI_APPLICATION - default application icon
  • IDI_ASTERISK - asterisk (used in informative messages).
  • IDI_EXCLAMATION - exclamation point (used in warning messages).
  • IDI_HAND - hand-shaped icon (used in serious warning messages).
  • IDI_QUESTION - question mark (used in prompting messages).
  • IDI_WINLOGO - Windows logo.

Delphi tips navigator:
» Overloading Delphi's ShowMessage to accept Integer, Boolean, Float, Currency ...
« Formatting a File Size in Bytes for Display - Convert Bytes to String using Delphi

©2012 About.com. All rights reserved.

A part of The New York Times Company.