如何知道当前那个控件有焦点 ( 积分: 200 )

  • 主题发起人 主题发起人 我要学
  • 开始时间 开始时间

我要学

Unregistered / Unconfirmed
GUEST, unregistred user!
是这样的,我做了一个后台程序,用键盘记录账号密码.<br><br>比如现在这样,当前窗口出现的是登陆界面.<br>我用FindWindowEx找到账号的轮输入框,(由于不可以用WM_GetTEXT,所以用键盘记录)<br><br>由于账号输入框和密码输入框是同一类,(TEDIT)<br>如何判断焦点在那个输入框?
 
是这样的,我做了一个后台程序,用键盘记录账号密码.<br><br>比如现在这样,当前窗口出现的是登陆界面.<br>我用FindWindowEx找到账号的轮输入框,(由于不可以用WM_GetTEXT,所以用键盘记录)<br><br>由于账号输入框和密码输入框是同一类,(TEDIT)<br>如何判断焦点在那个输入框?
 
GetForegroundWindow<br>GetFocus
 
同意楼上
 
抄的代码<br><br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=1406927<br><br>procedure TSysUser.SendKeys(sSend: string);<br>var<br> &nbsp;i: integer;<br> &nbsp;focushld, windowhld: hwnd;<br> &nbsp;threadld: dword;<br> &nbsp;ch: byte;<br>begin<br> &nbsp;windowhld := GetForegroundWindow;<br> &nbsp;threadld := GetWindowThreadProcessId(Windowhld, nil);<br> &nbsp;AttachThreadInput(GetCurrentThreadId, threadld, true);<br> &nbsp;Focushld := getfocus;<br> &nbsp;AttachThreadInput(GetCurrentThreadId, threadld, false);<br> &nbsp;if focushld = 0 then Exit;<br> &nbsp;i := 1;<br> &nbsp;while i &lt;= Length(sSend) do<br> &nbsp;begin<br> &nbsp; &nbsp;ch := byte(sSend); //send string//<br> &nbsp; &nbsp;if Windows.IsDBCSLeadByte(ch) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Inc(i);<br> &nbsp; &nbsp; &nbsp;SendMessage(focushld, WM_IME_CHAR, MakeWord(byte(sSend), ch), 0);<br> &nbsp; &nbsp;end<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;SendMessage(focushld, WM_IME_CHAR, word(ch), 0);<br> &nbsp; &nbsp;Inc(i);<br> &nbsp;end;<br> &nbsp;postmessage(focushld, WM_keydown, 13, 0); //enter//<br>end;
 
ymjy的方法我试了,我用FindWindowEx找到的 HAND<br>然后对比,IF (HAND=GetFocus()) THEN **** 无论焦点在这两个控件的任何一个对比都成立.<br>GetForegroundWindow这个返回的是类名,那两个输入框的类名都是TEDIT,如何对比
 
chenybin的方法还没看懂,现在开始试<br><br>谢谢
 
TO chenybin<br>我看了半天,你叫我模拟键盘,何用?<br>我只要判断出光标在那个控件上,账号还是密码框
 
搞得太复杂了吧,提供一下我的简单想法:<br>设一个变量a,界面在Onshow时SetFocus到EDIT1,a:='Edit1',<br>然后在这些TEDIT控件的onEnter事件中写下列代码<br>a := (sender as TEdit).name;<br>那么当前是哪个输入框只需看a的值就OK了。
 
我只是举个例子,找到窗口了再判断它的名字,或者如果只有两个编辑框就可以判断是不是具有密码属性,就知道是不是密码框了
 
TO chenybin<br>你说的对,但如何判断一个输入框是否具有密码属性.<br><br>帮人帮到底.<br><br>TO 里斯<br>这是一个没有窗体的程序.也不可以设置文字在人家的程序里,这样就等于告诉别人,我来偷了.<br>不过,你倒是提醒了我,可以设置那个控件的其它属性,比如高度.宽度等
 
form.activecontral
 
