It is currently Mon May 27, 2019 3:05 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: AppActivate by handle or title
PostPosted: Thu Jun 20, 2002 9:18 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Code updated for 5.20+

Restored from previous forum. Originally posted by PB.

This procedure lets you activate (give the focus to) a foreign window by either
its handle or window title. By default it activates by the handle, unless you
specify a window title (upon which the handle parameter is ignored).

Fred, could this become a native command please?

Code:
Procedure AppActivate(handle,title$)
  If title$ <> "" : handle=FindWindow_(0,title$) : EndIf
  thread1=GetWindowThreadProcessId_(GetForegroundWindow_(),0)
  thread2=GetWindowThreadProcessId_(handle,0)
  If thread1thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf
  SetForegroundWindow_(handle)
  If thread1thread2 : AttachThreadInput_(thread1,thread2,#False) : EndIf
  ProcedureReturn IsWindow_(handle)
EndProcedure
;
MessageRequester("Title test","Click OK to activate Calculator by its title...",0)
;
If AppActivate(0,"Calculator")=0
  Debug "Calculator not found..."
Else
  Debug "Calculator has the focus!"
EndIf
;
MessageRequester("Handle test","Click OK to activate Calculator by its handle...",0)
;
; Assume calc is the handle of the calculator from an earlier search...
If AppActivate(calc,"")=0
  Debug "Calculator not found..."
Else
  Debug "Calculator has the focus!"
EndIf



PB - Registered PureBasic Coder

Edited by - PB on 20 June 2002 23:02:26


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Jun 20, 2002 10:10 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by Paul.

Why could you not use...

SetFocus_(FindWindow_(0,"Calculator"))

??



Edited by - paul on 20 June 2002 23:13:02


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Jun 20, 2002 11:11 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by PB.

> Why could you not use...
> SetFocus_(FindWindow_(0,"Calculator"))

From http://216.26.168.92/vbapi/ref/s/setfocus.html :

"SetFocus gives a window the input focus. The window must be owned by the thread
calling the function, however -- the function will not set the focus to another
program's window."

Also, SetFocus may give the focus, but not bring the app to the foreground...
It's possible for a window have an input focus while not being forefront.


PB - Registered PureBasic Coder

Edited by - PB on 21 June 2002 00:19:08


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Jun 20, 2002 11:38 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by Paul.

ok then...

SetForeGroundWindow_(FindWindow_(0,"Calculator"))

Now the app is in the foreground and I can interact with it using Keybd_Event_() or Mouse_Event_()


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Jun 20, 2002 11:58 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by PB.

> ok then...
> SetForeGroundWindow_(FindWindow_(0,"Calculator"))

This may work for you, but it doesn't on all versions of Windows, for example,
on Windows 2000. The above doesn't work for me at all.

The SetForegroundWindow API is something I researched in my old Visual Basic
days about this very problem, and that's why I know the procedure I posted
above is the only 100% sure-fire way to do it on all versions of Windows.

Please see this URL for more information: http://tinyurl.com/fvf


PB - Registered PureBasic Coder


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Jun 21, 2002 2:26 am 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by Paul.

That is very strange because I have 98se, NT4 and Win2K here at it works fine on all 3 platforms. I also use the SetForeGroundWindow_() call in a piece of software that has been distributed to almost 300 systems. Not a single problem.

Oh well... everyone has their own way of doing things.


Are you sure your copy of windows wasn't manufactured on a Monday or a Friday?
Or quite possible this problem is only present when using Visual Basic?

:)



Edited by - paul on 21 June 2002 03:28:37


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Jun 21, 2002 9:41 am 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by PB.

> Or quite possible this problem is only present when using Visual Basic?

It's not just a VB problem -- your PureBasic example doesn't work for me either.
If I was to use one of your apps, I'd have to send you a bug report.

Seriously though, take a look at the bug reports at the following URL, which
shows the problem does exist for myself and many others, and not just with Visual
Basic. Note also that the solution provided, in every case, requires that the app
connects to the input thread of the target app...

http://groups.google.com/groups?as_q=setforegroundwindow%20problem


PB - Registered PureBasic Coder

Edited by - PB on 21 June 2002 10:42:21


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Jun 21, 2002 2:23 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by Paul.

Quote:
It's not just a VB problem -- your PureBasic example doesn't work for me either.
If I was to use one of your apps, I'd have to send you a bug report.


I'm out of town today but when I get back, I will send you a small app to test. (like I said, almost 300 users have no problems at all)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Jun 21, 2002 3:51 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by PB.

> I also use the SetForeGroundWindow_() call in a piece of software that has
> been distributed to almost 300 systems. Not a single problem.

Are you calling your own app's windows, or foreign windows?


PB - Registered PureBasic Coder


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Jun 21, 2002 4:09 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by Franco.

Hi guys,
here it is again: FindWindow_(0,"Calculator").
I suppose that 'Calculator' may work on the english Win version not in the italian version ...
Also some Applications have the working file name in the title like:

'SpecialApp - Workfile.txt'

In this case you will not find your Window... if you search only for 'SpecialApp'.
You could do:
Code:
WindowTitleLenght=Len(WindowTitle$)
NewWindowTitle$=ReplaceString(WindowTitle$,"SpecialApp", "",1)
NewWindowTitleLenght=Len(NewWindowTitle$)
If WindowTitleLenght > NewWindowTitleLenght
  ;YES, you found it...
ElseIF WindowTitleLenght = NewWindowTitleLenght
  ;sorry Window not found...
ElseIF WindowTitleLenght
EndIf

NOP, I didn't read the testimonies at the URLs above.


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.

Edited by - franco on 21 June 2002 17:36:43


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Jun 21, 2002 6:35 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by PB.

Hi Franco,

It's a problem with SetForegroundWindow, and not FindWindow, that we have a
difference about. Paul says SetForegroundWindow works for him and 300 of his
users, but I know it doesn't work for me and many others at the URLs I quoted.
So we're trying to figure out why there's a discrepancy happening...

I used the Calculator as an example app only.

PB - Registered PureBasic Coder


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat Jun 22, 2002 4:16 am 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by Paul.

Hi PB... give this a try and let me know what happens.
http://www.reelmediaproductions.com/test/test.exe

This creates a small text file, opens it, then brings the windows to the front and writes some stuff in it... using SetForegroundWindow_()

(it is basically a chunk cut and pasted out of my software... and works fine on 98, NT, and Win2K that I have here)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat Jun 22, 2002 5:14 am 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by Fangbeast.

Quote:
Hi PB... give this a try and let me know what happens.
http://www.reelmediaproductions.com/test/test.exe

This creates a small text file, opens it, then brings the windows to the front and writes some stuff in it... using SetForegroundWindow_()

(it is basically a chunk cut and pasted out of my software... and works fine on 98, NT, and Win2K that I have here)



Hey Paul!! What is "Taking to notepad ???? (innocent look)

Fangles


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat Jun 22, 2002 6:00 am 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by Paul.

hehe...

okay, so I can't spell :)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat Jun 22, 2002 9:04 am 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by PB.

> Hi PB... give this a try and let me know what happens.
> http://www.reelmediaproductions.com/test/test.exe

Yes, it works, but only because your app opened Notepad, ie. it shares the
input thread with Notepad because it opened it. Try it with an existing
Notepad window and it fails -- test it for yourself and you will see.

Remember, my tip here is to activate another third-party foreign window, and
not to activate a window created or opened by the app itself (although my tip
will do that anyway).


PB - Registered PureBasic Coder

Edited by - PB on 22 June 2002 10:07:01


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: flaith and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye