gpt4 book ai didi

windows - 同步使用 GetWindowText

转载 作者:可可西里 更新时间:2023-11-01 10:59:24 26 4
gpt4 key购买 nike

我不太明白 GetWindowText 如何在与调用者相同的线程上为窗口工作。如果 GetWindowText 阻塞直到消息被处理,线程如何调用 DispatchMessage?它已在 GetWindowText 中被阻止。这是否意味着 GetWindowText 必须始终从与操作消息循环的线程不同的线程调用?

最佳答案

GetWindowText 只是 SendMessage(WM_GETTEXT) 的薄包装。

发送到窗口的消息总是在创建窗口的线程中处理(窗口具有“线程关联”)。发送消息不经过DispatchMessage,而是GetMessage(或PeekMessageMsgWaitForMultipleObjects)直接调用窗口过程, 对于从另一个线程发送的消息。如果消息是从同一个线程发送的,SendMessage 将调用窗口过程。


SendMessage documentation说:

The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.

the GetMessage documentation says :

Retrieves a message from the calling thread's message queue. The function dispatches incoming sent messages until a posted message is available for retrieval.

来自 the PeekMessage docs :

Dispatches incoming sent messages, checks the thread message queue for a posted message, and retrieves the message (if any exist).


发送消息的行为之前已经在Raymond Chen's excellent blog, The Old New Thing上高亮显示了,所有 Win32 开发人员都应该订阅:

If you use any of the above send-type functions to send a message to a window that belongs to the sending thread, the call is made synchronously.

Recall that SendMessage delivers the message directly to the window procedure; the message pump never sees it.


当然,在 StackOverflow 上可以找到相关问题:

关于windows - 同步使用 GetWindowText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9346508/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com