PDA

View Full Version : قرار گرفتن متن در سمت راست group box



ali_72
دوشنبه 24 فروردین 1394, 13: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 قرار دادم ولی باید برابر چه چیزی قرار بدم که عدد ثابت نباشه؟

ali_72
سه شنبه 25 فروردین 1394, 07:58 صبح
آیا روش بهتری برای تغییر رنگ border گروپ باکس می شناسید؟

h.gheidrlou
یک شنبه 07 تیر 1394, 12:14 عصر
StringFormat sf = new StringFormat(StringFormatFlags.DirectionRightToLef t); //روش راست به چپ نوشتن متن چاپ

Font font = new System.Drawing.Font("Tahoma", 12, FontStyle.Regular);

strDisplay = richtxtbx_print.Text;
e.Graphics.DrawString(strDisplay, font, Brushes.Black, 750, 50, sf);

azitaj
یک شنبه 07 تیر 1394, 12:25 عصر
برای اینکه header text بره سمت راست باید توی قسمت properties->header Alingment ->far کنید