Windows脚本编程核心技术精解Chapter12.pdf
《Windows脚本编程核心技术精解Chapter12.pdf》由会员分享,可在线阅读,更多相关《Windows脚本编程核心技术精解Chapter12.pdf(26页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 12Gaining Full Control Over Any WindowIn This Chapter?Access any window directly using its handle?Retrieve any windows class name for safe identification?Bring a window safely into the foreground?Make windows invisible and change window style dynamically?Flash a windows title bar?Capture win
2、dow content and save it to disk?Explore window internals and walking window hierarchiesWindow handles are the key to manipulating any window.In this chapter,you learn many new methods for solving common problems.Amongthe many new solutions,your scripts will be able to switch windows to theforeground
3、 and capture entire window contents.Full Control Over Any WindowWindows are all over the placethey resemble universe containers for yourprograms and play an important role in reserving space.Surprisingly enough,the Scripting Host doesnt offer any window support at all.Microsoft didntinclude any meth
4、ods to control the size of windows,move them around,or bring them into the foreground.Fortunately,the Windows API contains numerous functions to remote controlany window.To access these features,all you need to do is open the COMproject I have designed for you:componentswinmanagerwinmanager.vbp.Even
5、 easier,you can install the prepared software package:installwinmanagersetup.exe.4684-8 ch12.f.qc 3/3/00 9:38 AM Page 367Accessing any windowThe first step into the window world is accessing individual windows.Windowsadministers individual windows similar to processes.Each window has itsindividual I
6、D,the window handle.Its just a number.You have numerous ways to find out the window handle of any window.Accessing a window by nameIf you know the name of the window you want to access,use GetWindow-Handle.The following example asks you for a window name.Type in anywindow name,but take care to type
7、in the window name exactly as it appears in the windows title bar.The script returns the window class(see Figure 12-1).12-1.VBSset tool=CreateObject(“window.manager”)winname=InputBox(“Please enter the window name as it appears in”_&“its title bar!”)handle=tool.GetWindowHandle(winname)if handle=0 the
8、nMsgBox“Couldnt find a window named“”&winname&“”elseclassname=tool.GetWindowClassName(handle)msg=“Window“”&winname&“”ID“&handle&vbCrmsg=msg&“is of class“”&classname&“”MsgBox msgend ifFigure 12-1:Script returns window handle and class name.It worked.Your script will retrieve the window handle and the
9、 window class,as shown in Figure 12-1.In addition to the window handle,Windows groups each window by kind.Each application can assign a class name to its windows.On Windows 2000,for example,each Explorer window carries the“CabinetWClass”class tag.Your script can find out the class of any window.Youl
10、l see a little later in the chapter how the class name can help identify the right window.368Part III:Starting and Controlling Other SoftwareII4684-8 ch12.f.qc 3/3/00 9:38 AM Page 368Accessing window by class nameAccessing a window by name is not very safe.For example,the applicationmight display th
11、e document name in its title bar,so the window name dependson the kind of document opened by the application.Class names can help.Lets assume you want to get access to a Notepadeditor window.Each Notepad window uses“notepad”as class name.Theclass name never changes,no matter which document the edito
12、r has loaded.The next script finds out if an editor window is open and displays its name:12-2.VBSset tool=CreateObject(“window.manager”)handle=tool.GetWindowHandleFromClass(“notepad”)if handle=0 thenMsgBox“Currently,no editor window open!”elseMsgBox“Notepad title:“&tool.GetWindowName(handle)end ifCl
13、ass is a reserved VBScript key word.Dont use it as a variable name.I did,and I regretted it.Accessing a window by class name and titleTo safely identify a window,you can even provide both pieces of information.Retrieve the handle by window name and class name.This way,you wont geta window of unexpec
14、ted type just because it may share the same name.Use GetWindowHandleand supply the class name as second argument.Accessing the foreground windowThe foreground window is especially important.Its the window that receivesthe keyboard and mouse events,and your SendKeysmethod sends keystrokesto this wind
15、ow.Finding out the foreground window is especially useful.For example,you canautomatically“hook”to a program window your script launched:12-3.VBSset tool=CreateObject(“window.manager”)set wshshell=CreateObject(“WScript.Shell”)launch editorwshshell.Run“notepad.exe”wait until window really is in foreg
16、rounddohandle=tool.GetForegroundWindowHandle find out window classclassname=lcase(tool.GetWindowClassName(handle)is right window in foreground?Chapter 12:Gaining Full Control Over Any Window369II4684-8 ch12.f.qc 3/3/00 9:38 AM Page 369if not classname=“notepad”then wait 1 secWScript.Sleep 1000counte
17、r=counter+1 waited 5 secs,ask whats going on!if counter5 thenMsgBox classnameresponse=MsgBox(“Please click notepad to”_&“foreground!”,vbOkCancel)if response=vbCancel then WScript.Quitcounter=0end ifelseok=trueend ifloop until okMsgBox“Notepad in foreground:Handle“&handleWhen comparing the class name
18、,always use lcaseto convert it to lowercaseletters and compare it to lowercase names.This way,the class name becomescase-insensitive.Wondering which class name a specific window uses?Checkout the scripts above to retrieve the class name.Class names never change,and once you know the class name of yo
19、ur application,you are set forever.The script demonstrates a solution to a common problem.Sometimes,the window you launch from your script doesnt jump into the foreground.Instead,it just nervously flashes its button in the task bar.The reason for thisodd behavior was already explained in Chapter 10.
20、The script works aroundit by checking the foreground window class and asking the user to manuallybring the window to the foreground.Its not very elegant,but since you dontknow the window handle of your launched window,you cant get around it.There are better ways,thoughread on!Accessing a launched wi
21、ndowTheres a better solution!Just use the custom Runcommand you developedin the previous chapter!It returns the ProcessID,and with the help of theProcessID,you can retrieve the associated window handle.As a side effect,you now have a safe way to determine when the window has initialized itself.12-4.
22、VBSset proc=CreateObject(“process.id”)set win=CreateObject(“window.manager”)procID=proc.Run(“notepad.exe”)if procID=0 thenMsgBox“Error launching the program.”WScript.Quitend if wait for the program to initializedohandle=win.GetWindowFromProcess(procID)370Part III:Starting and Controlling Other Softw
23、areII4684-8 ch12.f.qc 3/3/00 9:38 AM Page 370 wait if window isnt on screen yetif handle=0 thencounter=counter+1WScript.Sleep 200 wait 10 secs maxif counter50 thenMsgBox“Your software didnt launch!”WScript.Quitend ifend ifloop while handle=0MsgBox“Editor launched:ID“&handle,vbSystemModalMsgBox“Bury
24、it under some window so I can bring it up-front!”,_vbSystemModal bring window to frontwin.ActivateWindow handleThis script does a number of surprising things.First,it uses your custom Runmethod to retrieve the ProcessID of the program you launched.Next,it loops and waits for GetWindowFromProcessto r
25、eturn the window handle.The moment GetWindowFromProcessreturns a valid handle(non-zero),yourscript knows the program is visible on-screen.It outputs the window handle,as shown by Figure 12-2.Figure 12-2:Check whether or not a program launched,retrieve its window handle,andbring it to the front.GetWi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Windows 脚本 编程 核心技术 Chapter12
限制150内