简体   繁体   English

如何获得应用程序窗口的顶部

[英]How to get application window to top

Using C#, I would like to bring a specific window to the top of the screen, and after that I will run a macro on it. 使用C#,我想在屏幕顶部显示一个特定的窗口,然后在其上运行一个宏。

Tried, 试过了

[DllImportAttribute("User32.dll")]
private static extern int FindWindow(String ClassName, String WindowName);
[DllImportAttribute("User32.dll")]
private static extern int SetForegroundWindow(int hWnd);

and after, 之后,

        string main_title;
        Process[] processlist = Process.GetProcesses();
        foreach (Process theprocess in processlist)
        {
            if (theprocess.ProcessName.StartsWith(name))
            {
                main_title = theprocess.MainWindowTitle;
                hWnd = theprocess.Handle.ToInt32();
                break;
            }
            hWnd = FindWindow(null, main_title);
            if (hWnd > 0)
            {
                SetForegroundWindow(hWnd);
            }

and got the error, 并得到了错误,

the type or namespace name 'DllImportAttribute' could not be found 找不到类型或名称空间名称“ DllImportAttribute”

used, 用过的,

using System.Runtime;

and now im unable to get hWnd recognized in my code, although they all are in the same namespace and partial class. 现在我无法在我的代码中识别hWnd,尽管它们都在同一个名称空间和部分类中。

After trials, 经过审判

   IntPtr hWnd;
    Process[] processlist = Process.GetProcesses();
    foreach (Process theprocess in processlist)
    {
        if (theprocess.ProcessName.StartsWith("msnmsgr"))
        {
            main_title = theprocess.MainWindowTitle;
            hWnd = theprocess.MainWindowHandle;
            SetForegroundWindow(hWnd);

        }
    }

this looks like foregrounds the window, at least according to alt-tab order and applications looks like selected from toolbar, but it doesn't become visible. 至少按alt-tab顺序,这看起来像是窗口的前景,并且应用程序看起来像是从工具栏选择的,但它不可见。

Try using DllImport instead, like this: 尝试改为使用DllImport,如下所示:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.DLL")]
public static extern IntPtr FindWindow(string lpszClass, string lpszWindow);

Define hWnd like: 像这样定义hWnd:

IntPtr hWnd;

In your assignment do: 在您的作业中:

hWnd = theProcess.MainWindowHandle;  // or use your theProcess.Handle

Also, that break in your if statement will not set the window to foreground if it finds it. 另外,如果if语句中的该中断找到它,则不会将其设置为前台。 You need to re-work that code segment to something like: 您需要将该代码段重新处理为类似以下内容的代码:

if (theprocess.ProcessName.StartsWith(name))
{
    main_title = theprocess.MainWindowTitle;
    hWnd = theProcess.MainWindowHandle;
}
else
    hWnd = FindWindow(null, main_title);

if (hWnd != null && !hWnd.Equals(IntPtr.Zero))
{
    SetForegroundWindow(hWnd);
    // may want to put your break here
}

Window Class 窗类

Win32 is just a class that contains all the DllImport's described above, as I use them in multiple Win32 child classes. Win32只是一个包含上述所有DllImport的类,因为我在多个Win32子类中使用了它们。

public class Window : Win32
{
    public IntPtr Handle;

    public Window(IntPtr handle)
    {
        Handle = handle;
    }

    public bool Visible
    {
        get { return IsWindowVisible(Handle); }
        set
        {
            ShowWindow(Handle, value ? ShowWindowConsts.SW_SHOW :
                ShowWindowConsts.SW_HIDE);
        }
    }

    public void Show() 
    { 
        Visible = true;
        SetForegroundWindow(Handle);
        /*
        try { SwitchToThisWindow(Handle, false); } // this is deprecated - may throw on new window platform someday
        catch { SetForegroundWindow(Handle); } // 
        */
    }

    public void Hide() { Visible = false; }
}

The ShowWindowConsts class ShowWindowConsts类

namespace Win32
{
    public class ShowWindowConsts
    {
        // Reference: http://msdn.microsoft.com/en-us/library/ms633548(VS.85).aspx

        /// <summary>
        /// Minimizes a window, even if the thread that owns the window is not responding. 
        /// This flag should only be used when minimizing windows from a different thread.
        /// </summary>
        public const int SW_FORCEMINIMIZE = 11;

        /// <summary>
        /// Hides the window and activates another window.
        /// </summary>
        public const int SW_HIDE = 0;

        /// <summary>
        /// Maximizes the specified window.
        /// </summary>
        public const int SW_MAXIMIZE = 3;

        /// <summary>
        /// Minimizes the specified window and activates the next top-level window in the Z order.
        /// </summary>
        public const int SW_MINIMIZE = 6;

        /// <summary>
        /// Activates and displays the window. 
        /// If the window is minimized or maximized, the system restores it to 
        /// its original size and position. 
        /// An application should specify this flag when restoring a minimized window.
        /// </summary>
        public const int SW_RESTORE = 9;

        /// <summary>
        /// Activates the window and displays it in its current size and position.
        /// </summary>
        public const int SW_SHOW = 5;

        /// <summary>
        /// Sets the show state based on the public const long SW_ value specified in 
        /// the STARTUPINFO structure passed to the CreateProcess function by 
        /// the program that started the application.
        /// </summary>
        public const int SW_SHOWDEFAULT = 10;

        /// <summary>
        /// Activates the window and displays it as a maximized window.
        /// </summary>
        public const int SW_SHOWMAXIMIZED = 3;

        /// <summary>
        /// Activates the window and displays it as a minimized window.
        /// </summary>
        public const int SW_SHOWMINIMIZED = 2;

        /// <summary>
        /// Displays the window as a minimized window. 
        /// This value is similar to public const long SW_SHOWMINIMIZED, 
        /// except the window is not activated.
        /// </summary>
        public const int SW_SHOWMINNOACTIVE = 7;

        /// <summary>
        /// Displays the window in its current size and position. 
        /// This value is similar to public const long SW_SHOW, except that the window is not activated.
        /// </summary>
        public const int SW_SHOWNA = 8;

        /// <summary>
        /// Displays a window in its most recent size and position. 
        /// This value is similar to public const long SW_SHOWNORMAL, 
        /// except that the window is not activated.
        /// </summary>
        public const int SW_SHOWNOACTIVATE = 4;

        public const int SW_SHOWNORMAL = 1;
    }
}

So your code would become: 因此,您的代码将变为:

if (theprocess.ProcessName.StartsWith(name))
{
    main_title = theprocess.MainWindowTitle;
    hWnd = theProcess.MainWindowHandle;
}
else
    hWnd = FindWindow(null, main_title);

if (hWnd != null && !hWnd.Equals(IntPtr.Zero))
{
    new Window(hWnd).Show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM