Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you override MessageDlg calls to a Custom TForm/Dialog?

Tags:

delphi

I have been using code similar to this

MessageDlg('', mtWarning, [mbOK], 0);

throughout my project, (thanks to the GExperts Message Dialog tool :) ) and i was wondering if anyone knows of a way do override the call and show my own custom Form.

The only way i can think to do it its make a New Form with something like

function MessageDlg(const Msg: string; DlgType: TMsgDlgType;
  Buttons: TMsgDlgButtons; HelpCtx: Longint): Integer;
begin
  //show my own code here
end;

and put it each of my uses lists before the Dialogs unit but is there a guaranteed way to make sure it uses my code not the Dialogs unit Code.
I don't like the idea of copying the dialogs unit to a local dir and making changes to it.

Or is this all to much work and should i just use my own function call and replace all the MessageDlg with my own. (which would not be fun, ive prob used MessageDlg too much)

like image 320
Christopher Chase Avatar asked Oct 20 '08 23:10

Christopher Chase


3 Answers

BTW, you want to add it after the Dialogs unit in your uses clause.

You have three choices in my opinion:

  1. Add your own unit after the Dialogs unit that has a method called MessageDlg and has the same signature to create your own form.
  2. Or create a whole new method, or set of methods, that creates specific dialogs using your own form.
  3. Do a global Search & Replace for MessageDlg with DarkAxi0mMessageDlg and then add your DarkAxi0mDialogs unit to your uses clause.

The first one is problematic because you might miss a unit and still get the old MessageDlg. The second one takes a lot more use, but provides better flexibility in the long run. The third one is probably the easiest and with the least downsides. Make sure you backup before doing the replace, and then use a diff tool (like Beyond Compare) to check your changes.

like image 186
Jim McKeeth Avatar answered Sep 29 '22 00:09

Jim McKeeth


I would recommend you to encapsulate the MessageDlg inside of you own procedures, this way if you change your procedures all your Message dialogs will be changed and you keep a standard.

Example: Create some procedures like, Alert(), Error(), Warning(), etc. If you ever need to change your error message looks, you need to do it only in one place.

Someday you might want to add a picture to your error messages, alerts... whatever, who knows?

like image 22
Fabio Gomes Avatar answered Sep 29 '22 00:09

Fabio Gomes


You can use a tool like TextPad to search/replace all instances of a string across folders and subfolders. So, I would suggest that you replace "MessageDlg(" with "MyMessageDlg(" so that you can customize it at will. Should take all of 5 minutes.

I think it would cause you problems to create a replacement and leave it named as it is currently in conflict with the VCL.

like image 44
Argalatyr Avatar answered Sep 29 '22 00:09

Argalatyr