Place a brower window on the secondary monitor

programming multi monitor app with MS .Net

Page 1 of 1

9 Replies - 15660 Views - Last Post: 21 August 2007 - 10:45 PM Rate Topic: -----

#1 lovecreatesbea...@gmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-August 07

Place a brower window on the secondary monitor

Posted 08 August 2007 - 10:21 PM

I want to start an IE instance within a Windows Form program, and put the IE browser onto the secondary monitor.

But I got an exception at the following LINE 5 of the C# code when called WaitForInputIdle on a process. Could you please help me out of this trouble? Thank you.

  private void button2_Click(object sender, EventArgs e)
  {	
	Process p1 = new Process();
	Process.Start("IExplore.exe", "www.google.com");
	p1.WaitForInputIdle(2000); /*LINE 5: System.InvalidOperationException */
	IntPtr h1 = p1.MainWindowHandle;
	Form f1 = (Form)Control.FromHandle(h1);
	f1.DesktopLocation = Screen.AllScreens[0].Bounds.Location;
	f1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
	f1.DesktopBounds = Screen.AllScreens[0].Bounds;	
  }



Is This A Good Question/Topic? 0
  • +

Replies To: Place a brower window on the secondary monitor

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Place a brower window on the secondary monitor

Posted 09 August 2007 - 12:12 AM

1) Showing window on second monitor (just some tips)

Showing something on the 2nd monitor is simple, just show it in x-y coordinate of the 2nd monitor. Assuming your primary monitor has a resolution of 1024◊768, then the 2nd monitor starts from 1024,0 to 2048,768, that is if both screens have the same resolution.

Use Screen.AllScreens.Length to find out how many monitors the system has. If itís greater 1, use 0 for the primary monitor and 1 for the 2nd monitor (2 for the 3rd monitor, etc).

To get the Bounds of the 2nd monitor use Screens.AllScreens[1].Bounds. This will give the coordinates of the 2nd monitor. Set your Form.Bounds to it and your stuff will appear on the 2nd monitor.

2) System.InvalidOperationException

Do you have a PInvoke Signature for WaitForIdleInput

[DllImport("user32.dll")]
static extern uint WaitForInputIdle(IntPtr hProcess, uint dwMilliseconds);



It may not fix the problem but you can try

Have you tried taking out the "2000" from p1.WaitForInputIdle() just to see what happens?

What happens when you remove the p1.WaitForInputIdle all together?

You could also try something like this

private void button2_Click(object sender, EventArgs e)
  {	
	try
		  {
			  Process p1 = new Process();
			  Process.Start("IExplore.exe", "www.google.com");
			  // Check if process has started, has a pid and is responding.
			  // Not having a pid will throw a InvalidOperationException.
			  if ((p1.Id >= 0) & (p1.Responding))
			  {
				  p1.WaitForInputIdle(2000);

				  IntPtr h1 = p1.MainWindowHandle;
				  Form f1 = (Form)Control.FromHandle(h1);
				  f1.DesktopLocation = Screen.AllScreens[0].Bounds.Location;
				  f1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
				  f1.DesktopBounds = Screen.AllScreens[0].Bounds;
			   }
			   else
			   {
				   //Kill the process
				   ((Process)state).Kill();

				   // Optional: Make sure you have no dead object here.
				   state = null;
			   }
		   }
		   catch (System.InvalidOperationException ex)
		   {
			   MessageBox.Show(ex.Message,"Error Starting Process",MessageBoxButtons.OK,MessageBoxIcon.Error);
			   // Optional: Make sure you have no dead object here.
			   state = null;
		   }
  }



That checks to see if the process has started (and has a pid, not having one will throw a System.InvalidOperationException), also checks to make sure the process is responding. If both are true then it proceeds with your code, otherwise it kills the process.

Try that and see if it changes anything. Hope it helps (at least some) :)

Happy Coding!
Was This Post Helpful? 0
  • +
  • -

#3 lovecreatesbea...@gmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-August 07

