Advertisement
HaLo2FrEeEk

PanelHandle Control

Jan 11th, 2011
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.71 KB | None | 0 0
  1.     public class PanelHandle : Panel
  2.     {
  3.         public PanelHandle()
  4.         {
  5.             SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
  6.             this.BorderColor = Color.Black;
  7.             this.Border = DefaultBorder;
  8.         }
  9.  
  10.         public enum HitTest
  11.         {
  12.             Nowhere = 0,
  13.             Client = 1,
  14.             Caption = 2,
  15.             Left = 10,
  16.             Right = 11,
  17.             Top = 12,
  18.             TopLeft = 13,
  19.             TopRight = 14,
  20.             Bottom = 15,
  21.             BottomLeft = 16,
  22.             BottomRight = 17,
  23.             Border = 18
  24.         }
  25.  
  26.         #region Properties
  27.         private static Padding DefaultBorder = new Padding(6);
  28.         private Padding border;
  29.         private Color bordercolor;
  30.  
  31.         [Description("Defines the resizing border to be drawn")]
  32.         public Padding Border
  33.         {
  34.             get { return this.border; }
  35.             set
  36.             {
  37.                 this.border = value;
  38.                 InvalidateEx();
  39.             }
  40.         }
  41.  
  42.         [DefaultValue(typeof(Color), "Black"),
  43.         Description("Set the color of the border")]
  44.         public Color BorderColor
  45.         {
  46.             get { return bordercolor; }
  47.             set
  48.             {
  49.                 bordercolor = value;
  50.                 InvalidateEx();
  51.             }
  52.         }
  53.         #endregion
  54.  
  55.         #region Methods
  56.         private void InvalidateEx()
  57.         {
  58.             if (this.Parent == null)
  59.                 return;
  60.  
  61.             Parent.Invalidate(new Rectangle(this.Location, this.Size), true);
  62.         }
  63.  
  64.         private IntPtr calcNCHitTest(Rectangle rec)
  65.         {
  66.             IntPtr ret = IntPtr.Zero;
  67.  
  68.             // Check if it's in the middle region (not touching any borders)
  69.             if (rec.Location.Y > Border.Top &&
  70.                 rec.Location.Y < rec.Height - Border.Bottom &&
  71.                 rec.Location.X > Border.Left &&
  72.                 rec.Location.X < rec.Width - Border.Right)
  73.                 ret = (IntPtr)HitTest.Caption;
  74.  
  75.             // Left
  76.             else if (rec.Location.Y >= Border.Top &&
  77.                 rec.Location.Y <= rec.Height - Border.Bottom &&
  78.                 rec.Location.X < Border.Left)
  79.                 ret = (IntPtr)HitTest.Left;
  80.  
  81.  
  82.             // Top
  83.             else if (rec.Location.Y < Border.Top &&
  84.                 rec.Location.X >= Border.Left &&
  85.                 rec.Location.X <= rec.Width - Border.Right)
  86.                 ret = (IntPtr)HitTest.Top;
  87.  
  88.             // Right
  89.             else if (rec.Location.Y >= Border.Top &&
  90.                 rec.Location.Y <= rec.Height - Border.Bottom &&
  91.                 rec.Location.X > rec.Width - Border.Right)
  92.                 ret = (IntPtr)HitTest.Right;
  93.  
  94.             // Bottom
  95.             else if (rec.Location.Y > rec.Height - Border.Bottom &&
  96.                 rec.Location.X >= Border.Left &&
  97.                 rec.Location.X <= rec.Width - Border.Right)
  98.                 ret = (IntPtr)HitTest.Bottom;
  99.  
  100.             // Top-left
  101.             else if (rec.Location.Y < Border.Top &&
  102.                 rec.Location.X < Border.Left)
  103.                 ret = (IntPtr)HitTest.TopLeft;
  104.  
  105.             // Top-right
  106.             else if (rec.Location.Y < Border.Top &&
  107.                 rec.Location.X > rec.Width - Border.Right)
  108.                 ret = (IntPtr)HitTest.TopRight;
  109.  
  110.             // Bottom-left
  111.             else if (rec.Location.Y > rec.Height - Border.Bottom &&
  112.                 rec.Location.X < Border.Right)
  113.                 ret = (IntPtr)HitTest.BottomLeft;
  114.  
  115.             // Bottom-right
  116.             else if (rec.Location.Y > rec.Height - Border.Bottom &&
  117.                 rec.Location.X > rec.Width - Border.Right)
  118.                 ret = (IntPtr)HitTest.BottomRight;
  119.  
  120.             return ret;
  121.         }
  122.  
  123.         private Point calcHitPoint(IntPtr lParam)
  124.         {
  125.             int x = (lParam.ToInt32() << 16) >> 16;
  126.             int y = lParam.ToInt32() >> 16;
  127.             return PointToClient(new Point(x, y));
  128.         }
  129.  
  130.         private void ChangeColor(int wParam)
  131.         {
  132.             using (ColorDialog cd = new ColorDialog())
  133.             {
  134.                 cd.AnyColor = true;
  135.                 cd.FullOpen = true;
  136.                 switch (wParam)
  137.                 {
  138.                     case 2:
  139.                         cd.Color = this.BackColor;
  140.                         if (cd.ShowDialog() == DialogResult.OK)
  141.                             this.BackColor = cd.Color;
  142.                         break;
  143.                     default:
  144.                         cd.Color = this.BorderColor;
  145.                         if (cd.ShowDialog() == DialogResult.OK)
  146.                             this.BorderColor = cd.Color;
  147.                         break;
  148.                 }
  149.             }
  150.         }
  151.  
  152.         bool ShouldSerializeBorder()
  153.         {
  154.             return Border != DefaultBorder;
  155.         }
  156.  
  157.         void ResetBorder()
  158.         {
  159.             this.Border = DefaultBorder;
  160.         }
  161.         #endregion
  162.  
  163.         #region Overrides
  164.         protected override void WndProc(ref Message m)
  165.         {
  166.             base.WndProc(ref m);
  167.             switch (m.Msg)
  168.             {
  169.                 case 0x84:
  170.                     Rectangle rec = new Rectangle(calcHitPoint(m.LParam), ClientSize);
  171.                     IntPtr res = calcNCHitTest(rec);
  172.  
  173.                     m.Result = res;
  174.                     break;
  175.                 case 0x216:
  176.                     this.BringToFront();
  177.                     break;
  178.                 case 0x214:
  179.                     this.BringToFront();
  180.                     break;
  181.                 case 0xA5:
  182.                     ChangeColor(m.WParam.ToInt32());
  183.                     break;
  184.             }
  185.         }
  186.  
  187.         protected override void OnPaint(PaintEventArgs e)
  188.         {
  189.             using (System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath())
  190.             {
  191.                 using (SolidBrush b = new SolidBrush(BorderColor))
  192.                 {
  193.                     e.Graphics.FillRectangle(b, 0, 0, Border.Left, ClientSize.Height);                                  // Draw left border
  194.                     e.Graphics.FillRectangle(b, 0, 0, ClientSize.Width, Border.Top);                                    // Draw top border
  195.                     e.Graphics.FillRectangle(b, ClientSize.Width - border.Right, 0, Border.Right, ClientSize.Height);   // Draw right border
  196.                     e.Graphics.FillRectangle(b, 0, ClientSize.Height - border.Bottom, ClientSize.Width, Border.Bottom); // Draw bottom border
  197.                 }
  198.             }
  199.             base.OnPaint(e);
  200.         }
  201.         #endregion
  202.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement