Tcltk 使用twapi小结

关于Twapi的介绍,可以参考如下信息:

The Tcl Windows API (TWAPI) extension provides Tcl bindings tothe Windows API.

The extension provides access to the Windows API at two levels.A direct interface to the supported Windows API is provided wherethe Tcl commands directly map to Windows functions as described inMicrosoft Windows SDK. The recommended interface is a higher levelinterface that is more convenient, powerful and much easier to usethan the raw Windows API.

参考官方网址:http://twapi.sourceforge.net/


Twapi的安装:

下载程序包,并将相关文件加载到tcl库文件中即可,

通过package命令验证是否添加成功


Twapi示例:

1、最小化指定应用的窗口

# TWAPI example
# Minimize all windows for the given application
# Usage example: tclsh minimizeapp.example APPLICATIONNAME

package require twapi

if {$argc != 1} {
    puts stderr "Usage: [info nameofexecutable] $argv0 APPNAME"
    puts stderr "Example: [info nameofexecutable] $argv0 notepad"
    exit 1
}

set appname [lindex $argv 0]
# Add an extension if none specified
if {[file extension $appname] == ""} {
    append appname .*
}

# Get all pids with that app name
set pids [twapi::get_process_ids -glob -name $appname]

# Only minimize if they are marked as visible. This is important
# else hidden windows will be placed on the taskbar as icons
foreach hwin [twapi::find_windows -pids $pids -visible true] {
    twapi::minimize_window $hwin
}

2、向指定窗口发送文本信息

# TWAPI example
# Send a text string to the specified window

package require twapi

if {$argc != 2} {
    puts stderr "Usage: [info nameofexecutable] $argv0 WINDOWTITLE TEXT"
    exit 1
}

set title [lindex $argv 0]
set data  [lindex $argv 1]

# Get all windows with that title
set windows [twapi::find_windows -text $title]
if {[llength $windows]} {
    set win [lindex $windows 0]
    # Set the focus to the window
    twapi::set_focus $win
    # Feed data in to the input queue
    twapi::send_input_text $data
} else {
    puts stderr "No windows found with title '$title'"
}

关于twapi中使用COM接口,处理相关程序的问题,相关帮助文件获取以及方法,参考如下信息:

Discovering properties and methods

When scripting an application via COM, how does one know whatproperties and methods are implemented by a component, theparameter types, default values and so on?

The obvious answers are to look up the component documentation ordo an Internet search. For well documented and widely usedcomponents this is generally more than adequate but there aretimes when documentation is either unavailable orunclear[2].In this case, there are two ways you can get hold of theinterface definitions for a component including its methods andproperties.

  • The oleview program that comes with the Windows SDK candisplay all possible detail about every component registered onthe system including its IDL definition.

  • The -print method of a COMOBJ wrapper object will displaythe properties and methods of the underlying automation object inhuman readable form. This requires the automation object to haveimplemented certain interfaces that provide type information.

% $ie -print
→ IWebBrowser2
  Functions:
          (vtable 208) hresult Navigate2 1 ([in] variant* URL, [in optional] va...
          (vtable 212) hresult QueryStatusWB 1 ([in] OLECMDID cmdID, [out retva...
          (vtable 216) hresult ExecWB 1 ([in] OLECMDID cmdID, [in] OLECMDEXECOP...
          (vtable 220) hresult ShowBrowserBar 1 ([in] variant* pvaClsid, [in op...
          (vtable 224) hresult ReadyState 2 ([out retval] tagREADYSTATE* plRead...
          (vtable 228) hresult Offline 2 ([out retval] bool* pbOffline)
          (vtable 232) hresult Offline 4 ([in] bool pbOffline)
          (vtable 236) hresult Silent 2 ([out retval] bool* pbSilent)
...Additional lines omitted...

The output should be self-explanatory for the most part but alittle discussion is in order.

  • The output lists all interfaces that the object supports.

  • For each interface, all methods and properties are listed.

  • Methods and properties are all implemented as function calls.The integer following the function name indicates whether thefunction implements a method (1), a property retrieval (2) ora property set (4). Note Offline is a read-write propertyas there are entries for retrieval and setting whereasReadyState is a read-only property.

  • The vtable NNN indicates the index of the function into thevirtual dispatch table of the component. From our perspective,it has no real relevance.

  • The return type of all functions is hresult corresponding tothe HRESULT C type which indicates the status code from thecall. At the scripting level, this is not visible when a call issuccessful. In case of errors, it is stored in the errorCodeglobal variable and a Tcl exception is raised.

  • The “real” return value from a function, if any, is indicatedby the presence of the retval attribute on a parameter. Thecorresponding parameter is not actually to be passed as anargument to the command at the Tcl level. It is returned as theresult of the command.

  • Each parameter is marked with attributes that indicate whetherit is input or output, optional, default values and so on.

TipThe -print method can be particularly useful when you are notsure of the type of object returned from a method which makes itdifficult to look up the documentation.

参考地址:http://www.magicsplat.com/book/com.html#sect_com_discovery

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Overview Tcl Windows API extension overview. Installation Installation requirements and instructions. Win32 raw API Direct access to the Win32 API. Window management Commands related to management of windows. Desktops and Window stations Commands related to desktops and window stations. Windows Shell Windows shell commands. Keyboard and mouse input Simulation of user input and support for system-wide hotkeys. Sound Basic sound functions. Clipboard Commands related to accessing the clipboard. System Operating system and machine configuration and control. Processes, threads and DLLs Commands related to processes, threads and dynamic link libraries. Handles Commands for managing operating system handles. Services Commands for configuring and monitoring Windows services. Network configuration Commands related to network configuration. Disks and Volumes Commands related to disks and volumes. Network shares Commands related to management of network shares. Users and Groups Commands related to user and group accounts. Security and access control Commands related to authentication and access control. Event log Commands relating to reading and writing the Windows event log. Windows INI file Commands for accessing Windows INI files. Console Commands related to Windows consoles. Scheduler Commands related to managing scheduled jobs through the task scheduler. Internationalization Commands related to internationalization. Miscellaneous Various TWAPI ancillary commands. Printers Printer management. Examples Tcl Windows API usage examples. Version History Tcl Windows API extension version history.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值