我在Delphi中需要这段代码的帮助:
function say(mensagem: string):string;
var
H: HWND;
letra: Integer;
B: Byte;
begin
H := FindWindow(nil, 'tibia');
for letra := 1 to Length(mensagem) do
begin
B := Byte(mensagem[letra]);
SendMessage(H, WM_CHAR, B, 0);
end;
SendMessage(H, WM_CHAR, 13, 0);
end;
致C#:
public void say(string message)
{
int letra;
IntPtr hWnd = FindWindow("TibiaClient", null);
for (letra = 1; letra <= message.Length; letra++)
{
Byte B = Convert.ToByte(message[letra]);
SendMessage(hWnd, WM_CHAR, B, 0);
SendMessage(hWnd, WM_CHAR, 13, 0);
}
}
错误:Byte B = Convert.ToByte(message[letra]);
答案 0 :(得分:0)
您的Delphi代码假定为8位文本编码。因此在C#中,您需要使用等效编码将文本编码为字节,然后发送到其他窗口。你这样写:
foreach (byte b in Encoding.Default.GetBytes(message))
{
SendMessage(hWnd, WM_CHAR, b, 0);
}
SendMessage(hWnd, WM_CHAR, 13, 0);
另请注意:
FindWindow
的调用在两个版本之间有所不同。