TableLayoutPanelDesiner
TableLayoutPanel
从零开始,因为你将失去所有当前的设计师功能。
A真的很好
戏法
就是在设计时找到设计师并操纵设计师。看看这个
以及
背景色
新项目:
为此,您可以找到
表格布局面板
并对其进行操作。一个很好的观点是
OnHandleCreated
IDesignerHost
从
Site
然后可以获取控件的当前操作列表,并创建一个包含所有这些操作项的新操作列表,并向列表中添加一些新操作项。
MyTableLayoutPanel
using System;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
public class MyTableLayoutPanel : TableLayoutPanel
{
private IDesignerHost designerHost;
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (DesignMode && Site != null)
{
designerHost = Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
if (designerHost != null)
{
var designer = (ControlDesigner)designerHost.GetDesigner(this);
if (designer != null)
{
var actions = designer.ActionLists[0];
designer.ActionLists.Clear();
designer.ActionLists.Add(
new MyTableLayoutPanelActionList(designer, actions));
}
}
}
}
}
MyTableLayoutPanel操作列表
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows;
using System.Windows.Forms.Design;
public class MyTableLayoutPanelActionList : DesignerActionList
{
MyTableLayoutPanel control;
ControlDesigner designer;
DesignerActionList actionList;
public MyTableLayoutPanelActionList(ControlDesigner designer,
DesignerActionList actionList) : base(designer.Component)
{
this.designer = designer;
this.actionList = actionList;
control = (MyTableLayoutPanel)designer.Control;
}
public Color BackColor
{
get { return control.BackColor; }
set
{
TypeDescriptor.GetProperties(Component)["BackColor"]
.SetValue(Component, value);
}
}
private void DoSomething()
{
MessageBox.Show("My Custom Verb added!");
}
public override DesignerActionItemCollection GetSortedActionItems()
{
var items = new DesignerActionItemCollection();
foreach (DesignerActionItem item in actionList.GetSortedActionItems())
items.Add(item);
var category = "New Actions";
items.Add(new DesignerActionMethodItem(this, "DoSomething",
"Do something!", category, true));
items.Add(new DesignerActionPropertyItem("BackColor", "Back Color",
category));
return items;
}
}