使用 twapi 从工具包加载图标
Loading icon from kit using twapi
我在名为 App
的文件夹中有一个名为 main.tcl
的 tcl 脚本。脚本中的一行使用来自 twapi module 的命令(该行实际上在一个过程中,当用户通过 'X' 关闭应用程序时,我试图将应用程序最小化到系统托盘window 按钮):
package require twapi
# ... code here
set hand [twapi::load_icon_from_file tclkit.ico]
# ... code here
文件 tclkit.ico
与脚本位于同一目录中(即在文件夹 App
中)。
当 main.tcl
是 运行 到 wish
时,脚本可以正常工作,但通过命令行将其包装成可执行文件后,
> tclkit sdx.kit wrap App -runtime tclsh863.exe
可执行文件出现错误,特别是找不到图标文件:
The system cannot find the file specified.
The system cannot find the file specified.
while executing
"LoadImage $hmod $path $type $opts(width) $opts(height) $flags"
(procedure "twapi::_load_image" line 18)
invoked from within
"twapi::load_icon_from_file tclkit.ico"
(procedure "min_to_tray" line 2)
invoked from within
"min_to_tray"
(command for "WM_DELETE_WINDOW" window manager protocol)
目前的解决方法是在与 .exe 相同的目录中复制 tclkit.ico
文件,但我想尽可能避免这种情况,只使用独立的 .exe 文件。我尝试使用完整路径:
set hand [twapi::load_icon_from_file [file join [pwd] App.exe tclkit.ico]]
当我想在 .exe 中读取文件(.txt、.png 文件等)但没有成功时,这通常有效。
基本上,有没有一种方法可以让 .exe 从自身加载 .ico 文件,或者不需要依赖 .exe 应用程序外部文件的其他解决方法?
核心问题是相关的 Windows API 实际上需要一个文件名,而不是更容易包装从存档加载的东西(例如缓冲区)。这意味着您必须将文件从某个地方的存档中复制出来,然后将该名称传递给系统调用。这实际上是 Tcl 在内部为 load
所做的,当它从对 OS 不直接可见的源中提取 DLL 时;它不会为 TWAPI 自动执行它,因为该库的哲学立场是只是一个薄包装器并让调用者处理后果(这确实意味着你可以轻松地做更多的技巧,只要你'重新发明)。
我建议将文件复制到某个地方的临时文件(即,这些东西的标准位置;Tcl 8.6 has file tempfile
以帮助实现这种技巧),然后将完整的文件名传递到 TWAPI 打电话。我认为 Windows API 中的任何地方都可以传递一个简单的文件名,也可以传递一个完整的文件名。 (这样其实很方便。。。)
我在名为 App
的文件夹中有一个名为 main.tcl
的 tcl 脚本。脚本中的一行使用来自 twapi module 的命令(该行实际上在一个过程中,当用户通过 'X' 关闭应用程序时,我试图将应用程序最小化到系统托盘window 按钮):
package require twapi
# ... code here
set hand [twapi::load_icon_from_file tclkit.ico]
# ... code here
文件 tclkit.ico
与脚本位于同一目录中(即在文件夹 App
中)。
当 main.tcl
是 运行 到 wish
时,脚本可以正常工作,但通过命令行将其包装成可执行文件后,
> tclkit sdx.kit wrap App -runtime tclsh863.exe
可执行文件出现错误,特别是找不到图标文件:
The system cannot find the file specified.
The system cannot find the file specified.
while executing
"LoadImage $hmod $path $type $opts(width) $opts(height) $flags"
(procedure "twapi::_load_image" line 18)
invoked from within
"twapi::load_icon_from_file tclkit.ico"
(procedure "min_to_tray" line 2)
invoked from within
"min_to_tray"
(command for "WM_DELETE_WINDOW" window manager protocol)
目前的解决方法是在与 .exe 相同的目录中复制 tclkit.ico
文件,但我想尽可能避免这种情况,只使用独立的 .exe 文件。我尝试使用完整路径:
set hand [twapi::load_icon_from_file [file join [pwd] App.exe tclkit.ico]]
当我想在 .exe 中读取文件(.txt、.png 文件等)但没有成功时,这通常有效。
基本上,有没有一种方法可以让 .exe 从自身加载 .ico 文件,或者不需要依赖 .exe 应用程序外部文件的其他解决方法?
核心问题是相关的 Windows API 实际上需要一个文件名,而不是更容易包装从存档加载的东西(例如缓冲区)。这意味着您必须将文件从某个地方的存档中复制出来,然后将该名称传递给系统调用。这实际上是 Tcl 在内部为 load
所做的,当它从对 OS 不直接可见的源中提取 DLL 时;它不会为 TWAPI 自动执行它,因为该库的哲学立场是只是一个薄包装器并让调用者处理后果(这确实意味着你可以轻松地做更多的技巧,只要你'重新发明)。
我建议将文件复制到某个地方的临时文件(即,这些东西的标准位置;Tcl 8.6 has file tempfile
以帮助实现这种技巧),然后将完整的文件名传递到 TWAPI 打电话。我认为 Windows API 中的任何地方都可以传递一个简单的文件名,也可以传递一个完整的文件名。 (这样其实很方便。。。)