用GetWindowLong看看那个控件是否有ES_PASSWORD属性<br><br>Edit Styles<br>ES_AUTOHSCROLL &nbsp; Automatically scrolls text to the right by 10 characters when the user types a character at the end of the line. When the user presses the ENTER key, the control scrolls all text back to position 0.<br><br><br>ES_AUTOVSCROLL &nbsp; Automatically scrolls text up one page when the user presses ENTER on the last line.<br><br><br>ES_CENTER &nbsp; Centers text in a single-line or multiline edit control.<br><br><br>ES_LEFT &nbsp; Left-aligns text in a single-line or multiline edit control.<br><br><br>ES_LOWERCASE &nbsp; Converts all characters to lowercase as they are typed into the edit control.<br><br><br>ES_MULTILINE &nbsp; Designates a multiple-line edit control. (The default is single line.) If the ES_AUTOVSCROLL style is specified, the edit control shows as many lines as possible and scrolls vertically when the user presses the ENTER key. If ES_AUTOVSCROLL is not given, the edit control shows as many lines as possible and beeps if ENTER is pressed when no more lines can be displayed. If the ES_AUTOHSCROLL style is specified, the multiple-line edit control automatically scrolls horizontally when the caret goes past the right edge of the control. To start a new line, the user must press ENTER. If ES_AUTOHSCROLL is not given, the control automatically wraps words to the beginning of the next line when necessary; a new line is also started if ENTER is pressed. The position of the wordwrap is determined by the window size. If the window size changes, the wordwrap position changes and the text is redisplayed. Multiple-line edit controls can have scroll bars. An edit control with scroll bars processes its own scroll-bar messages. Edit controls without scroll bars scroll as described above and process any scroll messages sent by the parent window.<br><br><br>ES_NOHIDESEL &nbsp; Normally, an edit control hides the selection when the control loses the input focus and inverts the selection when the control receives the input focus. Specifying ES_NOHIDESEL deletes this default action.<br><br><br>ES_OEMCONVERT &nbsp; Text entered in the edit control is converted from the ANSI character set to the OEM character set and then back to ANSI. This ensures proper character conversion when the application calls the AnsiToOem Windows function to convert an ANSI string in the edit control to OEM characters. This style is most useful for edit controls that contain filenames.<br><br><br>ES_PASSWORD &nbsp; Displays all characters as an asterisk (*) as they are typed into the edit control. An application can use the SetPasswordChar member function to change the character that is displayed.<br><br><br>ES_RIGHT &nbsp; Right-aligns text in a single-line or multiline edit control.<br><br><br>ES_UPPERCASE &nbsp; Converts all characters to uppercase as they are typed into the edit control.<br><br><br>ES_READONLY &nbsp; Prevents the user from entering or editing text in the edit control. <br><br><br>ES_WANTRETURN &nbsp; Specifies that a carriage return be inserted when the user presses the ENTER key while entering text into a multiple-line edit control in a dialog box. Without this style, pressing the ENTER key has the same effect as pressing the dialog box’s default pushbutton. This style has no effect on a single-line edit control. <br><br><br>另外转一个代码,记得给我分哦<br><br>function lpEnumFunc(hwnd:integer;uint:integer):boolean;stdcall;<br>var hw,hs,wlong:integer;<br> &nbsp; &nbsp;sbuf,sb2:array[0..256] of char;<br> &nbsp; &nbsp;sb1:string;<br>begin<br> &nbsp; &nbsp;[blue]wlong:=GetWindowLong(hwnd,GWL_STYLE);<br> &nbsp; &nbsp;if (wlong and ES_PASSWORD)&lt;&gt;0 then[/blue] &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;inc(hcount);<br> &nbsp; &nbsp; &nbsp; &nbsp;hw:=GetWindowTextLength(hwnd);<br> &nbsp; &nbsp; &nbsp; &nbsp;sendmessage(hwnd,wm_gettext,40,integer(@sbuf));<br> &nbsp; &nbsp; &nbsp; &nbsp;strpcopy(sbuf,format('[Password %d] = %s',[hcount,sbuf]));<br> &nbsp; &nbsp; &nbsp; &nbsp;form1.listbox1.items.add(strpas(sbuf));<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;result:=true;<br>end;
 
TO chenybin,<br>没问题,分是拿来用的,正在调试中........<br><br>还有<br>&lt;&lt;用GetWindowLog看看那个控件是否有ES_PASSWORD属性&gt;&gt;<br>写错了<br><br>GetWindowLong
 
Self.ActiveControl. //当前焦点控件,如果没有控件有焦点,那就 = nil
 
TO 易名烦<br>由于两个输入的类名都是(TEDIT)<br>我不可能用这句去判断吧<br>if (self.ActiveControl is TEDIT) then
 
起手楼上的有几位兄弟有一件事情没搞明白(但愿我没有搞错),楼主说的是两个程序,不是在同一个程序里面。不能用is来判断的,如果是同一个程序方法可就多了去了
 
起手楼上的有几位兄弟有一件事情没搞明白(但愿我没有搞错),楼主说的是两个程序,不是在同一个程序里面。不能用is来判断的,如果是同一个程序方法可就多了去了
 
在传奇登陆界面中,无论焦点在那个控件上(账号和密码)<br><br>对比都能成立,<br><br><br>VAR<br>EEE,TE,EE:THandle;<br>begin<br>TE:= FindWindowEx(0,TE,'TFrmMain',NIL);//传奇登陆界面句柄<br>TE:= FindWindowEx(TE,0,NIL,NIL);//密码框句柄<br>EE:=GetWindow(TE,GW_HWNDNEXT);//账号框句柄<br>EEE:=GetFocus(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//当前焦点所在控件<br>IF (getwindowlong(TE,0)=getwindowlong(EEE,0)) THEN<br>FORM1.Caption :='焦点在密码框';<br>IF (getwindowlong(EE,0)=getwindowlong(EEE,0)) THEN<br>FORM1.Caption :='焦点在账号框';<br><br><br><br>我也用过了直接对比,就是比不出来有什么不同<br><br>IF (TE=GetFocus()) &nbsp;THEN <br>FORM1.Caption :='焦点在密码框';
 
搭车请教一下,呵呵:<br><br><br>TE:= FindWindowEx(0,TE,'TFrmMain',NIL);//传奇登陆界面句柄<br>怎么知道是传奇的句柄呀,TFrmMain? 是不是用什么东东提前分析好了呢?<br>如果是要找网易泡泡的句柄该怎么办呢?
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
864
DelphiTeacher的专栏
D
后退
顶部