本网站上的其他解决方案对我没有用,所以我想弄清楚我做错了什么:
这是WinForm App
private void dk_buff_box_Paint(object sender, PaintEventArgs e)
{
Console.WriteLine("INSIDE!!");
ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);
GroupBox box = sender as GroupBox;
DrawGroupBox(box, e.Graphics, Color.Red, Color.Black);
}
private void DrawGroupBox(GroupBox box, Graphics g, Color textColor, Color borderColor)
{
if (box != null)
{
Brush textBrush = new SolidBrush(textColor);
Brush borderBrush = new SolidBrush(borderColor);
Pen borderPen = new Pen(borderBrush);
SizeF strSize = g.MeasureString(box.Text, box.Font);
Rectangle rect = new Rectangle(box.ClientRectangle.X,
box.ClientRectangle.Y + (int)(strSize.Height / 2),
box.ClientRectangle.Width - 1,
box.ClientRectangle.Height - (int)(strSize.Height / 2) - 1);
// Clear text and border
g.Clear(this.BackColor);
// Draw text
g.DrawString(box.Text, box.Font, textBrush, box.Padding.Left, 0);
// Drawing Border
//Left
g.DrawLine(borderPen, rect.Location, new Point(rect.X, rect.Y + rect.Height));
//Right
g.DrawLine(borderPen, new Point(rect.X + rect.Width, rect.Y), new Point(rect.X + rect.Width, rect.Y + rect.Height));
//Bottom
g.DrawLine(borderPen, new Point(rect.X, rect.Y + rect.Height), new Point(rect.X + rect.Width, rect.Y + rect.Height));
//Top1
g.DrawLine(borderPen, new Point(rect.X, rect.Y), new Point(rect.X + box.Padding.Left, rect.Y));
//Top2
g.DrawLine(borderPen, new Point(rect.X + box.Padding.Left + (int)(strSize.Width), rect.Y), new Point(rect.X + rect.Width, rect.Y));
}
}
此代码永远不会触发“INSIDE !!”到我的控制台输出和组合框周围的边框(称为dk_buff_box)总是灰色且非常轻。 (我假设这是默认的?)
我需要做些什么才能让这个边框改变颜色? 我有几个一起工作的.cs文件(控件)主.cs文件是form1.cs。上面的代码位于一个名为darkknightinfo.cs
的独立.cs文件中代码应该在主窗体上吗?还是应该在.cs文件上有实际的组合框?
我需要做些什么才能让_Paint()正确激活并运行代码来更改组框边框颜色?
答案 0 :(得分:0)
如果您正在制作winForm应用程序,则必须以编程方式打开控制台,而不能仅使用Console.WriteLine
打开它。您还使用了PaintEventArgs
,用于(顾名思义)Paint,bitmap等。