01.WPF中制作无边框窗体
[引用:]http://blog.csdn.net/johnsuna/article/details/1893319
众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormBorderStyle属性设置为None来完成。
如果要制作成异形窗体,则需要使用图片或者使用GDI+自定义绘制。
那么,在WPF中,我们怎样制作一个无边框窗体呢?
答案是将Window的WindowStyle属性设置为None,即WindowStyle="None" 。如果是非矩形的异形窗体,则需要将背景设为Null,将允许透明设置为True,也就是:Background="{x:Null}" AllowsTransparency="True",可能有些人还希望这个窗口可以拖来拖去,那么,就还需要设置MouseLeftButtonDown事件,比如:MouseLeftButtonDown="DragWindow",这里DragWindow由Window的DragMove()来完成。想关闭窗口?那就自己做一个按钮,然后使用Window本身的Close()方法吧。
下面是效果:
这里右上角有个圆形的X按钮,是用Button,但将它的样式设置成了圆角矩形制作的。具体代码见下:
XAML代码:
// Window1.xaml
<Window x:Class="BorderlessWindow.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="BorderlessWindow" Height="300" Width="300"
WindowStyle="None" Background="{x:Null}" AllowsTransparency="True"
MouseLeftButtonDown="DragWindow"
>
<Window.Resources>
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<!--设置样式 -->
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle x:Name="Rectangle" Stroke="#FFFFFFFF" StrokeMiterLimit="1.000000" StrokeThickness="0.500000" RadiusX="10" RadiusY="10" Fill="#FF777777">
</Rectangle>
<ContentPresenter x:Name="ContentPresenter" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
</Grid>
<!-- 设置鼠标移到关闭按钮上的效果 -->
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Fill" TargetName="Rectangle">
<Setter.Value>
<SolidColorBrush Color="White"></SolidColorBrush>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Black"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<!-- 窗体中的内容 -->
<Grid>
<!--
窗体的边框,底色设置,注意将CornerRadius与左上角“X”叉形按钮的设置保持一致或约大于叉形按钮的RadiusX/Y设置 -->
<Border CornerRadius="10,10,10,10" Background="Orange" Height="Auto" BorderBrush="Teal" BorderThickness="1"> </Border>
<!--左上角的“X”叉形按钮-->
<Button Name="Button1" Style="{StaticResource ButtonStyle}" Click="CloseWindow" Width="15" Height="15" Content="X" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="3,3,3,3"></Button>
<Button Height="23" Margin="96,101,121,0" Name="button2" VerticalAlignment="Top">Test Button</Button>
</Grid>
</Window>
C#代码:
// Window1.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace BorderlessWindow
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
}
public void DragWindow(object sender, MouseButtonEventArgs args)
{
this.DragMove();
}
public void CloseWindow(object sender, RoutedEventArgs args)
{
this.Close();
}
}
}
01.WPF中制作无边框窗体的更多相关文章
- WPF中制作无边框窗体
原文:WPF中制作无边框窗体 众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormBorderStyle属性设置为None来完成.如果要制作成异形窗体,则需要使用图片或者使用G ...
- winform程序中为无边框窗体手动添加窗体拖动代码
Point oldMousePoint;//记录开始移动窗口前鼠标点下箭头的位置 Point oldFormPoint;//记录开始移动窗口前窗体位置 // ...
- WPF圆角透明无边框窗体
<Window x:Class="ImportData.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...
- 【转】【WPF】 WPF 调用API修改窗体风格实现真正的无边框窗体
WPF中设置无边框窗体似乎是要将WindowStyle设置为None,AllowTransparency=true,这样才能达到WinForm中无边框窗体的样式.但是AllowTransparency ...
- WPF 调用API修改窗体风格实现真正的无边框窗体
原文:WPF 调用API修改窗体风格实现真正的无边框窗体 WPF中设置无边框窗体似乎是要将WindowStyle设置为None,AllowTransparency=true,这样才能达到WinForm ...
- 利用WPF创建含多种交互特性的无边框窗体
咳咳,标题一口气读下来确实有点累,让我先解释一下.另外文章底部有演示程序的下载. 本文介绍利用WPF创建一个含有以下特性的窗口: 有窗口阴影,比如QQ窗口外围只有几像素的阴影: 支持透明且无边框,为了 ...
- 使用WPF创建无边框窗体
一.无边框窗口添加窗口阴影 实际上在WPF中添加无边框窗口的窗口阴影十分简单. 首先,设置WindowStyle="None"以及AllowsTransparency=" ...
- 2017-4-26 winform tab和无边框窗体制作
TabIndex-----------------------------------确定此控件将占用的Tab键顺序索引 Tabstop-------------------------------指 ...
- wpf无边框窗体移动和大小调整
原文:wpf无边框窗体移动和大小调整 using System; using System.Windows; using System.Windows.Interop; namespace Wpf ...
随机推荐
- 又拍云——图像处理师(GraphicsMagick、ImageMagick、FFmpeg)
云处理平台开发工程师 工作地:杭州 岗位职责:负责云处理平台研发工作: 岗位要求: 扎实的 C 语言编程基础及算法优化能力: 至少能够熟练使用一门脚本语言(Python.Ruby.Lua 等)进行日常 ...
- Haskell函数的语法
本章讲的就是 Haskell 那套独特的语法结构,先从模式匹配开始.模式匹配通过检查数据的特定结构来检查其是否匹配,并按模式从中取得数据. 在定义函数时,你可以为不同的模式分别定义函数本身,这就让代码 ...
- 14.8.2 Role of the .frm File for InnoDB Tables InnoDB 表得到 .frm文件的作用
14.8.2 Role of the .frm File for InnoDB Tables InnoDB 表得到 .frm文件的作用 Vsftp:/data01/mysql/zjzc# ls -lt ...
- MFS学习总结
MFS学习总结 MFS概述.特性和新版改进 MFS 工作原理和设计架构 MFS的安装.部署.配置 MFS的高级特性 MFS的性能测试 MFS集群的维护 MFS的常见问题和建议对策 一.MFS概述.特性 ...
- Fragment 常见问题
1. 因为Fragment是在3.0提出的,为了兼容低版本,需要引入一个android-support-v4.jar 2. 需要实例化的activity必须 extends FragmentActiv ...
- PuTTY DSA签名远程缓冲区溢出漏洞(CVE-2013-4207)
漏洞版本: Simon Tatham PuTTY 0.52 - 0.63 漏洞描述: BUGTRAQ ID: 61649 CVE(CAN) ID: CVE-2013-4207 PuTTY是Window ...
- C# 导出 Excel 数字列出现‘0’的解决办法
在DataGird的中某一列全是数字并且长度大于15的字符,在导出excel时数字列第15-18位全部为0. 解决办法:在需导出数字列前加入英文字符状态的单引号(‘ ), 如: <asp:Tem ...
- 利用NSIS软件制作C#安装包
最近在做C#程序安装包,结果网上看到这个软件还是不错的,可以尝试以下. NSIS 是“Nullsoft 脚本安装系统”(Nullsoft Scriptable Installation System) ...
- (java) Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * Lis ...
- GPUImage实现过程
GPUImage就是一个函数的类库,用于对图片实现滤镜的效果. 下面是实现一个最简单的GPUImage的程序和讲解: 首先新建一个项目,导入GPUImage类库(导入过程在我的另一个博客里面有写). ...