目录
FillRectangle(Brush, Int32, Int32, Int32, Int32)
FillEllipse(Brush, Int32, Int32, Int32, Int32)
实现效果
ControlStyles 枚举
字段
AllPaintingInWmPaint 8192
如果为 true,则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁。 仅当将 UserPaint 位设置为 true 时,才应用此样式。
CacheText 16384
如果为 true,则控件将保留文本的副本,而不是每次必需时从 Handle 中获取。 此样式默认为 false。 此行为可提高性能,但很难保持文本同步。
ContainerControl 1
如果为 true,则控件是类容器控件。
DoubleBuffer 65536
如果为 true,则在缓冲区中进行绘制,并且完成后将结果输出到屏幕。 双缓冲可以防止因重绘控件而引起的闪烁。 如果将 DoubleBuffer 设置为 true,则还应将 UserPaint 和 AllPaintingInWmPaint 设置为 true。
EnableNotifyMessage 32768
如果为 true,则将对发送到控件的 WndProc(Message) 的每个消息调用 OnNotifyMessage(Message) 方法。 此样式默认为 false。 EnableNotifyMessage 在部分信任中不起作用。
FixedHeight 64
如果为 true,则控件在自动缩放时具有固定高度。 例如,如果布局操作尝试重新缩放控件以适应新的 Font,则控件的 Height 保持不变。
FixedWidth 32
如果为 true,则控件在自动缩放时具有固定宽度。 例如,如果布局操作尝试重新缩放控件以适应新的 Font,则控件的 Width 保持不变。
Opaque 4
如果为 true,则控件会绘制为不透明,且不绘制背景。
OptimizedDoubleBuffer 131072
如果为 true,则控件将首先绘制到缓冲区而不是直接绘制到屏幕,这可以减少闪烁。 如果将此属性设置为 true,则还应将 AllPaintingInWmPaint 设置为 true。
ResizeRedraw 16
如果为 true,则控件会在调整大小时进行重绘。
Selectable 512
如果为 true,则控件可以接收焦点。
StandardClick 256
如果为 true,则控件实现标准 Click 行为。
StandardDoubleClick 4096
如果为 true,则控件实现标准 DoubleClick 行为。 如果未将 StandardClick 位设置为 true,则忽略此样式。
SupportsTransparentBackColor 2048
如果为 true,则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度。 仅当将 UserPaint 位设置为 true 且父控件从 Control 派生时,才会模拟透明度。
UserMouse 1024
如果为 true,则将由控件而不是操作系统处理其自身的鼠标事件。
UserPaint 2
如果为 true,则会由控件而不是由操作系统来绘制控件自身。 如果 false,则不会引发 Paint 事件。 此样式仅适用于从 Control 派生的类。
UseTextForAccessibility 262144
指定控件的 Text 属性的值,若设置,则确定控件的默认 Active Accessibility 名称和快捷键。
Graphics 类
FillRectangle(Brush, Int32, Int32, Int32, Int32) | 填充由一对坐标、一个宽度和一个高度指定的矩形的内部。 |
FillEllipse(Brush, Int32, Int32, Int32, Int32) | 填充边框所定义的椭圆的内部,该边框由一对坐标、一个宽度和一个高度指定。 |
实现代码
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Paint += Form1_Paint; //新增 Paint 事件
this.SetStyle(ControlStyles.ResizeRedraw, true); //设置控件在调整大小时进行重绘
//解决闪烁
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
Timer timer = new Timer();
timer.Interval = 20;
timer.Tick += Timer_Tick;
timer.Start(); //开始定时
}
private void Timer_Tick(object sender, EventArgs e)
{
this.Invalidate(); //重绘控件
}
Rectangle rec;
int EllipseWidth = 100;
int EllipseHeight = 100;
double inversion1 = 0;
double inversion2 = 15;
double Yindex = 0;
double Xindex = 0;
int gravity = 2;
private void Form1_Paint(object sender, PaintEventArgs e)
{
rec = this.ClientRectangle; //获取工作区大小
Graphics graphics = e.Graphics; //在窗体上绘画
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置消齿距
graphics.FillRectangle(Brushes.Black, rec); //画矩形
graphics.FillEllipse(Brushes.Red, (int)Xindex, (int)Yindex, EllipseWidth, EllipseHeight); //画圆
inversion1 += gravity; //加速度
Yindex += inversion1; //Y下落速度
Xindex += inversion2; //X横移速度
if (Yindex + EllipseHeight >= rec.Height) //判断Y是否到达工作区边界
{
Yindex = rec.Height - EllipseHeight;
inversion1 *= -0.9; //上升
}
if (Xindex >= rec.Width) //判断X是否到达工作区边界
{
Xindex = 0;
Yindex = 0;
}
}
}