Start program on a second monitor?

久未见 提交于 2019-11-28 18:47:24

Save the window position before program shutdown and restore them on startup. Multimonitor displays just increase the size of the desktop; other monitor surfaces just have a different section of the same X/Y plane with its origin at the top-left of the primary monitor.

This can be done automatically for you by any of several components.

BTW, the Screen variable in the Forms unit has a property called MonitorCount and another indexable property, Monitors[Index: Integer]: TMonitor. TMonitor has properties indicating the left, top, width, height etc., so all the information you need is there.

The global Screen object (part of Forms) has the concept of Monitors. I think this was added circa Delphi 6 or 7. The following code will work:

// Put the form in the upper left corner of the 2nd monitor
//   if more then one monitor is present.
if Screen.MonitorCount > 1 then
begin
  Left := Screen.Monitors[1].Left;
  Top := Screen.Monitors[1].Top;
end;

You could use any positive offset from that position to put it anywhere in that monitor. You can get the width and height from there too to know the dimensions.

Not really the answer your question implies, but couldn't you store the window settings (size, position, Maximized State) when ever the application is closed, and then apply them at startup?

sukhoy
procedure TMDIChild.btnShowMonClick(Sender: TObject);
begin
    if Screen.MonitorCount > 1 then
    begin
      FormShow.Left:=Screen.Monitors[1].Left;
      FormShow.Top:=Screen.Monitors[1].Top;
      FormShow.Width:=Screen.Monitors[1].Width;
      FormShow.Height:=Screen.Monitors[1].Height;
    end
    else
    begin
        FormShow.Show;
    end;
end;

I have done a similar thing a while ago in Delphi 5:

procedure TForm18.FormCreate(Sender: TObject);
var
  Mon: TMonitor;
  MonitorIdx: Integer;
begin
  MonitorIdx := 1; // better read from configuration
  if (MonitorIdx <> Monitor.MonitorNum) and (MonitorIdx < Screen.MonitorCount) then begin
    Mon := Screen.Monitors[MonitorIdx];
    Left := Left + Mon.Left - Monitor.Left;
    Top := Top + Mon.Top - Monitor.Top;
  end;
end;

Windows will let you specify the coordinates of a window in the CreateWindow API call. I don't know enough about Delphi or C++Builder to know if you have access to that part of the process.

You might also be able to move the window in a WM_CREATE handler.

EnumDisplayMonitors will give you the coordinates of each monitor in the system.


Evidently Delphi and C++ Builder have facilities that make this answer somewhat irrelevant. I'll leave it here in case someone comes across this question but needs it answered for a different environment.

I don't do much with windows systems, so I would suggest a hack like this.

Grab the width of the viewable desktop(both monitors combined), divide it by half and make that your starting position.

You may also look into what api tells you monitor2's dimensions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!