1. Computing & Technology

Discuss in my forum

How to retrieve control identifiers for all the controls on a common dialog.

By , About.com Guide

Need to change embedded text on ANY control in Windows common dialogs?

Suppose you have Form1 with PrintDialog1 (TPrintDialog) component and would like to change (localize) its "Print" button title from default '&Print' to something else...

The code below will give you the ID's of all the controls on a PrintDialog (or any other standard Dialog you want):

ps. make sure you have Button1 (TButton), Memo1 (TMemo) and PrintDialog1 (TPrintDialog) on a Form1 (TForm) ...

~~~~~~~~~~~~~~~~~~~~~~~~~
//handles Button1 OnClick
procedure TForm1.Button1Click(Sender: TObject) ;
begin
   with PrintDialog1 do Execute;
end;

//handles PrintDialog1 OnShow
procedure TForm1.PrintDialog1Show(Sender: TObject) ;
begin
   Memo1.clear;
   EnumChildWindows(PrintDialog1.Handle,
                    @EnumProc,
                    Integer(Memo1.Lines)) ;
end;

//the callback function
function EnumProc(AHandle: HWND;
                   Lines: TStrings): boolean; stdcall;
var
   buf, Caption: Array [0..255] of char;
begin
   result := true;
   GetClassname(AHandle, buf, 256) ;
   GetWindowText(AHandle, Caption, 256) ;
   Lines.add(Format('ID: %d, class: %s, caption: %s',
                    [GetDlgCtrlID(AHandle), buf, Caption])) ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
Related:
more about callbacks
about common dialogs
How to change a button title on an Open dialog

Delphi tips navigator:
» How to get the current line and column from a Memo
« Positioning the caret in a line in a Memo or RichEdit

©2012 About.com. All rights reserved.

A part of The New York Times Company.