获取 ToolStripDropDownMenu 的 (ToolStrip) 父级
Get the (ToolStrip) parent of a ToolStripDropDownMenu
我正在写自定义ToolStripProfessionalRenderer
例如,以下覆盖:
protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
if(e.ToolStrip is MenuStrip)
{
// It never is. It's always ToolStripDropDownMenu
}
}
我猜 OnRenderImageMargin
被下拉菜单调用,因为这是将要呈现的内容,但是我想获得父项 ToolStrip
/MenuStrip
/StatusStrip
导致 OnRenderImageMargin
调用。
这可能吗?
我以为 e.ToolStrip.Parent
属性 会是关键,但它总是 null
。
一个选项是在您的 ToolStripProfessionalRenderer
中创建构造函数,并传入对控件的引用。
class CustomRenderer : ToolStripProfessionalRenderer
{
// All those controls derive from ToolStrip so we can use the base class here
private ToolStrip ts;
public CustomRenderer(ToolStrip ts)
{
this.ts = ts;
}
protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
if (ts is MenuStrip)
{
}
else if (ts is StatusStrip)
{
}
else // ts is ToolStrip
{
}
}
然后在实例化的时候传入一个引用:
toolStrip1.Renderer = new CustomRenderer(toolStrip1);
statusStrip1.Renderer = new CustomRenderer(statusStrip1);
替代选项,修改自 this answer。
忘记构造函数并反复测试 Owner
直到获得正确的父控件:
protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
ToolStrip owner = e.ToolStrip;
while (owner is ToolStripDropDownMenu)
owner = (owner as ToolStripDropDownMenu).OwnerItem.Owner;
if (ts is MenuStrip)
{
}
else if (ts is StatusStrip)
{
}
else // ts is ToolStrip
{
}
}
我正在写自定义ToolStripProfessionalRenderer
例如,以下覆盖:
protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
if(e.ToolStrip is MenuStrip)
{
// It never is. It's always ToolStripDropDownMenu
}
}
我猜 OnRenderImageMargin
被下拉菜单调用,因为这是将要呈现的内容,但是我想获得父项 ToolStrip
/MenuStrip
/StatusStrip
导致 OnRenderImageMargin
调用。
这可能吗?
我以为 e.ToolStrip.Parent
属性 会是关键,但它总是 null
。
一个选项是在您的 ToolStripProfessionalRenderer
中创建构造函数,并传入对控件的引用。
class CustomRenderer : ToolStripProfessionalRenderer
{
// All those controls derive from ToolStrip so we can use the base class here
private ToolStrip ts;
public CustomRenderer(ToolStrip ts)
{
this.ts = ts;
}
protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
if (ts is MenuStrip)
{
}
else if (ts is StatusStrip)
{
}
else // ts is ToolStrip
{
}
}
然后在实例化的时候传入一个引用:
toolStrip1.Renderer = new CustomRenderer(toolStrip1);
statusStrip1.Renderer = new CustomRenderer(statusStrip1);
替代选项,修改自 this answer。
忘记构造函数并反复测试 Owner
直到获得正确的父控件:
protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
ToolStrip owner = e.ToolStrip;
while (owner is ToolStripDropDownMenu)
owner = (owner as ToolStripDropDownMenu).OwnerItem.Owner;
if (ts is MenuStrip)
{
}
else if (ts is StatusStrip)
{
}
else // ts is ToolStrip
{
}
}