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

<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. spring-data-redis --简单的用spring-data-redis

    spring-data-redis序列化策略 spring-data-redis提供了多种serializer策略,这对使用jedis的开发者而言,实在是非常便捷.sdr提供了4种内置的seriali ...

  2. Method Overloading in WCF zt

    Method overloading is the process of implementing Polymorphism in Object-Oriented Programming. A met ...

  3. Binomial Showdown

    Binomial Showdown TimeLimit: 1 Second   MemoryLimit: 32 Megabyte Totalsubmit: 2323   Accepted: 572 D ...

  4. [转]ESCAPE()、ENCODEURI()、ENCODEURICOMPONENT()区别详解

    escape().encodeURI().encodeURIComponent()区别详解 JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encode ...

  5. selenium webdriver 环境搭建--java

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

  6. codeforces Codeforces 650A Watchmen

    题意:两点(x1,y1), (x2,y2)的曼哈顿距离=欧几里得距离 也就是:x1=x2或y1=y2,再删除重合点造成的重复计数即可. #include <stdio.h> #includ ...

  7. jQuery技术内幕预览版.pdf1

    第一章 总体构架 jQuery模块可以分为3部分:入口模块.底层支持模块和功能模块 浏览器功能测试模块提供了针对不同浏览器功能和bug的测试结果,其它模块基于测试结果解决浏览器之间的兼容性问题 回调函 ...

  8. PHP 遍历文件目录

    /********************** 一个简单的目录递归函数 第一种实现办法:用dir返回对象 ***********************/ function tree($directo ...

  9. 问题-[Delphi]断点后怎么设置条件

    1.设置一个断点(这时某行代码有个红点).2.在红点上右击->"Breakpoint properties..."->"Condition:"-&g ...

  10. Notepad++强大的代码补全和代码提示功能的方法

    最近写项目,经常要打开一些文件去修改一些代码段.那么我的项目都是使用ied大型编辑器去写的,每次修改文件,哪怕是一个标点都要用一分钟时间去打开软件.当然,后来我也考虑到使用记事本,但总感觉不是很爽. ...