我可能打开好几个应用程序,当我随便打开一个应用程序的窗口时,我如何获得该窗口的句柄呢???谢谢请指教!!!

解决方案 »

  1.   

    研究下 FindWindow 这个函数。
      

  2.   

    研究下 FindWindow 这个函数。
      

  3.   

    研究下“FindWindow”这个函数。
      

  4.   

    HWND hWnd;
    hWnd = ::FindWindow("窗口类名如:Dialog" , "要得到窗口的标题");
    不过,如果不是很特别的窗口的话,在巧合的情况下,可能会出现,其他的窗口的类名,和标题与你要得到的那个窗口的相同,那么这个句柄
    就不是很确定了。
      

  5.   

    HWND hWnd;hWnd = ::FindWindow("窗口的类名,如Dialog" , "窗口标题");如果在打开的所有窗口中,有与你要得到的窗口的类名和窗口标题相同的情况下,那么你要得到的窗口句柄,可能就不是你
    想要的了。这个窗口的类名可以通过VC的spy++查看得到.
      

  6.   

    如楼上的,用GetActiveWindow,此外还可以在消息处理函数中获取.
      

  7.   

    如果你想要得到句柄的那个窗口就是当前活动窗口的话,那就用GetActiveWindow(),如果不是的话,那还是试试FindWindow吧。
      

  8.   

    GetActiveWindow()不行 这个是得到当前活动窗口句柄当前程序的 我想获得其他自己打开的程序窗口的句柄我先试试FindWindow
      

  9.   

    FindWindow也不行啊 我开始的意思是桌面上的任意几个打开的应用程序的窗口而不是同一程序中的不同窗口, 请指点一下!
      

  10.   

    GetActiveWindow好象不行,他获得的是本程序的当前窗口的句柄.
      

  11.   

    建议LZ这样做:用循环,先FindWindow或者每个窗口的句柄,然后GetWindowThreadProcessId获得每个窗口对应的线程和进程ID,用OpenProcess和GetModuleFileNameEx获得进程对应的EXE文件名及路径。
      

  12.   

    HWND GetForegroundWindow(VOID)
    也能行
      

  13.   

     [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
            static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    /// <summary>
            /// 获取有名字窗体句柄
            /// </summary>
            /// <param name="caption">窗体名</param>
            /// <param name="delay">循环查询次数</param>
            /// <param name="maxTries">停顿时间</param>
            /// <returns>句柄 IntPtr</returns>
            static IntPtr FindMainWindowHandle(String caption,int delay,int maxTries)
            {
                IntPtr mwh = IntPtr.Zero;
                bool foundWindow = false;
                int attempts = 0;            do
                {
                    mwh = FindWindow(null, caption);
                    if (mwh == IntPtr.Zero)
                    {
                        Console.WriteLine("Form not yet found\n");
                        Thread.Sleep(maxTries);
                        ++attempts;
                    }
                    else
                    {
                        Console.WriteLine("Form has been found");
                        foundWindow = true;
                    }
                }
                    while(!foundWindow && attempts<delay);                if(mwh != IntPtr.Zero)
                        return mwh;
                    else
                        throw new Exception("Coule not find Main Window");
            }
      

  14.   

    循环,FindWindow每个窗口的句柄,GetWindowThreadProcessId每个窗口对应的线程和进程ID,OpenProcess和GetModuleFileNameEx进程对应EXE文件名及路径。正解!
      

  15.   

    先在目标进程中注入一个 DLL ,再在DLL中用GetActiveWindow .此方法,可能被杀毒软件杀,呵呵。不要干坏事。
      

  16.   

    如果是在MFC中直接使用变量m_hWnd就可以
      

  17.   

    #19 楼正解。我已经测试过了。很好用。
    如果想用于是否等于你的程序,即为::GetForeWindow()==m_hWnd