原文:WPF中制作无边框窗体

众所周知,在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();
        }
    }
}

如果是异形窗体呢,比如是个这样的窗体形状:

如何做?其实道理都一样,只是画法不同而已(想知道怎么画出来的?那就看看这里吧:http://blog.csdn.net/johnsuna/archive/2007/09/19/1790997.aspx)。这就有待你举一返三了,不再赘述。

WPF中制作无边框窗体的更多相关文章

  1. 01.WPF中制作无边框窗体

    [引用:]http://blog.csdn.net/johnsuna/article/details/1893319   众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormB ...

  2. winform程序中为无边框窗体手动添加窗体拖动代码

            Point oldMousePoint;//记录开始移动窗口前鼠标点下箭头的位置        Point oldFormPoint;//记录开始移动窗口前窗体位置        // ...

  3. WPF圆角透明无边框窗体

    <Window x:Class="ImportData.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...

  4. 【转】【WPF】 WPF 调用API修改窗体风格实现真正的无边框窗体

    WPF中设置无边框窗体似乎是要将WindowStyle设置为None,AllowTransparency=true,这样才能达到WinForm中无边框窗体的样式.但是AllowTransparency ...

  5. WPF 调用API修改窗体风格实现真正的无边框窗体

    原文:WPF 调用API修改窗体风格实现真正的无边框窗体 WPF中设置无边框窗体似乎是要将WindowStyle设置为None,AllowTransparency=true,这样才能达到WinForm ...

  6. 利用WPF创建含多种交互特性的无边框窗体

    咳咳,标题一口气读下来确实有点累,让我先解释一下.另外文章底部有演示程序的下载. 本文介绍利用WPF创建一个含有以下特性的窗口: 有窗口阴影,比如QQ窗口外围只有几像素的阴影: 支持透明且无边框,为了 ...

  7. 使用WPF创建无边框窗体

    一.无边框窗口添加窗口阴影 实际上在WPF中添加无边框窗口的窗口阴影十分简单. 首先,设置WindowStyle="None"以及AllowsTransparency=" ...

  8. 2017-4-26 winform tab和无边框窗体制作

    TabIndex-----------------------------------确定此控件将占用的Tab键顺序索引 Tabstop-------------------------------指 ...

  9. wpf无边框窗体移动和大小调整

    原文:wpf无边框窗体移动和大小调整   using System; using System.Windows; using System.Windows.Interop; namespace Wpf ...

随机推荐

  1. Cocos2dx 小技巧(十六)再谈visit(getDescription)

    之前两篇都是介绍与Value相关的,这篇我继续这个话题吧,正好凑个"Value三板斧系列...".在非常久非常久曾经.我用写过一篇博客,关于怎样查看CCArray与CCDictio ...

  2. embed-it_Integrator memory compile工具使用之一

    embed-it_Integrator memory compile工具使用之一 主要内容 使用Integrator compile memory 使用Integrator 对比筛选适合的memory ...

  3. [React Native] Disable and Ignore Yellow Box Warnings in React Native

    Yellow box warnings in react native can be intrusive. We will use console.disableYellowBox to disabl ...

  4. php 图片加水印文字水印

    /*给图片加文字水印的方法*/ $dst_path = 'http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg';//保证路径正确 $dst = ...

  5. 【u018】电车

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在一个神奇的小镇上有着一个特别的电车网络,它由一些路口和轨道组成,每个路口都连接着若干个轨道,每个轨道 ...

  6. js如何使用正则表达式验证电话号码(可选区号)和邮箱?(分步骤)

    js如何使用正则表达式验证电话号码(可选区号)和邮箱?(分步骤) 一.总结 js进阶正则表达式16电话号码和邮箱正则(分类解决邮箱验证)(分组加?解决电话号码区号选择问题)([\w\.-]中括号解决邮 ...

  7. css实现图片未加载完成时占位显示

    通过css控制,可以实现加载网络图片时,未加载完成的时候显示本地一张占位图,加载完成后显示网络图片: 原理:通过在img标签的after伪元素上添加一张占位图,并且img都设置为position:re ...

  8. apt-get install安装软件时出现依赖错误解决方式

    在使用apt-get install安装软件时,常常会遇到如上图所看到的错误.该错误的意思为缺少依赖软件.解决方式为: aptitude install golang-go

  9. SQL Server2008生成数据库字典

    1.我们在开发过程中可能会遇到这样的一种情况"当我们进行维护其他人的项目时或者项目的二次开发时可能会对原始的数据表进行分析",这里为大家介绍一种方便快捷生成数据库字典的方式. 我们 ...

  10. 【codeforces 546B】Soldier and Badges

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...