SetForegroundWindow...

Windows specific forum
User avatar
HeX0R
Addict
Addict
Posts: 1221
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

SetForegroundWindow...

Post 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 ?
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Try ForegroundWindowSet / ForegroundWindowGet of the Droopy Lib
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post 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
User avatar
HeX0R
Addict
Addict
Posts: 1221
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Post 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...
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post 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:
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Post 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.
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
User avatar
HeX0R
Addict
Addict
Posts: 1221
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Post 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... ;)
Post Reply