在C#中的位置窗口 [英] Position Window in c#

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

问题描述

我正在尝试创建一个带有按钮的窗体,单击该窗体将搜索calc.exe,如果该窗体运行,它将calc窗口定位到屏幕左上角的0,0.如果它没有运行,它将启动calc,然后将其定位到屏幕左上角的0,0.我正在学习C#,因此请注释掉它.

I am trying to create a form with a button that when click will search for calc.exe, if its running, it positiones the calc window to 0,0 top left corner of the screen. if its not running, it launces calc and then positions it to 0,0 top left of screen. Im learning C# so please comment the heck out of it

推荐答案

这就像伪代码一样.您可以通过执行以下步骤来解决它.

This just like a pseudo code. You can solve it by using the following steps.

HWND iHandle = FindWindow(null, "Calculator");
            if (iHandle != IntPtr.Zero)
            {
                SetForegroundWindow(iHandle);
                Rectangle rect = new Rectangle();
                GetWindowRect(iHandle, ref rect);
                // set the position as required 
            }
            else 
            {
               Process runExe = new Process();
               runExe.StartInfo.FileName = "calc.exe";
               Process.Start("calc.exe");
               // set the position as required 
             }


可以使用System.Diagnostics.Process.Start启动正在运行的"calc.exe"进程.这是一个实例方法,因此您应该有一个先前从构造函数创建的System.Diagnostics.Process实例,或者它是返回此类实例的静态方法.保留对Process实例的引用以供将来使用.

您可以尝试使用System.Diagnostics.Process.GetProcessesByName方法查找正在运行的进程的实例.

这样,您便获得了外部运行进程的实例,无论它是在启动之前启动还是启动了.使用Process.MainWindowHandle获取其主窗口句柄.请参见此方法的说明:您将需要捕获指定的异常,并检查返回的句柄不是System.IntPtr.Zero.这表明进程已终止或尚未准备就绪,或者尚未创建主窗口.如果该过程没有终止,则可能需要再次调用此属性,直到获得主窗口.

请参阅: http://msdn.microsoft.com/en-us/library/system .diagnostics.process.aspx [ ^ ].

当您获得主窗口句柄时,请使用原始Windows API (已调用P/Invoked)移动窗口.
这是现成的P/Invoked代码: http://pinvoke.net/default.aspx/coredll /SetWindowPos.html [ ^ ] .

原始API: http://msdn.microsoft.com /en-us/library/windows/desktop/ms633545%28v=vs.85%29.aspx [ http://en.wikipedia.org/wiki/P/Invoke [ http://msdn.microsoft.com/en-us/library/Aa712982 [ ^ ].

有用的CodeProject文章:基本P/调用 [
The running "calc.exe" process can be started using System.Diagnostics.Process.Start. This is either an instance method, so you should have an instance of System.Diagnostics.Process previously created from a constructor, or it is a static method returning such instance. Keep this reference to the Process instance for future use.

You can try to find an instance of the running process using the method(s) System.Diagnostics.Process.GetProcessesByName.

This way, you got an instance of the external running process, no matter if it was started before or you started it. Get its main window handle using Process.MainWindowHandle. See the description of this method: you will need to catch the specified exceptions and check that the returned handle is not System.IntPtr.Zero. It would indicate that a process is terminated or not yet ready, or the main windows was not yet created. If the process is not terminated, you may need to call this property again until you get a main window.

Please see: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

When you get the main window handle, move the the window using raw Windows API , P/Invoked.
This is the ready-to-use P/Invoked code: http://pinvoke.net/default.aspx/coredll/SetWindowPos.html[^].

Raw API: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545%28v=vs.85%29.aspx[^]; see other window-related API in the menu on left.

See also:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/en-us/library/Aa712982[^].

A useful CodeProject article: Essential P/Invoke[^].

—SA


这篇关于在C#中的位置窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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