2010-01-21 260 views

回答

7

使用SendMessage插入文字编辑缓冲区(这听起来像你想):

HWND notepad = FindWindow(_T("Notepad"), NULL); 
HWND edit = FindWindowEx(notepad, NULL, _T("Edit"), NULL); 
SendMessage(edit, WM_SETTEXT, NULL, (LPARAM)_T("hello")); 

,如果你需要的键码和任意按键,你可以使用SendInput()(可在2K/XP和首选)或keybd_event()`(这将最终调用SendInput在较新的操作系统)一些例子在这里:

http://www.codeguru.com/forum/showthread.php?t=377393

这里还有WM_SYSCOMMAND/WM_KEYDOWN/WM_KEYUP/WM_CHAR的森活动dMessage你可能会感兴趣的。

+0

那如何将其发送到一个窗口? – H4cKL0rD 2010-01-22 00:04:15

+0

它没有。你需要在'hWnd'参数中传递一个窗口句柄。另外,窗口句柄是* first *参数,而不是第三个参数。 – 2010-01-22 00:12:14

+0

您不能使用SendMessage()发送击键。您无法控制键盘状态。特别是Shift,Control和Alt键。 – 2010-01-22 00:23:58

5

keybd_event()已被取代的SendInput(),所以它的最好使用。这里有一些示例代码可以完成你所要求的功能。您可以使用SendMessage()直接编辑记事本窗口的编辑控件,也可以使用SendInput()来合成要发送到窗口的按键。

使用SendInput()

int SendKeystrokesToNotepad(const TCHAR *const text) 
{ 
    INPUT *keystroke; 
    UINT i, character_count, keystrokes_to_send, keystrokes_sent; 
    HWND notepad; 

    assert(text != NULL); 

    //Get the handle of the Notepad window. 
    notepad = FindWindow(_T("Notepad"), NULL); 
    if(notepad == NULL) 
     return 0; 

    //Bring the Notepad window to the front. 
    if(!SetForegroundWindow(notepad)) 
     return 0; 

    //Fill in the array of keystrokes to send. 
    character_count = _tcslen(text); 
    keystrokes_to_send = character_count * 2; 
    keystroke = new INPUT[ keystrokes_to_send ]; 
    for(i = 0; i < character_count; ++i) 
    { 
     keystroke[ i * 2 ].type = INPUT_KEYBOARD; 
     keystroke[ i * 2 ].ki.wVk = 0; 
     keystroke[ i * 2 ].ki.wScan = text[ i ]; 
     keystroke[ i * 2 ].ki.dwFlags = KEYEVENTF_UNICODE; 
     keystroke[ i * 2 ].ki.time = 0; 
     keystroke[ i * 2 ].ki.dwExtraInfo = GetMessageExtraInfo(); 

     keystroke[ i * 2 + 1 ].type = INPUT_KEYBOARD; 
     keystroke[ i * 2 + 1 ].ki.wVk = 0; 
     keystroke[ i * 2 + 1 ].ki.wScan = text[ i ]; 
     keystroke[ i * 2 + 1 ].ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP; 
     keystroke[ i * 2 + 1 ].ki.time = 0; 
     keystroke[ i * 2 + 1 ].ki.dwExtraInfo = GetMessageExtraInfo(); 
    } 

    //Send the keystrokes. 
    keystrokes_sent = SendInput((UINT)keystrokes_to_send, keystroke, sizeof(*keystroke)); 
    delete [] keystroke; 

    return keystrokes_sent == keystrokes_to_send; 
} 

使用SendMessage()

int SendKeystrokesToNotepad(const TCHAR *const text) 
{ 
    HWND notepad, edit; 

    assert(text != NULL); 

    //Get the handle of the Notepad window. 
    notepad = FindWindow(_T("Notepad"), NULL); 
    if(notepad == NULL) 
     return 0; 

    //Get the handle of the Notepad window's edit control. 
    edit = FindWindowEx(notepad, NULL, _T("Edit"), NULL); 
    if(edit == NULL) 
     return 0; 

    SendMessage(edit, EM_REPLACESEL, (WPARAM)TRUE, (LPARAM)text); 
    return 1; 
} 

我希望帮助。

+0

在SendInput()例子中,KEYEVENTF_UNICODE是否在意文本的类型,它可能也是一个char?我看不到KEYEVENTF_SCANCODE,但是你把你的角色放在wScan中。 GetMessageExtraInfo()是否必需? – ManuelSchneid3r 2012-10-17 22:19:12

+1

@DevNoob:不,这应该适用于Unicode和非Unicode版本。 (我只是测试了它。)是的,GetMessageExtraInfo()_appears_是需要的,因为文档指定了它是必需的。顺便说一下,您的问题的答案也可以通过阅读我的答案中的链接使用相应的函数和类型的MSDN文档找到。 – Sam 2012-10-18 09:13:05

+0

@DevNoob,我不完全确定你的意思是'KEYEVENTF_KEYUP'被遗漏了。我也不确定你所引用文档的含义。如果您想知道为什么键盘事件和键盘事件都是必需的,那是因为它们被用来为目标窗口生成相应的WM_KEYDOWN和WM_KEYUP窗口消息。我们正在低层次上工作,并且我不认为Windows API提供了'WM_KEYPRESS'消息来表示单个键_press_。 – Sam 2012-10-18 23:33:37

相关问题