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

问题描述

我有一个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.

推荐答案

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

    }
}



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

09-25 06:12