Hi.

I need help with defining on which monitor a given form is to be presented. I know I can discover how many monitors a computer has with screen.MonitorCount. But I haven't found yet how to change the settings. For example, I'd like to have a one-form application to run on a secondary monitor from start-up.

Thanks.

Eduardo

Recommended Answers

All 6 Replies

In the DPR file before the main form is created, set the monitor to the one you want: application.mainForm.monitor := screen.monitors[ 1 ]; I've never done this before, nor have I ever had a dual-monitor system, so I can't say this will actually work... But I think it will.

Hope this helps.

In the DPR file before the main form is created, set the monitor to the one you want: application.mainForm.monitor := screen.monitors[ 1 ]; I've never done this before, nor have I ever had a dual-monitor system, so I can't say this will actually work... But I think it will.

Hope this helps.

Thanks, Duoas. As I don't a dual system myself, I will have to find one or try it on a client a few thousand kilometers away :) Do you think I can place that line of code in the creation procedure of the main and only form? Because then I could parse the parameters to see if the client has a dual-monitor system or not. A brute force solution would be to have two instances of the same small program, one for the primary monitor and another for running on a secondary monitor but I'd like to know for the sake of learning.

Thanks again

You know what, TCustomForm.Monitor is read-only. Sorry about that.

I've googled about a little and it looks like Delphi doesn't specifically provide a way to do what you want with the VCL (because the Win32 API doesn't either).

You'll have to set the left, top position of your form so that it is in the area of the desired monitor. For example:

constructor TForm1.Create(Sender: TObject);
  begin
  // Center the form on the third monitor
  with application.monitors[ 2 ] do begin
    self.left := left +(width  div 2) -(self.width  div 2);
    self.top  := top  +(height div 2) -(self.height div 2)
    end
  end;

This code presupposes that there are at least three monitors attached... You'll want to choose a monitor more carefully than my hardcoded number.

Sorry for the mistake earlier. Unless there is something else going on that I don't know about this should work. Remember, I've no way to test it.

Good luck. Let me know what works.

You know what, TCustomForm.Monitor is read-only. Sorry about that.

I've googled about a little and it looks like Delphi doesn't specifically provide a way to do what you want with the VCL (because the Win32 API doesn't either).

You'll have to set the left, top position of your form so that it is in the area of the desired monitor. For example:

constructor TForm1.Create(Sender: TObject);
  begin
  // Center the form on the third monitor
  with application.monitors[ 2 ] do begin
    self.left := left +(width  div 2) -(self.width  div 2);
    self.top  := top  +(height div 2) -(self.height div 2)
    end
  end;

This code presupposes that there are at least three monitors attached... You'll want to choose a monitor more carefully than my hardcoded number.

Sorry for the mistake earlier. Unless there is something else going on that I don't know about this should work. Remember, I've no way to test it.

Good luck. Let me know what works.

Hi, Duoas.

Thanks for the update but I think there is something weird about it. I could agree with the

self.left := left +(width  div 2) -(self.width  div 2);

because the monitors perhaps should be considered as being side by side. But, if you also change the top position, wouldn't you be considering there is a matrix of monitors? I will try in the morning attaching a monitor to my notebook to see if it works and I'm almost certain my computer could emulate a second monitor but I have to check it. If I'm right, I'll be able to check the code.

I will inform you of whatever I find out.

You are probably right, but make no assumption about how the monitors are arrayed. If you are right, the code will still properly center the form on the monitor (because application.monitors[ n ].top will be zero).

> I will inform you of whatever I find out.
Please do.

:)

I used the above information to sucessfully start an app with 2 forms one main and one monitor like a customer monitor for a cash register the modifications needed are shown below.

from the main form create the form but check if there are 2 monitors other wise skip the form

if Screen.MonitorCount > 1 then   // are there 2 or more monitors
 begin
  if not Assigned(CustMonForm) then
     CustMonForm := TCustMonForm.create(tFSPosLoginform);
  CustMonForm.Show;
 end;

On the create of the form you want to show on the other monitor

 if Screen.MonitorCount > 1 then
     with Screen.monitors[ 1 ] do begin
       self.left := left +(width  div 2) -(self.width  div 2);
      self.top  := top  +(height div 2) -(self.height div 2)
   end
 end;

This works as expected in windows 7/8 using Delphi XE2

This does not work as well on XP. Once I have debug the problem I post an update.

Thank you all for sharing

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.