2008-09-16 21 views
12

C# .NET'de Grup kutusundaki kenarlığın rengini program aracılığıyla değiştirmeye çalışıyorum.Sınırın rengini grup kutusunda nasıl değiştirirsiniz?

Güncelleştirme: .NET'e geçmeden önce bir winforms sistemi üzerinde çalışırken bu soru sorulmuştur.

+0

@Amy Check _user1944617_ yanıtı, kabul edilene göre gerçekten güzel ve üstün bir imho. sadece varsayılan grup kutusu tasarımıyla mükemmel bir şekilde çalıştı ve uyum sağladı. – Drake

cevap

14

Bina, grup kutusunun etiketini içeren daha iyi bir çözüm:

groupBox1.Paint += PaintBorderlessGroupBox; 

private void PaintBorderlessGroupBox(object sender, PaintEventArgs p) 
{ 
    GroupBox box = (GroupBox)sender; 
    p.Graphics.Clear(SystemColors.Control); 
    p.Graphics.DrawString(box.Text, box.Font, Brushes.Black, 0, 0); 
} 

Metnin x/y ayarlamak isteyebilirsiniz, ama benim kullanım için bu sadece doğru olduğunu .

+0

Teşekkürler @Mick Bruno, bana ciddi zaman kazandırdı :) –

+1

Thx! Sınırı kaldırmak için, yaptığım gibi 'box.Parent.BackColor 'öğesini kullanın. – dwo

1

Ben her durumda geçerli olduğu emin değilim, ama bu iş parçacığı sayesinde, hızla programlı kullanarak Boya olay girdiklerini:

GroupBox box = new GroupBox(); 
[...] 
box.Paint += delegate(object o, PaintEventArgs p) 
{ 
    p.Graphics.Clear(someColorHere); 
}; 

Şerefe!

önceki cevabına
5

Yalnızca bir kenarlık çizmek için bu eylemi herhangi bir nesneye (yalnızca düğmelerle değil) boya eylemini ayarla.

private void UserControl1_Paint(object sender, PaintEventArgs e) 
    { 
     ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Red, ButtonBorderStyle.Solid); 

    } 

Yine de orijinal gibi güzel ve yuvarlak olmayacak, ancak çok daha basit.

24

Sadece boya olayı ekleyin.

private void groupBox1_Paint(object sender, PaintEventArgs e) 
    { 
     GroupBox box = sender as GroupBox; 
     DrawGroupBox(box, e.Graphics, Color.Red, Color.Blue); 
    } 


    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)); 
     } 
    } 
+0

Sınırı kalınlaştırmak ve dikdörtgeni yuvarlaklaştırmanın bir yolu var mı? –

İlgili konular