如何获取当前窗口句柄? 我可能打开好几个应用程序,当我随便打开一个应用程序的窗口时,我如何获得该窗口的句柄呢???谢谢请指教!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 研究下 FindWindow 这个函数。 研究下 FindWindow 这个函数。 研究下“FindWindow”这个函数。 HWND hWnd;hWnd = ::FindWindow("窗口类名如:Dialog" , "要得到窗口的标题");不过,如果不是很特别的窗口的话,在巧合的情况下,可能会出现,其他的窗口的类名,和标题与你要得到的那个窗口的相同,那么这个句柄就不是很确定了。 HWND hWnd;hWnd = ::FindWindow("窗口的类名,如Dialog" , "窗口标题");如果在打开的所有窗口中,有与你要得到的窗口的类名和窗口标题相同的情况下,那么你要得到的窗口句柄,可能就不是你想要的了。这个窗口的类名可以通过VC的spy++查看得到. 如楼上的,用GetActiveWindow,此外还可以在消息处理函数中获取. 如果你想要得到句柄的那个窗口就是当前活动窗口的话,那就用GetActiveWindow(),如果不是的话,那还是试试FindWindow吧。 GetActiveWindow()不行 这个是得到当前活动窗口句柄当前程序的 我想获得其他自己打开的程序窗口的句柄我先试试FindWindow FindWindow也不行啊 我开始的意思是桌面上的任意几个打开的应用程序的窗口而不是同一程序中的不同窗口, 请指点一下! GetActiveWindow好象不行,他获得的是本程序的当前窗口的句柄. 建议LZ这样做:用循环,先FindWindow或者每个窗口的句柄,然后GetWindowThreadProcessId获得每个窗口对应的线程和进程ID,用OpenProcess和GetModuleFileNameEx获得进程对应的EXE文件名及路径。 HWND GetForegroundWindow(VOID)也能行 [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"); } 循环,FindWindow每个窗口的句柄,GetWindowThreadProcessId每个窗口对应的线程和进程ID,OpenProcess和GetModuleFileNameEx进程对应EXE文件名及路径。正解! 先在目标进程中注入一个 DLL ,再在DLL中用GetActiveWindow .此方法,可能被杀毒软件杀,呵呵。不要干坏事。 如果是在MFC中直接使用变量m_hWnd就可以 #19 楼正解。我已经测试过了。很好用。如果想用于是否等于你的程序,即为::GetForeWindow()==m_hWnd CFont中的nOrientation的作用是什么? 谁能不吝啬的告诉我下面函数什么意思或者说啥效果? 关于mfc的一些新手问题 求教一个刷新问题!!! MFC中使用OnOpenDocument打开文件程序崩溃? CAsyncSocket 服务端Close问题 在大连做VC的朋友请进 如何去掉窗口标题上的“无标题”三个字 BCGControlBar静态链接时,程序运行有bug,大家怎么解决的? 关于文件目录拷贝后的验证问题。。。。 如何通过hook BitBlt实现截屏? 如何把一个工程里所有源文件的//注释去掉?,会不会影响向导生成的代码?
hWnd = ::FindWindow("窗口类名如:Dialog" , "要得到窗口的标题");
不过,如果不是很特别的窗口的话,在巧合的情况下,可能会出现,其他的窗口的类名,和标题与你要得到的那个窗口的相同,那么这个句柄
就不是很确定了。
想要的了。这个窗口的类名可以通过VC的spy++查看得到.
也能行
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");
}
如果想用于是否等于你的程序,即为::GetForeWindow()==m_hWnd