简体   繁体   中英

Drawing shapes without visual studio designer. Shapes not showing and events not firing

I am simply trying to draw some shapes without the use of visual studio functionality, most tutorials use that, and I am reasonbly sure that the coding is different because of that, so last resort was to ask here.

Following some guides on forms, that didn't use any visual studio (and yeah, I know that it is much easier with it, but using such things has screwed me in the past, so wanna learn without) I got the form made and such, but when it came to actually drawing shapes, I hit a wall.

I did a bit of debugging, from what I have been able to determine, the event does not fire at all. And the graphics code in the main function does nothing, just a blank form shows up. I tried a couple of variations on both the in-main code, and the event code, which is visible in the code, I also tried using a variant of the event that used a "sender" argument, no idea if changing that made any difference, none of them fired anyways.

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Workflow.ComponentModel;

class Genetics : Form
{

    public static Form form1;
    static void Main(string[] args)
    {

        form1 = new Form();
        form1.Size = new Size(800, 800);

        Pen blackpen = new Pen(Color.Black, 10);
        Graphics G = form1.CreateGraphics();

        G.DrawRectangle(blackpen, form1.Width - form1.Width / 2, form1.Width - form1.Width / 2, 300, 300);

      form1.CreateGraphics().DrawRectangle(blackpen, form1.Width -    form1.Width / 2, form1.Width - form1.Width / 2, 300, 300);

        Application.Run(form1);


    }

    protected override void OnPaint(PaintEventArgs e)
    {        
        Graphics graphics;
        graphics = form1.CreateGraphics();

        Pen mypen = new Pen(Color.Black, 5);

        graphics.DrawLine(mypen, 20, 20, 200, 210);

        e.Graphics.DrawRectangle(mypen, 20, 20, 200, 210);

    }


}

I don't get any errors or anything, it just... Doesn't do anything, other than shows the form, but that is a bit of a bare minimum.

I know this is probarbly easy to fix, but again, all tutorials used visual studio forms, and unsure if the code was different. Since I have tried straight up copying that code and it gave me errors, I assume I am correct, but not 100% sure. And I am reasonbly experienced, I am just used to writing in unity, the process of going from there to pure code has been difficult for me though.

Just in case someone other than me was looking for this, the problem was two main things: 1, the events weren't connected to anything, so I needed to add a line like this:

[Form variable].[form event] += new PaintEventHandler([Class].[Method with an object sender and an event argument]
//Example of that
form1.Paint += new PaintEventHandler(Genetics.Paint);

Without this, nothing will happen since the methods and events aren't connected.

2, mostly a repeat of the comments, I needed to have all the draw functions in events, since they won't have anything to draw to, before the application is actually run. Then it's just a matter of invalidating the code inside those events, to force the form to be redrawn.

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