Sending messages/keystrokes to a control in a different app

Just starting out? Need help? Post your questions and find answers here.
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Sending messages/keystrokes to a control in a different app

Post by Max.² »

I am facing two problems:

1.

The handle of the control changes everytime the app is launched. For example, using Winspector I can find out that the Internet Explorer instance I am using to write this posting has a

ComboBox, handle $60344
Edit control, handle $5031C

Is there a way to figure out the handle dynamically?

2.

When using PB's sendkeys procedure

Code: Select all

Procedure SendKeys(handle,window$,keys$) 
  If window$<>"" : handle=FindWindow_(0,window$) : EndIf ; Use window$ instead of handle. 
  If IsWindow_(handle)=0 ; Does the target window actually exist? 
    MessageRequester("Error","Window not open:"+Chr(10)+window$) 
    ProcedureReturn 0 ; Nope, so report 0 for failure to type. 
  Else 
    ; This block gives the target window the focus before typing. 
    thread1=GetWindowThreadProcessId_(GetForegroundWindow_(),0) 
    thread2=GetWindowThreadProcessId_(handle,0) 
    If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#TRUE) : EndIf 
; just a bit of the entire procedure posted
using the handles retrieved manually with Winspector, it seems to be ok (changing the handles gives me an error) and I can see the messages passed to the control, but nothing else happens - no visual feedback of what I typed.

Any clue?

Edit: Seems the control doesn't receive the messages despite what I said. It were messages from other actions on the control.

Edit2:

Code: Select all

SendMessage_($5031C,#WM_SETTEXT ,0,"ABC")
Has worked... leaves me mainly with question 1. And sorry for monolog! :lol:
Randy Walker
Addict
Addict
Posts: 1171
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA
Contact:

Post by Randy Walker »

THIS POST HAS BEEN EDITED - - -

I wont promise this answers your question but maybe something to start with. My understanding is MS warns SendMessage should NEVER be used for one app to talk directly to another app... but it works for me :-)

Code: Select all

; Automate mouse placement over desired control item (gadget)
SetCursorPos_(x.w, y.w)
; OR Manually place mouse over desired control item (gadget)
;
;   Then...
;
; Get the mouse coordinates...
GetCursorPos_(@CursorPosition.POINT) 
h2.l = WindowFromPoint_(x.w, y.w) 
h.l = GetDlgCtrlID_(h2.l)
If h.l > 0   ;  found control so proceed...
  ;
  ; ; ;/      PLACE TEXT INTO OUTSIDE APPLICATION
  SendMessage_(h2.l,#WM_SETTEXT,0,Query$)
  ;
  s$ = Query$ ; (double girations to comply with Windows Strains)
  PostMessage_(h2.l,#WM_SETTEXT,0,s$) ; (XP, 2000, 98, 95 etc.)
  SendMessage_(h2.l,#WM_SETTEXT,0,Query$)
  ;
  ; ; ;/     PLACE FOCUS INTO CONTROL BENEATH X and Y
  e.l = y.w
  e.l = (e.l << 16)
  e.l = e.l + x.w
  SetCursorPos_(x.w, y.w)
  PostMessage_(h2.l,#WM_LBUTTONDOWN,#MK_LBUTTON,e.l)
  PostMessage_(h2.l,#WM_LBUTTONUP,#MK_LBUTTON,e.l)
  WaitWindowEvent()  ; allow processing delay
  
  ; ; ;/     SIMULATE PRESS ENTER MESSAGGE ON OUTSIDE CONTROL
  PostMessage_(h2.l,#WM_KEYDOWN,#VK_RETURN,$1C0000)
  PostMessage_(h2.l,#WM_KEYUP,#VK_RETURN,$C01C0001)
  WaitWindowEvent()  ; allow processing delay
  Delay(40)  ; allow more processing delay
EndIf
EDITED: Lparam values in postmessage lines above have been corrected.
Last edited by Randy Walker on Thu Jul 07, 2005 10:53 am, edited 1 time in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Sending messages/keystrokes to a control in a different

Post by PB »

> Is there a way to figure out the handle dynamically?

You'd have to find the parent window (Internet Explorer itself) and then use
the FindWindow API with the child parameter to get the buttons on it. Do a
search here, there's an example of parsing an IE window somewhere, and
definitely code for parsing child windows.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Post by Max.² »

Thanks, but my posting that suddenly was dug up is way outdated and void.
Post Reply