强制窗口位置和大小 [英] Force window position and size
本文介绍了强制窗口位置和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好.
我正在使用VB 2010 Express.我正在使用Shell("C:\ Path \ Program.exe")打开一个商业程序,并且效果很好.如何使该程序始终在第二个监视器上打开(扩展桌面模式)并设置其窗口大小?
谢谢,
Jim
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
推荐答案
我建议使用System.Diagnostics.Process
,请参阅 ^ ].
成功启动进程后,请使用Process
实例Process.MainWindowHandle
的属性,请参见 ^ ].
请注意此属性引发的异常.确保在处理尚未启动,尚未终止等情况时立即处理所有异常,以检测情况.在这种情况下,请稍后再试(是的,效果并不理想,但是您没有其他事件或条件).
获取窗口句柄时,请使用P/Invoked原始Windows APIMoveWindow
,SetWindowPos
或SetWindowPlacement
或它们的组合来移动应用程序.我建议使用这些变体,因为您可能需要将窗口置于正确的Z顺序位置,调整其大小并处理应用程序不情愿"地假定确切需要的大小时的情况,等等.
请参阅:
http://msdn.microsoft.com/en-us/library/system. diagnostics.process.mainwindowhandle.aspx [ ^ ],
http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms633545%28v=VS.85%29.aspx [ http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms633544%28v=VS.85%29.aspx [ http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms632611%28v=VS.85%29.aspx [ http://en.wikipedia.org/wiki/P/Invoke [ http://msdn.microsoft.com/en-us/library/Aa712982 [ ^ ].
在这里,您将能够找到这些功能和其他功能的已调用代码: http://www.pinvoke.net/ [ ^ ].
另外,此CodeProject文章可能会有所帮助:基本P/调用 [
I would advise to useSystem.Diagnostics.Process
, see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].
When a process is started successfully, use the property if theProcess
instanceProcess.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 APIMoveWindow
,SetWindowPos
orSetWindowPlacement
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
这篇关于强制窗口位置和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文