C# Can';t在由代码创建的窗体上绘制

C# Can';t在由代码创建的窗体上绘制,c#,C#,我遇到了一个新的和奇怪的问题,我想画一个简单的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.D

我遇到了一个新的和奇怪的问题,我想画一个简单的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);


    }
private void按钮5_单击(对象发送者,事件参数e)
{
int maxx=0,maxy=0;
int minx=100000000,miny=100000000;
int[]xts=新的int[1000];
int[]yts=新int[1000];
int[]xps=新int[1000];
int[]yps=新int[1000];
int counter=-1;
int counter=-1;
表单IM=新表单();
IM.FormBorderStyle=FormBorderStyle.FixedSingle;
System.IO.StreamReader文件1=
新System.IO.StreamReader(“./MainProgram/Target.txt”);
而(file1.Peek()>=0)
{
counter++;
string line=file1.ReadLine();
string[]words=line.Split();
xts[counter]=转换为32(字[0]);
如果(xts[counter T]>maxx)maxx=xts[counter T];
如果(xts[counter t]maxy)maxy=yts[counter T];
如果(yts[counter t]=0)
{
counter++;
string line=file2.ReadLine();
string[]words=line.Split();
xps[counter]=转换为32(字[0]);
如果(xps[counter]>maxx)maxx=xps[counter];
如果(xps[counter]maxy)maxy=yps[counter];
如果(yps[counter]500)| |(sizex/meghyas>500))
{
meghyas++;
}
IM.Width=(int)(sizex/meghyas);
IM.Height=(int)(sizey/meghyas);
//绘图开始
IM.Show();
System.Drawing.Graphics Graphics Sobj;
graphicsObj=IM.CreateGraphics();
Pen myPen=新笔(System.Drawing.Color.Green,5);
矩形myRectangle=新矩形(0,0,10,10);
graphicsObj.Dropellipse(我的笔,我的矩形);
}

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

你需要在正确的时间画画。不过,看起来你并没有这么做。很难说,因为你删除了所有重要的代码。我们不知道代码的位置和调用时间。你画的是每一个绘画周期吗?还是只画一次,希望表单永远不会重新绘制?这就是为什么使用CreateGraphics几乎总是一个坏主意。即使您成功地在窗体上绘制了某些内容,只要在窗体上拖动另一个窗体或将其最小化,当窗体重新绘制自身时,图形也会被覆盖。所有的绘制都应该在表单的OnPaint覆盖中。代码在一个按钮单击事件中,我只在按钮单击时绘制一次,将尝试在OnPaint事件中进行绘制OnPaint确实帮助了很多!你是在打无效电话吗?正如@DavidHeffernan在他对这个问题的评论中所指出的,绘制代码应该在绘制事件的处理程序中,这样绘制的内容就不会在第一次需要重新绘制时消失。(在这种情况下,
CreateGraphics
将是不必要的。)