无法在代码创建的表单上绘制

时间:2014-06-18 16:00:31

标签: c#

我遇到了一个新的奇怪的问题,我想在一个名为IM的代码中创建的表单中绘制一个简单的eclipse。绘制它的代码在这里:

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);


    }

1 个答案:

答案 0 :(得分:0)

您必须首先显示表单 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);