winform SplitContainer *拉伸

源程序链接:https://github.com/chinayixia/splitercontainer_20190819.git

 效果图:

winform  SplitContainer *拉伸

winform  SplitContainer *拉伸

splitercontainer 控件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace wf_foh100_demotool_release
{
[ToolboxBitmap(typeof(SplitContainer))]
public class SplitContainerEx : SplitContainer
{
enum MouseState
{
/// <summary>
/// 正常
/// </summary>
Normal,
/// <summary>
/// 鼠标移入
/// </summary>
Hover
}

public SplitContainerEx()
{
this.SetStyle(
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer, true);
this.SplitterWidth = 9;
this.Panel1MinSize = 0;
this.Panel2MinSize = 0;
}

[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public new int SplitterWidth
{
get
{
return base.SplitterWidth;
}
set
{
base.SplitterWidth = 9;
}
}

[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.Panel2;
/// <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 = 9;
}
else
{
mRect.X = this.SplitterDistance;
mRect.Y = this.Height <= 80 ? 0 : this.Height / 2 - 40;
mRect.Width = 9;
mRect.Height = 80;
}
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;
Color color = mMouseState == MouseState.Normal ? System.Drawing.Color.FromArgb(255, 140, 0) : System.Drawing.Color.FromArgb(255, 255, 0);
//需要绘制的图片
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);
}

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;
}
}
}

menubar控件:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace wf_foh100_demotool_release
{
public partial class UserControl_menubar : UserControl
{


public enum CheckStyle
{
style1 = 0, //black item close
style2 = 1, //black item grow
style3 = 2, //black item shrink
style4 = 3, //black item minimize
style5 = 4,
style6 = 5
};

public UserControl_menubar()
{
InitializeComponent();

//设置Style支持透明背景色并且双缓冲
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.BackColor = Color.Transparent;

this.Cursor = Cursors.Arrow;// Cursors.Hand;
this.Size = new Size(45, 30);//Size(25, 22);

}


bool isCheck = true;

/// <summary>
/// 是否选中
/// </summary>
public bool Checked
{
set { isCheck = value; this.Invalidate(); }
get { return isCheck; }
}

CheckStyle checkStyle = CheckStyle.style1;

/// <summary>
/// 样式
/// </summary>
public CheckStyle CheckStyleX
{
set { checkStyle = value; this.Invalidate(); }
get { return checkStyle; }
}

protected override void OnPaint(PaintEventArgs e)
{
Bitmap bitMapOn = null;
Bitmap bitMapOff = null;

if (checkStyle == CheckStyle.style1)
{
bitMapOn = global::wf_foh100_demotool_release.Properties.Resources.close4530_454548;
bitMapOff = global::wf_foh100_demotool_release.Properties.Resources.close4530_red;
}
else if (checkStyle == CheckStyle.style2)
{
bitMapOn = global::wf_foh100_demotool_release.Properties.Resources.grow4530_454548;
bitMapOff = global::wf_foh100_demotool_release.Properties.Resources.grow4530_636365;
}
else if (checkStyle == CheckStyle.style3)
{
bitMapOn = global::wf_foh100_demotool_release.Properties.Resources.shrink4530_454548;
bitMapOff = global::wf_foh100_demotool_release.Properties.Resources.shrink4530_636365;
}
else if (checkStyle == CheckStyle.style4)
{
bitMapOn = global::wf_foh100_demotool_release.Properties.Resources.minimize4530_454548;
bitMapOff = global::wf_foh100_demotool_release.Properties.Resources.minimize4530_636365;
}
//else if (checkStyle == CheckStyle.style5)
//{
// bitMapOn = global::myButtonCheckTest.Properties.Resources.btncheckon5;
// bitMapOff = global::myButtonCheckTest.Properties.Resources.btncheckoff5;
//}
//else if (checkStyle == CheckStyle.style6)
//{
// bitMapOn = global::myButtonCheckTest.Properties.Resources.btncheckon6;
// bitMapOff = global::myButtonCheckTest.Properties.Resources.btncheckoff6;
//}

Graphics g = e.Graphics;
Rectangle rec = new Rectangle(0, 0, this.Size.Width, this.Size.Height);

if (isCheck)
{
g.DrawImage(bitMapOn, rec);
}
else
{
g.DrawImage(bitMapOff, rec);
}
}


private void UserControl_close_mouseleave(object sender, EventArgs e)
{
isCheck = true;
this.Invalidate();
}
private void UserControl_close_mouseenter(object sender, EventArgs e)
{
isCheck = !isCheck;
this.Invalidate();
}
}
}

form :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;

namespace wf_foh100_demotool_release
{

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
menubarToolTip();
}

