文号
t80056
阅读
5442
回复
0
收藏
1
【技巧】用WPF制作部分半透明、不规则的窗口
2016/05/31
IP:四川
用WPF制作半透明、不规则窗口的好处:
1.简单,太简单了
2.可以实现部分半透明(用Win32实现的话很复杂,要用UpdateLayeredWindow)
3.可以实现无锯齿的圆弧边(用Win32实现的话会有锯齿)

但是它也有一些限制:
1.需要使用C#/VB和XAML(与传统C++交互起来有点困难)
2.所有依赖子窗口句柄的部件,包括WebBrowser和WindowsFormsHost,都无法正常显示(因此不能用在需要这些技术的窗口上)

注意以下几点:
1.AllowTransparency="True"必须配合WindowStyle="None",不然会报错
2.为了实现拖拽,可以在任意控件的MouseLeftButtonDown里面调用this.DragMove()以实现窗口拖拽移动
3.这里设置Window.Background="Transparent"并用Border代替Window管理边框,是因为Border提供了圆角功能
4.需要在任何一个地方设置this.DialogResult或调用this.Close()以关闭对话框或窗口

transparentwpf.png

XAML代码:
<window x:class="wpfround.MainWindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"         xmlns:local="clr-namespace:wpfround"         mc:ignorable="d"         title="MainWindow" height="300" width="400"         windowstyle="None"         allowstransparency="True"         mouseleftbuttondown="Window_MouseLeftButtonDown"         background="Transparent">
    <border borderbrush="#ff00ffff" borderthickness="2"             cornerradius="0, 20, 30, 40" background="#eeffffff">
        <grid x:name="rootgrid">
            <textbox x:name="textBox" horizontalalignment="Left" height="23" margin="79,72,0,0" textwrapping="Wrap" text="这里是文本框" verticalalignment="Top" width="203">
            <button x:name="btnOK" content="确定" horizontalalignment="Left" margin="148,234,0,0" verticalalignment="Top" width="75" click="btnOK_Click">
            </button><button x:name="btnCancel" content="取消" horizontalalignment="Left" margin="259,234,0,0" verticalalignment="Top" width="75" click="btnCancel_Click">
        </button></textbox></grid>
    </border>
</window>

C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
    
namespace wpfround
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove(); // 实现拖动
        }
    
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DialogResult = true;
            }
            catch (Exception)
            {
                this.Close();
            }
        }
    
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DialogResult = false;
            }
            catch (Exception)
            {
                this.Close();
            }
        }
    }
}
[ 修改于10年6个月前 - 2016/05/31 19:27:49 ]
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的