Positionner un MessageBox sur la Form

const
mbMessage = WM_USER + 1024;

type
...
private
procedure ChangeMessageBoxPosition( var Msg: TMessage); message mbMessage;
...
end;

var
Form1 : TForm1;
msgCaption : PChar; // var iable to hold the caption

implementation


{$R *.DFM}

procedure TForm1.ChangeMessageBoxPosition( var Msg: TMessage);
var
MbHwnd: longword;
MbRect: TRect;
x, y, w, h: integer;
begin
MbHwnd := FindWindow(MAKEINTRESOURCE(WC_DIALOG), msgCaption);
if (MbHwnd <> 0) then
begin
GetWindowRect(MBHWnd, MBRect);
with MbRect do
begin
w := Right - Left;
h := Bottom - Top;
end;
// center horzontal
x := Form1.Left + ((Form1.Width - w) div 2);
// keep on screen
if x < 0 then
x := 0
else if x + w > Screen.Width then x := Screen.Width - w;
file://center vertical
y := Form1.Top + ((Form1.Height - h) div 2);
// keep on screen
if y < 0 then y := 0
else if y + h > Screen.Height then y := Screen.Height - h;
// set new windows position
SetWindowPos(MBHWnd,0,x,y,0,0,SWP_NOACTIVATE or SWP_NOSIZE or SWP_NOZORDER);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
PostMessage(Handle, WM_USER +1024, 0, 0);
msgCaption := 'Confirm';
MessageBox(Handle, 'Has our MessageBox moved ?', msgCaption,
MB_ICONQUESTION OR MB_YESNO);
end;
Ajouter un commentaire
Pseudo
Adresse email
Site internet (optionnel)
Votre commentaire
Fiche de l'article
Mise a jour01/02/2002
VisualisationVu 793 fois
PublicInternaute zz
CategorieDelphi - Trucs et astuces - Objet
Auteur de l'article
SwissDelphiCenter SwissDelphiCenter
connecté le 02/11/2011
329 articles dans la section
Contacter l'auteur