创建 WPF 不规则窗口
本文為khler原作,轉(zhuǎn)載必須確保本文完整并完整保留原作者信息及本文原始鏈接
E-mail:?khler@163.com
QQ:?????23381103
MSN:???pragmac@hotmail.com
=================================================
?
相對于用MFC創(chuàng)建不規(guī)則窗口,WPF創(chuàng)建不規(guī)則窗體的過程就顯得相當享受了,原理清晰、實現(xiàn)簡單。
下面圖片就是演示程序運行效果:
圖1 :鏤空的灰太狼
?
我們的灰太狼先生漂浮在了我的blog編輯器上面;右上角放了一個可以關(guān)閉程序的按鈕;通過鼠標可以拖動灰太狼(窗口)。?
?
基本分兩步就可以輕松實現(xiàn)上面的不規(guī)則窗體效果:
1、背景窗口設(shè)置
????????? a、將窗口的 WindowStyle 設(shè)置成 "None" ,這樣就把 Title bar 去掉了;
????b、將窗口的 AllowsTransparency 設(shè)置成 "True" ,允許透明化處理;
??????????c、將窗口的 Background 設(shè)置成 "Transparent" ,就是把背景設(shè)置成透明色。
?
2、顯示元素的添加
????????? 這就很簡單了,把你要顯示的元素添加進去就行了。最常見的就是加一個不規(guī)則的圖片作為背景,然后在上面甚至“外面”添加其他元素,所謂“外面”,現(xiàn)實的效果就是懸浮在外部了。
?
下面是代碼:
Xaml文件:
?
<Window?????x:Class="NonRectangularWindow.Window1"
????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
????Title="不規(guī)則窗口演示"
????SizeToContent="WidthAndHeight"
????MouseLeftButtonDown="Window_MouseLeftButtonDown"
????WindowStyle="None"AllowsTransparency="True"Background="Transparent">
????<Grid?Width="550"Height="450">
????????<!--非矩形區(qū)域元素-->
????????<Image?Source="/NonRectangularWindow;component/灰太狼1.png"></Image>
????????<Button??Width="60"Height="40"Click="btnClose_Click"Margin="465,23,25,387">關(guān)閉</Button>
????</Grid>
</Window>
復(fù)制代碼
?
后臺C#代碼:
?
usingSystem;usingSystem.Windows;
usingSystem.Windows.Input;
namespaceNonRectangularWindow
{
????publicpartialclassWindow1?:?Window
????{
????????publicWindow1()
????????{
????????????InitializeComponent();
????????}
????????voidbtnClose_Click(objectsender,?RoutedEventArgs?e)
????????{
????????????this.Close();
????????}
????????privatevoidWindow_MouseLeftButtonDown(objectsender,?MouseButtonEventArgs?e)
????????{
????????????this.DragMove();
????????}
????}
} 復(fù)制代碼
?
?
?源代碼:
點擊這里下載本示例程序的源碼。
注:本示例程序需要VS2010、.net framework3.0及以上才能運行。
?
?
?
由于鏤空后有很多邊刺和雜色,加入OuterGlowBitmapEffect效果是個不錯的選擇,按照一樓的提示,修改后的結(jié)果如下:
?????????
?????????????????????? 圖2:以深色桌面為背景的改進版
?
Xaml如下:
?
<Window?????x:Class="NonRectangularWindow.Window1"
????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
????Title="不規(guī)則窗口演示"
????SizeToContent="WidthAndHeight"
????MouseLeftButtonDown="Window_MouseLeftButtonDown"
????WindowStyle="None"AllowsTransparency="True"Background="Transparent">
????<Grid?Width="550"Height="450">
????????<!--非矩形區(qū)域元素-->
????????<Image?Source="/NonRectangularWindow;component/灰太狼1.png">
????????????<Image.BitmapEffect>
????????????????<OuterGlowBitmapEffect?GlowColor="White"GlowSize="3"/>
????????????</Image.BitmapEffect>
????????</Image>
????????<Button??Width="60"Height="40"Click="btnClose_Click"Margin="465,23,25,387">關(guān)閉</Button>
????????<TextBlock?Height="23"HorizontalAlignment="Left"Margin="395,151,0,0"Name="textBlock1"Text="HE?YuanHui"VerticalAlignment="Top">
????????????<TextBlock.BitmapEffect>
????????????????<OuterGlowBitmapEffect/>
????????????</TextBlock.BitmapEffect>
????????</TextBlock>
????????<TextBlock?Height="23"HorizontalAlignment="Left"Margin="413,180,0,0"Name="textBlock2"Text="2010年4月29日"VerticalAlignment="Top"><TextBlock.BitmapEffect>
????????????????<OuterGlowBitmapEffect/>
????????????</TextBlock.BitmapEffect>
????????</TextBlock>
????????<TextBlock?Height="23"HorizontalAlignment="Left"Margin="413,209,0,0"Name="textBlock3"Text="All?rights?reserved."VerticalAlignment="Top"><TextBlock.BitmapEffect><OuterGlowBitmapEffect?/></TextBlock.BitmapEffect></TextBlock>
????</Grid>
</Window> 復(fù)制代碼
?
????? 添加了VS2008的解決方案文件,現(xiàn)在VS2008應(yīng)該也能打開了,不過注意,我的VS2008是打了SP1的,如果你還打不開,建議把VS2008SP1裝上,不過裝2008SP1,還不如裝個VS2010,因為WPF在VS2010里面有相當大的提高,編輯器已經(jīng)非常好用了。
????? 請在這里下載VS2008/VS2010版源代碼。
posted on 2011-12-13 15:33 碧空雪狐 閱讀(...) 評論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/yanpo/archive/2011/12/13/2286187.html
總結(jié)
以上是生活随笔為你收集整理的创建 WPF 不规则窗口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java jpanel 缓冲画图_Jav
- 下一篇: java js关键字_JavaScri