以下代码主要想实现枚举桌面窗口,并显示在listview里。通过时钟事件刷新并对listview进行增加删除操作。但是目前我只会新增,不懂得如何去检测窗口是否存在,望各位能帮忙给点思路,谢谢。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace WindowsApplication2
{    
    public partial class Form1 : Form
    {
        #region 枚举窗口
        public delegate bool CallBack(int hWnd, int lParam);
        private static Dictionary<int, string> DictWin = new Dictionary<int, string>();
        [DllImport("user32.dll")]
        public static extern int EnumWindows(CallBack x, int y);
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int GetWindowText(int hWnd, StringBuilder sbWinName, int nMaxCount);
        [DllImport("user32.dll")]
        public static extern int IsWindowVisible(int hWnd);
        [DllImport("user32.dll")]
        public static extern int GetParent(int hWnd);
        #endregion
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            CallBack myCallBack = new CallBack(Report);
            EnumWindows(myCallBack, 0);            
        }
        
        public  bool Report(int hWnd, int lParam)
        {
            if (GetParent(hWnd) == 0 && IsWindowVisible(hWnd) == 1)
            {
                
                StringBuilder sbWinName = new StringBuilder(512);
                GetWindowText(hWnd, sbWinName, sbWinName.Capacity);
                if (sbWinName.Length > 0)
                {
                    if (!DictWin.ContainsKey(hWnd))
                    {
                        DictWin.Add(hWnd, "句柄:"+hWnd.ToString()+"标题:"+sbWinName.ToString());
                        listView1.Items.Add(DictWin[hWnd].ToString());
                    }
                    
                }
                
            }
            return true;
        }
               private void timer1_Tick(object sender, EventArgs e)
        {        }        private void timer2_Tick(object sender, EventArgs e)
        {        }
    }
}