Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Windows icons/registry & ico package

10 views
Skip to first unread message

Kevin Walzer

unread,
Jul 5, 2006, 2:44:46 PM7/5/06
to
Retrieving and displaying Windows icons in a Tk application is
surprisingly hard.

I'm trying to put together a little BWidgets demo that simulates a
Windows Explorer directory display, but I'm having trouble retrieving
and displaying icons.

Here is my code for retrieving icons by document type/extension:

proc findnewIcon {extension} {

global icon
if {[catch {set doctype [registry get HKEY_CLASSES_ROOT\\$extension]}]} {
set doctype [registry get HKEY_CLASSES_ROOT\\$extension ""]
puts $doctype
}
if {[catch {set icon [registry get
HKEY_CLASSES_ROOT\\$doctype\\DefaultIcon {}]}]} {
set icon [registry get HKEY_CLASSES_ROOT\\$doctype\\DefaultIcon {} ]
}
return $icon
puts $icon
}

This code retrieves a reference to an icon. For instance, findnewIcon
.doc returns output like this: "C:/path/to/WordPad.exe", 1

However, I can't figure out how to display this icon, at least using the
::ico::getIcon command. I have tried to extract the path to WordPad with
this command:

set imageicon [split $icon ,]

but trying to load the icon with this command: ::ico::getIcon $imageicon
""

returns this error:

unsupported file format EXE"

Apparently it can't read the path because of the quote.

Has anyone successfully combined registry + ico to get the correct icon
for a specific document type to display? If so, any ideas on what I'm
doing wrong?

--
Kevin Walzer
Poetic Code
http://www.kevin-walzer.com

George Petasis

unread,
Jul 6, 2006, 1:35:57 AM7/6/06
to Kevin Walzer
O/H Kevin Walzer έγραψε:

You are using ::ico::getIcon in a wrong way. Try:


package req ico
pack [label .x -image [::ico::getIcon \
{C:\Program Files\Windows NT\Accessories\wordpad.exe} 1]]

If the above does not work, update your tklib package to the latest one,
or use a newer ActiveTcl distribution...

George

Kevin Walzer

unread,
Jul 6, 2006, 10:28:53 AM7/6/06
to
George Petasis wrote:

>
> You are using ::ico::getIcon in a wrong way. Try:
>
>
> package req ico
> pack [label .x -image [::ico::getIcon \
> {C:\Program Files\Windows NT\Accessories\wordpad.exe} 1]]
>
> If the above does not work, update your tklib package to the latest one,
> or use a newer ActiveTcl distribution...
>
> George

This approach seems to require that the path to the .exe/.dll be
hard-coded in my script. What I'm trying to do requires that paths be
parsed and loaded at runtime. I'd like to be able to query the registry
for the path to an .exe or .dll containing the default icon, then hand
that off to the ico package in a variable. Any suggestions on how to do
this (based on the code ample I posted earlier)?

Bryan Oakley

unread,
Jul 6, 2006, 11:32:49 AM7/6/06
to

You wrote the answer yourself - parse the output from the registry.
Quite often the answer to "how do I parse XXX" is "use regexp".

Maybe the solution is something like this:

set data [registry get ...]
# assuming data has something like: "foo.exe", 1
regexp {\"(.*?)\".*?([0-9]+)} $data -- filename n
::ico::getIcon $filename $n


--
Bryan Oakley
http://www.tclscripting.com

Cameron Laird

unread,
Jul 6, 2006, 12:55:16 PM7/6/06
to
In article <B2arg.127187$dW3....@newssvr21.news.prodigy.com>,
Bryan Oakley <oak...@bardo.clearlight.com> wrote:
.
.
.

>You wrote the answer yourself - parse the output from the registry.
>Quite often the answer to "how do I parse XXX" is "use regexp".
>
>Maybe the solution is something like this:
>
> set data [registry get ...]
> # assuming data has something like: "foo.exe", 1
> regexp {\"(.*?)\".*?([0-9]+)} $data -- filename n
> ::ico::getIcon $filename $n
.
.
.
[scan]

That is, while [regexp] is incredibly powerful, and *very* widely
applicable in computing, Tcl's [scan] is a little gem that I believe
is used less than it deserves, particularly in light of the times
when it yields a more readable result than [regexp]. If I under-
stand our hypothetical example correctly, it might yield to

set datum {"foo.exe", 1}
scan $datum {"%[.a-z]", %d} file number
set file
set number
scan

Ramon Ribó

unread,
Jul 6, 2006, 1:19:51 PM7/6/06
to

> set datum {"foo.exe", 1}
> scan $datum {"%[.a-z]", %d} file number
> set file
> set number
> scan

I thinks this is going to fail in the following cases:

a) The path in "datum" contains / or \

b) it contains spaces

c) it contains unicode chars different to [a-z]

Points a) and b) can be easily corrected. In order to fullfill
point c) I think that it is necessary to go to regexps.

Ramon Ribó

En Thu, 06 Jul 2006 18:55:16 +0200, Cameron Laird <cla...@lairds.us>
escribió:

--
Compass Ing. y Sistemas Dr. Ramon Ribo
http://www.compassis.com ram...@compassis.com
c/ Tuset, 8 7-2 tel. +34 93 218 19 89
08006 Barcelona, Spain fax. +34 93 396 97 46

Kevin Walzer

unread,
Jul 9, 2006, 12:36:34 PM7/9/06
to
After a bit more work on this, including regexps (thanks Bryan), I'm
gonna punt--the package that I was hoping to incorporate this work into
doesn't specifically require Windows support. (I was hoping to add
Windows as a nice extra.) Instead the package I release will be
extensible, so if someone with more Windows expertise than me cares to
take this on, then it will be easy for them to do so.
0 new messages