2012-02-20 256 views
1

我有代碼;如何從剪貼板粘貼文本?

HWND MShwnd = FindWindowA("MapleStoryClass", NULL); 
     PostMessage(MShwnd, WM_KEYDOWN, 0x09, MapVirtualKeyA(0x09, 0) << 16); 

它工作得很好。在我之前,我複製了一個文本到剪貼板。

我想知道的是如何使用postmessage並粘貼文本。

我到處搜索,不明白。

謝謝。

回答

1

這是一個C#代碼轉換,或使一個C#DLL我的代碼: (您需要添加參考Microsoft.VisualBasic

public string GetClipboardText() 
{ 
    Microsoft.VisualBasic.Devices.Computer c = new Microsoft.VisualBasic.Devices.Computer(); 
    return c.Clipboard.GetText(); 
} 

public void SetClipboardText(string stext) 
{ 
    Microsoft.VisualBasic.Devices.Computer c = new Microsoft.VisualBasic.Devices.Computer(); 
    c.Clipboard.SetText(stext); 
} 

更新C++代碼:

System::String^ GetClipboardText() 
{ 
    Microsoft::VisualBasic::Devices::Computer^ c = gcnew Microsoft::VisualBasic::Devices::Computer(); 
    return c->Clipboard->GetText(); 
} 

void SetClipboardText(System::String^ stext) 
{ 
    Microsoft::VisualBasic::Devices::Computer^ c = gcnew Microsoft::VisualBasic::Devices::Computer(); 
    c->Clipboard->SetText(stext); 
} 

更新2

我想你需要nativ e代碼,所以你沒有使用我的代碼在那裏,不需要處理,再加上如果你獲得HWND MShwnd = FindWindowA("MapleStoryClass", NULL);,所以你有一個句柄...任何方式我建議最後一種方法如下:

keybd_event(0x11, 0, 0, 0); // press ctrl 
keybd_event(0x56, 0, 0, 0); // press v 
keybd_event(0x56, 0, 2, 0); // release v 
keybd_event(0x11, 0, 2, 0); // release ctrl 
+0

我會在我回家時嘗試,如果這樣做,你會得到你的答案標記正確。 – Andrew 2012-02-20 15:22:54

+0

我需要它使用postmessage執行ctrl + v功能。 – Andrew 2012-02-20 22:07:43

+0

我試過這個文本框句柄的句柄,它的工作PostMessage(textBox1.Handle,0x302,0,0);對於詳細信息:http://social.msdn.microsoft.com/forums/en-US/csharplanguage/thread/891a05ca-2d39-4f9e-9922-df69c9c2a4b1/ – 2012-02-21 06:21:50