XAML Crazy Windows

From IronPython Cookbook

This is a port of the code described in this page: Transparent Windows in WPF

import clr
clr.AddReference("System.Xml")
clr.AddReference("PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
clr.AddReference("PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
from System.IO import StringReader
from System.Xml import XmlReader
from System.Windows.Markup import XamlReader
from System.Windows import Window, Application

xaml_str="""
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="A Crazy Window" Height="300" Width="300"
    WindowStyle="None" AllowsTransparency="True"
    Background="{x:Null}"
    >
    <Grid Name="gid">
      <Viewbox Stretch="Uniform">
        <Path Fill="#80D0E0FF" Stroke="Red" StrokeThickness="3" HorizontalAlignment="Center" VerticalAlignment="Center" 
              Data="M79,3L65,82 17,91 50,138 96,157 104,192 175,154 190,167 218,78 156,76 157,9 111,39z"/>
      </Viewbox>
      <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=Title}" 
                 FontSize="18" Background="Transparent" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20" 
                  FontFamily="Impact" Foreground="#C030A060"/>
      <TextBlock Text="Right Click Anywhere to Close" Background="Black" Width="200" Foreground="White" 
                 VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14" TextWrapping="Wrap" />
    </Grid>
</Window>
"""

if __name__ == "__main__":
    xr = XmlReader.Create(StringReader(xaml_str))
    win = XamlReader.Load(xr)
    
    win.FindName('gid').MouseRightButtonDown+=lambda x,y: win.Close()
    win.FindName('gid').MouseLeftButtonDown+=lambda x,y: win.DragMove()
    
    Application().Run(win)


Back to Contents.

TOOLBOX
LANGUAGES