最简单的办法,修改Program.cs代码如下:
using System;
using System.Windows.Forms;
namespace RR
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form f = new Form();
Button btn = new Button();
btn.MouseMove += (e, v) => { btn.BackColor = System.Drawing.Color.Green; };
btn.MouseLeave += (e, v) => { btn.BackColor = System.Drawing.Color.White; };
f.Controls.Add(btn);
Application.Run(f);
}
}
}
如果想多次使用,则是一下步骤
首先建议类文件,代码如下:
using System.Windows.Forms;
namespace RR
{
public class MyButton:Button
{
public MyButton()
{
MouseMove += (e, v) => { BackColor = System.Drawing.Color.Green; };
MouseLeave += (e, v) => { BackColor = System.Drawing.Color.White; };
}
}
}
再新建一个窗体,编译一次以后,再工具箱里找到这个叫MyButton的控件拖到窗体上,或者直接在Program.cs里将代码修改如下:
using System;
using System.Windows.Forms;
namespace RR
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form f = new Form();
f.Controls.Add(new MyButton());
Application.Run(f);
}
}
}
run一下,就能看到效果!
温馨提示:答案为网友推荐,仅供参考