一、UserControl类
UserControl
是 .NET 中的一个基类,用于创建自定义控件,主要用于 Windows Forms 和 WPF。通过继承 UserControl
,你可以设计和实现具有特定界面和功能的控件组件。UserControl
允许你将多个标准控件组合在一起,并添加自定义行为和事件处理。它支持设计时和运行时的自定义,并且可以像其他控件一样拖放到窗体或页面中。
创建 UserControl
的步骤
-
定义控件:
- 创建一个新类,继承自
UserControl
。 - 在设计器中添加所需的控件和布局,或在代码中手动添加控件。
- 创建一个新类,继承自
-
添加自定义功能:
- 编写方法和事件处理程序来定义控件的行为。
- 添加属性和方法,以便在其他控件或窗体中使用。
-
使用控件:
- 在窗体或其他容器控件中实例化并添加
UserControl
。 - 设置控件的属性,并处理其事件。
- 在窗体或其他容器控件中实例化并添加
使用案例:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestMain
{
public class PrintPanel2 : UserControl
{
private Button _Button;
private Label _Label;
public PrintPanel2()
{
_Button = new Button();
_Label = new Label();
_Button.Text = "点击我吧";
_Button.Click += Button_Click;
// 配置标签
_Label.Text = "Hello, World";
_Label.Location=new System.Drawing.Point(0,30);
Controls.Add(_Button);
Controls.Add(_Label);
// 配置 UserControl
this.Size = new System.Drawing.Size(200, 100);
}
private void Button_Click(object sender, EventArgs e)
{
_Label.Text = "1111";
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// PrintPanel2
//
this.Name = "PrintPanel2";
this.Size = new System.Drawing.Size(523, 298);
this.ResumeLayout(false);
}
}
public partial class MainFrom : Form
{
private TestMain.PrintPanel2 _PrintPanel;
public MainFrom()
{
_PrintPanel = new TestMain.PrintPanel2();
_PrintPanel.Dock = DockStyle.Fill;
this.Controls.Add(_PrintPanel);
}
}
}
二、Dockpanel和DockContent
dockpanel中提供了几个可用的类, 重要的有两个, 一是DockPanel, 一是DockContent,
DockPanel是从panel继承出来的, 用于提供可浮动的dock的子窗口进行浮动和dock的场所,
DockContent是从form类中继承出来的, 用于提供可浮动的窗口基类. 就是说: DockContent对象可以在DockPanel对象中任意贴边, 浮动, TAB化等.
DockContent
:代表一个可以被停靠的窗体。DockPanel
:用于管理和显示多个DockContent
实例。
添加库
添加引用:
using WeifenLuo.WinFormsUI.Docking;
Dockpanel
public class MyDockPanel : DockPanel
{
public MyDockPanel()
{
Label _a = new Label();
_a.Text= "我是MyDockPanel ";
Controls.Add( _a );
this.Size = new System.Drawing.Size(200, 200);
}
}
DockContent
public class MyDockContent : DockContent
{
public MyDockContent()
{
this.Text = "Dockable Window!!!!!!!!!!!!!!!!!!!";
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// MyDockContent
//
this.ClientSize = new System.Drawing.Size(284, 261);
this.Name = "MyDockContent";
this.ResumeLayout(false);
}
}
案例1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace TestMain
{
public class MyDockContent : DockContent
{
public MyDockContent()
{
this.Text = "Dockable Window!!!!!!!!!!!!!!!!!!!";
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// MyDockContent
//
this.ClientSize = new System.Drawing.Size(284, 261);
this.Name = "MyDockContent";
this.ResumeLayout(false);
}
}
public class MyDockPanel : DockPanel
{
public MyDockPanel()
{
Label _a = new Label();
_a.Text= "我是MyDockPanel ";
Controls.Add( _a );
this.Size = new System.Drawing.Size(200, 200);
}
}
public partial class Form2 : Form
{
private MyDockPanel _DockPanel;
public Form2()
{
InitializeComponent();
_DockPanel = new MyDockPanel();
this.Controls.Add(_DockPanel);
_DockPanel.Dock =DockStyle.Fill;
var _dockContent=new MyDockContent();
_dockContent.Show(_DockPanel,DockState.DockLeft);
}
}
}
案例2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace TestMain
{
public partial class Form3 : DockContent
{
public class MyDockContent2 : DockContent
{
public MyDockContent2()
{
this.Text = "abc!!";
}
}
public class MyDockPanel1 : DockPanel
{
public MyDockPanel1()
{
}
}
private MyDockPanel1 _DockPanel1;
public Form3()
{
InitializeComponent();
// 初始化 DockPanel
_DockPanel1 = new MyDockPanel1();
_DockPanel1.Dock = DockStyle.Fill;
this.Controls.Add(_DockPanel1);
// 创建并显示 DockContent
CreateAndShowDockContents();
}
private void CreateAndShowDockContents()
{
// 创建不同的 DockContent 实例
var dockContent1 = new MyDockContent2();
dockContent1.Text = "Content 1";
var dockContent2 = new MyDockContent2();
dockContent2.Text = "Content 2";
var dockContent3 = new MyDockContent2();
dockContent3.Text = "Content 3";
var dockContent4= new MyDockContent2();
dockContent4.Text = "Content 4";
// 显示 DockContent 并停靠到不同的位置
dockContent1.Show(_DockPanel1, DockState.DockLeft);
dockContent2.Show(_DockPanel1, DockState.DockLeft);
dockContent3.Show(_DockPanel1, DockState.DockLeft);
dockContent4.Show(_DockPanel1, DockState.DockLeft);
}
}
}
案例3
添加控件工具
设置停靠
设计子界面:
结果:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace TestMain
{
public partial class MainFrm : Form
{
Form4 wid1 = new Form4();
Form4 wid2 = new Form4();
FromA wid3 = new FromA();
Form4 wid4 = new Form4();
FromA wid5 = new FromA();
Form4 wid6 = new Form4();
Mav wid7 = new Mav();
public MainFrm()
{
InitializeComponent();
}
private void MainFrm_Load(object sender, EventArgs e)
{
this.IsMdiContainer = true;
//改变主题
//VS2022LightTheme vs2015 = new VS2022LightTheme();
//dockPanel1.Theme = vs2015;
//以各种方式停靠
wid1.Show(dockPanel1, DockState.DockLeft);
wid2.Show(dockPanel1, DockState.DockRight);
wid3.Show(dockPanel1, DockState.DockRight);
wid4.Show(dockPanel1, DockState.Document);
wid5.Show(dockPanel1, DockState.Document);
wid6.Show(dockPanel1, DockState.DockBottom);
wid7.Show(dockPanel1, DockState.Float);
}
}
}
三、 Drag&Drop事件
基本原理
1,DragDrop:在完成拖放到目标操作时发生。(接收方)
2,DragEnter:在将对象拖入目标控件的边界时发生。(接收方)
3,DragLeave:在将对象拖出目标控件的边界时或鼠标抬起事件发生。(接收方)
4,DragOver:在拖动到目标控件的边界里面就发生。(接收方)
5,DoDragDrop:开始拖放操作。(拖动方)
6,GiveFeedback:在执行拖动操作期间发生-鼠标按下并拖动的时候发生。(拖动方)
7,QueryContinueDrag:在拖放操作期间发生,并且允许拖动源确定是否应取消拖放操作。(拖动方)
鼠标拖动原始控件
鼠标拖动到目标控件
案例一
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
namespace TestMain
{
public partial class BaseFrm : Form
{
private Rectangle dragBox;
public BaseFrm()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
DateTime time = DateTime.Now;
// listBox1.Items.Add(time.ToString() +" 拖动方 - label1_GiveFeedback 发生");
}
/// <summary>
/// 鼠标按下功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label1_MouseDown(object sender, MouseEventArgs e)
{
// DateTime time = DateTime.Now;
// listBox1.Items.Add(time.ToString() + " abel1_MouseDown 鼠标按下功能 发生");
dragBox = new Rectangle(new Point(e.X - (SystemInformation.DragSize.Width / 2),
e.Y - (SystemInformation.DragSize.Height / 2)), SystemInformation.DragSize);
}
/// <summary>
/// 鼠标移动事件发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label1_MouseMove(object sender, MouseEventArgs e)
{
//listBox1.Items.Add("label1_MouseMove 鼠标移动事件发生 发生");
if ((e.Button & MouseButtons.Left) == MouseButtons.Left) // 左键
{
if (dragBox != Rectangle.Empty && !dragBox.Contains(e.X, e.Y))
{
var effect = label1.DoDragDrop(label1.Text, DragDropEffects.All | DragDropEffects.Link);
if (effect == DragDropEffects.Move)
label1.Text = "";
}
}
}
/// <summary>
/// 鼠标抬起事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label1_MouseUp(object sender, MouseEventArgs e)
{
// listBox1.Items.Add("label1_MouseUp 鼠标抬起事件 发生");
dragBox = Rectangle.Empty;
}
/// <summary>
/// 点击
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label3_Click(object sender, EventArgs e)
{
DateTime time = DateTime.Now;
listBox1.Items.Add(time.ToString() + " 接收方 - label3_Click 发生");
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label3_DragDrop(object sender, DragEventArgs e)
{
DateTime time = DateTime.Now;
//复杂
label3.Text = time.ToString()+ " : "+$"{e.Effect}:{(string)e.Data.GetData(typeof(string))}";
listBox1.Items.Add(time.ToString() + " 接收方 - label3_DragDrop 发生-----已经完成了拖动11111111111111");
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label3_DragEnter(object sender, DragEventArgs e)
{
DateTime time = DateTime.Now;
listBox1.Items.Add(time.ToString() + " 接收方 - label3_DragEnter 发生 已经进入接受方界面了");
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label3_DragOver(object sender, DragEventArgs e)
{
DateTime time = DateTime.Now;
if ((e.AllowedEffect & DragDropEffects.None) == DragDropEffects.None && (e.KeyState & (8 + 32)) == (8 + 32))
{
//CTRL+ALT
e.Effect = DragDropEffects.None;
label2.Text = $"按键状态:CTRL+ALT\r\n效果:None";
listBox1.Items.Add(time.ToString() + " 接收方 - label3_DragOver发生 2222222 ");
}
else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link
&& (e.KeyState & (32)) == (32))
{
//ALT
e.Effect = DragDropEffects.Link;
label2.Text = $"按键状态:ALT\r\n效果:Link";
listBox1.Items.Add(time.ToString() + " 接收方 - label3_DragOver发生 333333333 ");
}
else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy && (e.KeyState & (8)) == (8))
{
//CTRL
e.Effect = DragDropEffects.Copy;
label2.Text = $"按键状态:CTRL+ALT\r\n效果:Copy";
listBox1.Items.Add(time.ToString() + " 接收方 - label3_DragOver发生 44444444 ");
}
else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move && (e.KeyState & (4)) == (4))
{
//SHIFT
e.Effect = DragDropEffects.Move;
label2.Text = $"按键状态:SHIFT\r\n效果:Move";
}
else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
{
listBox1.Items.Add(time.ToString() + " 接收方 - label3_DragOver发生 ###### ");
//无
e.Effect = DragDropEffects.Copy;
label2.Text = $"按键状态:无\r\n效果:Copy";
}
else
{
e.Effect = DragDropEffects.None;
label2.Text = $"按键状态:无\r\n效果:None";
listBox1.Items.Add(time.ToString() + " 接收方 - label3_DragOver发生 5555 ");
}
}
private void label1_MouseLeave(object sender, EventArgs e)
{
DateTime time = DateTime.Now;
listBox1.Items.Add(time.ToString() + " 拖动方 - 鼠标已经 label1_MouseLeave 发生 已经出了拖出方界面了");
}
private void label3_DragLeave(object sender, EventArgs e)
{
DateTime time = DateTime.Now;
listBox1.Items.Add(time.ToString() + " 接收方 - 鼠标已经 label3_DragLeave 发生 已经出了接收方界面了或者鼠标抬起了");
}
}
}