AppActivate by handle or title

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

AppActivate by handle or title

Post by BackupUser »

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: Select all

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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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_()
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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=se ... %20problem


PB - Registered PureBasic Coder

Edited by - PB on 21 June 2002 10:42:21
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.
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)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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: Select all

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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.
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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

hehe...

okay, so I can't spell :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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
Post Reply