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.

Tip The -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

你可能感兴趣的:(二次开发,tcl,二次开发,tcltk,tcltk)