C#-按钮悬停触发OnPaint

时间:2019-03-06 09:47:37

标签: c# visual-studio button graphics onpaint

我正在尝试一个简单的程序,该程序将根据所选的单选按钮绘制一个矩形或一个圆形。按下“绘图”按钮后,绘图将发生变化。但是,我似乎对按钮有问题,当我将鼠标悬停在按钮上时,会触发OnPaint方法,并且最终在窗体上显示两个图形。这是代码:

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 Part_A_Attempt_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Pen myPen = new Pen(Color.Blue, 5);
            Graphics formGraphics = this.CreateGraphics();

            if (RectangleRadio.Checked)
                formGraphics.DrawRectangle(myPen, new Rectangle(10, 10, 300, 200));

            if (CircleRadio.Checked)
                formGraphics.DrawEllipse(myPen, 10, 10, 300, 300);

            myPen.Dispose();
            formGraphics.Dispose();
        }

        private void DrawButton_Click(object sender, EventArgs e)
        {

            this.Invalidate();
        }

1 个答案:

答案 0 :(得分:0)

只需将按钮FlatStyle更改为Flat

DrawButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

这将在悬停期间停止UI无效

相关问题