InertButtonBase.cs
上传用户:szlfmled
上传日期:2020-11-22
资源大小:978k
文件大小:3k
源码类别:

C#编程

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.Windows.Forms.Design;
  8. namespace WeifenLuo.WinFormsUI.Docking
  9. {
  10.     internal abstract class InertButtonBase : Control
  11.     {
  12.         protected InertButtonBase()
  13.         {
  14.             SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  15.             BackColor = Color.Transparent;
  16.         }
  17.         public abstract Bitmap Image
  18.         {
  19.             get;
  20.         }
  21.         private bool m_isMouseOver = false;
  22.         protected bool IsMouseOver
  23.         {
  24.             get { return m_isMouseOver; }
  25.             private set
  26.             {
  27.                 if (m_isMouseOver == value)
  28.                     return;
  29.                 m_isMouseOver = value;
  30.                 Invalidate();
  31.             }
  32.         }
  33.         protected override Size DefaultSize
  34.         {
  35.             get { return Resources.DockPane_Close.Size; }
  36.         }
  37.         protected override void OnMouseMove(MouseEventArgs e)
  38.         {
  39.             base.OnMouseMove(e);
  40.             bool over = ClientRectangle.Contains(e.X, e.Y);
  41.             if (IsMouseOver != over)
  42.                 IsMouseOver = over;
  43.         }
  44.         protected override void OnMouseEnter(EventArgs e)
  45.         {
  46.             base.OnMouseEnter(e);
  47.             if (!IsMouseOver)
  48.                 IsMouseOver = true;
  49.         }
  50.         protected override void OnMouseLeave(EventArgs e)
  51.         {
  52.             base.OnMouseLeave(e);
  53.             if (IsMouseOver)
  54.                 IsMouseOver = false;
  55.         }
  56.         protected override void OnPaint(PaintEventArgs e)
  57.         {
  58.             if (IsMouseOver && Enabled)
  59.             {
  60.                 using (Pen pen = new Pen(ForeColor))
  61.                 {
  62.                     e.Graphics.DrawRectangle(pen, Rectangle.Inflate(ClientRectangle, -1, -1));
  63.                 }
  64.             }
  65.             using (ImageAttributes imageAttributes = new ImageAttributes())
  66.             {
  67.                 ColorMap[] colorMap = new ColorMap[2];
  68.                 colorMap[0] = new ColorMap();
  69.                 colorMap[0].OldColor = Color.FromArgb(0, 0, 0);
  70.                 colorMap[0].NewColor = ForeColor;
  71.                 colorMap[1] = new ColorMap();
  72.                 colorMap[1].OldColor = Image.GetPixel(0, 0);
  73.                 colorMap[1].NewColor = Color.Transparent;
  74.                 imageAttributes.SetRemapTable(colorMap);
  75.                 e.Graphics.DrawImage(
  76.                    Image,
  77.                    new Rectangle(0, 0, Image.Width, Image.Height),
  78.                    0, 0,
  79.                    Image.Width,
  80.                    Image.Height,
  81.                    GraphicsUnit.Pixel,
  82.                    imageAttributes);
  83.             }
  84.             base.OnPaint(e);
  85.         }
  86.         public void RefreshChanges()
  87.         {
  88.             if (IsDisposed)
  89.                 return;
  90.             bool mouseOver = ClientRectangle.Contains(PointToClient(Control.MousePosition));
  91.             if (mouseOver != IsMouseOver)
  92.                 IsMouseOver = mouseOver;
  93.             OnRefreshChanges();
  94.         }
  95.         protected virtual void OnRefreshChanges()
  96.         {
  97.         }
  98.     }
  99. }