Advertisement
TLama

Untitled

Jul 15th, 2015
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.09 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.   private
  12.     procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;
  13.     procedure WMWindowPosChanged(var Msg: TWMWindowPosChanged); message WM_WINDOWPOSCHANGED;
  14.   public
  15.     procedure CenterForm;
  16.   end;
  17.  
  18. var
  19.   Form1: TForm1;
  20.  
  21. implementation
  22.  
  23. {$R *.dfm}
  24.  
  25. procedure TForm1.CenterForm;
  26. var
  27.   R: TRect;
  28. begin
  29.   R := Screen.MonitorFromWindow(Handle).WorkareaRect;
  30.   Top := (R.Bottom - R.Top - Height) div 2;
  31.   if Top < 0 then
  32.     Top := 0;
  33.   Left := (R.Right - R.Left - Width) div 2;
  34.   if Left < 0 then
  35.     Left := 0;
  36. end;
  37.  
  38. procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);
  39. begin
  40.   inherited;
  41.   if Msg.CmdType = SC_RESTORE then
  42.     CenterForm;
  43. end;
  44.  
  45. procedure TForm1.WMWindowPosChanged(var Msg: TWMWindowPosChanged);
  46. const
  47.   SWP_STATECHANGED = $8000; // undocumented
  48. begin
  49.   inherited;
  50.   if (Msg.WindowPos.flags and SWP_STATECHANGED <> 0) and (WindowState = wsNormal) then
  51.     CenterForm;
  52. end;
  53.  
  54. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement