多显示器系统现在很常见。放置在屏幕的中心可能会将表格分散到多个显示器上。这是不可取的。
所以我中心形式的监视器上:
R := Form.Monitor.WorkAreaRect;
Form.Left := (R.Left+R.Right-Form.Width) div 2;
Form.Top := (R.Top+R.Bottom-Form.Height) div 2;
由于@bummi指出的那样,你可以这样写:
Form.Position := poScreenCenter;
这几乎可以作为你的愿望。我将把这个表格放在屏幕上。但是,它总是选择默认监视器。因此,使用该代码可能会导致您的表单被移到不同的显示器上,我认为这绝对不可取。
而是迫使形式中心,你可以决定,而不是在所有侧面扩大或缩小它:
procedure TReportFrm.SpecialFilesComboBoxChange(Sender: TObject);
var
NewLeft, NewTop, NewWidth, NewHeight: Integer;
begin
if(SpecialFilesComboBox.ItemIndex = 0) then begin
//No special files
NewWidth := 412;
NewHeight := 423;
...
end
else begin
//Yes special files
NewWidth := 893;
NewHeight := 423;
...
end;
NewLeft := Left + (Width-NewWidth) div 2;
NewTop := Top + (Top-NewHeight) div 2;
NewBoundsRect := Rect(NewLeft, NewTop, NewLeft+NewWidth, NewTop+NewHeight);
BoundsRect := NewBoundsRect;
end;
如果你想获得真正可爱的你会调整边界RECT从而使新大小和定位的形式没有离开显示器的边缘。
procedure MakeAppearOnScreen(var Rect: TRect);
const
Padding = 24;
var
Monitor: HMonitor;
MonInfo: TMonitorInfo;
Excess, Width, Height: Integer;
begin
Monitor := MonitorFromPoint(Point((Rect.Left+Rect.Right) div 2, (Rect.Top+Rect.Bottom) div 2), MONITOR_DEFAULTTONEAREST);
if Monitor=0 then begin
exit;
end;
MonInfo.cbSize := SizeOf(MonInfo);
if not GetMonitorInfo(Monitor, @MonInfo) then begin
exit;
end;
Width := Rect.Right-Rect.Left;
Height := Rect.Bottom-Rect.Top;
Excess := Rect.Right+Padding-MonInfo.rcWork.Right;
if Excess>0 then begin
dec(Rect.Left, Excess);
end;
Excess := Rect.Bottom+Padding-MonInfo.rcWork.Bottom;
if Excess>0 then begin
dec(Rect.Top, Excess);
end;
Excess := MonInfo.rcWork.Left+Padding-Rect.Left;
if Excess>0 then begin
inc(Rect.Left, Excess);
end;
Excess := MonInfo.rcWork.Top+Padding-Rect.Top;
if Excess>0 then begin
inc(Rect.Top, Excess);
end;
Rect.Right := Rect.Left+Width;
Rect.Bottom := Rect.Top+Height;
end;
接着前面的代码示例将改变这样的:
NewBoundsRect := Rect(NewLeft, NewTop, NewLeft+NewWidth, NewTop+NewHeight);
MakeAppearOnScreen(NewBoundsRect);
BoundsRect := NewBoundsRect;
PS我可以'if'语句后没有简单的调用了这一点,我会得到一个访问冲突:'BugReportFrm.Position := poMainFormCenter;' – ikathegreat 2013-03-07 06:31:22
是BugReportFrm分配的?您的代码显示TReportFrm。只是不要使用实例:Position:= poMainFormCenter; – bummi 2013-03-07 06:38:54
只需根据需要设置“顶部”和“左侧”即可。从Monitor.WorkAreaRect – 2013-03-07 06:41:06