不能在代码创建的表单上绘图

本文关键字:表单 绘图 创建 代码 不能 | 更新日期: 2023-09-27 18:09:15

我面临着一个新的和奇怪的问题,我想画一个简单的eclipse在一种被称为IM的代码中创建的形式。绘制它的代码在这里:

System.Drawing.Graphics graphicsObj;
graphicsObj = IM.CreateGraphics();
Pen myPen = new Pen(System.Drawing.Color.Green, 5);
Rectangle myRectangle = new Rectangle(0, 0, 10, 10);
graphicsObj.DrawEllipse(myPen, myRectangle);
IM.Show();

这没有画出任何东西,但是当我把IM.CreateGraphics()变成this.CreateGraphics()时,它在原始形式上画得很好。我错过了什么?

创建表单IM的代码:
  Form IM = new Form();
  IM.FormBorderStyle = FormBorderStyle.FixedSingle;
  IM.Width = 500;
  IM.Height = 500;

完整代码:

    private void button5_Click(object sender, EventArgs e)
    {
        int maxx = 0, maxy = 0;
        int minx = 1000000000, miny = 1000000000;
        int[] xts = new int[1000];
        int[] yts = new int[1000];
        int[] xps = new int[1000];
        int[] yps = new int[1000];
        int countert = -1;
        int counterp = -1;
        Form IM = new Form();
        IM.FormBorderStyle = FormBorderStyle.FixedSingle;
        System.IO.StreamReader file1 =
        new System.IO.StreamReader("./MainProgram/Target.txt");
        while (file1.Peek() >= 0)
        {
            countert++;
            string line = file1.ReadLine();
            string[] words = line.Split();
            xts[countert] = Convert.ToInt32(words[0]);
            if (xts[countert] > maxx) maxx = xts[countert];
            if (xts[countert] < minx) minx = xts[countert];
            yts[countert] = Convert.ToInt32(words[1]);
            if (yts[countert] > maxy) maxy = yts[countert];
            if (yts[countert] < miny) miny = yts[countert];
        }
        System.IO.StreamReader file2 =
        new System.IO.StreamReader("./MainProgram/Pagepos.txt");
        while (file2.Peek() >= 0)
        {
            counterp++;
            string line = file2.ReadLine();
            string[] words = line.Split();
            xps[counterp] = Convert.ToInt32(words[0]);
            if (xps[counterp] > maxx) maxx = xps[counterp];
            if (xps[counterp] < minx) minx = xps[counterp];
            yps[counterp] = Convert.ToInt32(words[1]);
            if (yps[counterp] > maxy) maxy = yps[counterp];
            if (yps[counterp] < miny) miny = yps[counterp];
        }
        int sizex = maxx - minx;
        int sizey = maxy - miny;
        int meghyas=1;
        while ((sizey / meghyas > 500) ||  (sizex / meghyas > 500))
        {
            meghyas++;
        }
        IM.Width = (int) (sizex/meghyas);
        IM.Height = (int) (sizey/meghyas);


        //Start Of drawing
        IM.Show();
        System.Drawing.Graphics graphicsObj;
        graphicsObj = IM.CreateGraphics();
        Pen myPen = new Pen(System.Drawing.Color.Green, 5);
        Rectangle myRectangle = new Rectangle(0, 0, 10, 10);
        graphicsObj.DrawEllipse(myPen, myRectangle);

    }

不能在代码创建的表单上绘图

你必须先显示表单IM:

        Form IM = new Form();
        IM.FormBorderStyle = FormBorderStyle.FixedSingle;
        IM.Width = 500;
        IM.Height = 500;
        IM.Show();
        System.Drawing.Graphics graphicsObj;
        graphicsObj = IM.CreateGraphics();
        Pen myPen = new Pen(System.Drawing.Color.Green, 5);
        Rectangle myRectangle = new Rectangle(0, 0, 10, 10);
        graphicsObj.DrawEllipse(myPen, myRectangle);