如何用C#获得指定进程的窗口句柄!
不用FindWindow,而是根据进程PID获得窗口句柄!
如何实现,代码更好,回答有分
(只能给100不够另开帖补)

解决方案 »

  1.   

     IntPtr p=   System.Diagnostics.Process.GetProcessById(pid).MainWindowHandle;
      

  2.   

     to jinjazz
    给出的方法我试过了,部分情况下能获得句柄,有的就不能获得。(比如QQ),请问这个怎么解决呢。
    【50分】给你和ojlovecd 
      

  3.   

    API:
    [DllImport("kernel32.dll")] 
    public static extern IntPtr OpenProcess(UInt32 dwDesiredAccess,Int32 bInheritHandle,UInt32 dwProcessId) [DllImport("kernel32.dll")] 
    public static extern Int32 CloseHandle(IntPtr hObject)  这个OpenProcess函数的功能是获取一个已经存在的进程的句柄, 
    在C#中已经集成了对此函数的托管类System.Diagnostics.Process类 
    而且使用起来要比这个函数方便的多 
    比如获取本机上的所有进程: 
    System.Diagnostics.Process[] ps=System.Diagnostics.Process.GetProcesses(); 
    此时ps是Process 类型的数组,即每个元素是一个Process类的对象 
    还有根据进程ID找到此进程: 
    System.Diagnostics.Process p=System.Diagnostics.Process.GetProcessById(); 
    根据进程名称找到此进程: 
    System.Diagnostics.Process p=System.Diagnostics.Process.GetProcessesByName(); 
    获取进程句柄: 
    System.IntPtr ProcHandle=p.Handle; 还有参考下,不知道对你有没有用!
    http://hi.baidu.com/jerry_ma/blog/item/a6605966cd50a620ab184c83.html
      

  4.   

    多谢 ,Guyschaos
    你给出的东西,我先研究下,下午5点结贴 
      

  5.   

    贴个好玩的, 接分
    ///////////////
    namespace notepadinsp
    {
      class inspector
      {
        static void Main()
        {
          Process[] procs = Process.GetProcessesByName("bqq3.1.5");
          if (procs.Length != 0)
          {
              IntPtr hwnd = procs[0].MainWindowHandle;
              // do something with the handle
              //...
              MessageBox.Show(new WindowWrapper(hwnd), "Hello World!");
          }
          else
              MessageBox.Show("bqq is not running.");
        }
      }
      public class WindowWrapper : System.Windows.Forms.IWin32Window
      {
          public WindowWrapper(IntPtr handle)
          {
              _hwnd = handle;
          }
      
          public IntPtr Handle
          {
              get { return _hwnd; }
          }
      
          private IntPtr _hwnd;
      }
    }
      

  6.   

    用EnumWindows试下:
    public delegate bool EnumWindowsProcCallBack(int hwnd, int lParam); [DllImport("user32.dll")] 
    private static extern int GetWindowText(int hWnd, StringBuilder title, int size);
    [DllImport("user32.dll")] 
    private static extern bool IsWindowVisible(int hWnd);
    [DllImport("user32.dll")] 
    public static extern int EnumWindows(EnumWindowsProcCallBack proc, int lParam); 
    [DllImport("user32.dll")]
    public static extern int GetWindowThreadProcessId (int hwnd, ref int lpdwProcessId);
    public void test()

    EnumWindowsProcCallBack EnumWindowsProc  = new EnumWindowsProcCallBack(myEnumWindowsProc); 
    EnumWindows(EnumWindowsProc, 0); 
    }
    //call back function
    public static bool myEnumWindowsProc(int hwnd, int lParam) 

    //add you code here
    if(IsWindowVisible(hWnd))
    {  
    int processId;
    GetWindowThreadProcessId(hwnd, processId);//取进程ID,跟你的PID比较。
    if(processId == yourProcessID)
    {
    return false; //停止枚举
    }

    //StringBuilder title = new StringBuilder(255);
    //GetWindowText(hWnd, title, 255);
    //Console.Write(title);
    }
    return true;
    }