简体   繁体   English

获取当前窗口到GetWindowText的句柄?

[英]Getting handle of current window to GetWindowText?

I want to display the Title of the Dialog box: 我想显示对话框的标题:

HWND hWnd = ::GetActiveWindow();
char cc[101];
::GetWindowText(hWnd,cc,100);
MessageBox(cc);

but the result yields a blank "". 但结果产生一个空白的“”。

not sure what is wrong?? 不确定出什么问题了吗?

According to MSDN : 根据MSDN

Retrieves the window handle to the active window attached to the calling thread's message queue. 将窗口句柄检索到附加到调用线程的消息队列的活动窗口。

This means that if the thread which you are calling the function from doesn't own any window, the function will fail. 这意味着,如果您从中调用该函数的线程不拥有任何窗口,则该函数将失败。

You probably want GetForegroundWindow instead. 您可能需要GetForegroundWindow

This may coming a bit late but anyway. 无论如何,这可能会晚一点。 If you want to get the current (active) window on the system at any time, the best approach is by using a procedure implemented in a DLL, then installing a global hook that call this procedure. 如果您想随时获取系统上的当前(活动)窗口,最好的方法是使用在DLL中实现的过程,然后安装一个调用此过程的全局挂钩。

The following resources are quite helpful: 以下资源很有帮助:

  1. Creating and using your DLL 创建和使用您的DLL
  2. An overview on Hooks 钩子概述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM