强制窗口位置和大小 [英] Force window position and size

查看:106
本文介绍了强制窗口位置和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.

我正在使用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,请参阅 http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle.aspx [
请参阅:
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 use System.Diagnostics.Process, see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

When a process is started successfully, use the property if the Process instance Process.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 API MoveWindow, SetWindowPos or SetWindowPlacement 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