我需要您帮助解决以下问题(使用 .Net 3.5 和 Windows 窗体):
我只是想在位于窗体上的组合框(Windows 窗体)的中间画一条线。
我使用的代码是:
void comboBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen(Brushes.DarkBlue),
this.comboBox1.Location.X,
this.comboBox1.Location.Y + (this.comboBox1.Size.Height / 2),
this.comboBox1.Location.X + this.comboBox1.Size.Width,
this.comboBox1.Location.Y + (this.comboBox1.Size.Height / 2));
}
要触发绘制事件:
private void button1_Click(object sender, EventArgs e)
{
comboBox1.Refresh();
}
当我执行代码并按下按钮时,不会绘制线条。在调试中,绘画处理程序处的断点没有被命中。奇怪的是MSDN上ComBox 的事件列表中有一个绘制事件,但在 VS 2010 IntelliSense 中没有在 ComboBox 的成员中找到此类事件
谢谢。
最佳答案
public Form1()
{
InitializeComponent();
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);
}
void comboBox1_DrawItem(object sender, DrawItemEventArgs e) {
e.DrawBackground();
e.Graphics.DrawLine(Pens.Black, new Point(e.Bounds.Left, e.Bounds.Bottom-1),
new Point(e.Bounds.Right, e.Bounds.Bottom-1));
TextRenderer.DrawText(e.Graphics, comboBox1.Items[e.Index].ToString(),
comboBox1.Font, e.Bounds, comboBox1.ForeColor, TextFormatFlags.Left);
e.DrawFocusRectangle();
}
关于c# - 绘制的线不显示在组合框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11862252/