Click here to Skip to main content
15,891,925 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.

I'm using VB 2010 express. I'm using Shell("C:\Path\Program.exe") to open a commercial program and that works well. How can I also make this program always open on my second monitor (extended desktop mode) and set the size of it's window?

Thanks,
Jim
Posted
Comments
Sergey Alexandrovich Kryukov 10-Jan-12 11:44am    
Is it VB.NET? Tag it. VB10 is not relevant or correct. "Tag Visual Studio 2010" instead.
--SA

1 solution

I would advise to use System.Diagnostics.Process, see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

When a process is started successfully, use the property if the Process instance Process.MainWindowHandle, see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle.aspx[^].

Pay attention for the exceptions thrown by this property. Makes sure you process all exceptions immediately to detect the situation when the process is not yet started, already terminated, etc. If it happens or null is returned, it may mean that the handle is not yet created; in this case, try again later (yes, not perfectly effective, but you don't have another event or criterion).

When you obtain a window handle, move the application using P/Invoked raw Windows API MoveWindow, SetWindowPos or SetWindowPlacement or combination of those. I suggests the variants because you might need to put the window in proper Z-order position, adjust its size and handle the situations when the application is "reluctant" to assume exactly desired size, etc.

Please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545%28v=VS.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633544%28v=VS.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632611%28v=VS.85%29.aspx[^].

If you are yet to learn P/Invoke, read this:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/en-us/library/Aa712982[^].

Here you will be able to find already invoked code of those and other functions: http://www.pinvoke.net/[^].

Also, this CodeProject article can be helpful: Essential P/Invoke[^].

That's it.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900