Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Winform中在面板上绘图时出现问题_C#_.net_Winforms - Fatal编程技术网

C# 在Winform中在面板上绘图时出现问题

C# 在Winform中在面板上绘图时出现问题,c#,.net,winforms,C#,.net,Winforms,在表单加载的事件处理程序中,我有以下代码 Panel pHText = new Panel(); Font myFont = new Font("Arial", 14); pHText.Location=new Point(10,10); pHText.Size=new Size(200,200); pHText.BackColor = Color.White; Graphics g = pHText.CreateGraphics(); g

在表单加载的事件处理程序中,我有以下代码

    Panel pHText = new Panel();
    Font myFont = new Font("Arial", 14);
    pHText.Location=new Point(10,10);
    pHText.Size=new Size(200,200);
    pHText.BackColor = Color.White;
    Graphics g = pHText.CreateGraphics();
    g.DrawLine(new Pen(Color.Black), 0, 0, 10, 10);
    g.DrawString("text", myFont, Brushes.Blue, 10, 10);
    Controls.Add(pHText);

白色面板显示在表单中,但线条和字符串的绘制没有显示。

FormLoad是在图形上绘制的错误位置。 尝试使用OnPaint方法重载和e.Graphics

protected override void OnPaint(PaintEventArgs e)
{
   // If there is an image and it has a location, 
   // paint it when the Form is repainted.
   base.OnPaint(e);
   if(this.picture != null && this.pictureLocation != Point.Empty)
   {
      e.Graphics.DrawImage(this.picture, this.pictureLocation);
   }
}

FormLoad是在图形上绘制的错误位置。 尝试使用OnPaint方法重载和e.Graphics

protected override void OnPaint(PaintEventArgs e)
{
   // If there is an image and it has a location, 
   // paint it when the Form is repainted.
   base.OnPaint(e);
   if(this.picture != null && this.pictureLocation != Point.Empty)
   {
      e.Graphics.DrawImage(this.picture, this.pictureLocation);
   }
}

您需要在事件面板上绘制。您无法看到您的图形,因为在组件刷新后,它们将被重新绘制-但您不会看到。

您需要在事件面板上绘制。您无法看到您的图形,因为在组件刷新后,它们会被重新绘制,但您不会看到。

此代码将出现在表单加载事件中

Panel pHText = new Panel();
pHText.Name = "ctrId"; //specify control name, to access it in other parts of your code
pHText.Location = new Point(10, 10);
pHText.Size = new Size(200, 200);
pHText.BackColor = Color.White;
pHText.Paint += paintingUrCtr;//adding onpaint event
Controls.Add(pHText)
添加名为
paintingUrCtr
的绘制事件

private void paintingUrCtr(object sender, PaintEventArgs e)
{
        Font myFont = new Font("Arial", 14);
        e.Graphics.DrawLine(new Pen(Color.Black),  0, 0, 10, 10);
        e.Graphics.DrawString("text", myFont, Brushes.Blue, 10, 10);
}

此代码将出现在表单加载事件中

Panel pHText = new Panel();
pHText.Name = "ctrId"; //specify control name, to access it in other parts of your code
pHText.Location = new Point(10, 10);
pHText.Size = new Size(200, 200);
pHText.BackColor = Color.White;
pHText.Paint += paintingUrCtr;//adding onpaint event
Controls.Add(pHText)
添加名为
paintingUrCtr
的绘制事件

private void paintingUrCtr(object sender, PaintEventArgs e)
{
        Font myFont = new Font("Arial", 14);
        e.Graphics.DrawLine(new Pen(Color.Black),  0, 0, 10, 10);
        e.Graphics.DrawString("text", myFont, Brushes.Blue, 10, 10);
}

这不起作用,代码只在表单重新绘制时运行,而不是在面板上运行。控件覆盖的窗体窗口区域被剪裁。这不起作用,代码只在窗体重新绘制时运行,而不是在面板上运行。控件覆盖的窗体窗口区域被剪裁。不要使用CreateGraphics,请使用e.Graphics。乌尔是伊拉克的一座古城。这座城市运行得非常好。除了我使用了
图形g=e.图形,与Hans提到的相同。@Hans Passant编辑。。。至于你(把它当作一个笑话,然而……)不要使用CreateGraphics,使用e.Graphics。乌尔是伊拉克的一座古城。这座城市运行得非常好。除了我使用了
图形g=e.图形,与Hans提到的相同。@Hans Passant编辑。。。至于你(不过,把它当作一个玩笑……)