2

我有一个全屏表单,在 Paint 事件的处理程序中,我在整个表单周围绘制了一个 2px 的边框。我为连接到计算机的每个屏幕创建其中一个表单。出于某种原因,顶部和左侧边框未在任何非主显示器上绘制。表单的背景覆盖了整个屏幕,但我看不到在屏幕顶部向下约 3px 和屏幕左侧约 3px 的区域上绘制(使用 GDI)。

我的 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);
        }
    }

有没有人见过这个?

4

1 回答 1

1

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

    }
于 2013-09-23T19:35:09.213 回答