Recent

Author Topic: ShowMessage, control position? (SOLVED)  (Read 5714 times)

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
ShowMessage, control position? (SOLVED)
« on: January 03, 2018, 03:00:43 pm »
Is it possible to control position of a ShowMessage dialog?

It would be great if it was possible to capture mouse cursor position and let dialog pop up near mouse cursor. Maybe capture mouse position last time it clicked something in a form?
 
« Last Edit: January 08, 2018, 06:46:06 pm by mijen67 »

balazsszekely

  • Guest
Re: ShowMessage, control position?
« Reply #1 on: January 03, 2018, 03:51:44 pm »
Create a new form, add OnMouseUp event, then:
Code: Pascal  [Select][+][-]
  1. function MessageDlgEx(const AMsg: string; AX, AY: Integer; ADlgType: TMsgDlgType;
  2.   AButtons: TMsgDlgButtons): TModalResult;
  3. var
  4.   MsgFrm: TForm;
  5. begin
  6.   MsgFrm := CreateMessageDialog(AMsg, ADlgType, AButtons);
  7.   try    
  8.     MsgFrm.FormStyle := fsSystemStayOnTop;
  9.     MsgFrm.Position := poDefaultSizeOnly;
  10.     MsgFrm.Left := AX;
  11.     MsgFrm.Top := AY;
  12.     Result := MsgFrm.ShowModal;
  13.   finally
  14.     MsgFrm.Free
  15.   end;
  16. end;
  17.  
  18. procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  19.   Shift: TShiftState; X, Y: Integer);
  20. var
  21.   P: TPoint;
  22. begin
  23.   P.X := X;
  24.   P.Y := Y;
  25.   P := Form1.ClientToScreen(P);
  26.   MessageDlgEx('This is a test!', P.X, P.Y, mtInformation, [mbOk]);
  27. end;
« Last Edit: January 04, 2018, 05:24:00 am by GetMem »

guest61674

  • Guest
Re: ShowMessage, control position?
« Reply #2 on: January 03, 2018, 05:18:21 pm »
How to implement in this function the option of time to cancel if not pressed a button?

tks

balazsszekely

  • Guest
Re: ShowMessage, control position?
« Reply #3 on: January 03, 2018, 05:51:26 pm »
Quote
How to implement in this function the option of time to cancel if not pressed a button?
In that case you should implement your own custom form with a timer for example. The attached example also works.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: ShowMessage, control position?
« Reply #4 on: January 03, 2018, 07:37:37 pm »
Note that the Dialogs unit also includes this procedure

Code: Pascal  [Select][+][-]
  1. procedure ShowMessagePos(const aMsg: string; X, Y: Integer);

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: ShowMessage, control position?
« Reply #5 on: January 03, 2018, 11:40:53 pm »
Isn't it "MessageDlgPos(.... ?
« Last Edit: January 03, 2018, 11:44:38 pm by jamie »
The only true wisdom is knowing you know nothing

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: ShowMessage, control position?
« Reply #6 on: January 03, 2018, 11:51:08 pm »
Yes, there's MessageDlgPos() as well.
But that is not a simple Pos analogue of ShowMessage().
ShowMessagePos just takes  X and Y parameters in addition to the displayed text parameter.

MessageDlgPos() is more complex, and requires you to choose an icon and specify which (and how many) buttons to show. It also takes a HelpContext parameter.

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Re: ShowMessage, control position?
« Reply #7 on: January 06, 2018, 11:23:59 pm »
Create a new form, add OnMouseUp event, then:

Thanks! Works great!

To use it for click on button I used:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   P: TPoint;
  4. begin
  5.   P := Mouse.CursorPos;
  6.   // P := Form1.ClientToScreen(P);
  7.   MessageDlgEx('This is a test!', P.X, P.Y, mtInformation, [mbOk]);
  8. end;  



mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Re: ShowMessage, control position?
« Reply #8 on: January 06, 2018, 11:45:21 pm »
Note that the Dialogs unit also includes this procedure

Code: Pascal  [Select][+][-]
  1. procedure ShowMessagePos(const aMsg: string; X, Y: Integer);

Nice that the "standard" units can handle position as well (to keep things simple).

I changed a dialog from:

Code: Pascal  [Select][+][-]
  1. if MessageDlg('Question', 'Do you wish to show Selectable?',
  2.   mtConfirmation, [mbYes, MbNo], 0) = mrYes then

to:

Code: Pascal  [Select][+][-]
  1. var
  2.   pt: TPoint;
  3. ...
  4. if MessageDlgPos('Do you wish to show Selectable?',
  5.   mtConfirmation, [mbYes, MbNo], 0, pt.X, pt.Y) = mrYes then

But clearly the options for replacing MessageDlg with MessageDlgPos are not 1:1 see
http://lazarus-ccr.sourceforge.net/docs/lcl/dialogs/messagedlg.html and http://lazarus-ccr.sourceforge.net/docs/lcl/dialogs/messagedlgpos.html

 

TinyPortal © 2005-2018