我需要自定义我的组合框,就像这张图片一样。自定义Combobox中的中心文本
我的代码工作fine.But它有2个问题:
1.文本向左移动,当我最小化窗口,如下
这里是我的代码:
//DrawItem
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
if (e.Index >= 0) {
Graphics g = e.Graphics;
Brush brs = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ?
new SolidBrush(SelectedBackColor) : new SolidBrush(e.BackColor);
g.FillRectangle(brs, e.Bounds);
using (StringFormat sformat = new StringFormat()) {
sformat.LineAlignment = StringAlignment.Center;
sformat.Alignment = StringAlignment.Center;
e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds, sformat);
}
//paint
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (index >= 0) {
using (Brush br = new SolidBrush(this.ForeColor)) {
StringFormat sformat = new StringFormat();
sformat.LineAlignment = StringAlignment.Center;
sformat.Alignment = StringAlignment.Center;
e.Graphics.DrawString(this.Text, this.Font, br, this.ClientRectangle, sformat);
e.Graphics.DrawImage(Resource1.arrow,this.ClientRectangle.Right - 34, 0,32,32);
}
}
}
这样有什么不好?
你的代码不能编译;至少缺少两个关闭卷曲。请只发布真实的代码! - e.Bounds和/或ClientRectangle实际上是否留下箭头的空间?除此之外:错误情况听起来很有趣。在最小化之后,我想恢复,对齐/位置是否保持错误? – TaW
对不起,我的错误。我认为问题是e.Bounds。 Combobox大小为135,34,e.Bouds为133.当窗口最小化时,e.Bounds为112. e.Bounds在最小化时调整大小。如何解决它?@TaW – Jandy