/
FormFind.cs
412 lines (366 loc) · 13.1 KB
/
FormFind.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Be.Windows.Forms;
using System.Collections.Generic;
using System.Text;
namespace Be.HexEditor
{
/// <summary>
/// Summary description for FormFind.
/// </summary>
public class FormFind : Core.FormEx
{
private Be.Windows.Forms.HexBox hexFind;
private System.Windows.Forms.TextBox txtFind;
private System.Windows.Forms.RadioButton rbString;
private System.Windows.Forms.RadioButton rbHex;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel;
private Label lblPercent;
private Label lblFinding;
private CheckBox chkMatchCase;
private Timer timerPercent;
private Timer timer;
private FlowLayoutPanel flowLayoutPanel1;
private FlowLayoutPanel flowLayoutPanel2;
private Panel line;
private IContainer components;
public FormFind()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
rbString.CheckedChanged += new EventHandler(rb_CheckedChanged);
rbHex.CheckedChanged += new EventHandler(rb_CheckedChanged);
}
void ByteProvider_Changed(object sender, EventArgs e)
{
ValidateFind();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormFind));
this.txtFind = new System.Windows.Forms.TextBox();
this.rbString = new System.Windows.Forms.RadioButton();
this.rbHex = new System.Windows.Forms.RadioButton();
this.label1 = new System.Windows.Forms.Label();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.lblPercent = new System.Windows.Forms.Label();
this.lblFinding = new System.Windows.Forms.Label();
this.chkMatchCase = new System.Windows.Forms.CheckBox();
this.timerPercent = new System.Windows.Forms.Timer(this.components);
this.timer = new System.Windows.Forms.Timer(this.components);
this.hexFind = new Be.Windows.Forms.HexBox();
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.line = new System.Windows.Forms.Panel();
this.flowLayoutPanel2 = new System.Windows.Forms.FlowLayoutPanel();
this.flowLayoutPanel1.SuspendLayout();
this.flowLayoutPanel2.SuspendLayout();
this.SuspendLayout();
//
// txtFind
//
resources.ApplyResources(this.txtFind, "txtFind");
this.txtFind.Name = "txtFind";
this.txtFind.TextChanged += new System.EventHandler(this.txtString_TextChanged);
//
// rbString
//
resources.ApplyResources(this.rbString, "rbString");
this.rbString.Checked = true;
this.rbString.Name = "rbString";
this.rbString.TabStop = true;
//
// rbHex
//
resources.ApplyResources(this.rbHex, "rbHex");
this.rbHex.Name = "rbHex";
//
// label1
//
resources.ApplyResources(this.label1, "label1");
this.label1.ForeColor = System.Drawing.Color.Blue;
this.label1.Name = "label1";
//
// btnOK
//
resources.ApplyResources(this.btnOK, "btnOK");
this.btnOK.Name = "btnOK";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
resources.ApplyResources(this.btnCancel, "btnCancel");
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Name = "btnCancel";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// lblPercent
//
resources.ApplyResources(this.lblPercent, "lblPercent");
this.lblPercent.Name = "lblPercent";
//
// lblFinding
//
this.lblFinding.ForeColor = System.Drawing.Color.Blue;
resources.ApplyResources(this.lblFinding, "lblFinding");
this.lblFinding.Name = "lblFinding";
//
// chkMatchCase
//
resources.ApplyResources(this.chkMatchCase, "chkMatchCase");
this.chkMatchCase.Name = "chkMatchCase";
this.chkMatchCase.UseVisualStyleBackColor = true;
//
// timerPercent
//
this.timerPercent.Tick += new System.EventHandler(this.timerPercent_Tick);
//
// timer
//
this.timer.Interval = 50;
this.timer.Tick += new System.EventHandler(this.timer_Tick);
//
// hexFind
//
resources.ApplyResources(this.hexFind, "hexFind");
//
//
//
this.hexFind.BuiltInContextMenu.CopyMenuItemImage = global::Be.HexEditor.images.CopyHS;
this.hexFind.BuiltInContextMenu.CopyMenuItemText = resources.GetString("hexFind.BuiltInContextMenu.CopyMenuItemText");
this.hexFind.BuiltInContextMenu.CutMenuItemImage = global::Be.HexEditor.images.CutHS;
this.hexFind.BuiltInContextMenu.CutMenuItemText = resources.GetString("hexFind.BuiltInContextMenu.CutMenuItemText");
this.hexFind.BuiltInContextMenu.PasteMenuItemImage = global::Be.HexEditor.images.PasteHS;
this.hexFind.BuiltInContextMenu.PasteMenuItemText = resources.GetString("hexFind.BuiltInContextMenu.PasteMenuItemText");
this.hexFind.BuiltInContextMenu.SelectAllMenuItemText = resources.GetString("hexFind.BuiltInContextMenu.SelectAllMenuItemText");
this.hexFind.InfoForeColor = System.Drawing.Color.Empty;
this.hexFind.Name = "hexFind";
this.hexFind.ShadowSelectionColor = System.Drawing.Color.FromArgb(((int)(((byte)(100)))), ((int)(((byte)(60)))), ((int)(((byte)(188)))), ((int)(((byte)(255)))));
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Controls.Add(this.label1);
this.flowLayoutPanel1.Controls.Add(this.line);
resources.ApplyResources(this.flowLayoutPanel1, "flowLayoutPanel1");
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Paint += new System.Windows.Forms.PaintEventHandler(this.flowLayoutPanel1_Paint);
//
// line
//
resources.ApplyResources(this.line, "line");
this.line.BackColor = System.Drawing.Color.LightGray;
this.line.Name = "line";
//
// flowLayoutPanel2
//
resources.ApplyResources(this.flowLayoutPanel2, "flowLayoutPanel2");
this.flowLayoutPanel2.Controls.Add(this.btnCancel);
this.flowLayoutPanel2.Controls.Add(this.btnOK);
this.flowLayoutPanel2.Controls.Add(this.lblFinding);
this.flowLayoutPanel2.Controls.Add(this.lblPercent);
this.flowLayoutPanel2.Name = "flowLayoutPanel2";
//
// FormFind
//
this.AcceptButton = this.btnOK;
resources.ApplyResources(this, "$this");
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.BackColor = System.Drawing.SystemColors.Control;
this.CancelButton = this.btnCancel;
this.Controls.Add(this.flowLayoutPanel2);
this.Controls.Add(this.flowLayoutPanel1);
this.Controls.Add(this.chkMatchCase);
this.Controls.Add(this.rbHex);
this.Controls.Add(this.rbString);
this.Controls.Add(this.txtFind);
this.Controls.Add(this.hexFind);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "FormFind";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.Activated += new System.EventHandler(this.FormFind_Activated);
this.flowLayoutPanel1.ResumeLayout(false);
this.flowLayoutPanel1.PerformLayout();
this.flowLayoutPanel2.ResumeLayout(false);
this.flowLayoutPanel2.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private FindOptions _findOptions;
public FindOptions FindOptions
{
get
{
return _findOptions;
}
set
{
_findOptions = value;
Reinitialize();
}
}
public HexBox HexBox { get; set; }
private void Reinitialize()
{
rbString.Checked = _findOptions.Type == FindType.Text;
txtFind.Text = _findOptions.Text;
chkMatchCase.Checked = _findOptions.MatchCase;
rbHex.Checked = _findOptions.Type == FindType.Hex;
if (hexFind.ByteProvider != null)
hexFind.ByteProvider.Changed -= new EventHandler(ByteProvider_Changed);
var hex = this._findOptions.Hex != null ? _findOptions.Hex : new byte[0];
hexFind.ByteProvider = new DynamicByteProvider(hex);
hexFind.ByteProvider.Changed += new EventHandler(ByteProvider_Changed);
}
private void rb_CheckedChanged(object sender, System.EventArgs e)
{
txtFind.Enabled = rbString.Checked;
hexFind.Enabled = !txtFind.Enabled;
if(txtFind.Enabled)
txtFind.Focus();
else
hexFind.Focus();
}
private void rbString_Enter(object sender, EventArgs e)
{
txtFind.Focus();
}
private void rbHex_Enter(object sender, EventArgs e)
{
hexFind.Focus();
}
private void FormFind_Activated(object sender, System.EventArgs e)
{
if(rbString.Checked)
txtFind.Focus();
else
hexFind.Focus();
}
private void btnOK_Click(object sender, System.EventArgs e)
{
_findOptions.MatchCase = chkMatchCase.Checked;
var provider = this.hexFind.ByteProvider as DynamicByteProvider;
_findOptions.Hex = provider.Bytes.ToArray();
_findOptions.Text = txtFind.Text;
_findOptions.Type = rbHex.Checked ? FindType.Hex : FindType.Text;
_findOptions.MatchCase = chkMatchCase.Checked;
_findOptions.IsValid = true;
FindNext();
}
bool _finding;
public void FindNext()
{
if (!_findOptions.IsValid)
return;
UpdateUIToFindingState();
// start find process
long res = HexBox.Find(_findOptions);
UpdateUIToNormalState();
Application.DoEvents();
if (res == -1) // -1 = no match
{
MessageBox.Show(strings.FindOperationEndOfFile, Program.SoftwareName,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (res == -2) // -2 = find was aborted
{
return;
}
else // something was found
{
this.Close();
Application.DoEvents();
if (!HexBox.Focused)
HexBox.Focus();
}
}
private void UpdateUIToNormalState()
{
timer.Stop();
timerPercent.Stop();
_finding = false;
txtFind.Enabled = chkMatchCase.Enabled = rbHex.Enabled = rbString.Enabled
= hexFind.Enabled = btnOK.Enabled = true;
}
private void UpdateUIToFindingState()
{
_finding = true;
timer.Start();
timerPercent.Start();
txtFind.Enabled = chkMatchCase.Enabled = rbHex.Enabled = rbString.Enabled
= hexFind.Enabled = btnOK.Enabled = false;
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
if (_finding)
this.HexBox.AbortFind();
else
this.Close();
}
private void txtString_TextChanged(object sender, EventArgs e)
{
ValidateFind();
}
private void ValidateFind()
{
var isValid = false;
if (rbString.Checked && txtFind.Text.Length > 0)
isValid = true;
if (rbHex.Checked && hexFind.ByteProvider.Length > 0)
isValid = true;
this.btnOK.Enabled = isValid;
}
private void timer_Tick(object sender, EventArgs e)
{
if (lblFinding.Text.Length == 13)
lblFinding.Text = "";
lblFinding.Text += ".";
}
private void timerPercent_Tick(object sender, EventArgs e)
{
long pos = this.HexBox.CurrentFindingPosition;
long length = HexBox.ByteProvider.Length;
double percent = (double)pos / (double)length * (double)100;
System.Globalization.NumberFormatInfo nfi =
new System.Globalization.CultureInfo("en-US").NumberFormat;
string text = percent.ToString("0.00", nfi) + " %";
lblPercent.Text = text;
}
private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
}
}