ali_72
دوشنبه 24 فروردین 1394, 14:40 عصر
سلام
از کد زیر برای همرنگ شدن border گروپ باکس با ForeColor آن استفاده کردم
ولی text گروپ باکس سمت چپ اومده
با کدنویسی خاصیت RightToLeft را برابر yes قرار دادم ولی فایده ای نداشت
groupBox1.Paint += PaintBorderlessGroupBox;
private void PaintBorderlessGroupBox(object sender, PaintEventArgs p) {
GroupBox box = (GroupBox)sender;
DrawGroupBox(box, p.Graphics);
}
private void DrawGroupBox(GroupBox box, Graphics g)
{
if (box != null)
{
Brush textBrush = new SolidBrush(box.ForeColor);
Brush borderBrush = new SolidBrush(box.ForeColor);
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,0, 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));
box.RightToLeft = RightToLeft;
}
}
میدونم به خاطر اینه که تو خط g.DrawString(box.Text, box.Font, textBrush,0, 0);
x را برابر 0 قرار دادم ولی باید برابر چه چیزی قرار بدم که عدد ثابت نباشه؟
از کد زیر برای همرنگ شدن border گروپ باکس با ForeColor آن استفاده کردم
ولی text گروپ باکس سمت چپ اومده
با کدنویسی خاصیت RightToLeft را برابر yes قرار دادم ولی فایده ای نداشت
groupBox1.Paint += PaintBorderlessGroupBox;
private void PaintBorderlessGroupBox(object sender, PaintEventArgs p) {
GroupBox box = (GroupBox)sender;
DrawGroupBox(box, p.Graphics);
}
private void DrawGroupBox(GroupBox box, Graphics g)
{
if (box != null)
{
Brush textBrush = new SolidBrush(box.ForeColor);
Brush borderBrush = new SolidBrush(box.ForeColor);
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,0, 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));
box.RightToLeft = RightToLeft;
}
}
میدونم به خاطر اینه که تو خط g.DrawString(box.Text, box.Font, textBrush,0, 0);
x را برابر 0 قرار دادم ولی باید برابر چه چیزی قرار بدم که عدد ثابت نباشه؟