📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace HATTAT
{
#region Structs
public struct HWOCRConnections
{
public int X;
public int Y;
public HWOCRConnections(int x, int y)
{
X = x;
Y = y;
}
}
public struct HWOCRCharRecognition
{
public string Directions;
public string Connections;
public string RecognizeAs;
public HWOCRCharRecognition(string d, string c, string r)
{
Directions = d;
Connections = c;
RecognizeAs = r;
}
}
#endregion Structs
public partial class Form1 : Form
{
#region dlls
[DllImport("user32.dll")]
static extern short VkKeyScan(char ch);
[DllImport("user32.dll")]
static extern void keybd_event(short bVk, byte bScan, uint dwFlags, long dwExtraInfo);
[DllImport("user32")]
private static extern Int32 SetForegroundWindow(int hWnd);
#endregion dlls
#region Variables
private byte XDirection = 0;
private byte YDirection = 0;
private ArrayList X = new ArrayList();
private ArrayList Y = new ArrayList();
private StringBuilder OCR = new StringBuilder();
public byte History = 10;
public int Range = 5;
public int ConnectionRange = 10;
private ArrayList C = new ArrayList();
private ArrayList R = new ArrayList();
public int WritingTimeOut = 500;
private string PCRFPath = ""; //Personal Char. Recog. File
public static string RecognizeAs = "";
#endregion Variables
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
trackBar1.Maximum = 100;
trackBar1.Value = Convert.ToInt32(this.Opacity)*100;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
bool XDirChanged = false;
bool YDirChanged = false;
if (e.Button == MouseButtons.Left)
{
timer1.Enabled = false;
timer1.Interval = WritingTimeOut;
timer1.Enabled = true;
Graphics g = pictureBox1.CreateGraphics();
g.DrawEllipse(new Pen(Color.Brown), e.X, e.Y, 5, 5);
X.Add(e.X);
Y.Add(e.Y);
if (X.Count > History)
{
if (XDirection == 0)
{
if ((e.X - (int)X[X.Count - History]) > Range)
{
XDirection = 3;
XDirChanged = true;
}
else if (((int)X[X.Count - History] - e.X) > Range)
{
XDirection = 4;
XDirChanged = true;
}
}
else
{
if ((e.X - (int)X[X.Count - History]) > Range && XDirection == 4)
{
XDirection = 3;
XDirChanged = true;
}
else if (((int)X[X.Count - History] - e.X) > Range && XDirection == 3)
{
XDirection = 4;
XDirChanged = true;
}
}
}
if (Y.Count > History)
{
if (YDirection == 0)
{
if ((e.Y - (int)Y[Y.Count - History]) > Range)
{
YDirection = 2;
YDirChanged = true;
}
else if (((int)Y[Y.Count - History] - e.Y) > Range)
{
YDirection = 1;
YDirChanged = true;
}
}
else
{
if ((e.Y - (int)Y[Y.Count - History]) > Range && YDirection == 1)
{
YDirection = 2;
YDirChanged = true;
}
else if (((int)Y[Y.Count - History] - e.Y) > Range && YDirection == 2)
{
YDirection = 1;
YDirChanged = true;
}
}
}
if (XDirChanged && XDirection != 0)
{
OCR.Append(XDirection);
}
if (YDirChanged && YDirection != 0)
{
OCR.Append(YDirection);
}
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
XDirection = 0;
YDirection = 0;
X.Clear();
Y.Clear();
if (e.Button == MouseButtons.Right)
AnalyseThis();
else
C.Add(new HWOCRConnections(e.X, e.Y));
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
timer1.Enabled = false;
timer1.Interval = WritingTimeOut;
timer1.Enabled = true;
if (e.Button == MouseButtons.Left)
C.Add(new HWOCRConnections(e.X, e.Y));
}
private string ComputeConnections()
{
HWOCRConnections CP;
HWOCRConnections CN;
string CC = "";
for (int i = 1; i < C.Count; i++)
{
bool found = false;
CN = (HWOCRConnections)C[i];
for (int p = 0; p < i; p++)
{
CP = (HWOCRConnections)C[p];
if (((CP.X + ConnectionRange) >= CN.X && CN.X >= (CP.X - ConnectionRange)) &&
((CP.Y + ConnectionRange) >= CN.Y && CN.Y >= (CP.Y - ConnectionRange)))
{
int r = p + 1;
CC += r.ToString();
found = true;
}
}
if (!found)
CC += "0";
CC += "|";
}
C.Clear();
return CC;
}
private void AnalyseThis()
{
string CC = ComputeConnections();
bool found=false;
//
string OCRText = OCR.ToString();
foreach (HWOCRCharRecognition r in R)
{
if (r.Directions == OCRText && r.Connections == CC)
{
switch (r.RecognizeAs)
{
case "BACKSPACE":
if (checkBox1.Checked && comboBox1.Text != "")
SendTextToProcess("BACKSPACE");
else
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length-1);
break;
case "SPACE":
if (checkBox1.Checked && comboBox1.Text != "")
SendTextToProcess(" ");
else
textBox1.Text += " ";
break;
default:
if (checkBox1.Checked && comboBox1.Text!="")
SendTextToProcess(r.RecognizeAs);
else
textBox1.Text += r.RecognizeAs;
break;
}
found = true;
break;
}
}
OCR.Remove(0, OCR.Length);
if (!found && PCRFPath!="")
{
Form2 f2 = new Form2();
f2.ShowDialog();
if (Form1.RecognizeAs != "")
{
FileStream fs = new FileStream(PCRFPath, FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(OCRText + "#" + CC + "#" + Form1.RecognizeAs);
sw.Flush();
sw.Close();
fs.Close();
textBox1.Text += Form1.RecognizeAs;
Form1.RecognizeAs = "";
LoadCharRecognitionTable();
}
}
Graphics Gc = pictureBox1.CreateGraphics();
Gc.Clear(Color.White);
}
private void SendTextToProcess(string lettertosend)
{
Process p = Process.GetProcessById( Convert.ToInt32(comboBox2.Items[comboBox1.SelectedIndex]) );
SetForegroundWindow(p.MainWindowHandle.ToInt32());
switch (lettertosend)
{
case "BACKSPACE":
keybd_event(VkKeyScan(Convert.ToChar(Keys.Back)), 0, 0, 0);
keybd_event(VkKeyScan(Convert.ToChar(Keys.Back)), 0, 0x2, 0);
break;
default:
keybd_event(VkKeyScan(Convert.ToChar(lettertosend)), 0, 0, 0);
keybd_event(VkKeyScan(Convert.ToChar(lettertosend)), 0, 0x2, 0);
break;
}
//0x2 KEYUP
}
private void LoadCharRecognitionTable()
{
R.Clear();
FileStream fs = new FileStream(PCRFPath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
while (sr.Peek() > 0)
{
string Temp = sr.ReadLine();
string[] Temps = Temp.Split('#');
HWOCRCharRecognition CR = new HWOCRCharRecognition(Temps[0].ToString(), Temps[1].ToString(), Temps[2].ToString());
R.Add(CR);
}
sr.Close();
fs.Close();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
XDirection = 0;
YDirection = 0;
X.Clear();
Y.Clear();
AnalyseThis();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
PCRFPath = openFileDialog1.FileName;
LoadCharRecognitionTable();
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
}
private void button1_Click(object sender, EventArgs e)
{
LoadRunningProcesses();
}
private void LoadRunningProcesses()
{
comboBox1.Items.Clear();
comboBox2.Items.Clear();
Process[] procs = Process.GetProcesses();
for (int i = 0; i < procs.Length; i++)
{
if (procs[i].MainWindowTitle != "")
{
comboBox1.Items.Add(procs[i].ProcessName);
comboBox2.Items.Add(procs[i].Id);
}
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
this.Opacity =Convert.ToDouble(trackBar1.Value)/100;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -