设计起因:最近在做 winfrom 自定义打印工具,其中项目中需要为打印界面分四个区 于是想到了 splitcontainer,由于是在 tabcontrol 中放入 splitcontainer, 所以做成自定义 splitcontainer 是没毛病的
1. 先介绍自定义单个 SplitContainer 放大缩小:
在自己的 winfrom 项目下新建一个组件类, 名为 SplitContainerEx.cs
然后写上如下代码,具体可以自己看
可以在工具箱中看到 就算完成了.
public partial class SplitContainerEx : SplitContainer
{
enum MouseState
{
/// <summary>
/// 正常
/// </summary>
Normal,
/// <summary>
/// 鼠标移入
/// </summary>
Hover
}
public SplitContainerEx()
{
this.SetStyle(
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer, true);
this.SplitterWidth = 11;
this.Panel1MinSize = 0;
this.Panel2MinSize = 0;
this.Orientation = Orientation.Horizontal;
this.SplitterDistance = 50;
}
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public new int SplitterWidth
{
get
{
return base.SplitterWidth;
}
set
{
base.SplitterWidth = 11;
}
}
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public new int Panel1MinSize
{
get
{
return base.Panel1MinSize;
}
set
{
base.Panel1MinSize = 0;
}
}
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public new int Panel2MinSize
{
get
{
return base.Panel2MinSize;
}
set
{
base.Panel2MinSize = 0;
}
}
public enum SplitterPanelEnum
{
Panel1,
Panel2
}
SplitterPanelEnum mCollpasePanel = SplitterPanelEnum.Panel1;
/// <summary>
/// 进行折叠或展开的 SplitterPanel
/// </summary>
[DefaultValue(SplitterPanelEnum.Panel2)]
public SplitterPanelEnum CollpasePanel
{
get
{
return mCollpasePanel;
}
set
{
if (value != mCollpasePanel)
{
mCollpasePanel = value;
this.Invalidate(this.ControlRect);
}
}
}
bool mCollpased = false;
/// <summary>
/// 是否为折叠状态
/// </summary>
public bool IsCollpased
{
get { return mCollpased; }
}
Rectangle mRect = new Rectangle();
/// <summary>
/// 控制器绘制区域
/// </summary>
private Rectangle ControlRect
{
get
{
if (this.Orientation == Orientation.Horizontal)
{
mRect.X = this.Width <= 80 ? 0 : this.Width / 2 - 40;
mRect.Y = this.SplitterDistance;
mRect.Width = 80;
mRect.Height = 11;
}
else
{
mRect.X = this.SplitterDistance;
mRect.Y = this.Height <= 80 ? 0 : this.Height / 2 - 40;
mRect.Width = 11;
mRect.Height = 80;
}
return mRect;
}
}
private Rectangle ControlRect1
{
get
{
mRect.X = 0;
mRect.Y = this.SplitterDistance;
mRect.Width = 80;
mRect.Height = 11;
return mRect;
}
}
/// <summary>
/// 鼠标状态
/// </summary>
MouseState mMouseState = MouseState.Normal;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// 绘制参数
bool collpase = false;
if ((this.CollpasePanel == SplitterPanelEnum.Panel1 && mCollpased == false)
|| this.CollpasePanel == SplitterPanelEnum.Panel2 && mCollpased)
{
collpase = true;
}
Color color = mMouseState == MouseState.Normal ? SystemColors.ButtonShadow : SystemColors.ControlDarkDark;
// 需要绘制的图片
Bitmap bmp = CreateControlImage(collpase, color);
// 绘制区域
if (this.Orientation == Orientation.Vertical)
{
bmp.RotateFlip(RotateFlipType.Rotate90FlipX);
}
// 清除绘制区域
e.Graphics.SetClip(this.SplitterRectangle); // 这里需要注意一点就是需要清除拆分器整个区域,如果仅清除控制按钮区域,则会出现虚线状态
e.Graphics.Clear(this.BackColor);
// 绘制
e.Graphics.DrawImage(bmp, this.ControlRect);
e.Graphics.DrawString("text", new System.Drawing.Font("宋体", 9), new SolidBrush(Color.Black), this.ControlRect1);
}
public new bool IsSplitterFixed
{
get
{
return base.IsSplitterFixed;
}
set
{
base.IsSplitterFixed = value;
// 此处设计防止运行时更改 base.IsSplitterFixed 属性时导致 mIsSplitterFixed 变量判断失效
if (value && mIsSplitterFixed == false)
{
mIsSplitterFixed = true;
}
}
}
bool mIsSplitterFixed = true;
protected override void OnMouseMove(MouseEventArgs e)
{
// 鼠标在控制按钮区域
if (this.SplitterRectangle.Contains(e.Location))
{
if (this.ControlRect.Contains(e.Location))
{
// 如果拆分器可移动,则鼠标在控制按钮范围内时临时关闭拆分器
if (this.IsSplitterFixed == false)
{
this.IsSplitterFixed = true;
mIsSplitterFixed = false;
}
this.Cursor = Cursors.Hand;
mMouseState = MouseState.Hover;
this.Invalidate(this.ControlRect);
}
else
{
// 如果拆分器为临时关闭,则开启拆分器
if (mIsSplitterFixed == false)
{
this.IsSplitterFixed = false;
if (this.Orientation == Orientation.Horizontal)
{
this.Cursor = Cursors.HSplit;
}
else
{
this.Cursor = Cursors.VSplit;
}
}
else
{
this.Cursor = Cursors.Default;
}
mMouseState = MouseState.Normal;
this.Invalidate(this.ControlRect);
}
}
base.OnMouseMove(e);
}
protected override void OnMouseLeave(EventArgs e)
{
this.Cursor = Cursors.Default;
mMouseState = MouseState.Normal;
this.Invalidate(this.ControlRect);
base.OnMouseLeave(e);
}
protected override void OnMouseClick(MouseEventArgs e)
{
if (this.ControlRect.Contains(e.Location))
{
CollpaseOrExpand();
}
base.OnMouseClick(e);
}
int _HeightOrWidth;
/// <summary>
/// 折叠或展开
/// </summary>
public void CollpaseOrExpand()
{
if (mCollpased)
{
mCollpased = false;
this.SplitterDistance = _HeightOrWidth;
}
else
{
mCollpased = true;
_HeightOrWidth = this.SplitterDistance;
if (CollpasePanel == SplitterPanelEnum.Panel1)
{
this.SplitterDistance = 0;
}
else
{
if (this.Orientation == Orientation.Horizontal)
{
this.SplitterDistance = this.Height - 9;
}
else
{
this.SplitterDistance = this.Width - 9;
}
}
}
this.Invalidate(this.ControlRect); // 局部刷新绘制
}
/// <summary>
/// 需要绘制的用于折叠窗口的按钮样式
/// </summary>
/// <param name="collapse"></param>
/// <param name="color"></param>
/// <returns></returns>
private Bitmap CreateControlImage(bool collapse, Color color)
{
Bitmap bmp = new Bitmap(80, 9);
for (int x = 5; x <= 30; x += 5)
{
for (int y = 1; y <= 7; y += 3)
{
bmp.SetPixel(x, y, color);
}
}
for (int x = 50; x <= 75; x += 5)
{
for (int y = 1; y <= 7; y += 3)
{
bmp.SetPixel(x, y, color);
}
}
// 控制小三角底边向上或者向下
if (collapse)
{
int k = 0;
for (int y = 7; y >= 1; y--)
{
for (int x = 35 + k; x <= 45 - k; x++)
{
bmp.SetPixel(x, y, color);
}
k++;
}
}
else
{
int k = 0;
for (int y = 1; y <= 7; y++)
{
for (int x = 35 + k; x <= 45 - k; x++)
{
bmp.SetPixel(x, y, color);
}
k++;
}
}
return bmp;
}
2. 多个自定义 splitcontainer
新建一个组件类,名为 FreeSplit.cs,继承 UserControl 类 就可以任意拖动刚才自定义的 splitcontainer 了.效果如下
3. 在 tabcontrol 中铺满
private int _componentCount = 0;
private void toolStripButton4_Click(object sender, EventArgs e)
{
TabPage tabpage = new TabPage("New" + _userControlCount);
propertyGrid.FreeSplit sp = new propertyGrid.FreeSplit();
sp.Parent = tabpage;
sp.Dock = DockStyle.Fill;// 控件的各个边缘分别停靠在其包含控件的各个边缘,并且适当调整大小
tabControl1.TabPages.Add(tabpage);
tabControl1.SelectedIndex = tabControl1.TabPages.Count - 1;
}
来源: http://www.bubuko.com/infodetail-2476629.html