如何预先从列表框中提取所选项目 [英] How to bring a selected item from a listbox upfront

查看:81
本文介绍了如何预先从列表框中提取所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a windows form application. In which in a listbox, I am showing title of all the opened windows. I have done upto this. Now I want to double click on a title in the listbox.. and this will activate that window and will show upfront. My code is like this::

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





我试图通过以下方式打开所选项目..但这不起作用..



I tried to open the selected item by the following way.. But that is not working..

private void ListBox1_DoubleClick(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                //MessageBox.Show(listBox1.SelectedItem.ToString());
                const uint SW_SHOW = 5;
                string selected = listBox1.SelectedItem.ToString();
                IntPtr handleOfSelected = getHandle(selected);
                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;
        }





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



If anyone have any idea or piece of code.. Please try to help.

推荐答案

你需要调用ShowWindowAsync。



使用以下签名:

You need to call ShowWindowAsync.

Use the following signature:
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);





广告你的双击hadler应该是这样的:





Ad your double click hadler should look like this:

private void ListBox1_DoubleClick(object sender, EventArgs e)
{
    if (listBox1.SelectedItem != null)
    {
        const int SW_RESTORE = 9;
        string selected = listBox1.SelectedItem.ToString();
        IntPtr handleOfSelected = getHandle(selected);
        ShowWindowAsync(handleOfSelected, SW_RESTORE);
        SetForegroundWindow(handleOfSelected);

    }
}


我相信条件pList.MainWindowTitle.Contains(selectedItem)并未涵盖所有案例。您可能在标题中有多个具有selectedItem名称的进程,但并非所有进程都可能具有与之关联的窗口。因为这是在foreach循环中,所以只检索找到的最后一个进程句柄。



另外,另一个重要的验证是查看进程是否实际上有一个窗口关联用它。如果MainWindowHandle为0,则没有与之关联的窗口。
I believe the condition "pList.MainWindowTitle.Contains(selectedItem)" does not cover all cases. You may have multiple processes with the selectedItem name in the title, but not all processes may have a window associated with them. Since this is in a foreach loop only the last process handle found will be retrieved.

Also, another important verification would be to see if the process actually has a window associated with it. If MainWindowHandle is 0 there is no window associated with it.


这篇关于如何预先从列表框中提取所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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