gpt4 book ai didi

c# - 带有图标和复选框的 ToolStripControlHost。如何突出显示所选项目?在图标字段中添加图标?

转载 作者:行者123 更新时间:2023-11-30 17:51:25 31 4
gpt4 key购买 nike

我想创建 Custom ToolStripItem,其中包含 checkbox 和 Icon。我设法通过使用两个 picturebox 和标签创建自定义控件来做到这一点。左边的 picturebox 如果是图标,右边的是作为特殊的 checkbox 将位置添加到收藏夹。

我将该控件添加到 ToolStripControlHost

  1. 问题是左右两边的空白,我不想删除它们或将图标移到左侧栏并删除右侧的空白。我尝试将 PaddingMargin 值设置为 0,但它不起作用。我发现有类似问题的帖子,但没有正确答案。有可能吗??

enter image description here

  1. 我想像 ToolStripMenuItem 中的普通元素一样在 MouseEnter 上突出显示整行。当我覆盖 OnMouseEnter 和 OnMouseLeave 更改背景属性时,它只是更改托管控件的颜色,而不是 antirie ToolStripControlHost。是否可以模拟与 ToolStripMenuItem 相同的行为?

现在它看起来像图片“A”,我希望它看起来更像图片 B,但带有星形复选框:

enter image description here

基本上我想让我的 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);
}

最佳答案

我设法通过从 ToolStripMenuItem 驱动 MyControl 并覆盖 onPaint 方法来做我需要的事情

public class MyControl : ToolStripMenuItem
{
public MyControl(){}
public MyControl(string text, Image image ):base (text,image){}
public MyControl(string text):base(text){}
public MyControl(Image image):base(image){}
public MyControl(string text,Image image,EventHandler onClick):base(text,image,onClick){}
public MyControl(string text, Image image,int id):base(text,image){ this.ID = id;}
public MyControl(string text,Image image,int id,EventHandler onClick):base(text,image,onClick){this.ID = id; }

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

if (base.Checked == false)
{
Rectangle rect = new Rectangle(this.Width - 20, 1, 20, 20);
e.Graphics.DrawImage(Properties.Resources.BlackStar, rect);
}
else
{
Rectangle rect = new Rectangle(this.Width - 20, 1, 20, 20);
e.Graphics.DrawImage(Properties.Resources.YellowStar, rect);
}
}
public int ID { get; set; }
public bool StarClicked { get; set; }

protected override void OnMouseDown(MouseEventArgs e)
{
StarClicked = e.X > (this.Width - 20);
if (StarClicked)
{
this.Checked = this.Checked == true ? false : true;
}
else
base.OnClick(e);
base.OnMouseDown(e);
}

现在是这样

http://i.stack.imgur.com/ZwESK.png

表单代码:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AddMenuElements(7);
}
public void AddMenuElements(int licznik)
{
ToolStripMenuItem wszystkie = new ToolStripMenuItem("wszystkie", SystemIcons.Question.ToBitmap());
manu1ToolStripMenuItem.DropDownItems.Add(wszystkie);
menuStrip1.Renderer = new ToolStripSystemRenderer();
for (int i = 0; i < licznik; i++)
{
ToolStripMenuItem element = new ToolStripMenuItem(String.Format("element {0}", i), GetIcon(i),element2_Click);
element.Visible = i % 2 == 0 ? false:true;
manu1ToolStripMenuItem.DropDownItems.Add(element);
}


for (int i = 0; i < licznik; i++)
{
MyControl element2 = new MyControl(String.Format("element {0}",i), GetIcon(i),element2_Click);
element2.ID = i;
element2.CheckedChanged += (s, e) =>
{
MyControl control = s as MyControl;
manu1ToolStripMenuItem.DropDownItems[control.ID + 1].Visible = control.Checked;
};
element2.Checked = i % 2 == 0 ? false : true;

wszystkie.DropDownItems.Add(element2);

}
}
void element2_Click(object sender, EventArgs e)
{
MyControl kontener = (sender as MyControl);

if (kontener.ForeColor == Color.Red)
kontener.ForeColor = Color.Black;
else
kontener.ForeColor = Color.Red;

}
private static Image GetIcon(int i)
{
Image ikona = null;
switch (i)
{
case 0: ikona = SystemIcons.Application.ToBitmap();
break;
case 1: ikona = SystemIcons.Asterisk.ToBitmap();
break;
case 2: ikona = SystemIcons.WinLogo.ToBitmap();
break;
case 3: ikona = SystemIcons.Exclamation.ToBitmap();
break;
case 4: ikona = SystemIcons.Hand.ToBitmap();
break;
case 5: ikona = SystemIcons.Warning.ToBitmap();
break;
default: ikona = SystemIcons.Shield.ToBitmap();
break;
}
return ikona;
}
}

基本上它完成了我想要的,我只需要在点击星号时阻止它消失。感谢@LarsTech 的帮助,它给了我如何做到这一点的提示。

关于c# - 带有图标和复选框的 ToolStripControlHost。如何突出显示所选项目?在图标字段中添加图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19520899/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com