C# winform中ListBox各项之间没有分割线?(VS2013)

C# winform中ListBox各项之间没有分割线?(VS2013),第1张

listBox1.DrawMode = DrawMode.OwnerDrawFixed

首先将DrawMode为DrawMode.OwnerDrawFixed 或 DrawMode.OwnerDrawVariable 时,才触发该事件(DrawItem事件)

//在构造函数中写上触发的事件:

this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem)

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)

{

Brush myBrush = Brushes.Black

if ((e.State &DrawItemState.Selected) == DrawItemState.Selected)

{

myBrush = new SolidBrush(Color.FromArgb(0, 0, 255))//选中时背景颜色

}

else//没有选中时的背景颜色

{

myBrush = new SolidBrush(Color.White)

}

e.Graphics.FillRectangle(myBrush, e.Bounds)//填满矩形背景颜色

e.Graphics.DrawRectangle(new Pen(new SolidBrush(e.ForeColor)), e.Bounds)

e.DrawFocusRectangle()//焦点框

StringFormat stringformat = StringFormat.GenericDefault

stringformat.LineAlignment = StringAlignment.Center

e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds, stringformat)

}

在该窗口上现在只是创建了一个窗口(可能是一个LISTBOX),如果需要做分割窗口,那么在这个窗口上需要创建两个窗口,并且需要你自己写关于拉动等的代码,这个很容易写的。就可以实现你的了。

在这里并不建议使用MFC 的分割窗口,只是经验谈了。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11828746.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存