-1

EDIT: I said drawing line and the code tries to draw rectangle I know. I can change that when I can drive the rectangle. That's not the problem.

I can't draw a line. I checked out documents but I just can't do it.

https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-create-graphics-objects-for-drawing

https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-draw-a-line-on-a-windows-form

I want to declare a class that inherited from Form, and I declared a constructor there which will draw the rectangle. Then in main, I am calling it, but it doesn't work.

namespace game
{
    public class Screen : Form
    {
        public Screen()
        {  
            Graphics g = CreateGraphics();
            var rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
            g.DrawRectangle(System.Drawing.Pens.Black, rectangle);
        }
    }
    class Program
    {
        static void Main()
        {    
            Application.Run(new Screen());
        }

    }
}

edit2: g = this.CreateGraphics(); doesn't work,

in this one,

private void Form1_Paint(object sender,   
   System.Windows.Forms.PaintEventArgs pe)   
{  
   // Declares the Graphics object and sets it to the Graphics object  
   // supplied in the PaintEventArgs.  
   Graphics g = pe.Graphics;  
   // Insert code to paint the form here.  
}  

i don't see how i can give the arguments to call it

Zeki Kral
  • 35
  • 2
  • 10
  • _Graphics g = CreateGraphics();_ This will not draw a persistent graphics. Always code the `Paint` event and use `e.Graphics`. MSDN is quite misleading as it presents the three ways without speaking about the differences. – TaW Apr 01 '19 at 07:57
  • i understand somethings is wrong. i checked the documents too but i can't connect them. TaW – Zeki Kral Apr 01 '19 at 07:58
  • Simply use the 1st code example in the 1st link and forget about the 2nd one. CreateGraphics can be used in mouse events to create say, a rubberband line or rectangle. The 2nd link should work, although it will leak the pen and ought to use Pens.Black anyway.. – TaW Apr 01 '19 at 07:59
  • it doesn't work. i can't do it man. the document uses a part of the code. i don't know what to give to painteventargs arguument. or i can't call it in main. can't you just give me it full please – Zeki Kral Apr 01 '19 at 08:01
  • You __never call Paint__!! It gets called by the system when necessary or you __trigger__ it by calling `Invalidate();` when the data have changed. - Have you created the Paint event? (Go to the events pane of the properties window and doublclick Paint!) – TaW Apr 01 '19 at 08:03
  • listen, i need graphics object and drawing method right? there is graphics object and there is drawing method. its in the construction in Screen. so when i make a screen, it triggers the drawing part itself(because its in construction). but it doesn't – Zeki Kral Apr 01 '19 at 08:08
  • Just do as I told you. The graphics object needs to be valid and yours is not. Also: Graphics must be created procedurally and can't be preset in a constructor. (Except for option 3: You could create and draw upon a Bitmap which you set the Form's BackgroundImage.) Usually not what you want.. – TaW Apr 01 '19 at 08:09
  • i edited the topic – Zeki Kral Apr 01 '19 at 08:13
  • i understand what you're saying but i don't know how to do it – Zeki Kral Apr 01 '19 at 08:15
  • _Go to the events pane of the properties window and doublclick Paint!_ Did you do it?? – TaW Apr 01 '19 at 08:16

1 Answers1

1

The correct way to do this is:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        var rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
        e.Graphics.DrawRectangle(System.Drawing.Pens.Black, rectangle);

        base.OnPaint(e);
    }
}

The windows painting model is that the system detects when the screen needs to be refreshed and calls you to draw it

The reason is that when Windows was designed computers didn't have the memory required for Windows to store the image for every Window so the image only existed in the display buffer - and that image only contains what's actually on screen (and not areas covered by other windows for example) - so every time a window moves and new area is revealed the application has to redraw this area

If you need to trigger a redraw you can call Invalidate

Nir
  • 29,306
  • 10
  • 67
  • 103