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

<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. Codevs_1040_[NOIP2001]_统计单词个数_(划分型动态规划)

    描述 http://codevs.cn/problem/1040/ 与Codevs_1017_乘积最大很像,都是划分型dp. 给出一个字符串和几个单词,要求将字符串划分成k段,在每一段中求共有多少单词 ...

  2. eclipse android重新安装遇到各种问题

    1.JAVA_HOME环境变量失效的解决办法 原文网址:http://www.cnblogs.com/yjmyzz/p/3521554.html 晚上把oracle自带的weblogic给卸载了,然后 ...

  3. GCC参数详解

    GCC参数详解 [介绍] gcc and g++分别是gnu的c & c++编译器 gcc/g++在执行编译工作的时候,总共需要4步 1.预处理,生成.i的文件 2.将预处理后的文件不转换成汇 ...

  4. 计算几何(凸包):SHTSC 2012 信用卡凸包

    这道题是水题,发现平移某些边,答案就是圆心的凸包+一个圆的周长. 不要忽视精度误差! #include <algorithm> #include <iostream> #inc ...

  5. 计算几何(容斥原理,圆交):HDU 5120 Intersection

    Matt is a big fan of logo design. Recently he falls in love with logo made up by rings. The followin ...

  6. strcpy,memcpy,内存块重叠

    前段时间准备面试,看了一些库函数的实现,在看到memcpy时,发现有处理source和destination所指内存有重叠的情况,而strcpy没有,特别模仿库函数写了这个函数,并进行了测试.以下是具 ...

  7. Linux系统下如何修改主机名

    vi /etc/sysconfig/network vi /etc/hosts sudo hostname

  8. LCD framebuffer驱动设计文档

    内容提要:1. android display相关的名词2. 调试LCD驱动需要注意的步骤3. 关于帧缓冲区及I/O内存---------------------------------------- ...

  9. 转载-Linux下搭建VPN服务器(CentOS、pptp)

    转自:http://www.cnblogs.com/sixiweb/archive/2012/11/20/2778732.html 搭建过程参考这篇文章 先说我搭建过程中出现的问题吧: 按照 教程搭建 ...

  10. 【c++】虚函数描写叙述符override

    在C++11中为了帮助程序猿写继承结构复杂的类型,引入了虚函数描写叙述符override,假设派生类在虚函数声明时使用了override描写叙述符,那么该函数必须重载其基类中的同名函数,否则代码将无法 ...