Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Commit

Permalink
修复图标右键无法点击,以及获取剪切板有时失败的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
NPCDW committed Feb 4, 2022
1 parent 20e966f commit 0d08d39
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion WindowsFormsOCR/GetWords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static String Get()
{
SendCtrlC();
Thread.Sleep(200);
String text = GetDataFromClipboard();
String text = WinApiClipboard.GetText();
return text;
}

Expand Down
13 changes: 7 additions & 6 deletions WindowsFormsOCR/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion WindowsFormsOCR/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected override void WndProc(ref Message m)
case 0x0312: //这个是window消息定义的 注册的热键消息
if (m.WParam.ToString().Equals("845")) //如果是我们注册的那个热键
{
this.Translate_Click(null, null);
this.Translate_HotKey_Click(null, null);
}
if (m.WParam.ToString().Equals("846")) //如果是我们注册的那个热键
{
Expand All @@ -72,6 +72,11 @@ protected override void WndProc(ref Message m)
}

private void Translate_Click(object sender, EventArgs e)
{
MessageBox.Show("先用鼠标选出需要翻译的文本,然后按F2即可");
}

private void Translate_HotKey_Click(object sender, EventArgs e)
{
String getWordsResult = GetWords.Get();
TranslateAndOcrForm form = null;
Expand Down
4 changes: 2 additions & 2 deletions WindowsFormsOCR/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.2")]
[assembly: AssemblyFileVersion("1.0.2")]
4 changes: 2 additions & 2 deletions WindowsFormsOCR/SettingForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions WindowsFormsOCR/WinApiClipboard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsOCR
{
internal class WinApiClipboard
{
[DllImport("User32")]
internal static extern bool OpenClipboard(IntPtr hWndNewOwner);

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

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

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

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

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

internal static void SetText(string text)
{
if (!OpenClipboard(IntPtr.Zero))
{
SetText(text);
return;
}
EmptyClipboard();
SetClipboardData(13, Marshal.StringToHGlobalUni(text));
CloseClipboard();
}

internal static string GetText()
{
string value = string.Empty;
OpenClipboard(IntPtr.Zero);
if (IsClipboardFormatAvailable(13))
{
IntPtr ptr = GetClipboardData(13);
if (ptr != IntPtr.Zero)
{
value = Marshal.PtrToStringUni(ptr);
}
}
CloseClipboard();
return value;
}
}
}
1 change: 1 addition & 0 deletions WindowsFormsOCR/WindowsFormsOCR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<ItemGroup>
<Compile Include="BaiduAIHelper.cs" />
<Compile Include="BaiduCloudHelper.cs" />
<Compile Include="WinApiClipboard.cs" />
<Compile Include="GetWords.cs" />
<Compile Include="GlobalConfig.cs" />
<Compile Include="HotKey.cs" />
Expand Down

0 comments on commit 0d08d39

Please sign in to comment.