#region menubar set
//menubarToolTip grow/shrink/minimize/close prompt
private void menubarToolTip()
{
ToolTip tooltipclose = new ToolTip();
tooltipclose.InitialDelay = 200;
tooltipclose.AutoPopDelay = 10 * 1000;
tooltipclose.ReshowDelay = 200;
tooltipclose.ShowAlways = true;
tooltipclose.IsBalloon = false;

string tipOverwrite_close = "关闭";
string tipOverwrite_minimize = "最小化";
string tipOverwrite_grow = "最大化";
string tipOverwrite_shrink = "向下还原";
tooltipclose.SetToolTip(userControl_minimize, tipOverwrite_minimize);
tooltipclose.SetToolTip(userControl_close1, tipOverwrite_close);
tooltipclose.SetToolTip(userControl_shrink, tipOverwrite_shrink);
tooltipclose.SetToolTip(userControl_grow, tipOverwrite_grow);
}
private void userControl_minimize_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
// this.Hide();
}
private void userControl_grow_Click(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Maximized)
{
//还原窗体
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_RESTORE, 0);
this.WindowState = FormWindowState.Normal;
}
else if (this.WindowState == FormWindowState.Normal)
{
//最大化窗体
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
userControl_grow.Visible = false;
userControl_shrink.Visible = true;

}
}
private void userControl_shrink_Click(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Maximized)
{
//还原窗体
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_RESTORE, 0);
this.WindowState = FormWindowState.Normal;
userControl_shrink.Visible = false;
userControl_grow.Visible = true;
}
else if (this.WindowState == FormWindowState.Normal)
{
//最大化窗体
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
}
}
private void userControl_close1_Click(object sender, EventArgs e)
{
Application.Exit();
}

//mouse move menubar
[DllImport("user32.dll")]//namespace: System.Runtime.InteropServices;
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
public const int SC_MINIMIZE = 0xF020;
public const int SC_MAXIMIZE = 0xF030;
public const int SC_RESTORE = 0xF120;
public const int SC_SIZE = 0xF000;
//change form size,SC_SIZE+bellow valnue
public const int LEFT = 0x0001;//cursor on form left
public const int RIGHT = 0x0002;//right
public const int UP = 0x0003;//upper
public const int LEFTUP = 0x0004;//left upper
public const int RIGHTUP = 0x0005;//right upper
public const int BOTTOM = 0x0006;//bottom
public const int LEFTBOTTOM = 0x0007;//left bottom
public const int RIGHTBOTTOM = 0x0008;//right bottom
private void Panel_menubar_MouseDown(object sender, MouseEventArgs e)
{
if (e.Clicks <= 1)
{
//拖动窗体
ReleaseCapture();//释放label1对鼠标的捕捉
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
else if (e.Clicks == 2)
{
if (this.WindowState == FormWindowState.Maximized)
{
//还原窗体
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_RESTORE, 0);
this.WindowState = FormWindowState.Normal;
}
else if (this.WindowState == FormWindowState.Normal)
{
//最大化窗体
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
}
}
}

// click taskbar minimize
[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
public static extern int GetWindowLong(HandleRef hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);

protected override CreateParams CreateParams
{
get
{
const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义,可以从MFC中查看
CreateParams cp = base.CreateParams;
cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作
return cp;
}
}

// change size by click edge
private const int LFORM_HTLEFT = 10;
private const int LFORM_HTRIGHT = 11;
private const int LFORM_HTTOP = 12;
private const int LFORM_HTTOPLEFT = 13;
private const int LFORM_HTTOPRIGHT = 14;
private const int LFORM_HTBOTTOM = 15;
private const int LFORM_HTBOTTOMLEFT = 0x10;
private const int LFORM_HTBOTTOMRIGHT = 17;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0084:
base.WndProc(ref m);
Point vPoint = new Point((int)m.LParam & 0xFFFF, (int)m.LParam >> 16 & 0xFFFF);
vPoint = PointToClient(vPoint);
if (vPoint.X <= 5)
if (vPoint.Y <= 5)
m.Result = (IntPtr)LFORM_HTTOPLEFT;
else if (vPoint.Y >= ClientSize.Height - 5)
m.Result = (IntPtr)LFORM_HTBOTTOMLEFT;
else
m.Result = (IntPtr)LFORM_HTLEFT;
else if (vPoint.X >= ClientSize.Width - 5)
if (vPoint.Y <= 5)
m.Result = (IntPtr)LFORM_HTTOPRIGHT;
else if (vPoint.Y >= ClientSize.Height - 5)
m.Result = (IntPtr)LFORM_HTBOTTOMRIGHT;
else
m.Result = (IntPtr)LFORM_HTRIGHT;
else if (vPoint.Y <= 5)
m.Result = (IntPtr)LFORM_HTTOP;
else if (vPoint.Y >= ClientSize.Height - 5)
m.Result = (IntPtr)LFORM_HTBOTTOM;
break;
case 0x0201://鼠标左键按下的消息
m.Msg = 0x00A1;//更改消息为非客户区按下鼠标
m.LParam = IntPtr.Zero; //默认值
m.WParam = new IntPtr(2);//鼠标放在标题栏内
base.WndProc(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}

private void ExitMainForm()
{
this.Close();
this.Dispose();
Application.Exit();
}
private void HideMainForm()
{
this.Hide();
}
private void ShowMainForm()
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}


#endregion


}
}

原文链接: https://www.cnblogs.com/cxwx/archive/2011/01/11/1932620.html