【问题标题】:How to get application window to top如何让应用程序窗口置顶
【发布时间】:2012-01-12 17:45:32
【问题描述】:

使用 C#,我想将一个特定的窗口带到屏幕顶部,然后我将在其上运行一个宏。

试过了,

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

之后,

        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);
            }

得到了错误,

找不到类型或命名空间名称“DllImportAttribute”

用过,

using System.Runtime;

现在我无法在我的代码中识别 hWnd,尽管它们都在同一个命名空间和部分类中。

经过试验,

   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);

        }
    }

这看起来像是窗口的前景,至少根据 alt-tab 顺序,应用程序看起来像是从工具栏中选择的,但它不可见。

【问题讨论】:

  • 看来,您在使用 hWnd 变量之前没有定义它。只需在代码开头的某处添加int hWnd;

标签: c# macros


【解决方案1】:

尝试使用 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);

定义 hWnd 为:

IntPtr hWnd;

在你的任务中:

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

此外,if 语句中的中断不会将窗口设置为前台,如果它找到它。您需要将该代码段重新设计为:

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
}

窗口类

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; }
}

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;
    }
}

所以你的代码会变成:

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();
}

【讨论】:

  • 编辑了代码,看起来我们要去某个地方,但我无法使窗口可见
  • 使可见与突出不同。我将编辑代码以显示该部分。
  • 等待它,谢谢,任何与这些相关的可靠 c# 教程?
  • public Window(IntPtr handle),这个方法的返回类型是什么?
  • 这不是一个方法,而是一个构造函数。请参阅添加到帖子末尾的代码。
猜你喜欢
  • 2011-03-23
  • 2018-02-23
  • 1970-01-01
  • 2018-10-06
  • 2019-05-11
  • 2015-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多