繁体   English   中英

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

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

我有一个全屏的窗体,并且在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);
        }
    }

谁看过这个吗?

您的代码工作正确。 您应该知道何时使用this.Widththis.Height ,这些值是使用围绕窗体的框架计算的。 对于“高度”,将窗体控件的高度添加到计算出的高度中。 您可以使用以下代码:

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

更新:

如果要计算确切的大小,可以使用以下代码:

 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