diff --git a/ClipOne/html/js/main.js b/ClipOne/html/js/main.js index dc45280..aae4695 100644 --- a/ClipOne/html/js/main.js +++ b/ClipOne/html/js/main.js @@ -179,11 +179,11 @@ function trSelect(event) { if (!isShiftPressed) { selectItem(index); } - if (clipObj[index].Type == "image") { - previewTimeout = setTimeout(function () { - preview(clipObj[index].ClipValue); - }, 500); - } + //if (clipObj[index].Type == "image") { + // previewTimeout = setTimeout(function () { + // preview(clipObj[index].ClipValue); + // }, 500); + //} } //反选 @@ -272,9 +272,9 @@ function displayData() { } if (clipObj[i].Type == "image") { - trs = " " + num + " "; + trs = " " + num + " "; } else { - trs = " " + clipObj[i].DisplayValue + " " + num + " "; + trs = " " + clipObj[i].DisplayValue + " " + num + " "; } } @@ -452,7 +452,7 @@ function pasteValueByRange(startIndex, endIndex) { return; } - window.external.notify("PasteValueList:" + encodeURIComponent(JSON.stringify(obj))); + window.external.notify("PasteValueList:" + encodeURIComponent(JSON.stringify(clipList))); displayData(); } @@ -468,17 +468,7 @@ function changeWindowHeight(height) { window.external.notify("ChangeWindowHeight:" + height); } -//预览 -function preview(path) { - - window.external.notify("Preview:" + path); -} - -//隐藏预览 -function hidePreview() { - - window.external.notify("HidePreview:" + "11"); -} + function hideUi(){ //scrollTop(); } diff --git a/ClipOne/service/ClipService.cs b/ClipOne/service/ClipService.cs index 040ca98..9f9e824 100644 --- a/ClipOne/service/ClipService.cs +++ b/ClipOne/service/ClipService.cs @@ -153,13 +153,14 @@ public static void SetValueToClipboard(ClipModel result) { try { + IDataObject data = new DataObject(DataFormats.Text, result.ClipValue); System.Windows.Forms.Clipboard.SetDataObject(data, false); } catch (Exception e){ - + //Console.WriteLine(e.StackTrace); } } } diff --git a/ClipOne/view/MainWindow.xaml.cs b/ClipOne/view/MainWindow.xaml.cs index f2698a8..2318925 100644 --- a/ClipOne/view/MainWindow.xaml.cs +++ b/ClipOne/view/MainWindow.xaml.cs @@ -31,7 +31,7 @@ public partial class MainWindow : Window /// public static string cacheDir = "cache"; - + /// /// 配置文件路径 /// @@ -63,7 +63,7 @@ public partial class MainWindow : Window /// private double OpacityRatio = 0.06; - + /// /// 剪切板事件 /// @@ -108,7 +108,7 @@ public partial class MainWindow : Window /// private static int maxRecords = 300; - + /// /// 默认皮肤 @@ -137,11 +137,7 @@ public partial class MainWindow : Window /// private IntPtr wpfHwnd; - - /// - /// 预览窗口 - /// - private PreviewForm preview; + @@ -151,14 +147,14 @@ public MainWindow() System.IO.Directory.SetCurrentDirectory(System.Windows.Forms.Application.StartupPath); - + } private void Window_Loaded(object sender, RoutedEventArgs e) { - - if(!Directory.Exists(cacheDir)) + + if (!Directory.Exists(cacheDir)) { Directory.CreateDirectory(cacheDir); } @@ -167,11 +163,11 @@ private void Window_Loaded(object sender, RoutedEventArgs e) { InitConfig(); } - + //初始化浏览器 InitWebView(); - + //初始化托盘图标 InitialTray(); @@ -185,29 +181,16 @@ private void Window_Loaded(object sender, RoutedEventArgs e) { Hotkey_Click(null, null); } + - //初始化预览窗口 - InitPreviewForm(); - } - - /// - /// 初始化预览窗口 - /// - private void InitPreviewForm() - { - preview = new PreviewForm(this); - preview.Focusable = false; - preview.IsHitTestVisible = false; - preview.IsTabStop = false; - preview.ShowInTaskbar = false; - preview.ShowActivated = false; - } + + /// /// /加载设置项 @@ -248,7 +231,7 @@ private static void InitConfig() opacityValue = double.Parse(settingsMap["opacity"]); } - + } @@ -258,55 +241,50 @@ private static void InitConfig() /// private void InitWebView() { - + webView1.IsJavaScriptEnabled = true; webView1.IsScriptNotifyAllowed = true; - + webView1.IsIndexedDBEnabled = true; webView1.ScriptNotify += WebView1_ScriptNotify; - + webView1.NavigateToLocal(defaultHtml); - + } - + private void WebView1_ScriptNotify(object sender, Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlScriptNotifyEventArgs e) { string[] args = e.Value.Split(':'); - + if (args[0] == "PasteValue") { - - PasteValue(args[1]); - + + PasteValue(args[1]); + } else if (args[0] == "PasteValueList") { - - PasteValueList(args[1]); - + + + PasteValueList(args[1]); + } else if (args[0] == "DeleteImage") { - new Thread( new ParameterizedThreadStart(DeleteFile)).Start(args[1]); + new Thread(new ParameterizedThreadStart(DeleteFile)).Start(args[1]); } else if (args[0] == "ChangeWindowHeight") { ChangeWindowHeight(double.Parse(args[1])); } - else if (args[0] == "Preview") - { - ShowPreviewForm(args[1]); - } - else if (args[0] == "HidePreview") - { - HidePreview(); - } + else if (args[0] == "clearImage") { new Thread(new ParameterizedThreadStart(ClearImage)).Start(args[1]); - }else if (args[0] == "esc") + } + else if (args[0] == "esc") { DiyHide(); } @@ -314,33 +292,33 @@ private void WebView1_ScriptNotify(object sender, Microsoft.Toolkit.Win32.UI.Con private void ClearImage(object images) { - - string [] image = JsonConvert.DeserializeObject(HttpUtility.UrlDecode(images.ToString())); - foreach(var img in Directory.GetFiles(cacheDir)) + + string[] image = JsonConvert.DeserializeObject(HttpUtility.UrlDecode(images.ToString())); + foreach (var img in Directory.GetFiles(cacheDir)) { - if(!image.Contains(img.Replace("\\", "/"))) + if (!image.Contains(img.Replace("\\", "/"))) { DeleteFile(img); } - + } - + } - private void DeleteFile(object path) + private void DeleteFile(object path) { for (int i = 0; i < 3; i++) { Thread.Sleep(i * 500); try { - File.Delete(path.ToString()); + File.Delete(path.ToString()); return; } catch { } - + } } @@ -380,12 +358,12 @@ private void InitialTray() opaSet.Click += (sender, e) => { - + OpacitySet os = new OpacitySet(this, (1 - opacityValue) / OpacityRatio); os.Topmost = true; os.ShowDialog(); - + }; clear.Click += Clear_Click; @@ -637,7 +615,7 @@ private void Clear_Click(object sender, EventArgs e) string[] list = Directory.GetFiles(cacheDir); Directory.Delete(cacheDir, true); Directory.CreateDirectory(cacheDir); - + webView1.InvokeScript("clear"); } @@ -742,13 +720,13 @@ public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bo if (hotkeyAtom == wParam.ToInt32()) { - + activeHwnd = WinAPIHelper.GetForegroundWindow(); - + this.Topmost = true; this.Activate(); - - + + WinAPIHelper.POINT point = new WinAPIHelper.POINT(); if (WinAPIHelper.GetCursorPos(out point)) { @@ -799,9 +777,9 @@ private async void EnQueue(ClipModel clip) string json = JsonConvert.SerializeObject(clip); json = HttpUtility.UrlEncode(json); - - - await webView1.InvokeScriptAsync("addData", json ); + + + await webView1.InvokeScriptAsync("addData", json); } @@ -812,7 +790,7 @@ private async void EnQueue(ClipModel clip) /// private async void ShowWindowAndList() { - + await webView1.InvokeScriptAsync("showRecord"); @@ -824,7 +802,7 @@ private async void ShowWindowAndList() /// 页面高度 public void ChangeWindowHeight(double height) { - + this.Height = height + 21; WinAPIHelper.POINT point = new WinAPIHelper.POINT(); @@ -842,7 +820,7 @@ public void ChangeWindowHeight(double height) } - + private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { @@ -875,84 +853,32 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs public void PasteValue(string clipStr) { DiyHide(); - preview.Hide(); + ClipModel clip = JsonConvert.DeserializeObject(HttpUtility.UrlDecode(clipStr)); - - //从显示列表中获取记录,并根据sourceId从对保存列表中的该条记录做相应处理 - if (clip.Type == FILE_TYPE) - { - string[] files = clip.ClipValue.Split(','); - foreach (string str in files) - { - if (!File.Exists(str)) - { - MessageBox.Show("源文件缺失,粘贴失败!"); - return; - } - } - } SinglePaste(clip); - // new Thread(new ParameterizedThreadStart(SinglePaste)).Start(clip); - - - } - - /// - /// 隐藏预览窗口 - /// - public void HidePreview() - { - - if (preview.IsVisible) - preview.Hide(); - - - - } - - /// - /// 显示图片预览窗口 - /// - /// - public void ShowPreviewForm(string path) - { - preview.Hide(); - - preview.ImgPath = path; - - preview.Show(); } + /// /// 粘贴条目到活动窗口 /// /// 需要粘贴的值 /// /// 是否需要延时,单条需要,批量不需要 - private void SetValueToClip(ClipModel result, bool neadPause) + private void SetValueToClip(ClipModel result) { - //设置剪切板前取消监听 - WinAPIHelper.RemoveClipboardFormatListener(wpfHwnd); ClipService.SetValueToClipboard(result); - //this.Dispatcher.Invoke(new Action(delegate - //{ - //})); + Thread.Sleep(100); - //设置剪切板后恢复监听 - WinAPIHelper.AddClipboardFormatListener(wpfHwnd); - if (neadPause) - { - Thread.Sleep(50); - } System.Windows.Forms.SendKeys.SendWait("^v"); - + } @@ -968,16 +894,10 @@ private void Window_Deactivated(object sender, EventArgs e) /// private void WindowLostFocusHandle() { + + DiyHide(); - if (preview != null) - { - preview.Hide(); - } - - DiyHide(); - - } @@ -989,10 +909,12 @@ private void WindowLostFocusHandle() /// 起始索引 public void PasteValueList(string clipListStr) { + List clipList = JsonConvert.DeserializeObject>(HttpUtility.UrlDecode(clipListStr)); DiyHide(); - preview.Hide(); - new Thread(new ParameterizedThreadStart(BatchPaste)).Start(clipList); + //preview.Hide(); + // new Thread(new ParameterizedThreadStart(BatchPaste)).Start(clipList); + BatchPaste(clipList); } /// @@ -1001,32 +923,41 @@ public void PasteValueList(string clipListStr) /// private void SinglePaste(ClipModel clip) { - SetValueToClip(clip, true); + //设置剪切板前取消监听 + WinAPIHelper.RemoveClipboardFormatListener(wpfHwnd); + SetValueToClip(clip); + //设置剪切板后恢复监听 + WinAPIHelper.AddClipboardFormatListener(wpfHwnd); } /// /// 批量粘贴,由于循环太快、发送粘贴按键消息太慢,故延时200ms /// /// - private void BatchPaste(object clipList) + private void BatchPaste(List clipList) { - List list = (List)clipList; - for (int i = 0; i < list.Count; i++) + //设置剪切板前取消监听 + WinAPIHelper.RemoveClipboardFormatListener(wpfHwnd); + + + for (int i = 0; i < clipList.Count; i++) { - ClipModel clip = list[i]; - if (i != list.Count - 1) + ClipModel clip = clipList[i]; + if (i != clipList.Count - 1) { clip.ClipValue = clip.ClipValue + "\n"; } - SetValueToClip(clip, false); - Thread.Sleep(300); + SetValueToClip(clip); + Thread.Sleep(100); } + //设置剪切板后恢复监听 + WinAPIHelper.AddClipboardFormatListener(wpfHwnd); } - + /// /// 添加剪切板监听, 更改窗体属性,不在alt+tab中显示 @@ -1036,7 +967,7 @@ private void BatchPaste(object clipList) private void Window_SourceInitialized(object sender, EventArgs e) { - + HwndSource source = PresentationSource.FromVisual(this) as HwndSource; source.AddHook(WndProc); wpfHwnd = (new WindowInteropHelper(this)).Handle; @@ -1045,30 +976,30 @@ private void Window_SourceInitialized(object sender, EventArgs e) int exStyle = (int)WinAPIHelper.GetWindowLong(wpfHwnd, -20); exStyle |= (int)0x00000080; WinAPIHelper.SetWindowLong(wpfHwnd, -20, exStyle); - + } - + /// /// /// private void DiyHide() { - + this.Topmost = false; - - - if (activeHwnd != IntPtr.Zero) - { - WinAPIHelper.SetForegroundWindow(activeHwnd); - } - + + + if (activeHwnd != IntPtr.Zero) + { + WinAPIHelper.SetForegroundWindow(activeHwnd); + } + this.Left = HideLeftValue; } - - + + }