我想創建Custom ToolStripItem
,其中有checkbox
和Icon。 我通過使用兩個picturebox
和標籤創建自定義控件來做到這一點。 左picturebox
如果對於圖標而右是作爲特殊checkbox
將位置添加到收藏夾。帶有圖標和複選框的ToolStripControlHost。 Hightlight如何選擇項目?在圖標字段添加圖標?
我將該控件添加到ToolStripControlHost
。
- 問題是左右兩側的空白我不想將它們移除或將圖標移動到左側欄並刪除右側空間。我試圖設置
Padding
和Margin
值爲0,但它不起作用。 我發現類似的問題,但沒有正確的答案。是否有可能這樣做?
- 我想使上的MouseEnter全線亮點就像在正常ToolStripMenuItem元素。當我重寫OnMouseEnter和OnMouseLeave更改背景屬性時,只需更改託管控件的顏色,而不是Antirie ToolStripControlHost。 是否可以模擬與ToolStripMenuItem相同的行爲?
現在它看起來像圖片「A」,我希望它看起來更像是畫面B,但與星形複選框:
基本上我想我CustomToolStrip項目相似儘可能到ToolStripMenuItem。隨着點擊獨立事件(如果你點擊文本)和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);
}
你的問題不是很清楚。如果你想要一些完整的代碼,你應該用一些屏幕截圖描述你想要的東西(就像一些草圖就夠了)。如果你需要修正,你應該發佈你的代碼,顯示對說明性屏幕截圖不滿意的內容。 –
請參閱[ToolStripMenuItem無法顯示覆選標記和圖像(圖標)時,RenderMode是「系統」?](http://stackoverflow.com/a/710610/719186) – LarsTech
@LarsTech - 感謝您的鏈接,但沒有幫助我因爲我不知道如何添加新的Render方法到'ToolStripControlHost' @KingKing - 感謝你的提示,我會盡量讓它更清楚。 – GryzLi