【发布时间】:2022-02-01 03:15:03
【问题描述】:
我正在尝试在自定义控件中的 Combobox 内绘制图像,我使用以下代码:
public partial class Jo_ComboBox : ComboBox
{
#region Constructor
public Jo_ComboBox()
{
InitializeComponent();
}
#endregion
#region Fields
bool _IsRequired = false;
bool _IsEmpty = true;
Bitmap _xImg;
#endregion
#region Properties
[Category("Joul_Properties")]
public Bitmap xImg { get { return _xImg; } }
[Category("Joul_Properties")]
public bool IsEmpty { get { return _IsEmpty; } }
[Category("Joul_Properties")]
public bool IsRequired { get { return _IsRequired; } set { _IsRequired = value; } }
#endregion
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
if (IsRequired == true && this.Text == string.Empty)
{
_xImg = Resources._16Exc;
e.Graphics.DrawImage(xImg, new Point(this.Width - 18, 30));
_IsEmpty = true;
}
else
{
_IsEmpty = false;
}
}
#region Events
//OnLeave
protected override void OnLeave(EventArgs e)
{
base.OnLeave(e);
this.Invalidate();
}
#endregion
实际上,我得到了一个很好的结果,但我注意到图像不在 Combobox 的文本框上方,我注意到当我尝试更改组合框的高度时。
请观看视频以了解问题所在:
编辑: 如果我更改高度,请查看是否有编辑器区域覆盖了图像
【问题讨论】:
-
@dr.null 谢谢,我已经看到了该解决方案,但我无法应用它,实际上,我正在使用自定义控件,所以我尝试了但我不能,我不想要带有我只需要在文本行上的项目的图像
-
啊,我明白了。如果您需要将图像垂直居中:
e.Graphics.DrawImage(_xImg, Width - _xImg.Width - 3, (Height - _xImg.Height) / 2); -
@dr.null 抱歉,但还是同样的问题,请看帖子链接中的视频
-
@dr.null 帖子最后有一个链接,其中包含解释我的问题的视频
标签: c# combobox custom-controls