在WPF窗口中增加水印效果

**

原理:

**

以Canvas作为水印显示载体,在Canvas中创建若干个TextBlock控件用来显示水印文案,如下图所示

然后以每一个TextBlock的左上角为中心旋转-30°,最终效果会是如图红线所示:

为了达到第一行旋转后刚好与窗口上边沿齐平,需要计算第一行其实位置的Top坐标,由于旋转角度为-30,由正余弦可以得出第一行的高度应该是斜边(文字宽度,即上图中红线的长度)的一半(sin30°)

接下来确定行间距已经行中间距,首先可以确定行间距为150(height),为了达到如下图所示旋转后在同一直线上,见下图

即width = height * sqrt(3),如果计算结果width小于文字宽度加上文字间隔,则以文字宽度加文字间隔重新计算height

之后根据宽高可计算出一共需要多个TextBlock,双循环循环创建即可

为了使水印显示在其他控件上面,需要在xaml中最后位置创建Canvas,且Canvas的IsHitTestVisible必须为false(不响应鼠标事件)、透明度根据需要自行设置(如0.2)

最终显示效果见下图:

具体代码如下:

xaml:

c 复制代码
<Window x:Class="WpfApplication1.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:WpfApplication1"
        mc:Ignorable="d" FontSize="18"
        SizeChanged="Window_SizeChanged"
        Title="MainWindow" Height="400" Width="400">
    <Grid>
        <Button Height="32" Width="100" Content="btn" Click="Button_Click"/>

        <!-- 一定要放到最后面,以确保水印覆盖所有其他控件 -->
        <Canvas Name="canvas" Opacity="0.2" IsHitTestVisible="False"/>
    </Grid>
</Window>

c#:

c 复制代码
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string watermark = "Watermark";
        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnContentRendered(EventArgs e)
        {
            base.OnContentRendered(e);

            InitWatermark();
        }

        private void InitWatermark()
        {
            canvas.Children.Clear();

            var formattedText = new FormattedText(
                watermark,
                CultureInfo.CurrentCulture,
                FlowDirection.LeftToRight,
                new Typeface(FontFamily, FontStyle, FontWeight, FontStretch),
                FontSize,
                Brushes.Black,
                new NumberSubstitution(),
                TextFormattingMode.Display);

            var height = 150.0;
            var width = height * Math.Sqrt(3);

            if (width < formattedText.Width + 100)
            {
                width = formattedText.Width + 100;
                height = width / Math.Sqrt(3);
            }

            var firstRowHeight = formattedText.Width / 2;

            int colCount = (int)Math.Ceiling(ActualWidth / width);
            int rowCount = (int)Math.Ceiling((ActualHeight - firstRowHeight) / height);

            for (int i = 0; i < rowCount; ++i)
            {
                for (int j = 0; j < colCount; ++j)
                {
                    TextBlock block = new TextBlock();
                    block.Text = watermark;
                    Canvas.SetTop(block, firstRowHeight + i * height);
                    Canvas.SetLeft(block, j * width);

                    RotateTransform transform = new RotateTransform(-30, 0, 0);
                    block.RenderTransform = transform;

                    canvas.Children.Add(block);
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (canvas.IsVisible)
                canvas.Visibility = Visibility.Collapsed;
            else
                canvas.Visibility = Visibility.Visible;
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            InitWatermark();
        }
    }
}
相关推荐
SamChan901 天前
PDF页面旋转与CropBox陷阱:译文回填位置总是错位的根因实测
python·ai·pdf·wpf
SamChan902 天前
PDF翻译时页眉页脚总在捣乱?跨页重复文本块的检测与过滤实测
人工智能·python·ai·pdf·wpf
samble10 天前
从零搭建灌装监控系统(四):深色主题与样式系统,ResourceDictionary 统一管理
c#·wpf·mvvm·样式·工业控制
_Jonas10 天前
.net framework开发环境正式环境配置文件的切换
.net·wpf
dalong1011 天前
WPF:Modbus 状态监控
wpf·modbus
He BianGu12 天前
【WPF-VisionMaster】机器视觉通用平台V5.0版本发行说明
opencv·c#·wpf·halcon·机器视觉·visionmaster
fogota15 天前
Emoji与Segoe MDL2 Assets的比较
c#·wpf
fogota16 天前
C# WPF 按钮加载字体图标 / 动物头像
c#·.net·wpf
fogota16 天前
C# WPF 按钮加载系统图标 / 动物头像
c#·.net·wpf
军训猫猫头16 天前
C# 封装 MySQL 自动写入与查询工具类(基于 MySqlConnector)
mysql·c#·.net·wpf