积极答复者
怎么设置外部程序的窗口大小和位置

问题
答案
-
现在关键问题是,我的p.MainWindowHandle等于0
http://msdn.microsoft.com/zh-cn/library/system.diagnostics.process.mainwindowhandle(v=vs.110).aspx
请具体的看一下解释。
TopLevel 窗体)。</sentencetext> Refresh 方法刷新 Process 对象获取当前主窗口句柄。</sentencetext>
MainWindowHandle 属性。</sentencetext> MainWindowHandle 属性是一个唯一标识与进程关联的窗口的值。</sentencetext>
<sentencetext xmlns="http://www.w3.org/1999/xhtml">仅当进程有图形界面时,该进程才具有与其关联的主窗口。</sentencetext> MainWindowHandle 值为零。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">对于已隐藏的进程,也就是任务栏里不可见的进程,该值也为零。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">这可以是任务栏最右端通知区域中显示为图标的进程的事例。</sentencetext>
WaitForInputIdle 方法让该进程完成启动,从而确保创建了主窗口句柄。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">否则,将引发异常。</sentencetext>
平台说明:.UseShellExecute set to true. " id="mt17" xml:space="preserve"><sentencetext xmlns="http://www.w3.org/1999/xhtml">如果在启动进程时 ProcessStartInfo.UseShellExecute 设置为 true,则此属性在此平台上不可用。</sentencetext>
- 已标记为答案 CaillenModerator 2014年3月10日 2:07
全部回复
-
Process.Start会返回一个Process对象,该Process对象的MainWindowHandle属性是显示的窗口的句柄,利用SetWindowPos函数可以根据窗口句柄设置窗口的大小,有关SetWindowPos函数可以参考以下链接:
-
IE不可,它會從上次關閉的IE的視窗大小,來打開下一個的大小。
如果個別程式接受 SIZE的input parameter,那才可以呼叫時定下它打開的大小
大家一齊探討、學習和研究,謝謝!
MCSD, MCAD, MCSE+I, MCDBA, MCDST, MCSA, MCTS, MCITP, MCPD,
MCT, Microsoft Community Star(TW & HK),
Microsoft MVP for VB.NET since 2003
My MSMVP Blog -
噢!原來可以用Win32 API的,在這裡真的學習學習的好地方。
大家一齊探討、學習和研究,謝謝!
MCSD, MCAD, MCSE+I, MCDBA, MCDST, MCSA, MCTS, MCITP, MCPD,
MCT, Microsoft Community Star(TW & HK),
Microsoft MVP for VB.NET since 2003
My MSMVP Blog -
你好:
我使用MoveWindow这个函数是可以实现这个功能的。参考一下这个代码:
[DllImport("user32.dll", SetLastError = true)] internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); static void Main(string[] args) { Process p = Process.Start("NotePad.exe"); MoveWindow(p.MainWindowHandle, 200, 200, 50, 50, true); Console.Read(); }
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. -
现在关键问题是,我的p.MainWindowHandle等于0
http://msdn.microsoft.com/zh-cn/library/system.diagnostics.process.mainwindowhandle(v=vs.110).aspx
请具体的看一下解释。
TopLevel 窗体)。</sentencetext> Refresh 方法刷新 Process 对象获取当前主窗口句柄。</sentencetext>
MainWindowHandle 属性。</sentencetext> MainWindowHandle 属性是一个唯一标识与进程关联的窗口的值。</sentencetext>
<sentencetext xmlns="http://www.w3.org/1999/xhtml">仅当进程有图形界面时,该进程才具有与其关联的主窗口。</sentencetext> MainWindowHandle 值为零。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">对于已隐藏的进程,也就是任务栏里不可见的进程,该值也为零。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">这可以是任务栏最右端通知区域中显示为图标的进程的事例。</sentencetext>
WaitForInputIdle 方法让该进程完成启动,从而确保创建了主窗口句柄。</sentencetext> <sentencetext xmlns="http://www.w3.org/1999/xhtml">否则,将引发异常。</sentencetext>
平台说明:.UseShellExecute set to true. " id="mt17" xml:space="preserve"><sentencetext xmlns="http://www.w3.org/1999/xhtml">如果在启动进程时 ProcessStartInfo.UseShellExecute 设置为 true,则此属性在此平台上不可用。</sentencetext>
- 已标记为答案 CaillenModerator 2014年3月10日 2:07
-
我刚才试了下,估计应该还是句柄的问题,我用以下代码打开并获取句柄,然后移动窗口位置。
Process Now_Process = Process.Start("C:\\Users\\Zhou\\Desktop\\SSCOM32.exe"); Now_Process.WaitForInputIdle(); IntPtr ssHandle = Now_Process.MainWindowHandle; MessageBox.Show(ssHandle.ToString()); bool result1 = SetWindowPos(ssHandle, HWND_TOP, 0, 0, 800, 300, 2);
这样获取的窗口句柄虽然不为0了,但是SetWindowPos或者MoveWindow之后就是窗体移动了但是窗体控件没有移动。而且与我单独在timer里用下列方法获取的窗口句柄的值不一样。
int x = Cursor.Position.X; int y = Cursor.Position.Y; Point p= new Point(x, y); IntPtr ssHandle = WindowFromPoint(p);
我这样获取句柄之后,再利用这个句柄去移动就能得到一个正确的结果。
怎么才能得到用Process.Start()方法打开的那个程序界面的正确句柄呢?