简体   繁体   English

如何正确获取窗口标题

[英]How to get window title correctly

can I do the following to get the window's title of the another application 我可以执行以下操作来获取另一个应用程序的窗口标题吗?

char window_text[256];
LRESULT lResult = SendMessageA(pMouseHook->hwnd, WM_GETTEXT, sizeof(window_text) / sizeof(*window_text), (LPARAM)window_text);
printf("Copied %d characters. Contents: %s\n", lResult, window_text);

or should I first allocate the global memory and use it instead of the local char array? 还是应该首先分配全局内存并使用它而不是本地char数组?

Thanks in advance. 提前致谢。

It's perfectly fine to use a local character array. 使用本地字符数组非常好。 Of course, if the title is longer than the buffer you provide then the returned value will be truncated. 当然,如果标题比您提供的缓冲区长,则返回值将被截断。

For a window is a different process you might consider GetWindowText instead. 对于窗口是一个不同的过程,您可以考虑使用GetWindowText The documentation says: 文件说:

If the target window is owned by the current process, GetWindowText causes a WM_GETTEXT message to be sent to the specified window or control. 如果目标窗口由当前进程拥有,则GetWindowText会导致WM_GETTEXT消息发送到指定的窗口或控件。 If the target window is owned by another process and has a caption, GetWindowText retrieves the window caption text. 如果目标窗口由另一个进程拥有并具有标题,则GetWindowText检索窗口标题文本。 If the window does not have a caption, the return value is a null string. 如果窗口没有标题,则返回值为空字符串。 This behavior is by design. 此行为是设计使然。 It allows applications to call GetWindowText without becoming unresponsive if the process that owns the target window is not responding. 如果拥有目标窗口的进程没有响应,它允许应用程序调用GetWindowText而不会无响应。 However, if the target window is not responding and it belongs to the calling application, GetWindowText will cause the calling application to become unresponsive. 但是,如果目标窗口没有响应,并且它属于调用应用程序,则GetWindowText将导致调用应用程序变得无响应。

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

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