如何将选定的应用程序窗口从列表框中移到前面

时间:2015-03-07 04:40:54

标签: c# winforms c#-4.0 listbox double-click

我有一个Windows窗体应用程序。在列表框中,我显示所有打开的窗口的标题。我已经做到了这一点。现在我想双击列表框中的标题..这将激活该窗口并将显示在前面。我的代码是这样的::

    //this function is loading all the opened windows in the listbox 

    public void LoadOpenedWindows()
    {
        Process[] processlist = Process.GetProcesses();

        foreach (Process process in processlist)
        {
            if (!String.IsNullOrEmpty(process.MainWindowTitle))
            {
                listBox1.Items.Add(process.MainWindowTitle);

           }
        }
        listBox1.DoubleClick += new EventHandler(ListBox1_DoubleClick);

    }

我尝试按以下方式打开所选项目..但这不起作用..

    private void ListBox1_DoubleClick(object sender, EventArgs e)
    {
        if (listBox1.SelectedItem != null)
        {
            //MessageBox.Show(listBox1.SelectedItem.ToString());
            const uint SW_SHOW = 5;
            const int SW_RESTORE = 5;
            string selected = listBox1.SelectedItem.ToString();
            IntPtr handleOfSelected = getHandle(selected);
            ShowWindowAsync(handleOfSelected, SW_RESTORE);
            SetForegroundWindow(handleOfSelected);
            //BringWindowToTop(handleOfSelected);
            //ShowWindow(handleOfSelected, SW_SHOW);
        }
    }

    public IntPtr getHandle(string selectedItem)
    {
        IntPtr hWnd = IntPtr.Zero;
        foreach (Process pList in Process.GetProcesses())
        {
            if (pList.MainWindowTitle.Contains(selectedItem))
            {
                hWnd = pList.MainWindowHandle;
            }
        }
        return hWnd;
    }

如果有人有任何想法或一段代码..请尝试帮助。

1 个答案:

答案 0 :(得分:1)

这是一个很好的工作......

using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SOF_ProcessFront
{
public partial class Form1 : Form
{
    const UInt32 WS_MAXIMIZE = 365887488;
    const int GWL_STYLE = -16;
    [DllImport("user32.dll")]
    static extern bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool ShowWindow(IntPtr wHnd, int cmdShow);
    [DllImport("user32.dll", SetLastError = true)]
    static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    public Form1()
    {
        InitializeComponent();
        LoadOpenedWindows();
    }

    void bringProcessToFront(int pid)
    {
        Process proc = Process.GetProcessById(pid);
        int style = GetWindowLong(proc.MainWindowHandle, GWL_STYLE);
        ShowWindow(proc.MainWindowHandle,
            (style & WS_MAXIMIZE) == WS_MAXIMIZE ? 3 : 9 );
        SetForegroundWindow(Process.GetProcessById(pid).MainWindowHandle);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        bringProcessToFront(0);
    }

    public void LoadOpenedWindows()
    {
        Process[] processlist = Process.GetProcesses();
        foreach (Process process in processlist)
            if (!String.IsNullOrEmpty(process.MainWindowTitle))
                listBox1.Items.Add(new ProcessAttributes()
                {
                    ProcessName = process.MainWindowTitle,
                    ProcessID = process.Id
                });
        listBox1.DisplayMember = "ProcessName";
        listBox1.DoubleClick += listBox1_DoubleClick;
    }

    void listBox1_DoubleClick(object sender, EventArgs e)
    {
        bringProcessToFront(((ProcessAttributes)listBox1.SelectedItem).ProcessID);
    }

    class ProcessAttributes
    {
        public string ProcessName { get; set; }
        public int ProcessID { get; set; }
    }
}
}

这里用于WS_MAXIMIZE的常量正在为我工​​作@ Win 8.1 64位 我不知道你是否能找到你系统的号码。

ShowWindow函数设置Application的WindowState。即我们需要将其状态从最小化恢复到正常或最大化。 ShowWindow函数的第二个参数询问要设置的状态。即3为最大化,9为恢复其状态。如果应用程序未最小化并且处于最大化状态,我想将3作为参数传递。如果没有,那么该函数将尝试恢复最大化窗口,这迫使它从最大化到正常。您也可以在窗口最小化时调用showWindow函数。 Cauz应该恢复最小化的窗口以显示。之后,SetForegroundWindow将窗口置于前面。