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

<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. Ext.onReady(function(){} )函数的作用域分析(1)

    Ext.onReady(function(){ var genResultDelete = function(){ alert('delete') ; } var renderResult = fun ...

  2. zabbix监控zookeeper

    在github找到一个不错的模板,直接导入使用.下载地址: https://github.com/zhujinhe/zookeeper-zabbix-template 监控项监控类型为外部检查 zab ...

  3. Lua运算符

    + 加- 减* 乘/ 除^ 幂% 求余# 求长度= 赋值< 小于> 大于== 等于<= 不大于=> 不小于~= 不等于and 逻辑与or 逻辑或not 逻辑非.. 连接字符串

  4. Legal or Not

    Legal or Not Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  5. linux下使用vim替换文件中的^M换行符

    在linux下打开windows编辑过的文本,会出现由于换行符不一致而导致的内容格式错乱的问题.最常见的就是出现^M . 我出现的问题是:在windows编辑过的文件,传到linux上后再用vim打开 ...

  6. selenium webdriver 环境搭建--java

    selenium java环境的安装可以分为三个部分:jdk.eclipse和selenium. jdk jdk(java development kit)是sun公司针对java开发人员的产品,是整 ...

  7. vijosP1038 添加括号

    vijosP1038 添加括号 链接:https://vijos.org/p/1038 [思路] 区间DP. 本题的关键在于如何输出解.对于求和表达式而言可以用一个p[][]记录决策然后递归输出,对于 ...

  8. CF 294C(Java大数做计数)

    题目链接:http://codeforces.com/contest/294/problem/C 代码: import java.util.*; import java.math.*; public ...

  9. 1 storm基本概念 + storm编程规范及demo编写

    本博文的主要内容有 .Storm的单机模式安装 .Storm的分布式安装(3节点)   .No space left on device .storm工程的eclipse的java编写 http:// ...

  10. php开发中的页面跳转方法总结

    PHP页面跳转实现的功能就是将网站中一个网页跳转到另一个网页中.对于刚刚学习PHP语言的朋友来说,是必须要掌握的基础方法. 页面跳转可能是由于用户单击链接.按钮等触发的,也可能是系统自动产生的.页面自 ...