使用TWAPI获取RichEdit50W窗口的文本

时间:2013-09-22 10:21:54

标签: winapi tcl

我想检索特定窗口的文本。使用

twapi::get_window_text $handle 

我得到了窗口的标题。但是我如何获得实际内容?在C ++中我使用

EM_GETLINE

如何使用TCL的这些原始Windows API函数?例如,对于EM_GETLINE,我必须定义要提取的行数和存储它们的缓冲区。

有人可以告诉我如何使用TCL的原始Windows API函数或指向我可以找到示例的网站吗?感谢

1 个答案:

答案 0 :(得分:0)

您可以使用Twapi的raw-API发送消息。我并不熟悉这条消息的确切细节,但你知道这可能比我好:

package require twapi
proc get_richedit_text {hwnd line} {
    set MAX_LEN 0x0100
    # You have to lookup this value in the header.
    set EM_GETLINE 0x00C4
    set bufsize [expr {2 * ($MAX_LEN + 1)}]
    # yes, twapi has malloc.
    set szbuf [twapi::malloc $bufsize]
    # catch everything, so we can free the buffer.
    catch {
        # set the first word to the size. Whatever a word is.
        # I assume it is an int (type 1), but if it is a int64, use type 5, wchar is 3.
        # arguments to Twapi_WriteMemory: type pointer(void*) offset bufferlen value 
        twapi::Twapi_WriteMemory 1 $szbuf 0 $bufsize $MAX_LEN
        # send the message. You don't have SendMessage, only SendMessageTimeout
        set ressize [twapi::SendMessageTimeout $hwnd $EM_GETLINE $line [twapi::pointer_to_address $szbuf] 0x0008 1000]
        return [twapi::Twapi_ReadMemory 3 $szbuf 0 [expr {$ressize * 2}]]
    } res opt
    # free the buffer.
    twapi::free $szbuf
    return -options $opt $res
}

我使用了一些内部/未记录的twapi API,唯一的文档是twapi的源代码。