Re: transparency



That is awesome. I haven´t tried it out yet though; I still need to
figure out how to compile twapi first. I think this will be a very
powerful feature though. This should enable us to easily make shaped
windows, windows with holes in them, and desktop widgets like this
transparent clock. Excellent!


palmtcl@xxxxxxxxx wrote:
Following Jeff's pointer below, I added an interface to
SetLayeredWindowAttributes to TWAPI. Sample code to do what you want is
below. Note you need TWAPI 1.1, currently only in CVS, as 1.0 does not
have the SetLayeredWindowAttributes call (or you could use FFIDL to do
the same thing without needing all of TWAPI)

Working code - this works on my sample of one (XP SP2). Note to drag
the clock around you have to click the mouse on an opaque area.

package require Tk
package require twapi 1.1

proc every { ms body } {
eval $body
after $ms [list every $ms $body]

}

proc dragStart {windowX windowY} {
set ::DragHoldPosition(x) $windowX
set ::DragHoldPosition(y) $windowY

}

proc dragTo {screenX screenY} {
set positionX [expr { $screenX - $::DragHoldPosition(x) }]
set positionY [expr { $screenY - $::DragHoldPosition(y) }]
wm geometry . [winfo width .]x[winfo height .]+$positionX+$positionY

}

bind . <Button-1> { dragStart %x %y }
bind . <Button1-Motion> { dragTo %X %Y }
bind . <Button-2> { destroy . }

pack [label .lab -textvariable timevar -font "ansi 54 bold" -foreground
\#aa66ff -background red]
every 500 {set ::timevar [clock format [clock sec] -format %H:%M:%S]}

wm overrideredirect . 1
wm attributes . -topmost 1

# For some reason, we cannot change style bits until window is
visible/mapped
update idletasks

# Get the parent of the Tk toplevel - this is the real toplevel from
# the Windows perspective
set top_id [twapi::get_parent_window [winfo id .]]
# Set its style bits to allow layering
foreach {style exstyle} [twapi::get_window_style $top_id] break
twapi::set_window_style $top_id $style [expr {0x80000+$exstyle}]
# Set transparency color (red is 0x0000ff as per Windows COLORREF
struct
::twapi::SetLayeredWindowAttributes $top_id 0xff 255 1


/Ashok

Jeff Hobbs wrote:

Look for how SetLayeredWindowAttributes is called in tk/win/tkWinWm.c.
Instead of LWA_ALPHA, if you use LWA_COLORKEY and specify an RGB color
that should be transparent, then you could do essentially what you want.
Combine it with a wm overrideredirect window, making it white where
white is the transparent color, anything you draw on that will be all
that you should see. More info at:
Jeff Hobbs, The Tcl Guy, http://www.activestate.com/

.