110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CSharp
{
public partial class Form1 : Form
{
const string LICENSEID = "{00000000-0000-0000-0000-000000000000}";
const string MOUSEHOOK_CAPTURE_OK_MSG = "MOUSEHOOK_CAPTUREOK_MSG-" + LICENSEID;
const string HIGHLIGHT_CAPTURE_OK_MSG = "HIGHLIGHT_CAPTUREOK_MSG_XUFU_V1-" + LICENSEID;
const int MOD_ALT = 0x0001;
const int MOD_CONTROL = 0x0002;
const int MOD_SHIFT = 0x0004;
const int MOD_WIN = 0x0008;
const int VK_LBUTTON = 0x01;
const int VK_RBUTTON = 0x02;
const int VK_MBUTTON = 0x04;
const int SW_HIDE = 0;
const int SW_SHOW = 5;
UInt32 MOUSEHOOK_CAPTURE_OK = 0;
UInt32 HIGHLIGHT_CAPTURE_OK = 0;
const int HOTKEY_CAPTURE = 1000;
const int MAX_OUTPUT_LEN = 4096;
const int WM_HOTKEY = 0x0312;
bool bGetWordUnloaded = false;
[DllImport("user32.dll")]
static extern uint RegisterWindowMessage(string lpString);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, UInt32 fsModifiers, Keys vk);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("user32.dll", EntryPoint = "GetCursorPos")]
public extern static bool GetCursorPos(out System.Drawing.Point lpPoint);
[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("kernel32.dll", EntryPoint = "Sleep")]
public static extern void Sleep(int uMilliSec);
[DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
public static extern int FreeLibrary(int hLibModule);
[DllImport("kernel32.dll", EntryPoint = "GetModuleHandle")]
public static extern int GetModuleHandle(string lpModuleName);
[DllImport("Oleaut32.dll", EntryPoint = "SysFreeString")]
public static extern Int32 SysFreeString(string str);
[DllImport("ICall.dll")]
public static extern Int32 GetRealWindow(Int32 x, Int32 y);
[DllImport("ICall.dll")]
public static extern bool GetWord(Int32 hWndCap, Int32 x, Int32 y, [MarshalAs(UnmanagedType.LPWStr)] string pwstrout, Int32 nBufLen, ref Int32 nCursorPos);
[DllImport("ICall.dll")]
public static extern bool GetRectWord(Int32 hWndCap, Int32 left, Int32 top, Int32 right, Int32 bottom, [MarshalAs(UnmanagedType.LPWStr)] string pwstrout, Int32 nBufLen);
[DllImport("ICall.dll")]
public static extern bool GetWordEnableCap(bool bEnable);
[DllImport("ICall.dll")]
public static extern bool SetMouseHook(Int32 hWndNotify);
[DllImport("ICall.dll")]
public static extern bool RemoveMouseHook();
[DllImport("ICall.dll")]
public static extern void MouseSetDelay(Int32 uMilliSec);
[DllImport("ICall.dll")]
public static extern bool MouseEnableCap(bool Enable);
[DllImport("ICall.dll")]
public static extern bool GetHighlightText(Int32 hWndCap, [MarshalAs(UnmanagedType.BStr)] out string pwstrout);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// register the message so that we can get the notification from mouse moving
MOUSEHOOK_CAPTURE_OK = RegisterWindowMessage(MOUSEHOOK_CAPTURE_OK_MSG);
HIGHLIGHT_CAPTURE_OK = RegisterWindowMessage(HIGHLIGHT_CAPTURE_OK_MSG);
// set the global mouse hook
SetMouseHook(Handle.ToInt32());
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
bGetWordUnloaded = true;
// unregister hotkey
UnregisterHotKey(Handle, HOTKEY_CAPTURE);
// remove the global mouse hook
RemoveMouseHook();
int dllhwnd = GetModuleHandle("ICall.dll");
FreeLibrary(dllhwnd);
}
private void CaptureText()
{
// enable GetWord engine
MouseEnableCap(true);
GetWordEnableCap(true);
System.Drawing.Point ptCursor;
GetCursorPos(out ptCursor);
int nCursorPos = -1;
bool bOK = false;
Int32 hWndCap = GetRealWindow(ptCursor.X, ptCursor.Y);
Char[] wstrOut = new Char[MAX_OUTPUT_LEN];
string strtmp = new string(wstrOut);
bOK = GetWord(hWndCap, ptCursor.X, ptCursor.Y,
strtmp, MAX_OUTPUT_LEN, ref nCursorPos);
if (bOK)
{
string strTotal = strtmp;
textBoxAll.Text = strTotal;
textBoxCursorPos.Text = nCursorPos.ToString();
if (nCursorPos>=0)
{
string strCursor = strTotal.Substring(nCursorPos);
strCursor.TrimStart(null);
textBoxCursor.Text = strCursor;
}
else
{
textBoxCursor.Text = "";
}
}
else // failed
{
}
// disable GetWord engine
GetWordEnableCap(false);
// disable text-capturing with mouse cursor
MouseEnableCap(false);
}
private void CaptureHighlightText(IntPtr wParam, IntPtr lParam)
{
// enable GetWord engine
MouseEnableCap(true);
GetWordEnableCap(true);
System.Drawing.Point ptCursor;
GetCursorPos(out ptCursor);
Int32 hWndCap = GetRealWindow(ptCursor.X, ptCursor.Y);
bool bOK = false;
string pbstr = "";
bOK = GetHighlightText(hWndCap, out pbstr);
if (bOK)
{
textHighlight.Text = pbstr;
SysFreeString(pbstr);
}
else // failed
{
textHighlight.Text = "";
}
// disable GetWord engine
GetWordEnableCap(false);
// disable text-capturing with mouse cursor
MouseEnableCap(false);
}
#region Window Procedure
protected override void WndProc(ref Message msg)
{
if (!bGetWordUnloaded)
{
if (msg.Msg == WM_HOTKEY || msg.Msg == MOUSEHOOK_CAPTURE_OK)
CaptureText();
else if (msg.Msg == HIGHLIGHT_CAPTURE_OK)
CaptureHighlightText(msg.WParam, msg.LParam);
}
base.WndProc(ref msg);
}
#endregion Window Procedure
private void checkCursor_CheckedChanged(object sender, EventArgs e)
{
if (checkCursor.Checked)
{
timer.Start();
}
else
{
timer.Stop();
MouseEnableCap(false);
}
checkHighlight.Checked = checkCursor.Checked;
}
private void checkHighlight_CheckedChanged(object sender, EventArgs e)
{
if (checkHighlight.Checked)
{
timer.Start();
}
else
{
timer.Stop();
MouseEnableCap(false);
}
checkCursor.Checked = checkHighlight.Checked;
}
private void checkHotkey_CheckedChanged(object sender, EventArgs e)
{
if (checkHotkey.Checked)
{
// register hotkey
RegisterHotKey(Handle, HOTKEY_CAPTURE, MOD_CONTROL|MOD_ALT, Keys.P);
}
else
{
// unregister hotkey
UnregisterHotKey(Handle, HOTKEY_CAPTURE);
}
}
private void buttonCapture_Click(object sender, EventArgs e)
{
if (bGetWordUnloaded)
return;
int left, top, right, bottom;
left = int.Parse(textBoxLeft.Text);
top = int.Parse(textBoxTop.Text);
right = int.Parse(textBoxRight.Text);
bottom = int.Parse(textBoxBottom.Text);
ShowWindow(this.Handle, SW_HIDE);
Sleep(100);
GetWordEnableCap(true);
//if want to capture a specified window, pass in the window handle
//and the client rectangle of this window
//if want to capture all the windows in a rectangle, pass in a 0 for
//the window handle, and the screen rectangle
bool bOK = false;
Char[] wstrOut = new Char[MAX_OUTPUT_LEN];
string strtmp = new string(wstrOut);
bOK = GetRectWord(0, left, top, right, bottom, strtmp, MAX_OUTPUT_LEN);
if (bOK)
{
textBoxRect.Text = strtmp;
}
else // failed
{
textBoxRect.Text = "";
}
GetWordEnableCap(false);
ShowWindow(this.Handle, SW_SHOW);
}
private void timer_Tick(object sender, EventArgs e)
{
MouseEnableCap(true);
}
}
}
3.3版的原码,官方发布的。
现在都5.0版了,3.3在win10编译不了
官方现在没发布5.0示例源码
只有它网站有一个API引用指南
http://www.textcapture.com/gb/api.htm