Re: Place a brower window on the secondary monitor

Posted 09 August 2007 - 02:41 AM

View PostPsychoCoder, on 9 Aug, 2007 - 12:12 AM, said:

<snip>


Thank you, PsychoCoder.

> Do you have a PInvoke Signature for WaitForIdleInput

The method WaitForIdleInput is from .net, not a Win32 api, do I also need to use Pinvoke on it?

> Try that and see if it changes anything. Hope it helps (at least some)

It still has the exception, and this time the exception was caught in your code.

I've made some progress on my last code snippet. This time I get a System.NullReferenceException on the Form object.

Could you please help me to correct this?


private void button2_Click(object sender, EventArgs e)
{	
  Process p1 = new Process();
  p1.StartInfo.FileName = "IExplore.exe";
  p1.StartInfo.Arguments = "www.google.com";
  p1.Start();
  p1.WaitForInputIdle(2000);	
  IntPtr h1 = p1.MainWindowHandle;
  Form f1 = new Form();
  f1 = (Form)Control.FromHandle(h1);
  f1.DesktopLocation = Screen.AllScreens[0].Bounds.Location; /*LINE 11: System.NullReferenceException*/
  f1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
  f1.DesktopBounds = Screen.AllScreens[0].Bounds;	
}



Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Place a brower window on the secondary monitor

Posted 09 August 2007 - 02:48 AM

Why are you using this

f1.DesktopLocation = Screen.AllScreens[0].Bounds.Location; /*LINE 11: System.NullReferenceException*/



What do you need to location for?

Read below and see if this helps

Showing something on the 2nd monitor is simple, just show it in x-y coordinate of the 2nd monitor. Assuming your primary monitor has a resolution of 1024◊768, then the 2nd monitor starts from 1024,0 to 2048,768, that is if both screens have the same resolution.

Use Screen.AllScreens.Length to find out how many monitors the system has. If itís greater 1, use 0 for the primary monitor and 1 for the 2nd monitor (2 for the 3rd monitor, etc).

To get the Bounds of the 2nd monitor use Screens.AllScreens[1].Bounds. This will give the coordinates of the 2nd monitor. Set your Form.Bounds to it and your stuff will appear on the 2nd monitor.
Was This Post Helpful? 0
  • +
  • -

#5 lovecreatesbea...@gmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-August 07

Re: Place a brower window on the secondary monitor

Posted 09 August 2007 - 03:08 AM

View PostPsychoCoder, on 9 Aug, 2007 - 02:48 AM, said:

<snip>


> Set your Form.Bounds to it and your stuff will appear on the 2nd monitor.

Thank you, PsychoCoder.

I assign the Bounds at the following line #13.

It still has a NullReferenceException exception. I think maybe I use the Form object wrongly. How can I correct it?

private void button2_Click(object sender, EventArgs e)
{
  Process p1 = new Process();
  p1.StartInfo.FileName = "IExplore.exe";
  p1.StartInfo.Arguments = "www.google.com";
  p1.Start();
  p1.WaitForInputIdle(2000);

  IntPtr h1 = p1.MainWindowHandle;

  Form f1 = new Form();
  f1 = (Form)Control.FromHandle(h1);
  f1.DesktopBounds = Screen.AllScreens[0].Bounds; /*LINE 13: System.NullReferenceException*/
}


Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Place a brower window on the secondary monitor

Posted 09 August 2007 - 04:49 AM

