关于KeyBd_Event()和获取输入焦点所在Edit的Handle的问题。 (50分)

  • 主题发起人 主题发起人 易名烦
  • 开始时间 开始时间

易名烦

Unregistered / Unconfirmed
GUEST, unregistred user!
1. 请看代码: <br>&nbsp;procedure TForm1.Button1Click(Sender: TObject);<br>&nbsp;Var<br>&nbsp; S: Pchar;<br>&nbsp; i: Integer;<br>&nbsp;begin<br>&nbsp; S := 'abcdefg';<br>&nbsp; For i:=0 To Length(S)-1 Do Begin<br>&nbsp; &nbsp;KeyBd_Event(Ord(s),0,0,0);<br>&nbsp; &nbsp;KeyBd_Event(Ord(s),0,KeyEventF_KeyUp,0);<br>&nbsp; End;<br>end;<br>怎么会得到的是: 1234567<br>2. 如果我用程序控制另一非DELPHI的程序,用SendMessage()向它的某一EDIT中输入数据,<br>&nbsp; &nbsp;我怎么知道这个EDIT的句柄,要是这个EDIT无句柄怎么办?
 
1.<br>&nbsp; keybd_event(ord(s), MapVirtualKey(ord(s), 0), 0, 0);<br>&nbsp; keybd_event(ord(s), MapVirtualKey(ord(s), 0), KEYEVENTF_KEYUP, 0);<br>2.<br>&nbsp; Edit也是一个窗口,肯定是有handle的。只是如果有多个edit,分清是哪个不太容易。
 
找其它窗口中edit的句柄<br>var<br>&nbsp; h,h1: hwnd;<br>begin<br>&nbsp; h := findwindow(nil,'窗口标题');<br>&nbsp; h1 := findwindowex(h,0,'edit',nil);<br>&nbsp; sendMessage(h1,wm_setText,0,integer(pchar('1234567')));<br>end;<br><br>如果窗口里面不止一个edit,用EnumChildWindows()调用一个回调函数取得想要的句柄<br>
 
zw84611:<br>&nbsp; 1.我发现原来是小写字母不行,请解决一下。<br>&nbsp; 2.请你帮帮忙弄一下,我真的很着急,等了三四天了。
 
一少:<br>&nbsp; &nbsp; 能不能写个EnumChildWindows()的用法,因为我不太会。
 
procedure TForm1.Button1Click(Sender: TObject);<br>&nbsp; function EnumChildWindowsProc(H: HWnd; lparam: longint): Boolean; stdcall;<br>&nbsp; var<br>&nbsp; &nbsp; p,ptext:pchar;<br>&nbsp; begin<br>&nbsp; &nbsp; Result := True; //设定为true才会找下一个<br>&nbsp; &nbsp; getMem(p,255);<br>&nbsp; &nbsp; getclassname(h,p,255);<br>&nbsp; &nbsp; showmessage(strpas(p));<br>&nbsp; &nbsp; freeMem(p);<br>&nbsp; &nbsp; <br>&nbsp; &nbsp; getMem(ptext,255);<br>&nbsp; &nbsp; SendMessage(h,WM_GETTEXT,254,Integer(ptext));<br>&nbsp; &nbsp; showmessage(strpas(ptext));<br>&nbsp; &nbsp; freeMem(ptext);<br>&nbsp; &nbsp; <br>&nbsp; end;<br>begin<br>&nbsp; EnumChildWindows(getforegroundwindow, @EnumChildWindowsProc, 0);<br>end;
 
1.<br>Var<br>&nbsp; S,US: string;<br>&nbsp; i: Integer;<br>begin<br><br>&nbsp; S := 'abcdefg';<br>&nbsp; US := UpperCase(s);<br>&nbsp; For i:=1 To Length(US) Do<br>&nbsp; Begin<br>&nbsp; &nbsp;KeyBd_Event(Ord(US),0,0,0);<br>&nbsp; &nbsp;KeyBd_Event(Ord(US),0,KeyEventF_KeyUp,0);<br>&nbsp; End;<br><br>end;<br><br>2.<br>function Proc(AWnd: HWND; AlParam: LPARAM):Boolean;stdcall;<br>var<br>&nbsp; WndCaption: array[0..254] of Char;<br>&nbsp; ulpClassName : array[0..254] of Char;<br>&nbsp; i : integer ;<br>begin<br>&nbsp; GetWindowText(AWnd, WndCaption, 254);<br>&nbsp; GetClassName(AWnd, ulpClassName, 254);<br>&nbsp; if (WndCaption[0]&lt;&gt;chr(0)) and IsWindowVisible(AWnd)<br>&nbsp; then <br>&nbsp; begin<br>&nbsp; &nbsp; Form1.ListBox1.Items.Add(StrPas(WndCaption)+':'+StrPas(ulpClassName));<br>&nbsp; &nbsp; //如果ulpClassName是'TEdit'(Delphi/BCB程序)或'Edit'(VC/VB程序)则用FindWindowEx找到控件句柄<br>&nbsp; end;<br>&nbsp; Result := True;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; enumchildwindows(findwindow(nil,'Form1'),@proc,0);<br>end;
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
775
import
I
后退
顶部