简体   繁体   中英

Cannot draw on bottom or right side of a fullscreen form

I have the code below that is supposed to draw lines from the top to the bottom of a fullscreen form.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Graphics g;
        public Form1()
        {
            InitializeComponent();
            g = CreateGraphics();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Pen pen = new Pen(Color.Black);
            for (int i = 0; i < this.ClientSize.Height; i++)
            {
                g.DrawLine(pen, 100, i, 50, i);
            }
        }
    }
}

The form in question (Form1) is maximized, borderless and topmost. Result of the code is, form is displayed, lines are drawn one after another but when the number i of the loop reaches 1055 DrawLine starts not working and until the end of the loop from then on notghing else is drawn, therefore there is a blank space at the bottom of the form.

some extra information

My desktop resolution is 1920 x 1080

this.Size = 1920 x 1080

this.ClientSize = 1920 x 1080

Anyone is welcomed to create an empty project, set form properties (maximized, topmost, borderless) and copy-paste this code to reproduce the problem I have.

Here's fixed code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
//         Graphics g;
        public Form1()
        {
            InitializeComponent();
//            g = CreateGraphics();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Pen pen = new Pen(Color.Black);
            for (int i = 0; i < this.ClientSize.Height; i++)
            {
   //             g.DrawLine(pen, 100, i, 50, i);
                e.Graphics.DrawLine(pen, 100, i, 50, i);
            }
        }
    }
}

The graphics object is meant to be used right away and then disposed. The main reason is, there is a real windows Device Context (HDC) that is used by the graphics object. This is a limited resource, and if you tie up too many in the program windows will cut the program off. If you have too many programs using too many, you'll actually crash windows' UI, or at least parts of it, although each subsequent version of windows adds protections to prevent this. Disposing the HDC explicitly and immediately with .Dispose or the using construct, when you're done drawing, is required; by the time the GC gets around to it, you could have used them all up.

The framework will create the Graphics and Dispose of it for you in the paint event. So when you're drawing in paint (recommended), you don't have to dispose the graphics.

Any time you use creategraphics on a form, or Graphics.FromImage or similar, you should use it and dispose of it ASAP.

Because of this design and the requirements that go with it, the HDC makes assumptions about the window at the moment it is grabbed. In the interem, the window could maximize or resize itself before drawing on it occurs again. The paint event also grabs another one from the same window handle - an unusual usage of HDC - so it is probably one of these issues which is causing the strange behaviour.

Try using the e.Graphics from your Paint event instead of the CreateGraphics() method.

Example:

private void Form1_Paint(object sender, PaintEventArgs e) {

  // using your CreateGraphics:
  Pen pen = new Pen(Color.Black);
  for (int i = 0; i < this.ClientSize.Height; i++) {
    g.DrawLine(pen, 100, i, 50, i);
  }

  // using e.Graphics:
  for (int i = 0; i < this.ClientSize.Height; i++) {
    e.Graphics.DrawLine(Pens.Black, 200, i, 250, i);
  }

}

Difference:

在此输入图像描述

as far as I'm aware Windows Forms graphics coordinates are y=0 (topmost edge) to Height-1 (bottommost edge) in the Y-direction and x=0 (leftmost edge) to Width-1 (rightmost edge) in the X direction.

if this is so the first example will not work, as you are drawing solely outside the viewable area, however the second example will as you are drawing a vertical line that only draws one pixel outside of the viewable area.

Try changing to 1079 and see if it renders. Failing that, try incrementing y in the first example and find out where it stops. Do you have some Form Chrome to take into account? For instance a windows form Form with height = 1080 might use 20 pixels on the title bar and bottom edge, so the actual viewable area may be smaller.

Edit 1

Edited following your code addition to the question. I modified the code as follows and it draws a perfect rectangle on my system. Just out of interest, why are you creating a graphics object in the constructor of the form? You should use the one provided by the paint event args. The Graphics object will be created and disposed of by the .NET Runtime every time the form is repainted.

Also I added a resize event handler to repaint the form. Without this when you resize it doesnt set the rectangle to the new form size.

Can you test this and tell me if you get the same problem? Also what other properties am I missing (ie: Form.Borderstyle etc) to get the same issue as you?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Paint += new PaintEventHandler(Form1_Paint);
        this.Resize += new EventHandler(Form1_Resize);
    }

    void Form1_Resize(object sender, EventArgs e)
    {
        this.Invalidate(true);
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        using (Pen pen = new Pen(Color.Red, 2.0f))
        {
            e.Graphics.DrawRectangle(pen, new Rectangle(0, 0, ClientSize.Width-1, ClientSize.Height-1));
        }
    }
}

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