简体   繁体   中英

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. 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.

My Paint event handler code is below.

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. 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);
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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