引用:
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId); [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)] private static extern long GetWindowLong(IntPtr hwnd, int nIndex); [DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)] private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong); //private static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong); [DllImport("user32.dll", SetLastError = true)] private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags); [DllImport("user32.dll", SetLastError = true)] private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint); [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)] private static extern bool PostMessage(IntPtr hwnd, uint Msg, long wParam, long lParam); [DllImport("user32.dll")] private static extern int GetWindowRect(IntPtr hwnd, out Rect lpRect); [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)] private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); private const int SWP_NOOWNERZORDER = 0x200; private const int SWP_NOREDRAW = 0x8; private const int SWP_NOZORDER = 0x4; private const int SWP_SHOWWINDOW = 0x0040; private const int WS_EX_MDICHILD = 0x40; private const int SWP_FRAMECHANGED = 0x20; private const int SWP_NOACTIVATE = 0x10; private const int SWP_ASYNCWINDOWPOS = 0x4000; private const int SWP_NOMOVE = 0x2; private const int SWP_NOSIZE = 0x1; private const int GWL_STYLE = (-16); private const int WS_VISIBLE = 0x10000000; private const int WM_CLOSE = 0x10; private const int WS_CHILD = 0x40000000;
类:
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
外部程序句柄和窗体类名:

窗体代码:
public PrintForm(string content) { InitializeComponent(); this.textBox1.Text = content; exePath = System.AppDomain.CurrentDomain.BaseDirectory+ "\\FivePrint\\FivePrint.exe"; //先杀一遍需要打开的文件的进程 FileInfo file = new FileInfo(exePath); string SoftName = file.Name.Replace(file.Extension, string.Empty); Process[] Arr = Process.GetProcessesByName(SoftName); foreach (var p in Arr) { p.Kill(); } //读取appdata下的文件 // var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData); // FileStream fs = new FileStream(path + "\\set.ini", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // StreamReader reader = new StreamReader(fs,Encoding.Default); //var conStr = reader.ReadToEnd(); p = new Process();//新建进程 p.StartInfo.FileName = exePath;//设置进程名字 p.Start(); Rect rect = new Rect(); IntPtr intPtr = new IntPtr(0); //句柄 IntPtr hmain = FindWindow("ThunderRT6FormDC", null);//param: 窗体类名,窗体标题名称 任意一个参数即可 //IntPtr hmain = FindWindow(null, p.MainWindowTitle); //获取其所在位置坐标rect GetWindowRect(hmain, out rect); this.Location = new Point(rect.Top, rect.Left); this.StartPosition = FormStartPosition.Manual; }
浙公网安备 33010602011771号