BioImager  5.0.0
A .NET microscopy imaging application based on Bio library. Supports various microscopes by using imported libraries & GUI automation. Supports XInput game controllers to move stage, take images, run ImageJ macros on images or Bio C# scripts.
Loading...
Searching...
No Matches
CodeView.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Windows.Forms;
10
11namespace BioImager
12{
13 public partial class CodeView : UserControl
14 {
15 private ScrollTextBox textBox = new ScrollTextBox();
16 private ScrollTextBox lineBox = new ScrollTextBox();
17 private int tabSize = 15;
18 /* Creating a new instance of the CodeView class. */
19 public CodeView()
20 {
21 InitializeComponent();
22 textBox.Dock = DockStyle.Fill;
23 textBox.MouseWheel += new MouseEventHandler(Code_MouseWheel);
24 textBox.TextChanged += new EventHandler(textBox_TextChanged);
25 textBox.VScroll += new EventHandler(textBox_Scroll);
26 textBox.FontChanged += new EventHandler(textBox_FontChanged);
27 textBox.WordWrap = false;
28 textBox.AcceptsTab = true;
29 panel.Controls.Add(textBox);
30 lineBox.Dock = DockStyle.Fill;
31 lineBox.ScrollBars = RichTextBoxScrollBars.None;
32 panel2.Controls.Add(lineBox);
33 //MouseWheel += new MouseEventHandler(Code_MouseWheel);
34 textBox.SelectionTabs = new int[] { tabSize, tabSize * 2, tabSize * 3, tabSize * 4, tabSize * 5, tabSize * 6 };
36 }
37 public RichTextBox TextBox
38 {
39 get
40 {
41 return textBox;
42 }
43 }
44
45 /* A property of the textbox. */
46 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
47 public bool WordWrap
48 {
49 get
50 {
51 return textBox.WordWrap;
52 }
53 set
54 {
55 textBox.WordWrap = value;
56 }
57 }
59 public void UpdateScroll()
60 {
61 lineBox.VerticalScrollPosition = textBox.VerticalScrollPosition;
62 }
63
68 public class ScrollTextBox : RichTextBox
69 {
70
72 {
73 _components = new System.ComponentModel.Container();
74 // Calculate width of "W" to set as the small horizontal increment
75 OnFontChanged(null);
76 }
77
78 [System.ComponentModel.DefaultValue(0)
79 , System.ComponentModel.Category("Appearance")
80 , System.ComponentModel.Description
81 ("Gets or sets the vertical scroll bar's position"
82 )
83 ]
85 {
86 set { SetScroll(value, Win32.WM_VSCROLL, Win32.SB_VERT); }
87 get { return GetScroll(Win32.SB_VERT); }
88 }
89
90 [System.ComponentModel.DefaultValue(0)
91 , System.ComponentModel.Category("Appearance")
92 , System.ComponentModel.Description
93 ("Gets or sets the horizontal scroll bar's position"
94 )
95 ]
97 {
98 set { SetScroll(value, Win32.WM_HSCROLL, Win32.SB_HORZ); }
99 get { return GetScroll(Win32.SB_HORZ); }
100 }
101
102 [System.ComponentModel.Description
103 ("Fired when the scroll bar's vertical position changes"
104 )
105 , System.ComponentModel.Category("Property Changed")
106 ]
107 public event System.Windows.Forms.ScrollEventHandler ScrollChanged;
108
109 // Fire scroll event if the scroll-bars are moved
110 protected override void WndProc
111 (ref Message message
112 )
113 {
114 base.WndProc(ref message);
115 if (message.Msg == Win32.WM_VSCROLL
116 || message.Msg == Win32.WM_HSCROLL
117 || message.Msg == Win32.WM_MOUSEWHEEL
118 ) TryFireScrollEvent();
119 }
120
121 // Key-down includes navigation keys, but also typing and pasting can
122 // cause a scroll event
123 protected override void OnKeyDown
124 (System.Windows.Forms.KeyEventArgs e
125 )
126 {
127 base.OnKeyDown(e);
128 TryFireScrollEvent();
129 }
130
134 protected override void OnKeyUp
135 (System.Windows.Forms.KeyEventArgs e
136 )
137 {
138 base.OnKeyUp(e);
139 TryFireScrollEvent();
140 }
141
142 // Resizing can alter the word-wrap or fit the content causing the text
143 // to scroll
144 protected override void OnResize
145 (System.EventArgs e
146 )
147 {
148 base.OnResize(e);
149 TryFireScrollEvent();
150 }
151
152 // Clicking an empty space can move the carot to the beginning of the
153 // line causing a scroll event
154 protected override void OnMouseDown
155 (System.Windows.Forms.MouseEventArgs e
156 )
157 {
158 base.OnMouseDown(e);
159 TryFireScrollEvent();
160 }
161
165 protected override void OnMouseUp
166 (System.Windows.Forms.MouseEventArgs e
167 )
168 {
169 base.OnMouseUp(e);
170 TryFireScrollEvent();
171 }
172
173 // If the mouse button is down, a text selection is probably being done
174 // where dragging the selection can caues scroll events
175 protected override void OnMouseMove
176 (System.Windows.Forms.MouseEventArgs e
177 )
178 {
179 base.OnMouseMove(e);
180 if (e.Button != System.Windows.Forms.MouseButtons.None)
181 TryFireScrollEvent();
182 }
183
184 // Changing the size of the font can cause a scroll event. Also, when
185 // scrolling horizontally, the event will notify whether the scroll
186 // was a large or small change. For vertical, small increments are 1
187 // line, but for horizontal, it is several pixels. To guess what a
188 // small increment is, get the width of the W character and anything
189 // smaller than that will be represented as a small increment
190 protected override void OnFontChanged
191 (System.EventArgs e
192 )
193 {
194 base.OnFontChanged(e);
195 using (System.Drawing.Graphics graphics = this.CreateGraphics())
196 _fontWidth = (int)graphics.MeasureString("W", this.Font).Width;
197 TryFireScrollEvent();
198 }
199
204 protected override void Dispose
205 (bool disposing
206 )
207 {
208 if (disposing && (_components != null))
209 _components.Dispose();
210 base.Dispose(disposing);
211 }
212
219 private void SetScroll
220 (int value
221 , uint windowsMessage
222 , int scrollBarMessage
223 )
224 {
225 Win32.SetScrollPos
226 ((System.IntPtr)this.Handle
227 , scrollBarMessage
228 , value
229 , true
230 );
231 Win32.PostMessage
232 ((System.IntPtr)this.Handle
233 , windowsMessage
234 , 4 + 0x10000 * value
235 , 0
236 );
237 }
238
244 private int GetScroll
245 (int scrollBarMessage
246 )
247 {
248 return Win32.GetScrollPos
249 ((System.IntPtr)this.Handle
250 , scrollBarMessage
251 );
252 }
253
254 // Fire both horizontal and vertical scroll events seperately, one
255 // after the other. These first test if a scroll actually occurred
256 // and won't fire if there was no actual movement
257 private void TryFireScrollEvent()
258 { // Don't do anything if there is no event handler
259 if (ScrollChanged == null)
260 return;
261 TryFireHorizontalScrollEvent();
262 TryFireVerticalScrollEvent();
263 }
264
270 private void TryFireHorizontalScrollEvent()
271 {
272
273 // Don't do anything if there is no event handler
274 if (ScrollChanged == null)
275 return;
276
277 int lastScrollPosition = _lastHorizontalScrollPosition;
278 int scrollPosition = HorizontalScrollPosition;
279
280 // Don't do anything if there was no change in position
281 if (scrollPosition == lastScrollPosition)
282 return;
283
284 _lastHorizontalScrollPosition = scrollPosition;
285
287 (this
288 , new System.Windows.Forms.ScrollEventArgs
289 (scrollPosition < lastScrollPosition - _fontWidth
290 ? System.Windows.Forms.ScrollEventType.LargeDecrement
291 : scrollPosition > lastScrollPosition + _fontWidth
292 ? System.Windows.Forms.ScrollEventType.LargeIncrement
293 : scrollPosition < lastScrollPosition
294 ? System.Windows.Forms.ScrollEventType.SmallDecrement
295 : System.Windows.Forms.ScrollEventType.SmallIncrement
296 , lastScrollPosition
297 , scrollPosition
298 , System.Windows.Forms.ScrollOrientation.HorizontalScroll
299 )
300 );
301
302 }
303
307 private void TryFireVerticalScrollEvent()
308 {
309 // Don't do anything if there is no event handler
310 if (ScrollChanged == null)
311 return;
312 int lastScrollPosition = _lastVerticalScrollPosition;
313 int scrollPosition = VerticalScrollPosition;
314
315 // Don't do anything if there was no change in position
316 if (scrollPosition == lastScrollPosition)
317 return;
318
319 _lastVerticalScrollPosition = scrollPosition;
320
322 (this
323 , new System.Windows.Forms.ScrollEventArgs
324 (scrollPosition < lastScrollPosition - 1
325 ? System.Windows.Forms.ScrollEventType.LargeDecrement
326 : scrollPosition > lastScrollPosition + 1
327 ? System.Windows.Forms.ScrollEventType.LargeIncrement
328 : scrollPosition < lastScrollPosition
329 ? System.Windows.Forms.ScrollEventType.SmallDecrement
330 : System.Windows.Forms.ScrollEventType.SmallIncrement
331 , lastScrollPosition
332 , scrollPosition
333 , System.Windows.Forms.ScrollOrientation.VerticalScroll
334 )
335 );
336
337 }
338
339 private int _lastVerticalScrollPosition;
340 private int _lastHorizontalScrollPosition;
341 private int _fontWidth;
342 private System.ComponentModel.IContainer _components = null;
343
344 private static class Win32
345 {
346
347 public const uint WM_HSCROLL = 0x114;
348 public const uint WM_VSCROLL = 0x115;
349 public const uint WM_MOUSEWHEEL = 0x20A;
350 public const int SB_VERT = 0x1;
351 public const int SB_HORZ = 0x0;
352
353 [System.Runtime.InteropServices.DllImport("user32.dll")]
354 public static extern int SetScrollPos
355 (System.IntPtr hWnd
356 , int nBar
357 , int nPos
358 , bool bRedraw
359 );
360 [System.Runtime.InteropServices.DllImport("user32.dll")]
361 public static extern int GetScrollPos
362 (System.IntPtr hWnd
363 , int nBar
364 );
365
366 [System.Runtime.InteropServices.DllImport
367 ("User32.Dll"
368 , EntryPoint = "PostMessageA")
369 ]
370 public static extern bool PostMessage
371 (System.IntPtr hWnd
372 , uint msg
373 , int wParam
374 , int lParam
375 );
376
377 }
378
379 }
380 private void Code_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
381 {
382 UpdateScroll();
383 }
384 private void textBox_Scroll(object sender, EventArgs e)
385 {
386 UpdateScroll();
387 }
388
389 private void textBox_TextChanged(object sender, EventArgs e)
390 {
391 lineBox.Text = "";
392 for (int i = 0; i < textBox.Lines.Length; i++)
393 {
394 lineBox.Text += (i + 1).ToString() + Environment.NewLine;
395 }
396 UpdateScroll();
397 }
398
399 private void textBox_FontChanged(object sender, EventArgs e)
400 {
401 lineBox.Font = textBox.Font;
402 }
403
404 private void CodeView_Resize(object sender, EventArgs e)
405 {
406 UpdateScroll();
407 }
408 }
409}
TextBox with support for getting and setting the vertical scroll bar position, as well as listening t...
Definition CodeView.cs:69
override void OnFontChanged(System.EventArgs e)
Definition CodeView.cs:191
override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
Definition CodeView.cs:166
override void Dispose(bool disposing)
Definition CodeView.cs:205
override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
Definition CodeView.cs:155
override void OnResize(System.EventArgs e)
Definition CodeView.cs:145
override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
Definition CodeView.cs:124
System.Windows.Forms.ScrollEventHandler ScrollChanged
Definition CodeView.cs:107
override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
Definition CodeView.cs:176
override void WndProc(ref Message message)
Definition CodeView.cs:111
override void OnKeyUp(System.Windows.Forms.KeyEventArgs e)
Definition CodeView.cs:135
RichTextBox TextBox
Definition CodeView.cs:38
void UpdateScroll()
The lineBox's vertical scroll position is set to the textBox's vertical scroll position.
Definition CodeView.cs:59