A Collection of Delphi Tips Tricks
A Collection of Delphi Tips Tricks
A Collection of Delphi Tips Tricks
The code has been tested in Delphi 6/7 and might not work in previous releases.
Contents
Application
Minimize application to taskbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Prevent a screensaver to kick in as long as your application is running . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Forms
Show/hide titlebar of a form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Set a form to stay on top of all other (non-topmost) windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Capture Maximize/Minimize/Close button clicks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Disable (gray out) Close button of a form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Detect the movement of a form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Disabling the movement of a form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Create form with rounded corners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
Make transparent form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Detect when a form has been minimized, maximized or restored . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Dialogs
Modify the controls in common dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Repositioning common dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Controls
Customized tooltips. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4
Show controls’ hints on statusbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Disable the popup menu of TEdit and TMemo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Hot-tracking controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Implementing a control array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Using windows API in textbox programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Menus
Disable the popup menu of TEdit and TMemo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1
Append items into system menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Mouse
Check whether a mouse button is being pressed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Capture mouse clicks at statusbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Graphics
Drawing rotated text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Avoid flickering in graphics programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Changing text alignment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
Drawing transparent text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Save graphics to a bitmap file or a Windows Metafile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Installing a font on the fly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Fonts
Get a list of system fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Files and folders
Drag’n’drop files from Windows Explorer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Open a file using its associated application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Determine the size of a file without opening it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Send a file to Recycle bin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Retrive the path of the Windows directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Windows API
Check whether a mouse button is being pressed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Show/hide titlebar of a form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Set a form to stay on top of all other (non-topmost) windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Modify the controls in common dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Capture Maximize/Minimize/Close button clicks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Disable (gray out) Close button of a form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Hot-tracking controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Append items into system menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Drag’n’drop files from Windows Explorer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Send a file to Recycle bin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Open a file using its associated application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Drawing rotated text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Monitor the changes of clipboard’s content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Detect the movement of a form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Disabling the movement of a form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Repositioning common dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Retrive the path of the Windows directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Using windows API in textbox programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Changing text alignment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
2
Capture mouse clicks at statusbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Create form with rounded corners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
Make transparent form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Detect when a form has been minimized, maximized or restored . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Prevent a screensaver to kick in as long as your application is running . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Installing a font on the fly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Using WinHelp API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Listing system’s drives in a listbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Algorithms
Converting between decimal, binary, and hexadecimal representation of a number . . . . . . . . . . . . . . . 12
Miscellaneous
Monitor the changes of clipboard’s content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Delphi’s equivalent of the VB’s App object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Find if you are connected to the Internet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
3
1. Customized tooltips
...
type
TMyHintWindow = class(THintWindow)
constructor Create(AOwner: TComponent); override;
end;
...
4
style := GetWindowLong(Handle, GWL_STYLE);
if AShow then begin
if (style and WS_CAPTION) = WS_CAPTION then exit;
case BorderStyle of
bsSingle, bsSizeable:
SetWindowLong(Handle, GWL_STYLE, style or WS_CAPTION or WS_BORDER);
bsDialog:
SetWindowLong(Handle, GWL_STYLE, style or WS_CAPTION or DS_MODALFRAME or
WS_DLGFRAME);
end;
end else begin
if (style and WS_CAPTION) = 0 then exit;
case BorderStyle of
bsSingle, bsSizeable:
SetWindowLong(Handle, GWL_STYLE, style and (not(WS_CAPTION)) or WS_BORDER);
bsDialog:
SetWindowLong(Handle, GWL_STYLE, style and (not(WS_CAPTION)) or
DS_MODALFRAME or WS_DLGFRAME);
end;
end;
SetWindowPos(Handle, 0, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE or SWP_NOZORDER or
SWP_FRAMECHANGED or SWP_NOSENDCHANGING);
end;
end;
Second method:
type
TForm1 = class(TForm)
...
private
5
procedure ShowHint(Sender: TObject);
...
6
if ATop then
SetWindowPos(AForm.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE)
else
SetWindowPos(AForm.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE)
;
end;
7
begin
Handled := true;
end;
procedure TMyEdit.DoEnter;
begin
Color := clAqua;
inherited;
end;
procedure TMyEdit.DoExit;
begin
Color := clWindow;
inherited;
end;
Second method:
type
TMyLabel = class(TLabel)
protected
procedure WndProc(var AMessage : TMessage); override;
end;
...
8
13. Append items into system menu
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;
end;
...
const
ID_ABOUT = WM_USER + 1;
9
filename: string;
begin
DragQueryFile(Msg.Drop, 0, @buf, sizeof(buf));
DragFinish(Msg.Drop);
filename := string(buf);
...
end;
10
function OpenFile(AFile: string; ADir: string = nil; AParams: string = nil): boolean
;
begin
result := ShellExecute(Application.Handle, ’open’, PChar(AFile), ADir, AParams,
SW_SHOWNORMAL) >= 32;
end;
The ADir parameter specifies the default directory for the shell operation. If nil, your application’s
current directory is used. If AFile specifies an executable file then AParams should contain the command
line parameters to be passed to the application.
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
public
Edits: array[0..Count−1] of TEdit;
procedure EditChange(Sender: TObject);
end;
...
11
var
i: integer;
begin
for i := 0 to Count−1 do begin
Edits[i] := TEdit.Create(Self);
with Edits[i] do begin
Parent := Self;
SetBounds(10, 10 + i∗50, 200, 40);
Tag := i; //each control should remember its index
OnChange := EditChange; //same event handler for all controls
end;
end;
end;
SetLength(result, SizeOf(N)∗8);
neg := N < 0;
N := Abs(N);
i := 1;
while N <> 0 do begin
if N and 1 = 1 then
result[i] := ’1’
else
result[i] := ’0’;
12
N := N shr 1;
Inc(i);
end;
if neg then begin
result[i] := ’−’;
Inc(i);
end;
Delete(result, i, length(result));
result := ReverseString(result);
end;
type
TForm1 = class(TForm)
Image1: TImage;
procedure FormCreate(Sender: TObject);
public
procedure WMDrawClipboard(var Msg: TWMDrawClipBoard); message WM_DRAWCLIPBOARD;
end;
...
13
Windows, Forms;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
public
procedure WMMove(var Msg: TWMMove); message WM_MOVE;
end;
...
type
TForm1 = class(TForm)
public
procedure WMNCHitTest(var Msg: TWMNCHitTest); message WM_NCHITTEST;
end;
...
14
dlgWidth := rect.Right−rect.Left;
dlgHeight := rect.Bottom−rect.Top;
MoveWindow(hwnd, Left+(Width−dlgWidth) div 2, Top+(Height−dlgHeight) div 2,
dlgWidth, dlgHeight, true);
Abort;
end;
3. Handle the WM_ERASEBKGND Windows message and set Msg.Result := 1; in the handler.
4. Use off-screen bitmaps (like double-buffering, but works for any control)
15
var
buffer: array[0..MAX_PATH] of char;;
begin
GetWindowsDirectory(buffer, MAX_PATH);
result := string(buffer);
if RightStr(result,1) <> ’\’ then result := result + ’\’;
end;
Second method:
...
TForm1.Paint(Sender: TObject);
var
Text: string;
begin
Text := ’Hello World’;
with Canvas do
16
TextOut(100 − TextWidth(Text) div 2, 100 − TextHeight(Text), Text);
end;
interface
type
TApp = class
protected
//if the constructor is declared as protected then the
//class can be instantiated only within this unit
constructor Create;
public
Title: string;
Path: string;
ExeName: string;
HelpFile: string;
FileDescription: string;
FileVersion: string;
ProductName: string;
...
end;
var
App: TApp;
implementation
type
PLongInt = ^Longint;
constructor TApp.Create;
17
var
nBytes, nInfoLen: DWORD;
psBuffer: PChar;
pntValue: Pointer;
iLangID, iCharSetID: Word;
strID: string;
begin
ExeName := Application.ExeName;
Path := ExtractFilePath(ExeName);
if RightStr(Path,1) <> ’\’ then Path := Path + ’\’;
Title := Application.Title;
HelpFile := Application.HelpFile;
initialization
App := TApp.Create;
finalization
App.Free;
end.
18
31. Capture mouse clicks at statusbar
uses
Windows, Messages, Forms, Classes, SysUtils, Controls, ComCtrls, Commctrl;
type
TForm1 = class(TForm)
StatusBar1: TStatusBar;
procedure FormCreate(Sender: TObject);
private
procedure WMNotify(var AMsg: TWMNotify); message WM_NOTIFY;
end;
...
case pNMM^.hdr.code of
NM_CLICK:
Caption := Format(’%s was clicked at panel %d’,[ctrl.Name,iPanel]);
NM_DBLCLK:
Caption := Format(’%s was double−clicked at panel %d’,[ctrl.Name,iPanel]);
NM_RCLICK:
Caption := Format(’%s was right−clicked at panel %d’,[ctrl.Name,iPanel]);
NM_RDBLCLK:
19
Caption := Format(’%s was right−double−clicked at panel %d’,[ctrl.Name,iPanel
]);
end;
end;
procedure CheckConnection;
var
dwFlags: DWORD;
begin
if InternetGetConnectedState(@dwFlags, 0) then begin
if (dwFlags and INTERNET_CONNECTION_MODEM)=INTERNET_CONNECTION_MODEM then
ShowMessage(’Connected through modem’)
else if (dwFlags and INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN then
ShowMessage(’Connected through LAN’)
else if (dwFlags and INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY then
ShowMessage(’Connected through Proxy’)
else if (dwFlags and INTERNET_CONNECTION_MODEM_BUSY) =
INTERNET_CONNECTION_MODEM_BUSY then
ShowMessage(’Modem is busy’);
end else
ShowMessage(’Offline’);
end;
type
TForm1 = class(TForm)
20
TrackBar1: TTrackBar;
procedure TrackBar1Change(Sender: TObject);
end;
...
21
var
emf: TMetafile;
mfc: TMetafileCanvas;
begin
emf := TMetafile.Create;
emf.Enhanced := true; // save as Enhanced Metafile
emf.Width := 400;
emf.Height := 400;
mfc := TMetafileCanvas.Create(emf, 0);
with mfc do begin
//drawing commands
end;
mfc.Free;
emf.SaveToFile(AFile);
emf.Free;
end;
22
end;
23
VolumeSerialNumber : DWORD;
MaximumComponentLength : DWORD;
FileSystemFlags : DWORD;
VolumeNameBuffer : array[0..255] of char;
begin
GetVolumeInformation(PChar(aRoot),
VolumeNameBuffer,
sizeof(VolumeNameBuffer),
@VolumeSerialNumber,
MaximumComponentLength,
FileSystemFlags,
nil,
0);
result := string(VolumeNameBuffer);
end;
24