Re: Vbs Active Window
- From: mr_unreliable <kindlyReplyToNewsgroup@xxxxxxxxxxx>
- Date: Mon, 29 May 2006 16:50:25 -0400
hi Drkn,
If you are courageous enough (or foolhardy enough) to call
api's from script, then this becomes easy enough.
You use "GetActiveWindow" to get the system "handle" of
the active window, then "GetWindowText" to get the window's
caption.
To call those system api's you need to use a third-party
control. One such control is called Dynawrap.
The rest you can do with vbScript.
Here is an example script:
--- <snip> ---
' demo script to illustrate using "GetActiveWindowText", jw 07Mar01
'
Option Explicit
'
' instante ActX components here (to assure DynaWrap is available)...
Dim oDW : Set oDW = WScript.CreateObject("DynamicWrapper") ' no events
Dim oSH : Set oSH = WScript.CreateObject("WScript.Shell")
'
Dim hActWin, nRtn ' as long
Dim sWinCaption ' as string
Const MAX_PATH = 260
' --- end of declarations and constants ----------
' ================================================
' === MAIN LINE SCRIPT LOGIC =====================
' ================================================
' allocate space for the return string...
sWinCaption = String(MAX_PATH, vbNullChar)
oSH.Run "notepad.exe"
' wait for notepad to load...
' (using AppActivate's undocumented feature, re: returns t/f)...
Do : WScript.Sleep 100
Loop Until oSH.AppActivate("Notepad")
hActWin = GetActiveWindow()
BugAssert (hActWin <> 0), "no window activated at this time, sorry."
nRtn = GetWindowText(hActWin, sWinCaption, MAX_PATH)
MsgBox("The active window is: " & vbCrLf & vbCrLf & sWinCaption)
oSH.SendKeys "%{F4}" ' alt-F4 (to close window)
Set oDW = nothing ' clean up
Set oSH = nothing
WScript.Quit
' ================================================
' ================================================
' === "WRAPPERS" FOR API CALLS (DynaWrap style) ==
'
' the DynaWrap doc is rather inscrutable, but as best I can make out,
' DynaWrap can only accomodate ONE api declaration at a time.
' (If I'm wrong, maybe you experts can straighten me out on this).
'
' Assuming that my one-at-a-time hypothesis is correct,
' then one has two choices:
' - you need to set up a separate obj for EVERY api (ugh!), or
' - declare the api's to be used (one-at-a-time) as you go,
' which is what you see here (yes, it's "ugly")...
'
' ------------------------------------------------
' the DynaWrap parameters are:
' i => the number and data type of the function's parameters
' (n.b., if no paramaters, LEAVE OUT this "i=" argument)
' f => type of call _stdcall or _cdecl.
' (Default to _stdcall. If that doesn't work use _cdecl).
' r => return data type.
' the data type declarations are:
' c => VT_I4: c signed char
' d => VT_R8: d 8 byte real
' f => VT_R4: f 4 byte real
' h => VT_I4: h HANDLE
' l => VT_I4: l long
' p => VT_PTR: p pointer
' s => VT_LPSTR: s string
' t => VT_I2: t short
' u => VT_UINT: u unsigned int
' w => VT_LPWSTR: w wide string
' r => VT_BYREF: (pass by reference) for strings only.
' ================================================
' ================================================
Function GetActiveWindow()
Set oDW = nothing ' clear any previous instance
Set oDW = CreateObject("DynamicWrapper") ' start over...
' register (declare) this flavor of api call...
' (note: no parameters, so LEAVE OUT "i=" argument)...
oDW.Register "USER32.DLL", "GetActiveWindow", "f=s", "r=h"
GetActiveWindow = oDW.GetActiveWindow()
End Function
Function GetWindowText(hWnd, lpString, cch)
Set oDW = nothing ' clear any previous instance
Set oDW = CreateObject("DynamicWrapper") ' start over...
' register (declare) this flavor of api call...
' (note: string passed byref to allow for proper return address)...
oDW.Register "USER32.DLL", "GetWindowTextA", "i=lrl", "f=s", "r=h"
GetWindowText = oDW.GetWindowTextA(hWnd, lpString, cch)
End Function
' --- BUGASSERT (yes, it's for debugging) --------
Sub BugAssert (bTest, sErrMsg)
' BugAssert is a Bruce McKinney creation.
' It is used to test for intermediate results...
if bTest then Exit Sub
MsgBox "Error Detected by BugAssert: " & vbCr & vbCr & sErrMsg, _
vbCritical, " << BugAssert FAILED >> "
WScript.Quit
End Sub
--- </snip> ---
cheers, jw
____________________________________________________________
You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)
--- <DynaWrap Boilerplate> ---
It is possible to declare-and-call an api from script,
but you must use a third-party control to do so,
or else write one yourself.
It has already been correctly pointed out that there
is no api-capability in "pure" script.
If you are willing to use a third-party control, then
one such control, called "DynaWrap", can be found on
Guenter Born's website (note: Guenter refers to it as
"DynaCall"). Here is the link to it:
http://people.freenet.de/gborn/WSHBazaar/WSHDynaCall.htm
On that page you will find a download for the control,
plus some code samples.
Note: you may find additional sample code by searching
the archives of the wsh and vbscript ng's.
Note also: DynaWrap does have its limitations. There are
certain things it can't do. For example, you can't call
api's which take typedefs as parameters, and you can't call
api's "by ordinal". But it will work for most of the
"usual suspects".
And finally, DynaWrap doesn't work entirely as advertised.
For example, it is supposed to allow for the declaration of
several api definitions in one instance of itself. I could
never get that to work (in win9x). You will need a new
instance of DynaWrap for every api, or else re-instantiate
the object for every api. Someday I'm going to learn enough
c++ to fix that...
--- </DynaWrap Boilerplate> ---
drkn wrote:
Hi.
I want to know a method to know the title of active window and then
close the window
the idea of script is :
IF ( TitleActiveWindow = "Task Manager" Or CMD ) THEN
Close.ActiveWindow
end if
thank u
--
drkn
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------
- References:
- Vbs Active Window
- From: drkn
- Vbs Active Window
- Prev by Date: Re: countdown clock in powerpoint
- Next by Date: create folders and files if...
- Previous by thread: Re: Vbs Active Window
- Next by thread: Drag and drop a URL from Internet Explorer
- Index(es):
Relevant Pages
|
Loading