Mega Code Archive

 
Categories / Delphi / Forms
 

How to position a messagebox over a form

Title: How to position a messagebox over a form const mbMessage = WM_USER + 1024; type private procedure ChangeMessageBoxPosition(var Msg: TMessage); message mbMessage; end; var Form1: TForm1; msgCaption: PChar; // variable 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; //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