Winform 窗体缩放下 使用剪切板功能会出现页面闪动

问题

Winform窗体,在首次使用剪切板功能时会出现页面闪动。

原因

使用了PresentionCore.dll里的Clipboard导致的,具体原因未知

解决

使用其他方法进行剪切板功能,比如WindowAPI,Forms的Clipboard

演示代码

MyClipboard.cs

using System;
using System.Runtime.InteropServices;

namespace WindowsFormsApp2
{
    class MyClipboard
    {
        [DllImport("User32")]
        public static extern bool OpenClipboard(IntPtr hWndNewOwner);

        [DllImport("User32")]
        public static extern bool CloseClipboard();

        [DllImport("User32")]
        public static extern bool EmptyClipboard();

        [DllImport("User32")]
        public static extern bool IsClipboardFormatAvailable(int format);

        [DllImport("User32")]
        public static extern IntPtr GetClipboardData(int uFormat);

        [DllImport("User32", CharSet = CharSet.Unicode)]
        public static extern IntPtr SetClipboardData(int uFormat, IntPtr hMem);

        public static void FormsCopy()
        {
            System.Windows.Forms.Clipboard.SetDataObject("Forms");
        }

        public static void PresentationCoreCopy()
        {
            System.Windows.Clipboard.SetDataObject("PresentationCore");
        }

        public static void Win32Copy()
        {
            if (!OpenClipboard(IntPtr.Zero))
            {
                return;
            }
            EmptyClipboard();
            SetClipboardData(13, Marshal.StringToHGlobalUni("Win32"));
            CloseClipboard();
        }
    }
}

Form1.cs


using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Configuration;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Drawing;
using System.Threading;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Normal;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MyClipboard.FormsCopy();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MyClipboard.PresentationCoreCopy();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            MyClipboard.Win32Copy();
        }
    }
}

演示效果

你可能感兴趣的:(Winform 窗体缩放下 使用剪切板功能会出现页面闪动)