簡體   English   中英

在GroupBox C#周圍繪制邊框

[英]Draw a border around GroupBox C#

該站點上的其他解決方案對我而言不起作用,因此我試圖找出我做錯了什么:

這是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 !!” 到我的控制台輸出,groupbox周圍的邊框(稱為dk_buff_box)始終為灰色且非常淺。 (我認為這是默認設置?)

我需要怎么做才能使邊框變色? 我有幾個可以一起使用的.cs文件(控件)。主要的.cs文件是form1.cs。 上面的代碼在一個名為darkknightinfo.cs的單獨的.cs文件中

代碼應該在主表單上嗎? 還是應該在具有實際分組框的.cs文件上?

我需要怎么做才能使_Paint()正確激活並運行代碼以更改組框邊框顏色?

如果要制作winForm應用程序,則必須以編程方式打開控制台,而不能僅通過Console.WriteLine打開它。 您還使用PaintEventArgs ,其用於(顧名思義)Paint,位圖等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM