wjiachun, cAkk及其它大侠帮帮忙!(100分)

  • 主题发起人 我是坏蛋
  • 开始时间

我是坏蛋

Unregistered / Unconfirmed
GUEST, unregistred user!

我在我的程序里使用如下代码用来往另外一个程序的输入框发送字符串,
该输入框是Vc++的,经试验英文字符串能正常显示,但中文无法正常显示,
恳请高手们解救!


procedure TForm1.SendBtnClick(Sender: TObject);
var
i: Integer;
ch: byte;
TestStr: string;
EditHwnd: Hwnd;
begin
TestStr := SendEdit.Text;
EditHwnd := HWnd(StrToInt(HwndEdit.Text));
SendMessage(EditHwnd,WM_SETTEXT,0,LongInt(PChar('')));{清空输入框内容}
SendDBCSString(EditHwnd, TestStr);
end;

procedure SendDBCSString(hFocus: HWND; const sSend: string);
var
hActiveControl: HWND;
i: integer;
ch: byte;
begin
if hFocus = 0 then hFocus := GetFocus;
if hFocus = 0 then Exit;
i := 1;
while i <= Length(sSend) do
begin
ch := byte(sSend);
if Windows.IsDBCSLeadByte(ch) then
begin
Inc(i);
SendMessage(hFocus, WM_IME_CHAR, MakeWord(byte(sSend), ch), 0);
end
else
SendMessage(hFocus, WM_IME_CHAR, word(ch), 0);
Inc(i);
postmessage(hFocus,WM_keydown,13,0);
end;
end;
 
如果中文拷贝进去也不能正常显示的话, 就是字符集要改.
 
知错能改就不是“坏蛋” :)
您可以试试使用 PWideChar(WideString(S:String)) 转换为 WideChar 然后发送,不要让它装入 char
再转换回来。
 
uses Sendkeyunit————————

unit Sendkeyunit;
//Rewrite by wjiachun
interface
uses windows,messages;

procedure SendKeys(sSend:string);

implementation
procedure SendKeys(sSend:string);
var
i:integer;
focushld,windowhld:hwnd;
threadld:dword;
ch: byte;
begin
windowhld:=GetForegroundWindow;
//获得前台应用程序的活动窗口的句柄
threadld:=GetWindowThreadProcessId(Windowhld,nil);
//获取与指定窗口关联在一起的一个进程和线程标识符
AttachThreadInput(GetCurrentThreadId,threadld,true);
//通常,系统内的每个线程都有自己的输入队列。 //
//AttachThreadInput允许线程和进程共享输入队列。 //
//连接了线程后,输入焦点、窗口激活、鼠标捕获、键盘状态 //
//以及输入队列状态都会进入共享状态 //
Focushld:=getfocus;
//获得拥有输入焦点的窗口的句柄
AttachThreadInput(GetCurrentThreadId,threadld,false);
if focushld = 0 then Exit;
//如果没有输入焦点则退出发送过程
i := 1;
while i <= Length(sSend) do
//该过程发送指定字符串(中英文皆可以)
begin
ch := byte(sSend);
if Windows.IsDBCSLeadByte(ch) then
begin
Inc(i);
SendMessage(focushld, WM_IME_CHAR, MakeWord(byte(sSend), ch), 0);
end
else
SendMessage(focushld, WM_IME_CHAR, word(ch), 0);
Inc(i);
end;
postmessage(focushld,WM_keydown,13,0);
//发送一个虚拟Enter按键
end;
end.
 
楼上几位真是好人啊!真是把偶感动死了!
 
小雨哥, 小生愚钝,能否详细点?
 
to kkyy;
中文拷贝(手工)能正常显示哦!
 
wjiachun大侠,我在调试您的您的代码时到
AttachThreadInput(GetCurrentThreadId,threadld,true);
这一句就出不来了,最后不得不用win2000任务管理器把delphi强制关闭掉!
这是为什么?
 
哈哈哈!又一个问题得以解决!偶真系高兴死了!散分以示庆贺!来者皆有分!
 
顶部