G
garyusenet
For this first time today I used the System.Diagnositcs namespace to
launch a program from my c# code. The program launches OK but I have
something which has completely stumped me.
The SetWindowPos method does not work. If I run the code as it is
presented below, app.exe launches in its own window and is displayed at
the top left part of the screen. However it isn't repositioned which is
what the last piece of code should do. HOWEVER if i end my c# program
and leave app.exe open - and then comment out the launchapp() part of
Form1_load and just run my c# program again the window is moved as
expected. So for some reason the SetWindowPos method is only working if
app.exe is open BEFORE I run my c# code, and not when I use my C# code
to open app.exe. Any ideas why this is please?
NB
The original (originalapp.exe) ran in fullscreen. Somebody wrote a
programme app.exe which runs originalapp.exe in a window. This is the
reason why I have to use FindWindow to find the Hwnd of originalapp.exe
from within app.exe and can't just query the process p for it's Hwnd.
Thanks very much for comments,
Gary-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
#region static bool SetWindowPos (hWnd, hWndInsertAfter, X, Y,
cx, cy, uFlags) *via imported dll*
[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;
#endregion
#region static int FindWindow(_ClassName, _WindowName) *via
imported dll*
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError =
true)]
private static extern int FindWindow(string _ClassName,
string _WindowName);
#endregion
#region static void GetWindowText(h, s, nMaxCount) *via
imported dll*
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError =
true)]
public static extern void GetWindowText(int h,
StringBuilder s, int nMaxCount);
#endregion
public Form1()
{
InitializeComponent();
}
private void launchapp()
{
Process p = new Process();
p.StartInfo.FileName = @"C:\Program
Files\app\directory\app.exe";
p.StartInfo.WorkingDirectory = @"C:\Program
Files\app\directory\";
p.StartInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden;
p.Start();
return;
}
private void Form1_Load(object sender, EventArgs e)
{
launchapp();
// window handle, place window at top of Z order, WP left,
WP top, width, height
int handle = FindWindow(null, "Title of Application
Window");
SetWindowPos((IntPtr)handle, (IntPtr)HWND_TOP, 5, 5, 5,
5,0);
}
}
}
launch a program from my c# code. The program launches OK but I have
something which has completely stumped me.
The SetWindowPos method does not work. If I run the code as it is
presented below, app.exe launches in its own window and is displayed at
the top left part of the screen. However it isn't repositioned which is
what the last piece of code should do. HOWEVER if i end my c# program
and leave app.exe open - and then comment out the launchapp() part of
Form1_load and just run my c# program again the window is moved as
expected. So for some reason the SetWindowPos method is only working if
app.exe is open BEFORE I run my c# code, and not when I use my C# code
to open app.exe. Any ideas why this is please?
NB
The original (originalapp.exe) ran in fullscreen. Somebody wrote a
programme app.exe which runs originalapp.exe in a window. This is the
reason why I have to use FindWindow to find the Hwnd of originalapp.exe
from within app.exe and can't just query the process p for it's Hwnd.
Thanks very much for comments,
Gary-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
#region static bool SetWindowPos (hWnd, hWndInsertAfter, X, Y,
cx, cy, uFlags) *via imported dll*
[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;
#endregion
#region static int FindWindow(_ClassName, _WindowName) *via
imported dll*
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError =
true)]
private static extern int FindWindow(string _ClassName,
string _WindowName);
#endregion
#region static void GetWindowText(h, s, nMaxCount) *via
imported dll*
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError =
true)]
public static extern void GetWindowText(int h,
StringBuilder s, int nMaxCount);
#endregion
public Form1()
{
InitializeComponent();
}
private void launchapp()
{
Process p = new Process();
p.StartInfo.FileName = @"C:\Program
Files\app\directory\app.exe";
p.StartInfo.WorkingDirectory = @"C:\Program
Files\app\directory\";
p.StartInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden;
p.Start();
return;
}
private void Form1_Load(object sender, EventArgs e)
{
launchapp();
// window handle, place window at top of Z order, WP left,
WP top, width, height
int handle = FindWindow(null, "Title of Application
Window");
SetWindowPos((IntPtr)handle, (IntPtr)HWND_TOP, 5, 5, 5,
5,0);
}
}
}