我有一个远程管理工具,我在使用PostMessage api在任何窗口中接收字符串时遇到了麻烦。字符串通过服务器应用程序中的TextBox
组件发送。
例如,StackOverflow窗口中的单词 Coder 保持如下:
我使用的代码是:
服务器应用程序
private void tEnviarTexto_Click(object sender, EventArgs e)
{
String message = String.Format("<|MSG|>{0}", tEnviarMSG.Text);
ListViewItem item = listView_clients.FocusedItem;
String clientId = item.Name;
RemoteClient client = this.remoteClientManager.GetClientById(clientId);
if (client != null)
{
try
{
client.mreMessage.WaitOne();
client.queueMessage.AddMessage(message);
tEnviarMSG.Text = "";
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
}
}
客户端申请
public void EnviatextoHWND(string texto)
{
foreach (char caractere in texto)
{
int charValue = caractere;
string hexValue = charValue.ToString("X");
IntPtr val = new IntPtr((Int32)caractere);
string valor = val.ToString();
if (valor == "66") // Letra B no Operador
{
PostMessage(WindowHandle, WM_KEYDOWN, new IntPtr(VK_BACK), new IntPtr(0));
PostMessage(WindowHandle, WM_KEYUP, new IntPtr(VK_BACK), new IntPtr(0));
}
else if (valor == "83") // Letra S no Operador
{
PostMessage(WindowHandle, WM_KEYDOWN, new IntPtr(VK_SPACE), new IntPtr(0));
PostMessage(WindowHandle, WM_KEYUP, new IntPtr(VK_SPACE), new IntPtr(0));
}
else
{
PostMessage(WindowHandle, WM_KEYDOWN, val, new IntPtr(0));
PostMessage(WindowHandle, WM_CHAR, val, new IntPtr(0));
PostMessage(WindowHandle, WM_KEYUP, val, new IntPtr(0));
}
}
}
和用法:
public void CommunicationProc()
{
while (this.Connected)
{
else if (message.IndexOf("<|MSG|>") == 0)
{
String[] strSplit = value.Split(MenuRemoteClient.separator, StringSplitOptions.None);
string msg = strSplit[0]; // content from a TextBox on Server application
if (this.remotedNav)
{
Thread SendTextoHWND = new Thread(() => EnviatextoHWND(msg));
SendTextoHWND.Start();
}
}
}
}
那么,如何解决呢?
欢迎任何建议或指导。
答案 0 :(得分:0)
修正:
// PostMessage(WindowHandle, WM_KEYDOWN, val, new IntPtr(0));
PostMessage(WindowHandle, WM_CHAR, val, new IntPtr(0));
// PostMessage(WindowHandle, WM_KEYUP, val, new IntPtr(0));