关于c#:Winform-窗体缩放下-使用剪切板功能会出现页面闪动

43次阅读

共计 1673 个字符,预计需要花费 5 分钟才能阅读完成。

问题

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

演示成果

正文完
 0