本帖最后由 angelkyo 于 2018-3-12 20:56 编辑
选择文本,按下F7
附源码,调用的Ctrl+C方式,还是感觉不太完美
会C#的大神不要藏着掖着,谁会用C#实现这个帖子的取词功能就完美了
https://www.52pojie.cn/thread-706297-1-1.html
希望不要总是拿来主义
希望不要总是只进不出
热键F7版
C#屏幕取.rar
(12.81 KB, 下载次数: 81)
C#屏幕取词源码.rar
(829.83 KB, 下载次数: 189)
鼠标划词版
成品:
GetSelectedText_mouse.rar
(28.1 KB, 下载次数: 134)
源代码:
GetSelectedText.rar
(858.28 KB, 下载次数: 236)
划词语音版:
GetSelectedText_语音版.rar
(90.52 KB, 下载次数: 33)
语音就简单了,一行代码完事,如果需要日语什么的,lan=zh 改为 lan=ja,当然文本框里也得是日语。。。
[C#] 纯文本查看 复制代码
private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
{
this.axWindowsMediaPlayer1.URL = @"http://fanyi.baidu.com/gettts?lan=zh&text=" + textBox1.Text;
}
另:WIN32 API方法不可取,无法获取浏览器里选择的文本
[C#] 纯文本查看 复制代码
#region Win32 API 方法
#region SendMessage 重载
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern bool SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, StringBuilder lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wparam, IntPtr lparam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int SendMessage(IntPtr hWnd, uint Msg, out int wParam, out int lParam);
#endregion
private const uint WM_GETTEXTLENGTH = 0x000E;
private const uint WM_GETTEXT = 0x000D;
private const uint EM_GETSEL = 0xB0;
[DllImport("user32.dll", ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", ExactSpelling = true)]
private static extern IntPtr GetFocus();
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowThreadProcessId(int handle, out int processId);
[DllImport("user32", SetLastError = true, ExactSpelling = true)]
private static extern int AttachThreadInput(int idAttach, int idAttachTo, bool fAttach);
[DllImport("kernel32.dll")]
private static extern int GetCurrentThreadId();
#endregion
private string GetTextFromWin32Api()
{
//获取活动窗口的控件hWnd
int activeWinPtr = GetForegroundWindow().ToInt32();
int activeThreadId = 0;
int processId;
activeThreadId = GetWindowThreadProcessId(activeWinPtr, out processId);
int currentThreadId = GetCurrentThreadId();
if (activeThreadId != currentThreadId)
AttachThreadInput(activeThreadId, currentThreadId, true);
IntPtr activeCtrlId = GetFocus();
//获取全部文本长度
int textlength = (int)SendMessage(activeCtrlId, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero) + 1;
//Have any text at all?
if (textlength > 0)
{
//Get selection
int selstart;
int selend;
SendMessage(activeCtrlId, EM_GETSEL, out selstart, out selend);
StringBuilder sb = new StringBuilder(textlength);
SendMessage(activeCtrlId, WM_GETTEXT, (IntPtr)textlength, sb);
//Slice out selection
string value = sb.ToString();
sb.Clear();
if ((value.Length > 0) && (selend - selstart > 0) && (selstart < value.Length) && (selend < value.Length))
return value.Substring(selstart, selend - selstart);
}
//失败返回null
return null;
}
|