Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs;
- type
- TForm1 = class(TForm)
- private
- procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;
- procedure WMWindowPosChanged(var Msg: TWMWindowPosChanged); message WM_WINDOWPOSCHANGED;
- public
- procedure CenterForm;
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.CenterForm;
- var
- R: TRect;
- begin
- R := Screen.MonitorFromWindow(Handle).WorkareaRect;
- Top := (R.Bottom - R.Top - Height) div 2;
- if Top < 0 then
- Top := 0;
- Left := (R.Right - R.Left - Width) div 2;
- if Left < 0 then
- Left := 0;
- end;
- procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);
- begin
- inherited;
- if Msg.CmdType = SC_RESTORE then
- CenterForm;
- end;
- procedure TForm1.WMWindowPosChanged(var Msg: TWMWindowPosChanged);
- const
- SWP_STATECHANGED = $8000; // undocumented
- begin
- inherited;
- if (Msg.WindowPos.flags and SWP_STATECHANGED <> 0) and (WindowState = wsNormal) then
- CenterForm;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement