Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PanelHandle : Panel
- {
- public PanelHandle()
- {
- SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
- this.BorderColor = Color.Black;
- this.Border = DefaultBorder;
- }
- public enum HitTest
- {
- Nowhere = 0,
- Client = 1,
- Caption = 2,
- Left = 10,
- Right = 11,
- Top = 12,
- TopLeft = 13,
- TopRight = 14,
- Bottom = 15,
- BottomLeft = 16,
- BottomRight = 17,
- Border = 18
- }
- #region Properties
- private static Padding DefaultBorder = new Padding(6);
- private Padding border;
- private Color bordercolor;
- [Description("Defines the resizing border to be drawn")]
- public Padding Border
- {
- get { return this.border; }
- set
- {
- this.border = value;
- InvalidateEx();
- }
- }
- [DefaultValue(typeof(Color), "Black"),
- Description("Set the color of the border")]
- public Color BorderColor
- {
- get { return bordercolor; }
- set
- {
- bordercolor = value;
- InvalidateEx();
- }
- }
- #endregion
- #region Methods
- private void InvalidateEx()
- {
- if (this.Parent == null)
- return;
- Parent.Invalidate(new Rectangle(this.Location, this.Size), true);
- }
- private IntPtr calcNCHitTest(Rectangle rec)
- {
- IntPtr ret = IntPtr.Zero;
- // Check if it's in the middle region (not touching any borders)
- if (rec.Location.Y > Border.Top &&
- rec.Location.Y < rec.Height - Border.Bottom &&
- rec.Location.X > Border.Left &&
- rec.Location.X < rec.Width - Border.Right)
- ret = (IntPtr)HitTest.Caption;
- // Left
- else if (rec.Location.Y >= Border.Top &&
- rec.Location.Y <= rec.Height - Border.Bottom &&
- rec.Location.X < Border.Left)
- ret = (IntPtr)HitTest.Left;
- // Top
- else if (rec.Location.Y < Border.Top &&
- rec.Location.X >= Border.Left &&
- rec.Location.X <= rec.Width - Border.Right)
- ret = (IntPtr)HitTest.Top;
- // Right
- else if (rec.Location.Y >= Border.Top &&
- rec.Location.Y <= rec.Height - Border.Bottom &&
- rec.Location.X > rec.Width - Border.Right)
- ret = (IntPtr)HitTest.Right;
- // Bottom
- else if (rec.Location.Y > rec.Height - Border.Bottom &&
- rec.Location.X >= Border.Left &&
- rec.Location.X <= rec.Width - Border.Right)
- ret = (IntPtr)HitTest.Bottom;
- // Top-left
- else if (rec.Location.Y < Border.Top &&
- rec.Location.X < Border.Left)
- ret = (IntPtr)HitTest.TopLeft;
- // Top-right
- else if (rec.Location.Y < Border.Top &&
- rec.Location.X > rec.Width - Border.Right)
- ret = (IntPtr)HitTest.TopRight;
- // Bottom-left
- else if (rec.Location.Y > rec.Height - Border.Bottom &&
- rec.Location.X < Border.Right)
- ret = (IntPtr)HitTest.BottomLeft;
- // Bottom-right
- else if (rec.Location.Y > rec.Height - Border.Bottom &&
- rec.Location.X > rec.Width - Border.Right)
- ret = (IntPtr)HitTest.BottomRight;
- return ret;
- }
- private Point calcHitPoint(IntPtr lParam)
- {
- int x = (lParam.ToInt32() << 16) >> 16;
- int y = lParam.ToInt32() >> 16;
- return PointToClient(new Point(x, y));
- }
- private void ChangeColor(int wParam)
- {
- using (ColorDialog cd = new ColorDialog())
- {
- cd.AnyColor = true;
- cd.FullOpen = true;
- switch (wParam)
- {
- case 2:
- cd.Color = this.BackColor;
- if (cd.ShowDialog() == DialogResult.OK)
- this.BackColor = cd.Color;
- break;
- default:
- cd.Color = this.BorderColor;
- if (cd.ShowDialog() == DialogResult.OK)
- this.BorderColor = cd.Color;
- break;
- }
- }
- }
- bool ShouldSerializeBorder()
- {
- return Border != DefaultBorder;
- }
- void ResetBorder()
- {
- this.Border = DefaultBorder;
- }
- #endregion
- #region Overrides
- protected override void WndProc(ref Message m)
- {
- base.WndProc(ref m);
- switch (m.Msg)
- {
- case 0x84:
- Rectangle rec = new Rectangle(calcHitPoint(m.LParam), ClientSize);
- IntPtr res = calcNCHitTest(rec);
- m.Result = res;
- break;
- case 0x216:
- this.BringToFront();
- break;
- case 0x214:
- this.BringToFront();
- break;
- case 0xA5:
- ChangeColor(m.WParam.ToInt32());
- break;
- }
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- using (System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath())
- {
- using (SolidBrush b = new SolidBrush(BorderColor))
- {
- e.Graphics.FillRectangle(b, 0, 0, Border.Left, ClientSize.Height); // Draw left border
- e.Graphics.FillRectangle(b, 0, 0, ClientSize.Width, Border.Top); // Draw top border
- e.Graphics.FillRectangle(b, ClientSize.Width - border.Right, 0, Border.Right, ClientSize.Height); // Draw right border
- e.Graphics.FillRectangle(b, 0, ClientSize.Height - border.Bottom, ClientSize.Width, Border.Bottom); // Draw bottom border
- }
- }
- base.OnPaint(e);
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement