Create custom InputQuery/MessageBox etc. without pain.

Create custom InputQuery/MessageBox etc. without pain.


Using a technique that may look like a hack, it is possible to modify generic message boxes and change the icon, edit control, buttons, size, and more.

The idea behind this customization is to create a custom message and post it to the queue before calling the message box. In the message handler, you retrieve the handle of the window (if it is a MessageBox) or the form (if it is an InputBox, InputQuery, or MessageDlg) and modify whatever you need. Here are two examples: one for a regular API MessageBox and another for an InputQuery.

1st example: You need to change the default caption of the Yes/No buttons of a regular MessageBox.

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure WMHackBox(var message: TMessage); message WM_USER + 1025;
end;
...
implementation
...
procedure TForm1.Button1Click(Sender: TObject);
begin
// Posting our custom message
PostMessage(Handle, WM_USER + 1025, 0, 0);
// MessageBox call
Application.MessageBox('I' completely stupid.', 'MyApplication', MB_YESNO or MB_ICONINFORMATION);
end;

procedure TForm1.WMHackBox(var message: TMessage);
var
h: HWND;
begin
// The message box is shown, let's retrieve its handle
h := FindWindow(WC_DIALOG, 'MyApplication');
// If this is the right message box, let's modify it
if GetParent(h) = Application.Handle then
begin
SetDlgItemText(h, ID_YES, 'I &agree');


SetDlgItemText(h, ID_NO, 'I &disagree');
end;
end;

Note that if you use MessageDlg, it is not a regular API message box but a form created dynamically by Delphi. See the 2nd example.

Result

2nd example: You need to change a property of an InputQuery edit control, for instance the PasswordChar.

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure WMHack(var message: TMessage); message WM_USER + 1024;
end;
...
implementation
...
const
ACaption = 'Caption';

procedure TForm1.Button1Click(Sender: TObject);
begin
PostMessage(Handle, WM_USER + 1024, 0, 0);
InputBox(ACaption, 'Prompt', 'Default');
end;

procedure TForm1.WMHack(var message: TMessage);
var
i: Integer;
j: Integer;
begin
for i := 0 to Screen.FormCount-1 do
if Screen.Forms[i].Caption = ACaption then
begin
for j := 0 to Screen.Forms[i].ControlCount-1 do
if Screen.Forms[i].Controls[j] is TEdit then
begin
TEdit(Screen.Forms[i].Controls[j]).PasswordChar := '#';
TEdit(Screen.Forms[i].Controls[j]).SelectAll;
Exit;
end;
end;
end;
Result

 







Create custom InputQuery/MessageBox etc. without pain.


Create custom InputQuery/MessageBox etc. without pain. : Multi-thousand tips to make your life easier.


Using a technique that may look like a hack, it is possible to modify generic message boxes and change the icon, edit control, buttons, size, and more.

The idea behind this customization is to create a custom message and post it to the queue before calling the message box. In the message handler, you retrieve the handle of the window (if it is a MessageBox) or the form (if it is an InputBox, InputQuery, or MessageDlg) and modify whatever you need. Here are two examples: one for a regular API MessageBox and another for an InputQuery.

1st example: You need to change the default caption of the Yes/No buttons of a regular MessageBox.

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure WMHackBox(var message: TMessage); message WM_USER + 1025;
end;
...
implementation
...
procedure TForm1.Button1Click(Sender: TObject);
begin
// Posting our custom message
PostMessage(Handle, WM_USER + 1025, 0, 0);
// MessageBox call
Application.MessageBox('I' completely stupid.', 'MyApplication', MB_YESNO or MB_ICONINFORMATION);
end;

procedure TForm1.WMHackBox(var message: TMessage);
var
h: HWND;
begin
// The message box is shown, let's retrieve its handle
h := FindWindow(WC_DIALOG, 'MyApplication');
// If this is the right message box, let's modify it
if GetParent(h) = Application.Handle then
begin
SetDlgItemText(h, ID_YES, 'I &agree');


SetDlgItemText(h, ID_NO, 'I &disagree');
end;
end;

Note that if you use MessageDlg, it is not a regular API message box but a form created dynamically by Delphi. See the 2nd example.

Result

2nd example: You need to change a property of an InputQuery edit control, for instance the PasswordChar.

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure WMHack(var message: TMessage); message WM_USER + 1024;
end;
...
implementation
...
const
ACaption = 'Caption';

procedure TForm1.Button1Click(Sender: TObject);
begin
PostMessage(Handle, WM_USER + 1024, 0, 0);
InputBox(ACaption, 'Prompt', 'Default');
end;

procedure TForm1.WMHack(var message: TMessage);
var
i: Integer;
j: Integer;
begin
for i := 0 to Screen.FormCount-1 do
if Screen.Forms[i].Caption = ACaption then
begin
for j := 0 to Screen.Forms[i].ControlCount-1 do
if Screen.Forms[i].Controls[j] is TEdit then
begin
TEdit(Screen.Forms[i].Controls[j]).PasswordChar := '#';
TEdit(Screen.Forms[i].Controls[j]).SelectAll;
Exit;
end;
end;
end;
Result

 

Create custom InputQuery/MessageBox etc. without pain.

Create custom InputQuery/MessageBox etc. without pain. : Multi-thousand tips to make your life easier.
Create custom InputQuery/MessageBox etc. without pain.
Create custom InputQuery/MessageBox etc. without pain.
Recommend to friends
  • gplus
  • pinterest

Comment

Leave comment

Rating