Skip to content

Commit

Permalink
当有其他进程占用剪切板时,WPF的Clipboard会有BUG,winform的没有,所以暂时用winform的
Browse files Browse the repository at this point in the history
  • Loading branch information
silentmoooon committed Feb 27, 2020
1 parent 2f3f373 commit 9d27e16
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
30 changes: 16 additions & 14 deletions service/ClipService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public string SaveImage(BitmapSource bs)
/// </summary>
/// <param name="result"></param>
public void SetValueToClipboard(ClipModel result)
{
{

if (result.Type == WECHAT_TYPE)
{
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(result.ClipValue));
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);


}
Expand Down
3 changes: 3 additions & 0 deletions util/WinAPIHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
23 changes: 17 additions & 6 deletions view/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -681,14 +681,24 @@ public void PasteValue(string clipStr)
/// /// <param name="neadPause">是否需要延时,单条需要,批量不需要</param>
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);//弹起
Expand All @@ -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);



}
Expand Down

0 comments on commit 9d27e16

Please sign in to comment.