You are now getting a NullReferenceException because your MainWindowHandle variable (h1) is NULL, you need to do some checking to ensure your process is now running (otherwise h1 is always NULL). I have updated the code and given you 2 options for said checking, both are commented in the code snippet
private void button2_Click(object sender, EventArgs e)
{
  try
			{
				Process p1 = new Process();
				p1.StartInfo.FileName = "IExplore.exe";
				p1.StartInfo.Arguments = "www.google.com";
				p1.Start();
				p1.WaitForInputIdle(2000);

				//you have one of 2 choices, I will have them both here
				//You can loop through all the processes now running and search for iexplore (IE)
				//and grab that MainWindowHandle, which would work as long as IE wasnt open
				//multiple times (I could be wrong so try both). 

				//Or you need to check that p1 has a pid and is responding. Ill
				//demonstrate both here and let you decide

				//I suggest you try both to see which one offers the best results

				//Option #1
				//now we need to make sure the process is running
				//loop through all the processes running
				foreach (System.Diagnostics.Process clsProcess in System.Diagnostics.Process.GetProcesses)
				{
					//look for the process we just opened (in this case iexplore = Internet Explorer
					if (clsProcess.ProcessName.Contains("iexplore"))
					{
						//since IE is running we need to get the main window handle from it
						IntPtr h1 = clsProcess.MainWindowHandle;
					}
				}				

				//Option #2
				//Check to make sure p1 has a pid, and that it is responding
				if ((p1.Id >= 0) & (p1.Responding))
				{
					//get the MainWindowHandle from it
					IntPtr h1 = p1.MainWindowHandle;
				}

				Form f1 = new Form();
				f1 = (Form)Control.FromHandle(h1);
				f1.DesktopBounds = Screen.AllScreens[0].Bounds;
			}
			catch (System.Exception ex)
			{
				MessageBox.Show(ex.Message,"Error Starting Process",MessageBoxButtons.OK,MessageBoxIcon.Error);
			 }
}



Let me know if this solves your problem :)

Happy Coding!
Was This Post Helpful? 0
  • +
  • -

#7 lovecreatesbea...@gmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-August 07

Re: Place a brower window on the secondary monitor

Posted 09 August 2007 - 08:27 PM

View PostPsychoCoder, on 9 Aug, 2007 - 04:49 AM, said:

<snip>


> You are now getting a NullReferenceException because your MainWindowHandle
> variable (h1) is NULL, you need to do some checking to ensure your process is
> now running (otherwise h1 is always NULL).

In the following code snippet, the control flow reaches line #16 and an exception is thrown then. I'm now having two doubts on this failure:

1) I'm not sure if I declare and assign the Form object correctly.

2) Can Control.FromHandle() be used upon an IExplore.exe browser window that isn't a .net object? If this really can't be done, can I use another way to position this kind of window on the second monitor?

private void button2_Click(object sender, EventArgs e)
{	
  Process p1 = new Process();
  p1.StartInfo.FileName = "IExplore.exe";
  p1.StartInfo.Arguments = "www.google.com";
  p1.Start();
  p1.WaitForInputIdle(2000);

  while (true){
	if ((p1.Id >= 0) & (p1.Responding)){
	  //get the MainWindowHandle from it
	  IntPtr h1 = p1.MainWindowHandle;
	  Form f1 = new Form();
	  f1 = (Form)Control.FromHandle(h1);
	  f1.DesktopBounds = Screen.AllScreens[0].Bounds; /*LINE 16: System.NullReferenceException*/
	  break;
	}
  }
}


Was This Post Helpful? 0
  • +
  • -

#8 lovecreatesbea...@gmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-August 07

Re: Place a brower window on the secondary monitor

Posted 10 August 2007 - 01:30 AM

Thanks PsychoCoder.

I give up starting an ie instance now, I use a WebBrowser control to do the task instead, and it works.
Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Place a brower window on the secondary monitor

Posted 10 August 2007 - 02:27 AM

Well I'm glad you got it to work, and I'm sorry I couldn't find a solution for the instance of IE, I will keep looking to see if there is a way with opening an instance of IE. I know you can make an instance of IE invisible, so I was confused as to why I couldn't move it to a different monitor.
Was This Post Helpful? 0
  • +
  • -

#10 lovecreatesbea...@gmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-August 07

Re: Place a brower window on the secondary monitor

Posted 21 August 2007 - 10:45 PM

Hi, there is a code snippet on placing a window of an IE instance on a specified monitor here,

http://forums.micros.......23&SiteID=1
Was This Post Helpful? 1

Page 1 of 1