Advertisement
dreadngel

Custom draw SplitContainer

Oct 10th, 2012
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Drawing;
  9.  
  10. namespace Splitter
  11. {
  12.     public enum Direction
  13.     {
  14.         Right = 0,
  15.         Left,
  16.         Up,
  17.         Down
  18.     }
  19.  
  20.     public class ACSplitter : SplitContainer
  21.     {
  22.         public ACSplitter()
  23.         {
  24.             //InitializeComponent();
  25.             SetStyle(ControlStyles.ResizeRedraw, true);
  26.             this.KeyPress += new KeyPressEventHandler(ACSplitter_KeyPress);
  27.             this.KeyUp +=new KeyEventHandler(ACSplitter_KeyUp);
  28.         }
  29.  
  30.         #region private properties
  31.  
  32.         private System.Drawing.Color hotColor = CalculateColor(SystemColors.Highlight, SystemColors.Window, 70);
  33.  
  34.         #endregion
  35.  
  36.         private void ACSplitter_KeyPress(object sender, KeyPressEventArgs e)
  37.         {
  38.             this.Invalidate();
  39.             e.Handled = false;
  40.         }
  41.  
  42.         private void ACSplitter_KeyUp(object sender, KeyEventArgs e)
  43.         {
  44.             this.Invalidate();
  45.             e.Handled = false;
  46.         }
  47.  
  48.  
  49.         // solid color obtained as a result of alpha-blending
  50.         private static Color CalculateColor(Color front, Color back, int alpha)
  51.         {
  52.             Color frontColor = Color.FromArgb(255, front);
  53.             Color backColor = Color.FromArgb(255, back);
  54.  
  55.             float frontRed = frontColor.R;
  56.             float frontGreen = frontColor.G;
  57.             float frontBlue = frontColor.B;
  58.             float backRed = backColor.R;
  59.             float backGreen = backColor.G;
  60.             float backBlue = backColor.B;
  61.  
  62.             float fRed = frontRed * alpha / 255 + backRed * ((float)(255 - alpha) / 255);
  63.             byte newRed = (byte)fRed;
  64.             float fGreen = frontGreen * alpha / 255 + backGreen * ((float)(255 - alpha) / 255);
  65.             byte newGreen = (byte)fGreen;
  66.             float fBlue = frontBlue * alpha / 255 + backBlue * ((float)(255 - alpha) / 255);
  67.             byte newBlue = (byte)fBlue;
  68.  
  69.             return Color.FromArgb(255, newRed, newGreen, newBlue);
  70.         }
  71.  
  72.         // This creates a point array to draw a arrow-like polygon
  73.         private Point[] ArrowPointArray(int x, int y, Direction direction)
  74.         {
  75.             Point[] point = new Point[3];
  76.  
  77.             // decide which direction the arrow will point
  78.             if (direction == Direction.Right)
  79.             {
  80.                 // right arrow
  81.                 point[0] = new Point(x, y);
  82.                 point[1] = new Point(x + 3, y + 3);
  83.                 point[2] = new Point(x, y + 6);
  84.             }
  85.             if (direction == Direction.Left)
  86.             {
  87.                 // left arrow
  88.                 point[0] = new Point(x + 3, y);
  89.                 point[1] = new Point(x, y + 3);
  90.                 point[2] = new Point(x + 3, y + 6);
  91.             }
  92.             if (direction == Direction.Up)
  93.             {
  94.                 // up arrow
  95.                 point[0] = new Point(x + 3, y);
  96.                 point[1] = new Point(x + 6, y + 4);
  97.                 point[2] = new Point(x, y + 4);
  98.             }
  99.             if (direction == Direction.Down)
  100.             {
  101.                 // down arrow
  102.                 point[0] = new Point(x, y);
  103.                 point[1] = new Point(x + 2, y + 3);
  104.                 point[2] = new Point(x + 5, y);
  105.             }
  106.             return point;
  107.         }
  108.  
  109.         protected override void OnPaint(PaintEventArgs pe)
  110.         {
  111.             Graphics g = pe.Graphics;
  112.  
  113.             Rectangle r = this.ClientRectangle;
  114.             g.FillRectangle(new SolidBrush(this.BackColor), r);
  115.  
  116.  
  117.             if (this.Orientation == System.Windows.Forms.Orientation.Horizontal)
  118.             {
  119.                 this.SplitterWidth = 9;
  120.                 int recWidth = SplitterRectangle.Width / 3;
  121.                 Rectangle split_rect = new Rectangle(SplitterRectangle.X + recWidth, SplitterRectangle.Y, SplitterRectangle.Width - recWidth, SplitterRectangle.Height);
  122.  
  123.                 int x = split_rect.X;
  124.                 int y = split_rect.Y + 3;
  125.  
  126.                 g.DrawLine(new Pen(SystemColors.ControlLightLight), x, y, x, y + 2);
  127.                 g.DrawLine(new Pen(SystemColors.ControlLightLight), x, y, x + recWidth, y);
  128.                 g.DrawLine(new Pen(SystemColors.ControlDark), x, y + 2, x + recWidth, y + 2);
  129.                 g.DrawLine(new Pen(SystemColors.ControlDark), x + recWidth, y, x + recWidth, y + 2);
  130.             }
  131.             else
  132.             {
  133.                 this.SplitterWidth = 9;
  134.                 int recHeight = SplitterRectangle.Height / 3;
  135.                 Rectangle split_rect = new Rectangle(SplitterRectangle.X, SplitterRectangle.Y + recHeight, SplitterRectangle.Width, SplitterRectangle.Height - 2 * recHeight);
  136.  
  137.                 int x = split_rect.X + 3;
  138.                 int y = split_rect.Y;
  139.  
  140.                 g.DrawLine(new Pen(SystemColors.ControlLightLight), x, y, x + 2, y);
  141.                 g.DrawLine(new Pen(SystemColors.ControlLightLight), x, y, x, y + recHeight);
  142.                 g.DrawLine(new Pen(SystemColors.ControlDark), x + 2, y, x + 2, y + recHeight);
  143.                 g.DrawLine(new Pen(SystemColors.ControlDark), x, y + recHeight, x + 2, y + recHeight);
  144.             }
  145.             // Calling the base class OnPaint
  146.             base.OnPaint(pe);
  147.         }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement