🏆 作者简介,愚公搬代码 🏆《头衔》:华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,CSDN博客专家,阿里云专家博主,腾讯云优秀博主,掘金优秀博主,51CTO博客专家等。 🏆《近期荣誉》:2022年CSDN博客之星TOP2,2022年华为云十佳博主等。 🏆《博客内容》:.NET、Java、Python、Go、Node、前端、IOS、Android、鸿蒙、Linux、物联网、网络安全、大数据、人工智能、U3D游戏、小程序等相关领域知识。 🏆🎉欢迎 👍点赞✍评论⭐收藏
图形图像的重绘是指在对图形或图像进行修改后,需要重新绘制该图形或图像以反映修改后的结果。重绘可以在屏幕上直接进行,也可以在内存中进行,最后再将修改后的图形或图像显示在屏幕上。
在计算机图形学中,图形图像的重绘通常是使用图形库或绘图软件完成的。这些工具提供了各种绘制工具和绘制函数,能够快速、准确地绘制出修改后的图形或图像。在重绘时,需要注意如下几个方面:
下面是一个简单的WinForms应用程序,用于在窗体中绘制和重绘一个圆形:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace GdiPlusDemo
{
public partial class Form1 : Form
{
private int radius = 50;
private Point center;
public Form1()
{
InitializeComponent();
center = new Point(ClientSize.Width / 2, ClientSize.Height / 2);
DoubleBuffered = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics graphics = e.Graphics;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.Clear(Color.White);
graphics.DrawEllipse(Pens.Red,
center.X - radius, center.Y - radius, radius * 2, radius * 2);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
center = new Point(ClientSize.Width / 2, ClientSize.Height / 2);
Invalidate();
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
radius += 10;
Invalidate();
}
else if (e.Button == MouseButtons.Right)
{
radius -= 10;
Invalidate();
}
}
}
}
在该程序中,通过重写OnPaint方法来绘制圆形,并在OnResize方法中重绘。OnMouseDown方法用于捕获鼠标事件,当左键或右键被按下时,修改圆形的半径并重绘。
在重绘图形时,调用Invalidate方法来请求重新绘制窗体。在OnPaint方法中,使用Graphics对象进行绘制,可以使用GDI+提供的各种绘图函数和属性来实现不同的效果。要对图形进行平滑处理,可以将SmoothingMode属性设置为AntiAlias。
注意,在窗体的构造函数中,需要将DoubleBuffered属性设置为true,以启用双缓冲技术,避免图形的闪烁现象。
在WindForm中,SetStyle
、Invalidate
、Update
和 Refresh
是用于处理界面控件的重要方法。让我为你详细解释每个方法的作用和用法:
SetStyle
方法:SetStyle
方法用于设置控件的样式和行为属性。ControlStyles.ResizeRedraw
样式来启用在调整控件大小时重绘控件。使用掩码来指定要设置的样式,例如:SetStyle(ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer, true);
Invalidate
方法:Invalidate
方法用于标记控件的指定区域为无效,需要重新绘制。Invalidate(new Rectangle(0, 0, Width, Height));
Update
方法:Update
方法用于立即使控件重绘,而不是等待消息队列处理。Invalidate
方法一起使用,以立即触发控件的重绘。这在需要即时响应用户操作时非常有用。例如:Invalidate(); Update();
Refresh
方法:Refresh
方法用于立即使控件重绘,并在内部调用 Invalidate
和 Update
方法。Update
类似,但更方便,因为它会自动调用 Invalidate
和 Update
。例如:Refresh();
public partial class lblFont : Form
{
int index = 0;
public lblFont()
{
InitializeComponent();
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = e.Graphics;
switch (index)
{
case 0:
Rectangle rectangle = new Rectangle(0, 0, 400, 400);
graphics.FillRectangle(Brushes.Black, rectangle);
break;
case 1:
rectangle = new Rectangle(0, 0, 200, 200);
graphics.FillRectangle(Brushes.Black, rectangle);
break;
case 2:
rectangle = new Rectangle(0, 0, 100, 100);
graphics.FillRectangle(Brushes.Black, rectangle);
break;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
index = comboBox1.SelectedIndex;
this.Invalidate();
this.Update();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。