最近一直在搞wpf相关的东东,由于还在门外徘徊,所以第一篇blog写了简单的制作扁平化的wpf button样式,这一篇也简单的制作属于自己wpf 窗体的样式。
废话少说,下面就开始制作自己的窗体样式之旅(建立wpf工程就不在这里赘述):
设置自己的窗体要隐藏默认窗体title,需要设置windows的属性: AllowsTransparency 为 true ,
注意:当AllowsTransparency 为true时,WindowStyle.None 是 WindowStyle 的唯一有效值。并且设置windows的Background="Transparent",为透明的效果。
 
通过上面设置,默认windows的样式就变成透明的了,下面就来设置自己的颜色背景的窗体:
窗体主要有两部分,上面的Head,现实标题和,关闭,最大化,最小化按钮、另一部分是现实内容。
我们就把windows分成两部分,上面部分的height设置为30,用来放置窗体的标题和按钮。下面部分为现实内容。
代码如下:
 <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
                <Grid Name="Title">
//title
</Grid>
            <Grid Grid.Row="1" Background="White">
//content
            </Grid>
        </Grid>
上面完成后,就来填充title部分添加如下代码:
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Center"> My window</TextBlock>
                <Button Content="X" HorizontalAlignment="Right" Margin="0,0,5,0" Height="20" Width="20" Style="{DynamicResource myBtnStyle}" VerticalAlignment="Center" Click="Button_Click" />
注:上面的按钮样式为本人上篇博客的设计的简单按钮样式。
现实窗体的名称和关闭按钮。
到现在为止,窗体的基本样式算是搞定了,但是如果一个窗体一直在那里,不能够自由移动,是什么样的一种心情呢。!!惆怅加寂寞。。。!那就人道点,给点自由吧,不做独裁的coder。
在name为title的grid上面添加MouseDown事件:MouseDown="Grid_MouseDown"
具体后台代码实现:
if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
实现左键按下拖动窗体。可以拖动的窗体还是舒服啊。哎
 
到目前为止,有人要问了,你看别人的窗体基本上是圆角的,不能out的一直用尖角吧?那好吧,本人好人一个,就给你圆角的窗体。
在最外层的grid外面加上border标签,进行圆角窗体的设计。
设置其  CornerRadius="4"实现圆角效果。
ok了,一个简单的个性的窗体就实现了,看效果图:
 
代码XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfAppBlog.MainWindow"
x:Name="Window" Background="Transparent"
Title="MainWindow" AllowsTransparency="True" WindowStyle="None"
Width="640" Height="480">
    <Window.Resources>
        <Style x:Key="myBtnStyle" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition x:Name="columnDefinition1" Width="0.982*"/>
                                <ColumnDefinition x:Name="columnDefinition" Width="0.018*"/>
                            </Grid.ColumnDefinitions>
                            <Rectangle x:Name="rectangle" RadiusY="2" RadiusX="2" Stroke="{x:Null}" Fill="#FF0DAD5F" Grid.ColumnSpan="2"/>
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsFocused" Value="True">
                                <Setter Property="Stroke" TargetName="rectangle" Value="{x:Null}"/>
                                <Setter Property="Fill" TargetName="rectangle" Value="#FF0EED80"/>
                            </Trigger>
                            <Trigger Property="IsDefaulted" Value="True">
                                <Setter Property="Fill" TargetName="rectangle" Value="#FF0DAD5F"/>
                                <Setter Property="Stroke" TargetName="rectangle" Value="{x:Null}"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Stroke" TargetName="rectangle" Value="{x:Null}"/>
                                <Setter Property="Fill" TargetName="rectangle" Value="#FF83D245"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Stroke" TargetName="rectangle" Value="{x:Null}"/>
                                <Setter Property="Fill" TargetName="rectangle" Value="#FF17D256"/>
                                <Setter Property="Width" TargetName="columnDefinition" Value="Auto"/>
                                <Setter Property="MinWidth" TargetName="columnDefinition" Value="0"/>
                                <Setter Property="Width" TargetName="columnDefinition1" Value="*"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Stroke" TargetName="rectangle" Value="{x:Null}"/>
                                <Setter Property="Fill" TargetName="rectangle" Value="{x:Null}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Border  BorderBrush="#FF0DAD5F" BorderThickness="3" CornerRadius="4">
    <Grid >
            <Grid.RowDefinitions>
                <RowDefinition Height="30"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
        <Border Background="#FF0DAD5F" MouseDown="Grid_MouseDown" CornerRadius="2,2,0,0">
            <Grid Name="Title">
                <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center"> My window</TextBlock>
                <Button Content="X"  Foreground="White"  HorizontalAlignment="Right" Margin="0,0,5,0" Height="20" Width="20" Style="{DynamicResource myBtnStyle}" VerticalAlignment="Center" Click="Button_Click" />
            </Grid>
            </Border>
            <Grid Grid.Row="1" Background="White">

                <Button Content="Button" Height="56" Margin="130,127,209,0" Style="{DynamicResource myBtnStyle}" VerticalAlignment="Top"/>

            </Grid>
        </Grid>

    </Border>
</Window>
 
后台:
  private void Button_Click(object sender, RoutedEventArgs e)
         {
             this.Close();
         }

         private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
         {
             if (e.LeftButton == MouseButtonState.Pressed)
             {
                 DragMove();
             }
         }
本次制作的简单的窗体,title上面的按钮只实现了关闭按钮,最大化、最小化按钮没有实现,下次有机会才完成了。
 
 
 

Wpf 简单制作自己的窗体样式的更多相关文章

  1. Wpf 简单制作自己的窗体样式(2)

    上一篇blog讲了制作简单的样式的窗体,对于一个传统的窗体,不仅仅可以拖动,和关闭操作.还具有最大化.最小化.隐藏,以及改变窗体的大小等.这篇blog就是对上篇的补充,完善窗体的改变大小和最大化最小化 ...

  2. WPF中制作无边框窗体

    原文:WPF中制作无边框窗体 众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormBorderStyle属性设置为None来完成.如果要制作成异形窗体,则需要使用图片或者使用G ...

  3. 01.WPF中制作无边框窗体

    [引用:]http://blog.csdn.net/johnsuna/article/details/1893319   众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormB ...

  4. wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合

    wpf 导出Excel   1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...

  5. WPF自定义Window窗体样式

    资源文件代码: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation ...

  6. WPF简单入门总结

    WPF简单总结 最近看了点关于WPF的东西,总结了点点入门的东西. XAML语法基础 1.  定义样式 <Window.Resources><!--窗体资源的定义--> < ...

  7. .NET CORE(C#) WPF简单菜单MVVM绑定

    微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. .NET CORE(C#) WPF简单菜单MVVM绑定 阅读导航 本文背景 代码实现 本文参考 ...

  8. Winform自定义窗体样式,实现标题栏可灵活自定义

    最近在编写C/S结构应用程序时,感觉窗体的标题栏样式太死板了,标题文字不能更改大小.颜色.字体等,按钮不能隐藏等问题,在网上也查找了许多相关的资料,没有找到合适的解决方案,发现许多人也在寻求这个问题, ...

  9. WPF流程图制作系列相关基础一

    WPF流程图制作相关基础一   需求是要通过wpf开发流程图,这个流程图是用户自行拖动配置.   使用过流程图的话,应该大体能想象出流程图拖动配置的样子.这里主要会涉及到的技术知识点就是 wpf拖动相 ...

随机推荐

  1. ArcEngine中打开各种数据源(WorkSpace)的连接http://www.cnblogs.com/feilong3540717/archive/2011/08/07/2129906.html

    ArcEngine中打开各种数据源(WorkSpace)的连接 ArcEngine中打开各种数据源(WorkSpace)的连接 (SDE.personal/File.ShapeFile.CAD数据.影 ...

  2. Windows主机通过SSH连接虚拟机里的Linux系统

    Windows 7 + VMware 11 VMWare里:编辑-虚拟网络编辑器-VMnet8(NAT模式)-NAT设置...添加主机端口和虚拟机IP地址以及虚拟机端口 Windows7系统:wind ...

  3. A real ROCA using Bootstrap, jQuery, Thymeleaf, Spring HATEOAS and Spring MVC

    http://www.tuicool.com/articles/ENfe2u https://github.com/tobiasflohre/movie-database What is the be ...

  4. Linux下VirtualBox出现kernel driver not installed的解决方法

    今天安装好rhel-server-6.6-i386后,再安装VirtualBox成功,但是再VirtualBox中创建虚拟机的时候出现了“不能为xx虚拟机打开新任务” 并弹出如下的错误信息:

  5. CF Two Buttons (BFS)

    Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  6. 【数论】UVa 10586 - Polynomial Remains

    Problem F: Polynomial Remains Given the polynomial a(x) = an xn + ... + a1 x + a0, compute the remai ...

  7. 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10(转)

     在网站开发中不免因为各种兼容问题苦恼,针对兼容问题,其实IE给出了解决方案Google也给出了解决方案百度也应用了这种方案去解决IE的兼容问题 百度源代码如下 <!Doctype html&g ...

  8. Java Concurrency - Lock

    Lock 是 Java API 提供的另一种线程同步机制,它提供了比 synchronized 关键字更为灵活.强大的锁定操作. 锁是控制多个线程对共享资源进行访问的工具.通常,所提供了对共享资源的独 ...

  9. MyBatis(3.2.3) - Dynamic SQL

    Sometimes, static SQL queries may not be sufficient for application requirements. We may have to bui ...

  10. Django学习--9 Admin

    1.vim settings.py 打开  'django.contrib.admin' vim urls.py 打开 from django.contrib import admin     (注意 ...