首先创建自定义窗体的资源文件

<ControlTemplate x:Key="BaseWindowControlTemplate" TargetType="Window">
<Grid>
<Grid.Background>
<ImageBrush ImageSource="Images/MainBackground.png" Stretch="Fill"/>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="9*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" >
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Margin="30,20,0,0" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left" Source="pack://application:,,,/WpfControlStyles;component/Images/logo.png"/>
<StackPanel Grid.Column="1" Margin="0,0,20,0" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right">
<StackPanel.Background>
<ImageBrush ImageSource="pack://application:,,,/WpfControlStyles;component/Images/CloseBackground.png"/>
</StackPanel.Background>
<Button x:Name="BtnMinimized" Style="{StaticResource BtnMinimization}" VerticalAlignment="Center" HorizontalAlignment="Center" Content="最小化"/>
<Button x:Name="BtnClose" Style="{StaticResource BtnCloseBaseWindow}" VerticalAlignment="Center" HorizontalAlignment="Center" Content="关闭"/>
</StackPanel>
</Grid>
<ContentPresenter Grid.Row="1" Margin="30"/>
</Grid>
</ControlTemplate>

<Style x:Key="BaseWindow" TargetType="Window">
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="WindowState" Value="Maximized"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="ResizeMode" Value="NoResize"/>
<Setter Property="Template" Value="{StaticResource BaseWindowControlTemplate}"/>
</Style>

将资源文件的样式加载到窗体中

public class BaseWindow : Window
{
ResourceDictionary res;

public BaseWindow()
{
res = new ResourceDictionary();

//获取资源文件
res.Source = new Uri("WpfControlStyles;component/Generic.xaml", UriKind.Relative);

//获取window窗体的样式
this.Style = res["BaseWindow"] as Style;
this.Loaded += BaseWindow_Loaded;
}

public void BaseWindow_Loaded(object sender, RoutedEventArgs e)
{
ControlTemplate baseWindowTemplate = res["BaseWindowControlTemplate"] as ControlTemplate;
Button button = baseWindowTemplate.FindName("BtnClose", this) as Button;
button.Click += delegate
{
this.Close();
};

Button buttonMin = baseWindowTemplate.FindName("BtnMinimized", this) as Button;
buttonMin.Click += delegate
{
this.WindowState = WindowState.Minimized;
};
}
}

wpf 自定义窗体的实现的更多相关文章

  1. WPF自定义窗体仿新毒霸关闭特效(只能在自定义窗体中正常使用)

    比较简单的一个小功能,和新毒霸类似的效果. 效果代码: bool closeStoryBoardCompleted = false; DoubleAnimation closeAnimation1; ...

  2. WPF自定义控件与样式(13)-自定义窗体Window & 自适应内容大小消息框MessageBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 自定义 ...

  3. WPF 创建自定义窗体

    在前面的一篇博客"WPF 自定义Metro Style窗体",展示了如何创建一个类似于Metro Style的Window,并在程序中使用.但是这个窗体不能够自由的改变大小.今天的 ...

  4. WPF 之 自定义窗体标题栏

    在WPF中自定义窗体标题栏,首先需要将窗体的WindowStyle属性设置为None,隐藏掉WPF窗体的自带标题栏.然后可以在窗体内部自定义一个标题栏. 例如,标题栏如下: <WrapPanel ...

  5. 【转】WPF自定义控件与样式(13)-自定义窗体Window & 自适应内容大小消息框MessageBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 自定义Window窗体样式: 基于自定义窗体实现自定义MessageB ...

  6. WPF自定义Window窗体样式

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

  7. WPF自定义产品框架

    在讲解之前先看一下效果,有助于理解: 这是客户端的效果图.整个产品分为两部分:1.WPF开发的展示效果的客户端   2.WCF开发的提供数据服务接口的服务端   本章主要讲解一下实际中开发WPF开发客 ...

  8. WPF自定义Window样式(2)

    1. 引言 在上一篇中,介绍了如何建立自定义窗体.接下来,我们需要考虑将该自定义窗体基类放到类库中去,只有放到类库中,我们才能在其他地方去方便的引用该基类. 2. 创建类库 接上一篇的项目,先添加一个 ...

  9. WPF自定义Window样式(1)

    1. 引言 WPF是制作界面的一大利器.最近在做一个项目,用的就是WPF.既然使用了WPF了,那么理所当然的,需要自定义窗体样式.所使用的代码是在网上查到的,遗憾的是,整理完毕后,再找那篇帖子却怎么也 ...

随机推荐

  1. Windows Azure 上的 Symfony,适用于 PHP 开发者的强大组合

     发布于 2014-06-13 作者 陈 忠岳 Symfony 是针对 PHP 开发者的流行开源 Web 应用框架.现在,您可以更轻松地在 Windows Azure 上使用它,这都归功于 Ben ...

  2. 【转】JAVA字符串格式化-String.format()的使用--不错

    原文网址:http://blog.csdn.net/lonely_fireworks/article/details/7962171 常规类型的格式化 String类的format()方法用于创建格式 ...

  3. 滚动轮播插件——jCarouselLite

    jcarousellite(上下.水平滚动元素插件)插件使用: 参数说明: btnPrev     string 上一个按钮的class名, 比如  btnPrev: ".prev" ...

  4. HDU 2602 Bone Collector

    http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...

  5. Microsoft SQL Server 2008 安装图解(Windows 7)

    简介 本文详细记录了一次完整的Microsoft SQL Server 2008在Windows 7操作系统上的安装过程.注意:Microsoft SQL Server 2008与Windows 7操 ...

  6. Linux Shell脚本中点号和source命令

    Linux中一个文件是根据其是否具有执行属性来判断他是否可以直接运行的.就像Windows下的exe一样.如果我们要执行某一个文件,可以先将其权限修改为可执行(必须是所有者或者root才能修改).然后 ...

  7. ASP.NET内部原理(HttpHandler和HttpModule)

    [IT168 技术文档]在以前的ASP时候,当请求一个*.asp页面文件的时候,这个HTTP请求首先会被一个名为 inetinfo.exe进程所截获,这个进程实际上就是www服务.截获之后它会将这个请 ...

  8. 将多个Sheet导入到同一个Excel文件中

    实体类excel import java.util.List; /** * 功能: * 描述: * @author * @date 2015-3-19 下午5:15:48 */ public clas ...

  9. 存储过程实例总结(开发中的错误与总结,调试,数据库函数DATEDIFF计算当前日期是否在本周内)

    USE [POND] GO /****** Object: StoredProcedure [dbo].[OrderChargeList] Script Date: 04/16/2014 13:32: ...

  10. dd usb 启动盘制作 成功版本

    在linux系统中,使用dd命令制作启动盘成功.方法是在终端中输入命令:     dd if=/root/opensuse.iso of=/dev/sdb bs=4M     说明:      1.o ...