-
Notifications
You must be signed in to change notification settings - Fork 13
/
MainForm.cs
188 lines (161 loc) · 6.96 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using LiteDB;
namespace ExamSystem
{
public partial class MainForm : Form
{
#region api
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private LowLevelKeyboardProc _proc ;
private IntPtr _hookID = IntPtr.Zero;
/// <summary>
/// 启动监测程序
/// </summary>
public void RunHook()
{
_hookID = SetHook(_proc);
}
/// <summary>
/// 关闭监测程序
/// </summary>
public void UnHook()
{
UnhookWindowsHookEx(_hookID);
}
private IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
/// <summary>
/// 设置默认值与数据
/// </summary>
/// <param name="initInt"></param>
/// <param name="outString">"F:\图片\精品分类\品牌图片\google徽标\@_@.gif"</param>
public void Init(int initInt, string outString)
{
i = initInt;
str = outString;
}
private static int i = 0;
private static string str = @"I:\over\Images\google\@_@.jpg";
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
Keys key = (Keys)vkCode;
//同时按下Ctrl+V键的时候
if (Control.ModifierKeys == Keys.Control && key.Equals(Keys.C))
{
//获得剪切板数据
IDataObject iData = Clipboard.GetDataObject();
string data = ((String)iData.GetData(DataFormats.Text)).Trim();// Clipboard.GetText(TextDataFormat.UnicodeText).Trim();
#region
txtKeyValue.Text = data;
var models = list.Where(n => n.ProbText.Contains(data)).ToList();
if (models.Count == 1)
{
lblTypeName1.Text = models[0].TypeName;
lblAnswer1.Text = String.IsNullOrEmpty(models[0].Answer) ? "题目寻找答案" : models[0].Answer;
txtDisp1.Text = models[0].ProbText;
}
if (models.Count >= 2)
{
lblTypeName1.Text = models[0].TypeName;
lblAnswer1.Text = String.IsNullOrEmpty(models[0].Answer) ? "题目寻找答案" : models[0].Answer;
txtDisp1.Text = models[0].ProbText;
lblTypeName2.Text = models[1].TypeName;
lblAnswer2.Text = String.IsNullOrEmpty(models[1].Answer) ? "题目寻找答案" : models[1].Answer;
txtDisp2.Text = models[1].ProbText;
}
#endregion
//重新设置剪切板数据
DataObject m_data = new DataObject();
m_data.SetData(DataFormats.Text, true, str.Replace("@_@", Convert.ToString(++i)));
Clipboard.SetDataObject(m_data, true);
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
#endregion
#region 调用API
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
#endregion
public MainForm()
{
InitializeComponent();
using (var db = new LiteDatabase("Prob.db"))
{
var col = db.GetCollection<Problem>("Problem");
var c = col.Count();
list = col.FindAll().ToList();
}
_proc = HookCallback;
RunHook();
}
List<Problem> list = new List<Problem>();
private void button1_Click(object sender, EventArgs e)
{
//每次搜索前都要情况其他控件
txtDisp1.Text = String.Empty;
txtDisp2.Text = String.Empty;
lblAnswer1.Text = String.Empty;
lblAnswer2.Text = String.Empty;
try
{
//获取剪切板数据
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Text)) txtKeyValue.Text = ((String)iData.GetData(DataFormats.Text)).Trim();
//查找满足关键词的题目
var models = list.Where(n => n.ProbText.Contains(txtKeyValue.Text)).ToList();
//最多只显示2个包括选择关键词的题目
if (models.Count == 1)
{
lblTypeName1.Text = models[0].TypeName;
lblAnswer1.Text = String.IsNullOrEmpty(models[0].Answer) ? "题目寻找答案" : models[0].Answer;
txtDisp1.Text = models[0].ProbText;
}
if (models.Count >= 2)
{
lblTypeName1.Text = models[0].TypeName;
lblAnswer1.Text = String.IsNullOrEmpty(models[0].Answer) ? "题目寻找答案" : models[0].Answer;
txtDisp1.Text = models[0].ProbText;
lblTypeName2.Text = models[1].TypeName;
lblAnswer2.Text = String.IsNullOrEmpty(models[1].Answer) ? "题目寻找答案" : models[1].Answer;
txtDisp2.Text = models[1].ProbText;
}
}
catch(Exception err)
{
MessageBox.Show("出现错误,重启后再试!");
}
}
private void 导入题库ToolStripMenuItem_Click(object sender, EventArgs e)
{
ExportForm ef = new ExportForm();
ef.Show();
}
}
}