简体   繁体   English

C#:在非主监视器上使用DrawRectangle绘制的矩形不会呈现上下边框

[英]C#: Rectangles drawn with DrawRectangle on a non-primary monitor doesn't render top and left borders

I have a full screen form and in the handler for the Paint event I am drawing a 2px border around the entire form. 我有一个全屏的窗体,并且在Paint事件的处理程序中,我在整个窗体周围绘制了2px的边框。 I create one of these forms for each screen attached to the computer. 我为连接到计算机的每个屏幕创建这些表格之一。 For some reason, the top and left borders are not drawing on any non-primary monitors. 由于某些原因,在任何非主要显示器上都没有画出上下边框。 The form's background covers the entire screen but I can't see to draw (using GDI) on area about 3px down from the top and 3px in from the left of the screen. 表单的背景覆盖了整个屏幕,但我看不到在屏幕顶部大约3px处和屏幕左侧3px处绘制(使用GDI)。

My Paint event handler code is below. 我的Paint事件处理程序代码如下。

private void OnPaint(object sender, PaintEventArgs e)
    {
        using (Graphics g = this.CreateGraphics())
        {
            int border = 2;
            int startPos = 0;
            // offset used to correctly paint all the way to the right and bottom edges
            int offset = 1;
            Rectangle rect = new Rectangle(startPos, startPos, this.Width - border + offset, this.Height - border + offset);
            Pen pen = new Pen(Color.Red, border);

            // draw a border 
            g.DrawRectangle(pen, rect);
        }
    }

Has anyone seen this before? 谁看过这个吗?

Your code works Correct. 您的代码工作正确。 You should know when you are using this.Width or this.Height , these values calculating with the frame that surround your form. 您应该知道何时使用this.Widththis.Height ,这些值是使用围绕窗体的框架计算的。 For the Height, the height of your form controls added to calculated height. 对于“高度”,将窗体控件的高度添加到计算出的高度中。 You can using this code : 您可以使用以下代码:

using (Graphics g = this.CreateGraphics())
            {
                int border = 2;
                int startPos = 0;
                // offset used to correctly paint all the way to the right and bottom edges
                int offset = 1;
                Rectangle rect = new Rectangle(startPos, startPos, this.Width-20, this.Height-40);
                Pen pen = new Pen(Color.Red, border);

                // draw a border 
                g.DrawRectangle(pen, rect);
            }

UPDATE : 更新:

If you want to calculating exact size you can use this code : 如果要计算确切的大小,可以使用以下代码:

 int width,height;
    public Form1()
    {
        InitializeComponent();
        PictureBox pc = new PictureBox();
        pc.Dock = DockStyle.Fill;
        this.Controls.Add(pc);
        pc.Visible = false;
        width = pc.Width;
        height = pc.Height;
        pc.Dispose();
    }
    private void Form1_Paint(object sender, PaintEventArgs e)
    {


        using (Graphics g = this.CreateGraphics())
        {
            int border = 2;
            int startPos = 0;
            // offset used to correctly paint all the way to the right and bottom edges
            int offset = 1;
            Rectangle rect = new Rectangle(startPos, startPos, width,height);
            Pen pen = new Pen(Color.Red, border);

            // draw a border 
            g.DrawRectangle(pen, rect);
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM