C#如何查找所有正在运行的窗体及其标题

解决方案 »

  1.   

      foreach (Process p in Process.GetProcessesByName("baiduhi"))
                {
                    try
                    {
                        Console.WriteLine("ModuleName={0},WindowTitle={1}", p.MainModule.ModuleName, p.MainWindowTitle);
                    }
                    catch
                    {
                    }
                }
      

  2.   

    谢谢。实际上是这样的,我想调用迅雷的组件每天定时下载一些资料,但迅雷每下载一个文件都会弹出一个对话框,对话框中有个“立即下载”的按钮,尽管不点此按钮下载也会继续,但是我的程序在下载过程不能得到任何信息,且此对话框在下载完后不会自动关闭。因此我想在软件中查找该对话框,再查找对话框中的“立即下载”按钮,然后激发CLICK事件。网上有相关的帖子,但不管用。帖子的代码如下:
    using   System.Runtime.InteropServices;   //这个肯定要的 Dll   Import#region   Dll   Import   
      
            [DllImport( "User32.dll ",EntryPoint= "FindWindow ")]   
            private   static   extern   IntPtr   FindWindow(string   lpClassName,   
    string   lpWindowName);   
      
            [DllImport( "user32.dll ",EntryPoint= "FindWindowEx ")]   //找子窗体 
            private   static   extern   IntPtr   FindWindowEx(IntPtr   hwndParent,   
    IntPtr   hwndChildAfter,   string   lpszClass,   string   lpszWindow);   
      
            [DllImport( "User32.dll ",EntryPoint= "SendMessage ")]   //用于发送信息给窗体 
            private   static   extern   int   SendMessage(IntPtr   hWnd,   
    int   Msg,   IntPtr   wParam,   string   lParam);   
      
            #endregion   
    //这个是DLL导入 
      private   int   SearchWindow()   
            {   
                    string   lpszParentClass   =   "#32770 ";   //整个窗口的类名   
                    string   lpszParentWindow   =   "保存网页 ";   //窗口标题   
                    string   Save_Submit   =   "Button ";   //需要查找的Button的类名   
                  string   Save_type= "Edit "; 
                    string   text   =   " ";   
      
                    IntPtr   ParenthWnd   =   new   IntPtr(0);   
                    IntPtr   EdithWnd   =   new   IntPtr(0);   
      
                    //查到窗体,得到整个窗体   
                    ParenthWnd   =   FindWindow(lpszParentClass,lpszParentWindow);   
      
                    //判断这个窗体是否有效   
                    if   (!ParenthWnd.Equals(IntPtr.Zero))   
                    {   
                          MessageBox.show( "保存网页对话框存在 "); 
                      else 
                  {MessageBox.show( "保存网页对话框不存存在 ");} 
                              //得到User   Name这个子窗体,并设置其内容   
                          EdithWnd   =   FindWindowEx(ParenthWnd,EdithWnd,Save_Submit, " ");   //获取button句柄 
                          if(!EditWnd.Equals(IntPtr.Zero)) 
                          MessageBox.show( "保存按扭存在 ");   
                          else 
                        {MessageBox.show( "保存按扭不存在 ");} 
                          EdithWnd   =   FindWindowEx(ParenthWnd,EdithWnd,Save_type, " ");   //获取button句柄 
                          if(!EditWnd.Equals(IntPtr.Zero)) 
                          MessageBox.show( "文件类型框存在 ");   
                          else 
                        {MessageBox.show( "文件类型框不存在 ");} 

    先打开一个网页保存对话框,然后运行程序 
    private   void   button1_Click(object   sender,   System.EventArgs   e) 
    {SearchWindow()   ; 
      

  3.   

            private void ClosePopWindow(object obj)
            {
                //这些用spy++可以看到
                string lpszParentClass = "#32770"; //整个窗口的类名 
                string lpszParentWindow = "新建任务"; //窗口标题 
                string lpszClass_Submit = "Button"; //需要查找的Button的类名 
                string lpszName_Submit = "立即下载"; //需要查找的Button的标题                      IntPtr ParenthWnd = new IntPtr(0);
                IntPtr EdithWnd = new IntPtr(0);
                int i = 0;
                string stext="";
                while (true)
                {
                    //查到窗体,得到整个窗体 
                    ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow);
                    //判断这个窗体是否有效 
                    while(!ParenthWnd.Equals(IntPtr.Zero))
                    //if (!ParenthWnd.Equals(IntPtr.Zero))
                    {                   ////得到Button这个子窗体,并触发它的Click事件
                        EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass_Submit, lpszName_Submit);
                        if (!EdithWnd.Equals(IntPtr.Zero))
                        {
                            SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0");
                        }
                        return;
                    }
                    Thread.Sleep(1000);
                    i++;
                    //   Console.WriteLine("第"+i.ToString()+"次检查"); 5秒都没显示出来就推出循环
                    if (i > 5)
                    {
                        break;
                    }
                }
            }
         ThreadPool.QueueUserWorkItem(new WaitCallback(ClosePopWindow));