c# - C# - 按钮悬停触发 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();
}
解决方案
只需将按钮更改FlatStyle
为 Flat
DrawButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
这会在悬停期间停止 UI 失效
推荐阅读
- ios - 您如何处理具有非 Apple 帐户系统的应用程序中的自动续订订阅?
- laravel - 包含句点/点 (.) 字符的 Laravel 路由
- python - 如何在 sklearn 中禁用 GLM(PoissonRegressor)的正则化?
- shell - 使终端光标闪烁
- html - 如何调整按钮css之间的间距
- php - 在管理员的 WooCommerce 电子邮件通知中显示自定义字段
- html - 如何使用 display: flex 使页眉占据网页的整个宽度;和 flex-direction: 正文中的列?
- oauth-2.0 - 如何使用 OAuth 2.0 使网页上空闲的用户超时,并要求重新输入凭据?
- azure-ad-b2c - Azure B2C 自定义策略令牌缺少信息
- javascript - 使用 Node.js 将 Raspberry Pi 连接到 Windows PC | 制作远程 GRBL 控件