Page 1 of 1

SetForegroundWindow...

Posted: Sat Jul 02, 2005 11:19 am
by HeX0R
I am messing with pb's SendKeys-Procedure

Snippet for the lazy guys:

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?
    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
    SetForegroundWindow_(handle) ; Target window now has the focus for typing. THIS FAILS!
I am trying to run a program on my game-server, which should send Keystrokes to another Window.
This all works like a charme, but only if i really interact with my program.

This server is meant to be remotely controlled, so no real user, and GetForegroundWindow_() will return NULL , therefor the Sendkey-Function fails.

I've tried to SetForegroundWindow but no luck at all.

Some applications can be controled through SendMessage and #WM_CHAR, but not these i'm trying to reach, so this possibility won't help me either.

Any ideas, how to force my Application to get the Foreground back ?
Or maybe another way ?

Posted: Sat Jul 02, 2005 11:33 am
by Droopy
Try ForegroundWindowSet / ForegroundWindowGet of the Droopy Lib

Posted: Sat Jul 02, 2005 11:41 am
by Droopy
With this code :

Code: Select all

Handle=OpenWindow(0, 0, 0, 195, 260, #PB_Window_TitleBar ,"Windows #1")

For n=1 To 50000
  Delay(1)
  z=WindowEvent()
  x=ForegroundWindowGet()
  If x<>Handle
    ForegroundWindowSet(Handle)
    Beep(400,200)
  EndIf
Next

Posted: Sat Jul 02, 2005 12:15 pm
by HeX0R
This doesn't help me... and btw, why shoud i use a lib, when this does the same ? :

Code: Select all

Handle=OpenWindow(0, 0, 0, 195, 260, #PB_Window_TitleBar ,"Windows #1")

For n=1 To 50000
  Delay(1)
  z=WindowEvent()
  x=GetForegroundWindow_()
  If x<>Handle
    SetForegroundWindow_(Handle)
    Beep(400,200)
  EndIf
  If z = #WM_CLOSE
    End
  EndIf
Next
As i said : When the server is in remote-mode, none of the Windows is in foreground!
This means, even your ForegroundWindowGet() Function returns NULL, and you are no longer able to Set a foreground...

Posted: Sat Jul 02, 2005 1:01 pm
by Droopy
This is the code of ForegroundWindowSet (Author = PB )

Code: Select all

ProcedureDLL ForegroundWindowSet(WindowsHandle) 
  
  thread1=GetWindowThreadProcessId_(GetForegroundWindow_(),0) 
  thread2=GetWindowThreadProcessId_(WindowsHandle,0) 
  If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf 
  SetForegroundWindow_(WindowsHandle) 
  ;Sleepex_(250,0) ; Delay to stop fast CPU issues. 
  If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#False) : EndIf 
EndProcedure 
Sorry if this code don't help :oops:

Posted: Mon Aug 29, 2005 6:50 am
by lexvictory
SetForegroundWindow_(handle) ; Target window now has the focus for typing. THIS FAILS!
try this:

Code: Select all

SetWindowPos_(handle, #HWND_TOP, 0,0,0,0, #SWP_NOMOVE | #SWP_NOSIZE)
not quite sure this will work cos ive never had to do this before, but please tell me if it does.

Posted: Mon Aug 29, 2005 2:50 pm
by HeX0R
Thanks for the reply lexvictory , but i'm quite sure i also played with your suggestion without any success.
Problem is/was, i configure this server through the RemoteConsole and whenever i log off, there is no Foregroundwindow any longer (if it is ontop or not, this doesn't really matter).

But nevertheless, i came around this problem... somehow... ;)