我想创建Custom ToolStripItem
其中包含checkbox
和 Icon 的内容。picturebox
我想通过创建带有两个和标签的自定义控件来做到这一点。左边picturebox
是图标,右边是特殊checkbox
的添加位置到收藏夹。
我将该控件添加到ToolStripControlHost
.
- 问题是左侧和右侧的空白我不想删除它们或将图标移动到左侧栏并删除右侧空间。我尝试将值设置为 0
Padding
,Margin
但它不起作用。我发现有类似问题的帖子,但不是正确答案。有可能这样做吗?
- 我想在 MouseEnter 上突出显示整行,就像 ToolStripMenuItem 中的普通元素一样。当我覆盖 OnMouseEnter 和 OnMouseLeave 更改背景属性时,它只会更改托管控件的颜色,而不是 antirie ToolStripControlHost。是否可以模拟与 ToolStripMenuItem 相同的行为?
现在它看起来像图片“A”,我希望它看起来更像图片 B,但带有星形复选框:
基本上我想让我的 CustomToolStrip 项目尽可能类似于 ToolStripMenuItem。对于Click(如果您单击文本)和ChackChange(如果您单击星号)具有单独的事件
任何想法如何做到这一点?
这是我的 ToolStripControlHost 代码的一部分:
private void AddEvents()
{
this.ToolStripICItemControl.eMouseEnter += new EventHandler(On_MouseEnter);
this.ToolStripICItemControl.eMouseLeave += new EventHandler(On_MouseLeave);
}
private void AutoSizeControl(Control control, int textPadding)
{
// Create a Graphics object for the Control.
Graphics g = control.CreateGraphics();
// Get the Size needed to accommodate the formatted Text.
Size preferredSize = g.MeasureString(
control.Text, control.Font).ToSize();
// Pad the text and resize the control.
this.Size = new Size(
preferredSize.Width + 5 + 50 + (textPadding * 2),
this.Size.Height /*+ (textPadding * 2)*/ );
// Clean up the Graphics object.
g.Dispose();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
protected override void OnSubscribeControlEvents(Control c)
{
// Call the base so the base events are connected.
base.OnSubscribeControlEvents(c);
// Cast the control to a ToolStripCheckBox control.
ToolStripImageAndCheckBox checkBoxToolStrip = (ToolStripImageAndCheckBox)c;
// Add the event.
checkBoxToolStrip.LabelClick += new EventHandler(checkBoxToolStrip_LabelClick);
checkBoxToolStrip.CheckedChanged += new EventHandler(checkBoxToolStrip_CheckedChanged);
}
protected override void OnUnsubscribeControlEvents(Control c)
{
// Call the base method so the basic events are unsubscribed.
base.OnUnsubscribeControlEvents(c);
// Cast the control to a ToolStripCheckBox control.
ToolStripImageAndCheckBox checkBoxToolStrip = (ToolStripImageAndCheckBox)c;
// Remove the event.
checkBoxToolStrip.LabelClick -= new EventHandler(checkBoxToolStrip_LabelClick);
}
protected override void OnMouseEnter(EventArgs e)
{
DefaultBackColor = this.BackColor;
base.OnMouseEnter(e);
this.BackColor = Color.Aquamarine;
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
this.BackColor = DefaultBackColor;
}
void checkBoxToolStrip_LabelClick(object sender, EventArgs e)
{
if (Click != null)
Click(this, e);
}
void checkBoxToolStrip_CheckedChanged(object sender, EventArgs e)
{
if (CheckedChange != null)
CheckedChange(this, e);
}
void On_MouseLeave(object sender, EventArgs e)
{
OnMouseLeave(e);
//throw new NotImplementedException();
}
void On_MouseEnter(object sender, EventArgs e)
{
this.Select();
this.Focus();
OnMouseEnter(e);
}