/[RomCheater]/trunk/deps/WeifenLuo.WinFormsUI.Docking/DockSample/Customization/InertButton.cs
ViewVC logotype

Contents of /trunk/deps/WeifenLuo.WinFormsUI.Docking/DockSample/Customization/InertButton.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 140 - (show annotations) (download)
Fri May 11 11:29:29 2012 UTC (9 years, 2 months ago) by william
File size: 14341 byte(s)


1 using System;
2 using System.Drawing;
3 using System.Drawing.Imaging;
4 using System.Windows.Forms;
5 using System.ComponentModel;
6
7 namespace DockSample.Customization
8 {
9 internal class InertButton : Button
10 {
11 private enum RepeatClickStatus
12 {
13 Disabled,
14 Started,
15 Repeating,
16 Stopped
17 }
18
19 private class RepeatClickEventArgs : EventArgs
20 {
21 private static RepeatClickEventArgs _empty;
22
23 static RepeatClickEventArgs()
24 {
25 _empty = new RepeatClickEventArgs();
26 }
27
28 public new static RepeatClickEventArgs Empty
29 {
30 get { return _empty; }
31 }
32 }
33
34 private IContainer components = new Container();
35 private int m_borderWidth = 1;
36 private bool m_mouseOver = false;
37 private bool m_mouseCapture = false;
38 private bool m_isPopup = false;
39 private Image m_imageEnabled = null;
40 private Image m_imageDisabled = null;
41 private int m_imageIndexEnabled = -1;
42 private int m_imageIndexDisabled = -1;
43 private bool m_monochrom = true;
44 private ToolTip m_toolTip = null;
45 private string m_toolTipText = "";
46 private Color m_borderColor = Color.Empty;
47
48 public InertButton()
49 {
50 InternalConstruct(null, null);
51 }
52
53 public InertButton(Image imageEnabled)
54 {
55 InternalConstruct(imageEnabled, null);
56 }
57
58 public InertButton(Image imageEnabled, Image imageDisabled)
59 {
60 InternalConstruct(imageEnabled, imageDisabled);
61 }
62
63 private void InternalConstruct(Image imageEnabled, Image imageDisabled)
64 {
65 // Remember parameters
66 ImageEnabled = imageEnabled;
67 ImageDisabled = imageDisabled;
68
69 // Prevent drawing flicker by blitting from memory in WM_PAINT
70 SetStyle(ControlStyles.ResizeRedraw, true);
71 SetStyle(ControlStyles.UserPaint, true);
72 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
73
74 // Prevent base class from trying to generate double click events and
75 // so testing clicks against the double click time and rectangle. Getting
76 // rid of this allows the user to press then release button very quickly.
77 //SetStyle(ControlStyles.StandardDoubleClick, false);
78
79 // Should not be allowed to select this control
80 SetStyle(ControlStyles.Selectable, false);
81
82 m_timer = new Timer();
83 m_timer.Enabled = false;
84 m_timer.Tick += new EventHandler(Timer_Tick);
85 }
86
87 protected override void Dispose(bool disposing)
88 {
89 if (disposing)
90 {
91 if (components != null)
92 components.Dispose();
93 }
94 base.Dispose(disposing);
95 }
96
97 public Color BorderColor
98 {
99 get { return m_borderColor; }
100 set
101 {
102 if (m_borderColor != value)
103 {
104 m_borderColor = value;
105 Invalidate();
106 }
107 }
108 }
109
110 private bool ShouldSerializeBorderColor()
111 {
112 return (m_borderColor != Color.Empty);
113 }
114
115 public int BorderWidth
116 {
117 get { return m_borderWidth; }
118
119 set
120 {
121 if (value < 1)
122 value = 1;
123 if (m_borderWidth != value)
124 {
125 m_borderWidth = value;
126 Invalidate();
127 }
128 }
129 }
130
131 public Image ImageEnabled
132 {
133 get
134 {
135 if (m_imageEnabled != null)
136 return m_imageEnabled;
137
138 try
139 {
140 if (ImageList == null || ImageIndexEnabled == -1)
141 return null;
142 else
143 return ImageList.Images[m_imageIndexEnabled];
144 }
145 catch
146 {
147 return null;
148 }
149 }
150
151 set
152 {
153 if (m_imageEnabled != value)
154 {
155 m_imageEnabled = value;
156 Invalidate();
157 }
158 }
159 }
160
161 private bool ShouldSerializeImageEnabled()
162 {
163 return (m_imageEnabled != null);
164 }
165
166 public Image ImageDisabled
167 {
168 get
169 {
170 if (m_imageDisabled != null)
171 return m_imageDisabled;
172
173 try
174 {
175 if (ImageList == null || ImageIndexDisabled == -1)
176 return null;
177 else
178 return ImageList.Images[m_imageIndexDisabled];
179 }
180 catch
181 {
182 return null;
183 }
184 }
185
186 set
187 {
188 if (m_imageDisabled != value)
189 {
190 m_imageDisabled = value;
191 Invalidate();
192 }
193 }
194 }
195
196 public int ImageIndexEnabled
197 {
198 get { return m_imageIndexEnabled; }
199 set
200 {
201 if (m_imageIndexEnabled != value)
202 {
203 m_imageIndexEnabled = value;
204 Invalidate();
205 }
206 }
207 }
208
209 public int ImageIndexDisabled
210 {
211 get { return m_imageIndexDisabled; }
212 set
213 {
214 if (m_imageIndexDisabled != value)
215 {
216 m_imageIndexDisabled = value;
217 Invalidate();
218 }
219 }
220 }
221
222 public bool IsPopup
223 {
224 get { return m_isPopup; }
225
226 set
227 {
228 if (m_isPopup != value)
229 {
230 m_isPopup = value;
231 Invalidate();
232 }
233 }
234 }
235
236 public bool Monochrome
237 {
238 get { return m_monochrom; }
239 set
240 {
241 if (value != m_monochrom)
242 {
243 m_monochrom = value;
244 Invalidate();
245 }
246 }
247 }
248
249 public bool RepeatClick
250 {
251 get { return (ClickStatus != RepeatClickStatus.Disabled); }
252 set { ClickStatus = RepeatClickStatus.Stopped; }
253 }
254
255 private RepeatClickStatus m_clickStatus = RepeatClickStatus.Disabled;
256 private RepeatClickStatus ClickStatus
257 {
258 get { return m_clickStatus; }
259 set
260 {
261 if (m_clickStatus == value)
262 return;
263
264 m_clickStatus = value;
265 if (ClickStatus == RepeatClickStatus.Started)
266 {
267 Timer.Interval = RepeatClickDelay;
268 Timer.Enabled = true;
269 }
270 else if (ClickStatus == RepeatClickStatus.Repeating)
271 Timer.Interval = RepeatClickInterval;
272 else
273 Timer.Enabled = false;
274 }
275 }
276
277 private int m_repeatClickDelay = 500;
278 public int RepeatClickDelay
279 {
280 get { return m_repeatClickDelay; }
281 set { m_repeatClickDelay = value; }
282 }
283
284 private int m_repeatClickInterval = 100;
285 public int RepeatClickInterval
286 {
287 get { return m_repeatClickInterval; }
288 set { m_repeatClickInterval = value; }
289 }
290
291 private Timer m_timer;
292 private Timer Timer
293 {
294 get { return m_timer; }
295 }
296
297 public string ToolTipText
298 {
299 get { return m_toolTipText; }
300 set
301 {
302 if (m_toolTipText != value)
303 {
304 if (m_toolTip == null)
305 m_toolTip = new ToolTip(this.components);
306 m_toolTipText = value;
307 m_toolTip.SetToolTip(this, value);
308 }
309 }
310 }
311
312 private void Timer_Tick(object sender, EventArgs e)
313 {
314 if (m_mouseCapture && m_mouseOver)
315 OnClick(RepeatClickEventArgs.Empty);
316 if (ClickStatus == RepeatClickStatus.Started)
317 ClickStatus = RepeatClickStatus.Repeating;
318 }
319
320 /// <exclude/>
321 protected override void OnMouseDown(MouseEventArgs e)
322 {
323 base.OnMouseDown(e);
324
325 if (e.Button != MouseButtons.Left)
326 return;
327
328 if (m_mouseCapture == false || m_mouseOver == false)
329 {
330 m_mouseCapture = true;
331 m_mouseOver = true;
332
333 //Redraw to show button state
334 Invalidate();
335 }
336
337 if (RepeatClick)
338 {
339 OnClick(RepeatClickEventArgs.Empty);
340 ClickStatus = RepeatClickStatus.Started;
341 }
342 }
343
344 /// <exclude/>
345 protected override void OnClick(EventArgs e)
346 {
347 if (RepeatClick && !(e is RepeatClickEventArgs))
348 return;
349
350 base.OnClick (e);
351 }
352
353 /// <exclude/>
354 protected override void OnMouseUp(MouseEventArgs e)
355 {
356 base.OnMouseUp(e);
357
358 if (e.Button != MouseButtons.Left)
359 return;
360
361 if (m_mouseOver == true || m_mouseCapture == true)
362 {
363 m_mouseOver = false;
364 m_mouseCapture = false;
365
366 // Redraw to show button state
367 Invalidate();
368 }
369
370 if (RepeatClick)
371 ClickStatus = RepeatClickStatus.Stopped;
372 }
373
374 /// <exclude/>
375 protected override void OnMouseMove(MouseEventArgs e)
376 {
377 base.OnMouseMove(e);
378
379 // Is mouse point inside our client rectangle
380 bool over = this.ClientRectangle.Contains(new Point(e.X, e.Y));
381
382 // If entering the button area or leaving the button area...
383 if (over != m_mouseOver)
384 {
385 // Update state
386 m_mouseOver = over;
387
388 // Redraw to show button state
389 Invalidate();
390 }
391 }
392
393 /// <exclude/>
394 protected override void OnMouseEnter(EventArgs e)
395 {
396 // Update state to reflect mouse over the button area
397 if (!m_mouseOver)
398 {
399 m_mouseOver = true;
400
401 // Redraw to show button state
402 Invalidate();
403 }
404
405 base.OnMouseEnter(e);
406 }
407
408 /// <exclude/>
409 protected override void OnMouseLeave(EventArgs e)
410 {
411 // Update state to reflect mouse not over the button area
412 if (m_mouseOver)
413 {
414 m_mouseOver = false;
415
416 // Redraw to show button state
417 Invalidate();
418 }
419
420 base.OnMouseLeave(e);
421 }
422
423 /// <exclude/>
424 protected override void OnPaint(PaintEventArgs e)
425 {
426 base.OnPaint(e);
427 DrawBackground(e.Graphics);
428 DrawImage(e.Graphics);
429 DrawText(e.Graphics);
430 DrawBorder(e.Graphics);
431 }
432
433 private void DrawBackground(Graphics g)
434 {
435 using (SolidBrush brush = new SolidBrush(BackColor))
436 {
437 g.FillRectangle(brush, ClientRectangle);
438 }
439 }
440
441 private void DrawImage(Graphics g)
442 {
443 Image image = this.Enabled ? ImageEnabled : ((ImageDisabled != null) ? ImageDisabled : ImageEnabled);
444 ImageAttributes imageAttr = null;
445
446 if (null == image)
447 return;
448
449 if (m_monochrom)
450 {
451 imageAttr = new ImageAttributes();
452
453 // transform the monochrom image
454 // white -> BackColor
455 // black -> ForeColor
456 ColorMap[] colorMap = new ColorMap[2];
457 colorMap[0] = new ColorMap();
458 colorMap[0].OldColor = Color.White;
459 colorMap[0].NewColor = this.BackColor;
460 colorMap[1] = new ColorMap();
461 colorMap[1].OldColor = Color.Black;
462 colorMap[1].NewColor = this.ForeColor;
463 imageAttr.SetRemapTable(colorMap);
464 }
465
466 Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
467
468 if ((!Enabled) && (null == ImageDisabled))
469 {
470 using (Bitmap bitmapMono = new Bitmap(image, ClientRectangle.Size))
471 {
472 if (imageAttr != null)
473 {
474 using (Graphics gMono = Graphics.FromImage(bitmapMono))
475 {
476 gMono.DrawImage(image, new Point[3] { new Point(0, 0), new Point(image.Width - 1, 0), new Point(0, image.Height - 1) }, rect, GraphicsUnit.Pixel, imageAttr);
477 }
478 }
479 ControlPaint.DrawImageDisabled(g, bitmapMono, 0, 0, this.BackColor);
480 }
481 }
482 else
483 {
484 // Three points provided are upper-left, upper-right and
485 // lower-left of the destination parallelogram.
486 Point[] pts = new Point[3];
487 pts[0].X = (Enabled && m_mouseOver && m_mouseCapture) ? 1 : 0;
488 pts[0].Y = (Enabled && m_mouseOver && m_mouseCapture) ? 1 : 0;
489 pts[1].X = pts[0].X + ClientRectangle.Width;
490 pts[1].Y = pts[0].Y;
491 pts[2].X = pts[0].X;
492 pts[2].Y = pts[1].Y + ClientRectangle.Height;
493
494 if (imageAttr == null)
495 g.DrawImage(image, pts, rect, GraphicsUnit.Pixel);
496 else
497 g.DrawImage(image, pts, rect, GraphicsUnit.Pixel, imageAttr);
498 }
499 }
500
501 private void DrawText(Graphics g)
502 {
503 if (Text == string.Empty)
504 return;
505
506 Rectangle rect = ClientRectangle;
507
508 rect.X += BorderWidth;
509 rect.Y += BorderWidth;
510 rect.Width -= 2 * BorderWidth;
511 rect.Height -= 2 * BorderWidth;
512
513 StringFormat stringFormat = new StringFormat();
514
515 if (TextAlign == ContentAlignment.TopLeft)
516 {
517 stringFormat.Alignment = StringAlignment.Near;
518 stringFormat.LineAlignment = StringAlignment.Near;
519 }
520 else if (TextAlign == ContentAlignment.TopCenter)
521 {
522 stringFormat.Alignment = StringAlignment.Center;
523 stringFormat.LineAlignment = StringAlignment.Near;
524 }
525 else if (TextAlign == ContentAlignment.TopRight)
526 {
527 stringFormat.Alignment = StringAlignment.Far;
528 stringFormat.LineAlignment = StringAlignment.Near;
529 }
530 else if (TextAlign == ContentAlignment.MiddleLeft)
531 {
532 stringFormat.Alignment = StringAlignment.Near;
533 stringFormat.LineAlignment = StringAlignment.Center;
534 }
535 else if (TextAlign == ContentAlignment.MiddleCenter)
536 {
537 stringFormat.Alignment = StringAlignment.Center;
538 stringFormat.LineAlignment = StringAlignment.Center;
539 }
540 else if (TextAlign == ContentAlignment.MiddleRight)
541 {
542 stringFormat.Alignment = StringAlignment.Far;
543 stringFormat.LineAlignment = StringAlignment.Center;
544 }
545 else if (TextAlign == ContentAlignment.BottomLeft)
546 {
547 stringFormat.Alignment = StringAlignment.Near;
548 stringFormat.LineAlignment = StringAlignment.Far;
549 }
550 else if (TextAlign == ContentAlignment.BottomCenter)
551 {
552 stringFormat.Alignment = StringAlignment.Center;
553 stringFormat.LineAlignment = StringAlignment.Far;
554 }
555 else if (TextAlign == ContentAlignment.BottomRight)
556 {
557 stringFormat.Alignment = StringAlignment.Far;
558 stringFormat.LineAlignment = StringAlignment.Far;
559 }
560
561 using (Brush brush = new SolidBrush(ForeColor))
562 {
563 g.DrawString(Text, Font, brush, rect, stringFormat);
564 }
565 }
566
567 private void DrawBorder(Graphics g)
568 {
569 ButtonBorderStyle bs;
570
571 // Decide on the type of border to draw around image
572 if (!this.Enabled)
573 bs = IsPopup ? ButtonBorderStyle.Outset : ButtonBorderStyle.Solid;
574 else if (m_mouseOver && m_mouseCapture)
575 bs = ButtonBorderStyle.Inset;
576 else if (IsPopup || m_mouseOver)
577 bs = ButtonBorderStyle.Outset;
578 else
579 bs = ButtonBorderStyle.Solid;
580
581 Color colorLeftTop;
582 Color colorRightBottom;
583 if (bs == ButtonBorderStyle.Solid)
584 {
585 colorLeftTop = this.BackColor;
586 colorRightBottom = this.BackColor;
587 }
588 else if (bs == ButtonBorderStyle.Outset)
589 {
590 colorLeftTop = m_borderColor.IsEmpty ? this.BackColor : m_borderColor;
591 colorRightBottom = this.BackColor;
592 }
593 else
594 {
595 colorLeftTop = this.BackColor;
596 colorRightBottom = m_borderColor.IsEmpty ? this.BackColor : m_borderColor;
597 }
598 ControlPaint.DrawBorder(g, this.ClientRectangle,
599 colorLeftTop, m_borderWidth, bs,
600 colorLeftTop, m_borderWidth, bs,
601 colorRightBottom, m_borderWidth, bs,
602 colorRightBottom, m_borderWidth, bs);
603 }
604
605 /// <exclude/>
606 protected override void OnEnabledChanged(EventArgs e)
607 {
608 base.OnEnabledChanged(e);
609 if (Enabled == false)
610 {
611 m_mouseOver = false;
612 m_mouseCapture = false;
613 if (RepeatClick && ClickStatus != RepeatClickStatus.Stopped)
614 ClickStatus = RepeatClickStatus.Stopped;
615 }
616 Invalidate();
617 }
618 }
619 }

  ViewVC Help
Powered by ViewVC 1.1.22