如何在用户选择控件时使 OnPaint 运行 具有不同的 ControlPaint.DrawBorderStyle 功能
How to make OnPaint run different ControlPaint.DrawBorderStyle functions, when user selects the control
我有一个具有以下 onPaint 方法的用户控件(Groupbox):
protected override void OnPaint(PaintEventArgs e) {
Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor, ButtonBorderStyle.Solid);
Rectangle textRect = e.ClipRectangle;
textRect.X += 6;
textRect.Width = tSize.Width;
textRect.Height = tSize.Height;
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
}
当用户select使用这个控件时,我想把边框改得更深一些,所以我尝试了以下逻辑:
protected override void OnPaint(PaintEventArgs e) {
Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
if (ContainsFocus) {
ControlPaint.DrawBorder(e.Graphics, borderRect,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid);
} else {
ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor, ButtonBorderStyle.Solid);
}
Rectangle textRect = e.ClipRectangle;
textRect.X += 6;
textRect.Width = tSize.Width;
textRect.Height = tSize.Height;
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
}
即使我 select 我的用户控件,我的控件似乎也在不断地进入 else。在这方面的任何帮助将不胜感激。
为了解决这个问题,我使用了 onMouseClick 事件,结合声明一个构造函数,该构造函数将初始 borderColor 设置为不同的值.像这样
private Color borderColor;
private int entered;
public TUserControlGroupBox() {
this.borderColor = SystemColors.ControlDark;
}
public Color BorderColor {
get { return this.borderColor; }
set { this.borderColor = value; }
}
public BorderState Border {
set {
switch (value) {
case BorderState.Unselected:
this.BorderColor = Color.LightGray;
break;
case BorderState.Selected:
this.BorderColor = Color.Black;
break;
case BorderState.SelectedFirst:
this.BorderColor = SystemColors.ControlDark;
break;
}
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e) {
Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
if ((this.BorderColor == Color.Black || this.borderColor == SystemColors.ControlDark) && this.entered == 1) {
ControlPaint.DrawBorder(e.Graphics, borderRect,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid);
} else {
ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor, ButtonBorderStyle.Solid);
}
Rectangle textRect = e.ClipRectangle;
textRect.X += 6;
textRect.Width = tSize.Width;
textRect.Height = tSize.Height;
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
}
protected override void OnMouseClick(MouseEventArgs e) {
this.entered = 1;
this.Invalidate();
base.OnMouseClick(e);
}
希望这个答案对那些在未来寻找类似解决方案的人有所帮助。
我有一个具有以下 onPaint 方法的用户控件(Groupbox):
protected override void OnPaint(PaintEventArgs e) {
Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor, ButtonBorderStyle.Solid);
Rectangle textRect = e.ClipRectangle;
textRect.X += 6;
textRect.Width = tSize.Width;
textRect.Height = tSize.Height;
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
}
当用户select使用这个控件时,我想把边框改得更深一些,所以我尝试了以下逻辑:
protected override void OnPaint(PaintEventArgs e) {
Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
if (ContainsFocus) {
ControlPaint.DrawBorder(e.Graphics, borderRect,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid);
} else {
ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor, ButtonBorderStyle.Solid);
}
Rectangle textRect = e.ClipRectangle;
textRect.X += 6;
textRect.Width = tSize.Width;
textRect.Height = tSize.Height;
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
}
即使我 select 我的用户控件,我的控件似乎也在不断地进入 else。在这方面的任何帮助将不胜感激。
为了解决这个问题,我使用了 onMouseClick 事件,结合声明一个构造函数,该构造函数将初始 borderColor 设置为不同的值.像这样
private Color borderColor;
private int entered;
public TUserControlGroupBox() {
this.borderColor = SystemColors.ControlDark;
}
public Color BorderColor {
get { return this.borderColor; }
set { this.borderColor = value; }
}
public BorderState Border {
set {
switch (value) {
case BorderState.Unselected:
this.BorderColor = Color.LightGray;
break;
case BorderState.Selected:
this.BorderColor = Color.Black;
break;
case BorderState.SelectedFirst:
this.BorderColor = SystemColors.ControlDark;
break;
}
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e) {
Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
if ((this.BorderColor == Color.Black || this.borderColor == SystemColors.ControlDark) && this.entered == 1) {
ControlPaint.DrawBorder(e.Graphics, borderRect,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid);
} else {
ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor, ButtonBorderStyle.Solid);
}
Rectangle textRect = e.ClipRectangle;
textRect.X += 6;
textRect.Width = tSize.Width;
textRect.Height = tSize.Height;
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
}
protected override void OnMouseClick(MouseEventArgs e) {
this.entered = 1;
this.Invalidate();
base.OnMouseClick(e);
}
希望这个答案对那些在未来寻找类似解决方案的人有所帮助。