【技巧】用WPF制作部分半透明、不规则的窗口
acmilan2016/05/31软件综合 IP:四川
用WPF制作半透明、不规则窗口的好处:
1.简单,太简单了
2.可以实现部分半透明(用Win32实现的话很复杂,要用UpdateLayeredWindow)
3.可以实现无锯齿的圆弧边(用Win32实现的话会有锯齿)

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

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

transparentwpf.png

XAML代码:
<code class="lang-xml"><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></code>

C#代码:
<code class="lang-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();
            }
        }
    }
}</code>

[修改于 8年0个月前 - 2016/05/31 19:27:49]

来自:计算机科学 / 软件综合
0
已屏蔽 原因:{{ notice.reason }}已屏蔽
{{notice.noticeContent}}
~~空空如也

想参与大家的讨论?现在就 登录 或者 注册

所属专业
所属分类
上级专业
同级专业
acmilan
进士 学者 笔友
文章
461
回复
2934
学术分
4
2009/05/30注册,5年3个月前活动
暂无简介
主体类型:个人
所属领域:无
认证方式:邮箱
IP归属地:未同步
文件下载
加载中...
{{errorInfo}}
{{downloadWarning}}
你在 {{downloadTime}} 下载过当前文件。
文件名称:{{resource.defaultFile.name}}
下载次数:{{resource.hits}}
上传用户:{{uploader.username}}
所需积分:{{costScores}},{{holdScores}}下载当前附件免费{{description}}
积分不足,去充值
文件已丢失

当前账号的附件下载数量限制如下:
时段 个数
{{f.startingTime}}点 - {{f.endTime}}点 {{f.fileCount}}
视频暂不能访问,请登录试试
仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。
音频暂不能访问,请登录试试
支持的图片格式:jpg, jpeg, png
插入公式
评论控制
加载中...
文号:{{pid}}
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

加载中...
详情
详情
推送到专栏从专栏移除
设为匿名取消匿名
查看作者
回复
只看作者
加入收藏取消收藏
收藏
取消收藏
折叠回复
置顶取消置顶
评学术分
鼓励
设为精选取消精选
管理提醒
编辑
通过审核
评论控制
退修或删除
历史版本
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{format('YYYY/MM/DD HH:mm:ss', toc)}}