简体   繁体   English

在没有视觉工作室设计师的情况下绘制形状。 形状未显示且事件未触发

[英]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.我只是想在不使用 Visual Studio 功能的情况下绘制一些形状,大多数教程都使用它,并且我有理由确定编码因此而不同,所以最后的手段是在这里问。

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.我知道这可能很容易修复,但同样,所有教程都使用 Visual Studio 表单,并且不确定代码是否不同。 Since I have tried straight up copying that code and it gave me errors, I assume I am correct, but not 100% sure.由于我尝试直接复制该代码并且它给了我错误,我认为我是正确的,但不是 100% 确定。 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:以防万一我以外的其他人正在寻找这个,问题是两个主要问题:1,事件没有连接到任何东西,所以我需要添加这样的一行:

[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. 2,主要是评论的重复,我需要在事件中拥有所有绘制函数,因为在应用程序实际运行之前,它们将没有任何可绘制的内容。 Then it's just a matter of invalidating the code inside those events, to force the form to be redrawn.然后只需使这些事件中的代码无效,即可强制重绘表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM