From 9d27e16bc6bb3159cbab114d3d6ad14a6f070eb7 Mon Sep 17 00:00:00 2001 From: vessel Date: Thu, 27 Feb 2020 22:46:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=E6=9C=89=E5=85=B6=E4=BB=96=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B=E5=8D=A0=E7=94=A8=E5=89=AA=E5=88=87=E6=9D=BF=E6=97=B6?= =?UTF-8?q?,WPF=E7=9A=84Clipboard=E4=BC=9A=E6=9C=89BUG,winform=E7=9A=84?= =?UTF-8?q?=E6=B2=A1=E6=9C=89,=E6=89=80=E4=BB=A5=E6=9A=82=E6=97=B6?= =?UTF-8?q?=E7=94=A8winform=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/ClipService.cs | 30 ++++++++++++++++-------------- util/WinAPIHelper.cs | 3 +++ view/MainWindow.xaml.cs | 23 +++++++++++++++++------ 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/service/ClipService.cs b/service/ClipService.cs index 5e139f9..2d43894 100644 --- a/service/ClipService.cs +++ b/service/ClipService.cs @@ -80,8 +80,8 @@ public string SaveImage(BitmapSource bs) /// /// public void SetValueToClipboard(ClipModel result) - { - + { + if (result.Type == WECHAT_TYPE) { MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(result.ClipValue)); @@ -98,11 +98,11 @@ public void SetValueToClipboard(ClipModel result) bool isExplorer = false; Process[] ps = Process.GetProcesses(); - WinAPIHelper.GetWindowThreadProcessId(WinAPIHelper.GetForegroundWindow(), out int pid); - + WinAPIHelper.GetWindowThreadProcessId(WinAPIHelper.GetForegroundWindow(), out int pid); + foreach (Process p in ps) - { - + { + if (p.Id == pid && p.ProcessName.ToLower() == "explorer") { isExplorer = true; @@ -111,8 +111,8 @@ public void SetValueToClipboard(ClipModel result) } //如果是桌面或者资源管理器则直接粘贴为文件 if (isExplorer) - { - + { + string[] tmp = Path.GetFullPath(result.ClipValue).Split(','); IDataObject data = new DataObject(DataFormats.FileDrop, tmp); @@ -176,12 +176,14 @@ public void SetValueToClipboard(ClipModel result) catch { return; } } else - { - - IDataObject data = new DataObject(DataFormats.Text, result.ClipValue); - - Clipboard.SetDataObject(data, true); - //System.Windows.Forms.Clipboard.SetDataObject(data, true); + { + + IDataObject data = new DataObject(DataFormats.Text, result.ClipValue); + + // Clipboard.SetDataObject(data, true); + + //当有其他进程占用剪切板时,WPF的Clipboard会有BUG,winform的没有,所以暂时用winform的 + System.Windows.Forms.Clipboard.SetDataObject(data, true); } diff --git a/util/WinAPIHelper.cs b/util/WinAPIHelper.cs index 7adff27..b2f9d5c 100644 --- a/util/WinAPIHelper.cs +++ b/util/WinAPIHelper.cs @@ -91,6 +91,9 @@ public static extern IntPtr GetWindow( int wCmd ); + + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); diff --git a/view/MainWindow.xaml.cs b/view/MainWindow.xaml.cs index a685fbc..3f0fedc 100644 --- a/view/MainWindow.xaml.cs +++ b/view/MainWindow.xaml.cs @@ -681,14 +681,24 @@ public void PasteValue(string clipStr) /// /// 是否需要延时,单条需要,批量不需要 private void SetValueToClip(ClipModel result) { - + + try { - clipService.SetValueToClipboard(result); + clipService.SetValueToClipboard(result); + } - catch { } - Thread.Sleep(50); - + catch (Exception e){ + //Console.WriteLine(e.StackTrace); + //IntPtr hwnd = WinAPIHelper.GetOpenClipboardWindow(); + //StringBuilder sb = new StringBuilder(501); + //WinAPIHelper.GetWindowText(hwnd, sb, 500); + //Console.WriteLine("=="); + //Console.WriteLine(sb.ToString()); + //Console.WriteLine("=="); + } + Thread.Sleep(50); + //WinAPIHelper.keybd_event((byte)KeyInterop.VirtualKeyFromKey(Key.LeftCtrl),0,0,0);//按下 //WinAPIHelper.keybd_event((byte)KeyInterop.VirtualKeyFromKey(Key.V),0,0,0);//按下 //WinAPIHelper.keybd_event((byte)KeyInterop.VirtualKeyFromKey(Key.V),0,2,0);//弹起 @@ -698,7 +708,8 @@ private void SetValueToClip(ClipModel result) KeyboardKit.Keyboard.Press(Key.V); KeyboardKit.Keyboard.Release(Key.LeftCtrl); - KeyboardKit.Keyboard.Release(Key.V); + KeyboardKit.Keyboard.Release(Key.V); + }