为什么我在运行代码时看不到任何图形?

时间:2019-11-28 02:58:31

标签: visual-studio

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp5
{
   public partial class Form1 : Form
   {
        public Form1()
        {
            InitializeComponent();
        }
        Pen red = new Pen(Color.Red);
        Pen green = new Pen(Color.Green);

        System.Drawing.SolidBrush fillRed = new System.Drawing.SolidBrush(Color.Red);
        System.Drawing.SolidBrush fillYellow = new System.Drawing.SolidBrush(Color.Yellow);

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
          e.Graphics.DrawRectangle(red, 0, 0, 220, 90);
          e.Graphics.DrawEllipse(green, 0, 0, 220, 90);
        }
      }
}

2 个答案:

答案 0 :(得分:1)

似乎您上载的代码不包含将Form1_Paint方法添加到this.Paint事件或直接调用该方法的行。我认为Form1_Paint可能在一开始就没有执行,导致图形在我执行时被正确加载

public Form1()
{
    InitializeComponent();
    this.Paint += Form1_Paint;
    this.Invalidate(); /// this fires Paint event
}

在我的环境中。

答案 1 :(得分:0)

您需要确认是否已订阅事件“ Paint”。

有关更多信息,请参阅文档How to: Subscribe to and Unsubscribe from Events

相关问题