设置外部应用程序的屏幕位置 [英] set screen position of external application

查看:114
本文介绍了设置外部应用程序的屏幕位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你们能帮助我如何启动外部应用程序并在屏幕上设置屏幕位置或位置吗?不知道从哪里开始。谢谢。

Can you guys help me on how to launch an external application and set its screen position or location on the screen? Don't know whereto start. Thanks.

推荐答案

要移动任何窗口,您需要知道它的句柄( HWND )。然后你可以使用原始的Windows API SetWindowPos

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545%28v=vs.85%29.aspx [ ^ ]。



要在.NET代码中使用它,你可以使用P / Invoke:

< a href =http://en.wikipedia.org/wiki/P/Invoke> http://en.wikipedia.org/wiki/P/Invoke [ ^ ],

http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp [ ^ ]。



对于此功能,一切都已为您完成: http: //www.pinvoke.net/default.aspx/user32.setwindowpos [ ^ ]。



哪里可以获得HWND?如果这是一个主要的应用程序窗口,你想从你的代码开始,这不是太难。您启动该过程并返回其类型 System.Diagnostics.Process 的.NET包装器:

http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process [<一个href =http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.processtarget =_ blanktitle =New Window> ^ ],

http://msdn.microsoft.com/en-us/ library / vstudio / system.diagnostics.process.start [ ^ ]。



拥有进程的实例,您可以获取其主窗口的句柄: http:// msdn。 microsoft.com/en-us/library/vstudio/system.d iagnostics.process.mainwindowhandle [ ^ ]。



有一个小问题:即使存在这样的窗口(如你所知,它可能不存在,但是你的问题是关于你拥有它并希望移动的情况:-)),调用此属性可能会抛出异常。之所以会发生这种情况,是因为当应用程序尚未就绪时,您可能会过早地调用此属性(是的,读取某些属性的值是对其 getter 的调用)。然后你需要等待一段时间再尝试再次调用这个属性,直到得到窗口句柄。当您需要将调用置于try-catch块的try块下时,这是相对罕见的情况之一,并且在catch块中,停止传播异常。我通常称这种技术很脏,仅用于补偿某些坏API的缺陷,但情况确实如此。]



我也想知道通过运行它来使用其他应用程序通常是一个非常糟糕的主意,特别是如果这是一个窗口应用程序。流程很好地隔离,很少为此类协作而设计。这些技巧的结果通常是丑陋的并且不可靠。在您自己的应用程序中拥有所需的一切要好得多,它可以使用一些库,.NET程序集或非托管DLL。



-SA
To move any window, you need to know its handle (HWND). Then you can use raw windows API SetWindowPos:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545%28v=vs.85%29.aspx[^].

To use it in .NET code, you can use P/Invoke:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

For this function, everything is already done for you: http://www.pinvoke.net/default.aspx/user32.setwindowpos[^].

Where can you get the HWND? If this is a main application window you want to start from your code, this is not too hard. You start the process and return its .NET wrapper of the type System.Diagnostics.Process:
http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process[^],
http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.start[^].

Having the instance of the Process, you can get the handle of its main window: http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.mainwindowhandle[^].

There is one little problem with that: even if such window exist (as you understand, it may not exist, but your question is about the case when you have it and want to move :-)), calling this property can throw an exception. It can happen because you may call this property (yes, reading the value of some property is a call, a call to its getter) too early, when the application is not ready yet. Then you would need to wait some time and try to call this property again, until you get the window handle. This is one of the relatively rare cases when you need to put the call under the try block of the try-catch block, and in the catch block, stop propagating of the exception. I usually call this technique dirty, used only to compensate for defects of some "bad" API, but this is exactly the case.]

I also want to know that using some other application by running it is usually a pretty bad idea, especially if this is a windowed application. Processes are well isolated and rarely designed for such collaboration. Results of such tricks are usually ugly and not reliable. It's much better to have everything you need in your own application, which can use some libraries, .NET assemblies or unmanaged DLLs.

—SA


按照讨论:外部应用程序窗口位置 [ ^ ]


这篇关于设置外部应用程序的屏幕位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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