前言
C#Winform 自定义控件、实现窗体布局控件。
1、控件继承Panel 后添加自定义功能:调整视图大小,显示/隐藏视图。
2、视图分为Top 、Bottom、Left、Right,Container,分别表示上、下、左、右、中。
3、添加DockTo(Control control, DockStyle dockStyle)停靠方法,将控件填充停靠到指定位置。
效果展示
代码
UVPanel 控件类
/// <summary>
/// 用户视图面板
/// </summary>
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[DefaultEvent("Opening")]
[Serializable]
public class UVPanel : Panel
{
#region 委托、事件
/// <summary>
/// 隐藏事件
/// </summary>
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public Action<object, bool> OnHideButton;
/// <summary>
/// 隐藏事件
/// </summary>
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public Action<object, bool> OnHideStateBar;
/// <summary>
/// 隐藏事件
/// </summary>
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public Action<object, bool> OnHideTitleBar;
/// <summary>
/// 关闭事件
/// </summary>
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public Action<object, bool> OnCloseButton;
#endregion
#region 字段、属性
#region 字段
private Panel titlePanel = new Panel(); //标题栏
private Panel statePanel = new Panel(); //状态栏
private Panel containerPanel = new Panel(); //主容器
private Label titleName = new Label(); //标题名称
private Button hideButton = new Button(); //隐藏按钮
private Button closeButton = new Button(); //关闭按钮
private Size titleButtonSize = new Size(24,24); //按钮大小
private bool isHide = false; //是否隐藏
private bool isHideStateBar = false; //是否隐藏状态栏
private bool isHideTitleBar = false; //是否隐藏标题栏
private bool isResizing; //是否重设大小
private string resizeDirection = ""; //重设方向
private Point lastMousePosition; //鼠标最后位置
#endregion
#region 属性
/// <summary>
/// 隐藏按钮
/// </summary>
[Category("MVisionUI"),Description("标题名称"),DefaultValue("uvPanelName1")]
public string TitleName {
get => titleName.Text;
set
{
titleName.Text = value;
this.Invalidate();
}
}
/// <summary>
/// 标题按钮大小
/// </summary>
[Category("MVisionUI"), Description("标题按钮大小"), DefaultValue(typeof(Size), "24, 24")]
public Size TitleButtonSize {
get => titleButtonSize;
set
{
titleButtonSize = value;
this.Invalidate();
}
}
/// <summary>
/// 标题按钮大小
/// </summary>
[Category("MVisionUI"), Description("标题背景颜色"), DefaultValue(typeof(Color), "90, 0, 90")]
public Color TitleBackgroudColor {
get {
return titlePanel.BackColor;
}
set
{
titlePanel.BackColor = value;
this.Invalidate();
}
}
/// <summary>
/// 标题按钮大小
/// </summary>
[Category("MVisionUI"), Description("隐藏控件"), DefaultValue(false)]
public bool IsHide
{
get { return isHide; }
set
{
isHide = value;
OnHideButton?.Invoke(this.Name,value);
if (isHide) this.Hide();
else this.Show();
}
}
/// <summary>
/// 是否隐藏状态栏
/// </summary>
[Category("MVisionUI"), Description("隐藏状态栏"), DefaultValue(false)]
public bool IsHideStateBar {
get => isHideStateBar;
set
{
isHideStateBar = value;
OnHideStateBar?.Invoke(this.Name, value);
if (isHideStateBar) this.Hide();
else this.Show();
}
}
/// <summary>
/// 是否隐藏标题栏
/// </summary>
[Category("MVisionUI"), Description("隐藏标题栏"), DefaultValue(false)]
public bool IsHideTitleBar {
get => isHideTitleBar;
set
{
isHideTitleBar = value;
OnHideTitleBar?.Invoke(this.Name, value);
if (!isHideTitleBar) this.Hide();
else this.Show();
}
}
#endregion
#endregion
#region 构造函数
public UVPanel()
{
this.SuspendLayout();
this.DoubleBuffered = true;
this.BackColor = Color.AliceBlue;
this.MinimumSize = new Size(50,50);
this.AllowDrop = true;
InintSubView();
InitView();
this.SetVisibleCore(true);
this.isHide = true;
this.ResumeLayout(false);
}
#endregion
#region 初始化
/// <summary>
/// 初始化视图
/// </summary>
private void InitView()
{
titleName.TextAlign = ContentAlignment.MiddleCenter;
titleName.BackColor = Color.Purple;
titleName.ForeColor = Color.White;
titlePanel.Size = new System.Drawing.Size(16,16);
titlePanel.Dock = DockStyle.Top;
titlePanel.Controls.Add(titleName);
titlePanel.Controls.Add(hideButton);
titlePanel.Controls.Add(closeButton);
statePanel.Size = new System.Drawing.Size(24, 24);
statePanel.Dock = DockStyle.Bottom;
statePanel.BackColor = Color.FromArgb(255,125,225,125);
statePanel.Controls.Add(new Label()
{
TextAlign = ContentAlignment.MiddleRight,
BackColor = Color.Azure,
Dock = DockStyle.Right,
Size = new Size(25,statePanel.Height),
Text = "<<"
});
containerPanel.Margin = new Padding(5, 5, 5, 5);
containerPanel.Padding = new Padding(5, 5, 5, 5);
containerPanel.Dock = DockStyle.Fill;
this.Controls.Add(this.titlePanel);
this.Controls.Add(this.statePanel);
this.Controls.Add(this.containerPanel);
}
/// <summary>
/// 初始化子视图
/// </summary>
private void InintSubView()
{
hideButton.Size = titleButtonSize;
hideButton.Dock = DockStyle.Right;
hideButton.Image = MVisionViews.Properties.Resources.pin_h_8b;
hideButton.Click += HideButton_Click;
closeButton.Size = titleButtonSize;
closeButton.Dock = DockStyle.Right;
closeButton.Image = MVisionViews.Properties.Resources.circle_close_8b;
closeButton.Click += CloseButton_Click;
}
/// <summary>
/// 点击关闭按钮
/// </summary>
private void CloseButton_Click(object sender, System.EventArgs e)
{
IsHide = true;
Console.WriteLine($"close on {isHide}");
}
/// <summary>
/// 隐藏按钮
/// </summary>
private void HideButton_Click(object sender, System.EventArgs e)
{
IsHide = true;
Console.WriteLine($"hide on {isHide}");
}
#endregion
#region 调整面板大小
#region 绑定事件回调
/// <summary>
/// 注册鼠标事件
/// </summary>
public void RegisterMouseEvents(string direction)
{
if (direction.Equals("Bottom"))
RegisterMouseEvents(titlePanel, direction);
else if (direction.Equals("Top"))
RegisterMouseEvents(statePanel, direction);
else
RegisterMouseEvents(containerPanel, direction);
}
public void RegisterMouseEvents(Control control, string direction)
{
if (direction.Equals("Bottom"))
RegisterMouseEvents(titlePanel, direction);
else if (direction.Equals("Top"))
RegisterMouseEvents(statePanel, direction);
else
RegisterMouseEvents(control, direction);
}
/// <summary>
/// 取消注册鼠标事件
/// </summary>
public void UnRegisterMouseEvents(string direction)
{
if (direction.Equals("Bottom"))
UnRegisterMouseEvents(titlePanel, direction);
else if (direction.Equals("Top"))
UnRegisterMouseEvents(statePanel, direction);
else
UnRegisterMouseEvents(containerPanel, direction);
}
/// <summary>
/// 为指定的面板注册鼠标事件
/// </summary>
/// <param name="panel">面板对象</param>
/// <param name="direction">方向标识(Top, Bottom, Left, Right)</param>
private void RegisterMouseEvents(Panel panel, string direction)
{
panel.MouseMove += (s, e) => Panel_MouseMove(s, e, direction);
panel.MouseDown += (s, e) => Panel_MouseDown(s, e, direction);
panel.MouseUp += (s, e) => Panel_MouseUp(s, e, direction);
panel.MouseLeave += (s, e) => panel.Cursor = Cursors.Default;
}
/// <summary>
/// 为指定的面板取消注册鼠标事件
/// </summary>
/// <param name="panel">面板对象</param>
/// <param name="direction">方向标识(Top, Bottom, Left, Right)</param>
private void UnRegisterMouseEvents(Panel panel, string direction)
{
panel.MouseMove -= (s, e) => Panel_MouseMove(s, e, direction);
panel.MouseDown -= (s, e) => Panel_MouseDown(s, e, direction);
panel.MouseUp -= (s, e) => Panel_MouseUp(s, e, direction);
panel.MouseLeave -= (s, e) => panel.Cursor = Cursors.Default;
}
#endregion
#region 事件回调
/// <summary>
/// 面板移动
/// </summary>
private void Panel_MouseMove(object sender, MouseEventArgs e, string direction)
{
this.SuspendLayout(); // 暂停布局
Panel panel = sender as Panel;
//显示箭头
if (!isResizing)
{
// 判断鼠标是否靠近边缘10px
bool nearEdge = false;
//显示箭头方向
switch (direction)
{
case "Top":
if (e.Y >= panel.Height - 10 && e.Y <= panel.Height)
{ panel.Cursor = Cursors.SizeNS; nearEdge = true; }
break;
case "Bottom":
if (e.Y >= 0 && e.Y <= 10)
{ panel.Cursor = Cursors.SizeNS; nearEdge = true; }
break;
case "Left":
if (e.X >= panel.Width - 10 && e.X <= panel.Width)
{ panel.Cursor = Cursors.SizeWE; nearEdge = true; }
break;
case "Right":
if (e.X >= 0 && e.X <= 10)
{ panel.Cursor = Cursors.SizeWE; nearEdge = true; }
break;
default: panel.Cursor = Cursors.Default; break;
}
// 如果不在边缘,恢复默认光标
if (!nearEdge)
{
panel.Cursor = Cursors.Default;
}
}
//重设大小
else
{
int delta;// 处理调整大小逻辑
if (resizeDirection == "Top")
{
delta = e.Y - lastMousePosition.Y;
if (this.Height - delta > 20) // 设置最小高度
{
this.Height += delta;
lastMousePosition = e.Location;
}
}
else if (resizeDirection == "Bottom")
{
delta = e.Y - lastMousePosition.Y;
if (this.Height - delta > 20)
{
this.Height -= delta;
lastMousePosition = e.Location;
}
}
else if (resizeDirection == "Left")
{
delta = e.X - lastMousePosition.X;
if (this.Width - delta > 20) // 设置最小宽度
{
this.Width += delta;
lastMousePosition = e.Location;
}
}
else if (resizeDirection == "Right")
{
delta = e.X - lastMousePosition.X;
if (this.Width - delta > 20)
{
this.Width -= delta;
lastMousePosition = e.Location;
}
}
}
this.ResumeLayout(); // 恢复布局
}
/// <summary>
/// 面板按下
/// </summary>
private void Panel_MouseDown(object sender, MouseEventArgs e, string direction)
{
Panel panel = sender as Panel;
///鼠标按下左键
if (e.Button == MouseButtons.Left)
{
bool nearEdge = false;
switch (direction)
{
case "Top":
if (e.Y >= panel.Height - 10 && e.Y <= panel.Height)
nearEdge = true;
break;
case "Bottom":
if (e.Y >= 0 && e.Y <= 10)
nearEdge = true;
break;
case "Left":
if (e.X >= panel.Width - 10 && e.X <= panel.Width)
nearEdge = true;
break;
case "Right":
if (e.X >= 0 && e.X <= 10)
nearEdge = true;
break;
default:
panel.Cursor = Cursors.Default;
break;
}
//这里是边缘
if (nearEdge)
{
isResizing = true;
resizeDirection = direction;
lastMousePosition = e.Location;
}
}
}
/// <summary>
/// 面板抬起
/// </summary>
private void Panel_MouseUp(object sender, MouseEventArgs e, string direction)
{
if (isResizing)
{
isResizing = false;
resizeDirection = "";
Panel panel = sender as Panel;
panel.Cursor = Cursors.Default;
}
}
#endregion
#endregion
#region 其他方法
public void DockTo(Control control)
{
control.Dock = DockStyle.Fill;
this.containerPanel.Controls.Add(control);
this.containerPanel.Controls.SetChildIndex(control, 0);
}
#endregion
#region 重写方法
/// <summary>
/// 允许设计器添加控件
/// </summary>
protected override ControlCollection CreateControlsInstance()
{
return new ControlCollection(this);
}
// 使控件在设计器中可见
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Browsable(true)]
public new ControlCollection Controls
{
get { return base.Controls; }
}
#endregion
}
UVAutoLayoutPanel 控件类
/// <summary>
/// 活动布局面板
/// </summary>
[DefaultEvent("Click")]
[Serializable]
public class UVAutoLayoutPanel : Panel
{
#region 委托 事件
/// <summary>
/// 隐藏按钮触发
/// </summary>
public event Action<object, bool> OnHideButton;
#endregion
#region 字段、属性、集合(字段、属性)
#region 字段
/// <summary>
/// 面板名前缀
/// </summary>
private readonly string panelNamePrefixes = "panel_";
/// <summary>
/// 视图名集合
/// </summary>
private List<string> viewNameList = new List<string> { "Fill", "Right", "Left", "Top", "Bottom" };
/// <summary>
///
/// </summary>
private List<string> viewTitleNameList = new List<string> { "图像窗口", "流程栏", "工具栏", "导航栏", "信息栏" };
/// <summary>
/// 视图颜色集合
/// </summary>
private List<Color> viewColorList = new List<Color> { Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Cyan };
/// <summary>
/// 视图停靠布局集合
/// </summary>
private List<DockStyle> viewDockList = new List<DockStyle>()
{
DockStyle.Fill , DockStyle.Left,DockStyle.Left, DockStyle.Top, DockStyle.Bottom
};
/// <summary>
/// 视图属性列表
/// </summary>
private List<ViewProperties> viewPropertiesList = new List<ViewProperties>();
/// <summary>
/// 用户面板
/// </summary>
private List<UVPanel> viewUVPanelList = new List<UVPanel>();
#endregion
#region 属性
/// <summary>
/// 视图名列表
/// </summary>
[Category("MVisonUI"), Description("视图名列表")]
public List<string> ViewNameList { get => viewNameList; }
/// <summary>
/// 视图属性集合
/// </summary>
[Category("MVisonUI"), Description("视图属性集合")]
public List<ViewProperties> ViewPropertiesList
{
get { return viewPropertiesList; }
//set
//{
// viewPropertiesList = value;
// for (int i = 0; i < viewNameList.Count; i++)
// {
// string panelName = panelNamePrefixes + viewNameList[i];
// UVPanel panel = this.Controls[panelName] as UVPanel;
// panel.Name = panelNamePrefixes + viewPropertiesList[i].Name;
// panel.Dock = viewPropertiesList[i].Dock;
// panel.BackColor = viewPropertiesList[i].Color;
// panel.TitleName = viewPropertiesList[i].TitleName;
// }
// this.Invalidate();
//}
}
/// <summary>
/// 视图面板集合
/// </summary>
[Category("MVisonUI"), Description("视图面板集合")]
public List<UVPanel> UVPanelList
{
get
{
return viewUVPanelList;
}
}
/// <summary>
/// 视图面板集合
/// </summary>
[Category("MVisonUI"), Description("视图标题名文本")]
public List<string> ViewTitleNameList
{
get { return viewTitleNameList; }
set
{
viewTitleNameList = value;
if (this.Controls.Count > 0)
{
for (int i = 0; i < viewNameList.Count; i++)
{
string panelName = panelNamePrefixes + viewNameList[i];
UVPanel panel = this.Controls[panelName] as UVPanel;
panel.TitleName = viewTitleNameList[i];
}
}
}
}
#endregion
#endregion
#region 构造函数
public UVAutoLayoutPanel()
{
this.DoubleBuffered = true;
this.Dock = DockStyle.Fill;
this.AllowDrop = true; // 允许拖放
///初始化基本视图属性
for (int i = 0; i < viewNameList.Count; i++)
{
viewPropertiesList.Add(new ViewProperties(viewNameList[i], ViewTitleNameList[i],
viewColorList[i], viewDockList[i]));
}
///添加布局面板
foreach (var item in viewPropertiesList)
{
UVPanel panel = new UVPanel();
panel.Name = panelNamePrefixes + item.Name;
panel.TitleName = item.TitleName;
panel.BackColor = item.Color;
panel.TitleBackgroudColor = Color.Beige;
panel.Dock = item.Dock;
if (!item.Name.Equals("Fill"))
{
panel.RegisterMouseEvents(item.Dock.ToString());
}
panel.OnHideButton += OnButtonCallback;
panel.OnCloseButton += OnButtonCallback;
this.Controls.Add(panel);
UVPanelList.Add(panel);
}
}
#endregion
#region 重写方法
#endregion
#region 其他方法
/// <summary>
/// 隐藏面板
/// </summary>
public void HidePanel(string panelName, bool flag = true)
{
int index = this.Controls.IndexOfKey($"{panelNamePrefixes}{panelName}");
if (index != -1)
{
UVPanel control = this.Controls[index] as UVPanel;
control.IsHide = flag;
}
}
/// <summary>
/// 隐藏所有面板
/// </summary>
public void HideAllPanel()
{
foreach (var item in this.Controls)
{
if (item is UVPanel panel && !panel.Name.Equals($"{panelNamePrefixes}Top"))
{
panel.IsHide = true;
}
}
}
/// <summary>
/// 显示面板
/// </summary>
public void ShowPanel(string panelName, bool flag = false)
{
int index = this.Controls.IndexOfKey($"{panelNamePrefixes}{panelName}");
if (index != -1)
{
UVPanel control = this.Controls[index] as UVPanel;
control.IsHide = flag;
}
}
/// <summary>
/// 显示所有面板
/// </summary>
public void ShowAllPanel()
{
foreach (var item in this.Controls)
{
if (item is UVPanel panel)
{
panel.IsHide = false;
}
}
}
/// <summary>
/// 触发隐藏面板
/// </summary>
public void OnButtonCallback(object sender, bool flag)
{
string panelName = viewNameList.FirstOrDefault(name => sender.ToString().Contains(name));
OnHideButton?.Invoke(panelName, flag);
}
/// <summary>
/// 停靠方法
/// </summary>
public void DockTo(Control control, DockStyle dockStyle)
{
if (control is Form form)
{
form.TopLevel = false;
form.FormBorderStyle = FormBorderStyle.None;
}
bool exists = this.viewNameList.Contains($"{dockStyle.ToString()}");
string panelName = panelNamePrefixes + dockStyle.ToString();
if (exists)
{
control.Dock = DockStyle.Fill;
UVPanel panel = this.Controls[panelName] as UVPanel;
panel.DockTo(control);
control.Show();
}
}
/// <summary>
/// 添加右键菜单到面板
/// </summary>
public void AddRightMenuTo(ContextMenuStrip menu, DockStyle dockStyle)
{
string panelName = panelNamePrefixes + dockStyle.ToString();
this.Controls[panelName].ContextMenuStrip = menu;
}
/// <summary>
/// 移除指定的右键菜单
/// </summary>
public void RemoveRightMenuTo(DockStyle dockStyle)
{
string panelName = panelNamePrefixes + dockStyle.ToString();
this.Controls[panelName].ContextMenuStrip = null;
}
/// <summary>
/// 注册变更面板大小事件
/// </summary>
public void RegisterChangedPanelSizeEvents(Control control, DockStyle dockStyle)
{
string panelName = panelNamePrefixes + dockStyle.ToString();
UVPanel panel = this.Controls[panelName] as UVPanel;
panel.RegisterMouseEvents(control, dockStyle.ToString());
}
/// <summary>
/// 设置面板宽度
/// </summary>
public void SetDockPanelWidth(DockStyle dockStyle, int width)
{
string panelName = panelNamePrefixes + dockStyle.ToString();
UVPanel panel = this.Controls[panelName] as UVPanel;
panel.Width = width;
}
/// <summary>
/// 设置面板高度
/// </summary>
public void SetDockPanelHeight(DockStyle dockStyle, int height)
{
string panelName = panelNamePrefixes + dockStyle.ToString();
UVPanel panel = this.Controls[panelName] as UVPanel;
panel.Height = height;
}
#endregion
}
#region 类
/// <summary>
/// 视图属性
/// </summary>
[Serializable]
public class ViewProperties
{
/// <summary>
/// 视图属性
/// </summary>
public ViewProperties(string name, string titleName, Color color, DockStyle dock)
{
Name = name;
TitleName = titleName;
Color = color;
Dock = dock;
}
/// <summary>
/// 布局面板名称
/// </summary>
[Category("MVisionUI"), Description("布局面板名称")]
public string Name { get; set; }
/// <summary>
/// 布局面板名称
/// </summary>
[Category("MVisionUI"), Description("布局面板名称")]
public string TitleName { get; set; }
/// <summary>
/// 面板颜色
/// </summary>
[Category("MVisionUI"), Description("面板颜色"), DefaultValue(typeof(Color), "255,255,0")]
public Color Color { get; set; }
/// <summary>
/// 面板停靠方向
/// </summary>
[Category("MVisionUI"), Description("面板停靠方向"), DefaultValue(typeof(DockStyle), "None")]
public DockStyle Dock { get; set; }
}
#endregion
MainForm 设计器
partial class MainForm
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.Cmsx_PanelLayout = new System.Windows.Forms.ContextMenuStrip(this.components);
this.topToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.bottomToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.leftToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.rightToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.fillToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.setToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.allToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.uvAutoLayoutPanel1 = new MVisionViews.UVViews.UVAutoLayoutPanel();
this.Cmsx_PanelLayout.SuspendLayout();
this.uvAutoLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// Cmsx_PanelLayout
//
this.Cmsx_PanelLayout.Font = new System.Drawing.Font("Microsoft YaHei UI", 12F);
this.Cmsx_PanelLayout.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.topToolStripMenuItem,
this.bottomToolStripMenuItem,
this.leftToolStripMenuItem,
this.rightToolStripMenuItem,
this.fillToolStripMenuItem,
this.setToolStripMenuItem,
this.allToolStripMenuItem});
this.Cmsx_PanelLayout.Name = "Cmsx_PanelLayout";
this.Cmsx_PanelLayout.Size = new System.Drawing.Size(138, 186);
this.Cmsx_PanelLayout.Text = "Cmsx_PanelLayout";
//
// topToolStripMenuItem
//
this.topToolStripMenuItem.Name = "topToolStripMenuItem";
this.topToolStripMenuItem.Size = new System.Drawing.Size(137, 26);
this.topToolStripMenuItem.Text = "Top";
//
// bottomToolStripMenuItem
//
this.bottomToolStripMenuItem.Name = "bottomToolStripMenuItem";
this.bottomToolStripMenuItem.Size = new System.Drawing.Size(137, 26);
this.bottomToolStripMenuItem.Text = "Bottom";
//
// leftToolStripMenuItem
//
this.leftToolStripMenuItem.Name = "leftToolStripMenuItem";
this.leftToolStripMenuItem.Size = new System.Drawing.Size(137, 26);
this.leftToolStripMenuItem.Text = "Left";
//
// rightToolStripMenuItem
//
this.rightToolStripMenuItem.Name = "rightToolStripMenuItem";
this.rightToolStripMenuItem.Size = new System.Drawing.Size(137, 26);
this.rightToolStripMenuItem.Text = "Right";
//
// fillToolStripMenuItem
//
this.fillToolStripMenuItem.Name = "fillToolStripMenuItem";
this.fillToolStripMenuItem.Size = new System.Drawing.Size(137, 26);
this.fillToolStripMenuItem.Text = "Fill";
//
// setToolStripMenuItem
//
this.setToolStripMenuItem.Name = "setToolStripMenuItem";
this.setToolStripMenuItem.Size = new System.Drawing.Size(137, 26);
this.setToolStripMenuItem.Text = "Set";
//
// allToolStripMenuItem
//
this.allToolStripMenuItem.Name = "allToolStripMenuItem";
this.allToolStripMenuItem.Size = new System.Drawing.Size(137, 26);
this.allToolStripMenuItem.Text = "All";
//
// uvAutoLayoutPanel1
//
this.uvAutoLayoutPanel1.AllowDrop = true;
this.uvAutoLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.uvAutoLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.uvAutoLayoutPanel1.Name = "uvAutoLayoutPanel1";
this.uvAutoLayoutPanel1.Size = new System.Drawing.Size(968, 565);
this.uvAutoLayoutPanel1.TabIndex = 0;
this.uvAutoLayoutPanel1.ViewTitleNameList = ((System.Collections.Generic.List<string>)(resources.GetObject("uvAutoLayoutPanel1.ViewTitleNameList")));
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(968, 565);
this.Controls.Add(this.uvAutoLayoutPanel1);
this.Name = "MainForm";
this.Cmsx_PanelLayout.ResumeLayout(false);
this.uvAutoLayoutPanel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private MVisionViews.UVViews.UVAutoLayoutPanel uvAutoLayoutPanel1;
private ContextMenuStrip Cmsx_PanelLayout;
private ToolStripMenuItem topToolStripMenuItem;
private ToolStripMenuItem bottomToolStripMenuItem;
private ToolStripMenuItem leftToolStripMenuItem;
private ToolStripMenuItem rightToolStripMenuItem;
private ToolStripMenuItem fillToolStripMenuItem;
private ToolStripMenuItem setToolStripMenuItem;
private ToolStripMenuItem allToolStripMenuItem;
}
MainForm 后台代码
/// <summary>
/// 主窗体
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
DoubleBuffered = true;
this.WindowState = FormWindowState.Maximized;
this.MinimumSize = new System.Drawing.Size(500,500);
//右键菜单
this.bottomToolStripMenuItem.Click += ToolStripMenuItem_Click;
this.leftToolStripMenuItem.Click += ToolStripMenuItem_Click;
this.rightToolStripMenuItem.Click += ToolStripMenuItem_Click;
this.fillToolStripMenuItem.Click += ToolStripMenuItem_Click;
this.allToolStripMenuItem.Click += ToolStripMenuItem_Click;
//
foreach (ToolStripMenuItem item in Cmsx_PanelLayout.Items)
{
item.Checked = true;
}
uvAutoLayoutPanel1.OnHideButton += OnHideCallback;
///添加流程管理界面
Frm_FlowControl flowCtrl = Frm_FlowControl.Install();
uvAutoLayoutPanel1.DockTo(flowCtrl, DockStyle.Right);
uvAutoLayoutPanel1.AddRightMenuTo(Cmsx_PanelLayout, DockStyle.Top);
//添加工具箱
Frm_UVToolBox tooModel = new Frm_UVToolBox();
uvAutoLayoutPanel1.DockTo(tooModel, DockStyle.Left);
uvAutoLayoutPanel1.SetDockPanelWidth(DockStyle.Right, 500);
}
private void MainForm_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 隐藏回调方法
/// </summary>
private void OnHideCallback(object sender, bool arg)
{
foreach (ToolStripMenuItem item in Cmsx_PanelLayout.Items)
{
if (item.Text.Equals(sender.ToString()))
{
item.Checked = !arg;
break;
}
}
Console.WriteLine( $"{sender} = {arg}");
}
/// <summary>
/// 布局右键菜单
/// </summary>
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem toolStripMenuItem = sender as ToolStripMenuItem;
toolStripMenuItem.Checked = !toolStripMenuItem.Checked;
switch (sender.ToString())
{
case "All":
if (toolStripMenuItem.Checked)
uvAutoLayoutPanel1.ShowAllPanel();
else
uvAutoLayoutPanel1.HideAllPanel();
break;
default:
if (toolStripMenuItem.Checked)
uvAutoLayoutPanel1.ShowPanel(sender.ToString(), !toolStripMenuItem.Checked);
else
uvAutoLayoutPanel1.HidePanel(sender.ToString(), !toolStripMenuItem.Checked);
break;
}
}
}
结语
既是分享,也是备份。
最后
如果你觉得这篇文章对你有帮助,不妨点个赞支持一下!
如果有任何疑问或需要进一步的帮助,欢迎随时留言。
也可以加入微信公众号 [编程笔记in] 社区,交流学习!