How launch app from within code?

  • Thread starter Thread starter Ronald S. Cook
  • Start date Start date
R

Ronald S. Cook

How do I launch, say, Microsoft Word from within a C# Win app? Word should
launch exterior to my app, of course. Is it easy to set size and position
of where Word window will be placed?

Thanks,
Ron
 
Process.Start() will do the trick. There is a "WindowStyle" enum that
lets you control the initial window state somewhat:

Process p = new Process();
p.StartInfo.FileName = @"C:\Program Files\Microsoft
Office\Office\winword.exe";
p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
p.Start();
 
This was exactly what I needed John.. thanks.

Any chance you can tell me how to control the X, Y position of where it
appears on the screen (and size of window)?

Thanks,
Ron
 
Ronald S. Cook said:
This was exactly what I needed John.. thanks.

Any chance you can tell me how to control the X, Y position of where it
appears on the screen (and size of window)?

You might need P/Invoke... although I don't see a managed version, the
STARTUPINFO structure used by Win32 CreateProcess has

dwX
If dwFlags specifies STARTF_USEPOSITION, this member is the x offset of the
upper left corner of a window if a new window is created, in pixels.
Otherwise, this member is ignored.
The offset is from the upper left corner of the screen. For GUI processes,
the specified position is used the first time the new process calls
CreateWindow to create an overlapped window if the x parameter of
CreateWindow is CW_USEDEFAULT.
dwY
If dwFlags specifies STARTF_USEPOSITION, this member is the y offset of the
upper left corner of a window if a new window is created, in pixels.
Otherwise, this member is ignored.
The offset is from the upper left corner of the screen. For GUI processes,
the specified position is used the first time the new process calls
CreateWindow to create an overlapped window if the y parameter of
CreateWindow is CW_USEDEFAULT.
dwXSize
If dwFlags specifies STARTF_USESIZE, this member is the width of the window
if a new window is created, in pixels. Otherwise, this member is ignored.
For GUI processes, this is used only the first time the new process calls
CreateWindow to create an overlapped window if the nWidth parameter of
CreateWindow is CW_USEDEFAULT.
dwYSize
If dwFlags specifies STARTF_USESIZE, this member is the height of the window
if a new window is created, in pixels. Otherwise, this member is ignored.
For GUI processes, this is used only the first time the new process calls
CreateWindow to create an overlapped window if the nHeight parameter of
CreateWindow is CW_USEDEFAULT.
 
Ronald S. Cook said:
This was exactly what I needed John.. thanks.

Any chance you can tell me how to control the X, Y position of where it
appears on the screen (and size of window)?

Or use Process.WaitForInputIdle, Process.MainWindowHandle, and
SetWindowPos... might be necessary for apps that don't respect their startup
parameters.
 
Ben,

Could you show me at all what that code would look like? I would greatly
appreciate it.

Thanks,
Ron
 
Hi Ronald,
You might want to look at SetWindowPos. Something like this:

[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
int X, int Y, int cx, int cy, uint uFlags);
private const int HWND_TOP = 0;

[STAThread]
static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.FileName = @"C:\Program Files\Microsoft
Office\Office\winword.exe";
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.Start();

SetWindowPos(p.MainWindowHandle, (IntPtr)HWND_TOP, 100, 100, 400, 400,
0);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top