在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:

<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#:

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();
        }
    }
}


  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您可以使用Microsoft.Office.Interop.PowerPoint命名空间的类来实现在WPF窗口播放PPT。具体步骤如下: 1. 添加对Microsoft.Office.Interop.PowerPoint的引用。 2. 在WPF窗口添加一个WindowsFormsHost控件。 3. 在WindowsFormsHost控件添加一个AxHost控件,并将其类型设置为Microsoft.Office.Interop.PowerPoint.Presentation。 4. 在窗口加载事件,使用Microsoft.Office.Interop.PowerPoint.Application类加载PPT文件。 5. 在AxHost控件的CreateControl方法,使用Microsoft.Office.Interop.PowerPoint.SlideShowSettings类设置PPT的播放方式。 下面是示例代码: ```C# using System.Windows.Forms.Integration; using Microsoft.Office.Interop.PowerPoint; //添加引用:Microsoft.Office.Core、Microsoft.Office.Interop.PowerPoint //在WPF窗口添加一个WindowsFormsHost控件 WindowsFormsHost host = new WindowsFormsHost(); this.grid.Children.Add(host); //在WindowsFormsHost控件添加一个AxHost控件,并将其类型设置为Microsoft.Office.Interop.PowerPoint.Presentation AxHost axHost = new AxHost(); axHost.Dock = DockStyle.Fill; axHost.CreateControl(); axHost.GetType().InvokeMember("Enable", BindingFlags.InvokeMethod, null, axHost, new object[] { true }); axHost.GetType().InvokeMember("DisplayAlerts", BindingFlags.SetProperty, null, axHost, new object[] { PpAlertLevel.ppAlertsNone }); host.Child = axHost; //在窗口加载事件,使用Microsoft.Office.Interop.PowerPoint.Application类加载PPT文件 private void Window_Loaded(object sender, RoutedEventArgs e) { Application pptApp = new Application(); Presentation pptPresentation = pptApp.Presentations.Open(@"D:\test.pptx", MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); axHost.GetType().InvokeMember("Ocx", BindingFlags.SetProperty, null, axHost, new object[] { pptPresentation }); } //在AxHost控件的CreateControl方法,使用Microsoft.Office.Interop.PowerPoint.SlideShowSettings类设置PPT的播放方式 protected override void CreateControl() { base.CreateControl(); if (this.ActiveXInstance != null) { SlideShowSettings slideShowSettings = ((Presentation)this.ActiveXInstance).SlideShowSettings; slideShowSettings.ShowType = PpSlideShowType.ppShowTypeSpeaker; slideShowSettings.Run(); } } ``` 注意:在使用AxHost控件播放PPT时,需要在CreateControl方法设置PPT的播放方式。此外,由于AxHost控件是Windows Forms控件,因此需要使用WindowsFormsHost控件将其嵌入到WPF窗口

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

砖农L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值