protected override void OnPaint(PaintEventArgs e)在何时运行?

haodd123 2010-01-17 11:40:50
protected override void OnPaint(PaintEventArgs e)在何时运行?


我下载了一些源码,看到有人在窗体代码里有:
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
..........
}
这样的写法,来在窗体上边画一些图案。
查到Form里有:
protected override void OnPaint(PaintEventArgs e);
//
//
// 参数:
// e:
// 包含事件数据的 System.EventArgs。
[EditorBrowsable(EditorBrowsableState.Advanced)]

开始搞不清她是方法,还是事件?(即如果是方法,不知道她是何时被调用的?)

后来回忆起,老师讲C#语言规范时,拿事件event举例:写事件的发行者时触发事件要 提供一个受保护的虚拟方法,来触发事件,即按老师说的上边Form里
应该是这样的:
protected override void OnPaint(PaintEventArgs e);
{
PaintEventHandler handler = paint;
//声明一个临时的委托,防止可能的线程同步
if (handler != null)
{
handler(this, e);
}
}

public delegate void PaintEventHandler (object sender,PubEventArgs e);//声明所需代理
public event PaintEventHandler paint;//事件的声明


他是重写了这个受保护的虚拟方法吗?他是在哪被触发而运行的呢?
好像有点乱,呵呵
...全文
701 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dobzhansky 2010-01-18
  • 打赏
  • 举报
回复
windows 是基于消息(Message)的, 绘制消息是 WM_PAINT

winform 是基于事件的, 绘制事件是 Paint,

消息到事件的转换是在控件类的 WndProc 函数中完成的.
ouc_ajax 2010-01-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dobzhansky 的回复:]
System.Windows.Forms.Control 的 OnPaint 实现
C# code
[EditorBrowsable(EditorBrowsableState.Advanced)]protectedvirtualvoid OnPaint(PaintEventArgs e)
{
PaintEventHandler handler= (PaintEventHandler)base.Events[EventPaint];if (handler!=null)
{
handler(this, e);
}
}
[/Quote]

OnPaint用于在窗体区域失效时重新绘制。一个简单的例子就是窗口拖动到屏幕外会使部分区域失效。
如果窗口再被拖回到屏幕,会发现窗口中该有的东西并未丢失。这就是OnPaint在大显身手了。
实际上,在被拖到屏幕以外的过程中,OnPaint仍在起作用,只是有些部分被屏幕裁剪了。

OnPaint会在任何窗体区域需要重绘的时候运行! 例如最大化、拖动

xray2005 2010-01-18
  • 打赏
  • 举报
回复
OnPaint会在任何窗体区域需要重绘的时候运行
Dobzhansky 2010-01-17
  • 打赏
  • 举报
回复

System.Windows.Forms.Control 的 OnPaint 实现

[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void OnPaint(PaintEventArgs e)
{
PaintEventHandler handler = (PaintEventHandler) base.Events[EventPaint];
if (handler != null)
{
handler(this, e);
}
}
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace DoubleBufferDraw { public partial class DrawLine : Form { class LineObj { private Point m_start; private Point m_end; public LineObj(Point start, Point end) { this.m_start = start; this.m_end = end; } public void Draw(Graphics g, Pen pen) { g.DrawLine(pen, m_start, m_end); } } private Point m_startPoint = Point.Empty; List lineList = new List(); public DrawLine() { InitializeComponent(); } private void drawLine(Graphics graphics, Point startPoint, Point endPoint) { BufferedGraphicsContext context = BufferedGraphicsManager.Current; BufferedGraphics bg = context.Allocate(graphics, this.ClientRectangle); bg.Graphics.Clear(this.BackColor); foreach (LineObj line in this.lineList) { line.Draw(bg.Graphics, SystemPens.ControlText); } bg.Graphics.DrawLine(SystemPens.ControlText, startPoint, endPoint); bg.Render(); bg.Dispose(); bg = null; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); foreach (LineObj line in this.lineList) { line.Draw(e.Graphics, SystemPens.ControlText); } } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); this.m_startPoint = new Point(e.X, e.Y); } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (e.Button == MouseButtons.Left) { this.drawLine(this.CreateGraphics(), this.m_startPoint, new Point(e.X, e.Y)); } } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); LineObj line = new LineObj(this.m_startPoint, e.Location); this.lineList.Add(line); } } }
using System; using System.Drawing; using System.Windows.Forms; namespace ClipImage { public partial class FormImage : Form { #region private Point position; private Rectangle clip; #endregion public FormImage() { #region InitializeComponent(); this.TopMost = true; // 前端显示。 this.ShowInTaskbar = false; // 在 Windows 任务栏中隐藏窗体。 this.DoubleBuffered = true; // 双缓冲绘制图形。 this.FormBorderStyle = FormBorderStyle.None; // 窗体无边框。 this.Bounds = Screen.GetBounds(this); // 获取显示器的桌面区域。 this.TransparencyKey = this.BackColor; // 窗体背景透明化。 NotifyIcon notify = new NotifyIcon(); notify.Visible = true; // 图标在任务栏的通知区域中可见。 notify.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath); // 启动程序图标。 notify.Text = AppDomain.CurrentDomain.FriendlyName; // 启动程序名称。 notify.MouseClick += new MouseEventHandler(notify_MouseClick); #endregion } #region OnMouseDown protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); switch (e.Button) { case MouseButtons.Left: position = e.Location; // 设置起始位置。 break; case MouseButtons.Right: if (clip.Width > 1 && clip.Height > 1) { clip.Offset(1, 1); // 平移。 using (Bitmap bmp = new Bitmap(--clip.Width, --clip.Height)) using (Graphics g = Graphics.FromImage(bmp)) { g.CopyFromScreen(clip.Location, Point.Empty, clip.Size); // 截图。 bmp.Save("Image.png", bmp.RawFormat); // 保存图片。 Clipboard.SetImage(bmp); // 图片存储到剪贴板中。 } System.Diagnostics.Process.Start("mspaint.exe", "Image.png"); // 用画图打开图片。 } clip = Rectangle.Empty; BackgroundImage.Dispose(); BackgroundImage = null; break; } } #endregion #region OnMouseMove protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (e.Button == MouseButtons.Left) { clip.X = Math.Min(position.X, e.X); clip.Y = Math.Min(position.Y, e.Y); clip.Width = Math.Abs(position.X - e.X); clip.Height = Math.Abs(position.Y - e.Y); this.Refresh(); // 立即重绘图形。 } } #endregion #region OnPaint protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.DrawRectangle(Pens.Red, clip); e.Dispose(); } #endregion #region NotifyIcon private void notify_MouseClick(object sender, MouseEventArgs e) { switch (e.Button) { case MouseButtons.Left: this.BackgroundImage = new Bitmap(this.Width, this.Height); using (Graphics g = Graphics.FromImage(this.BackgroundImage)) { g.CopyFromScreen(Point.Empty, Point.Empty, this.Size); } this.Activate(); // 激活窗体并给予它焦点。 break; case MouseButtons.Right: (sender as NotifyIcon).Dispose(); Application.Exit(); break; } } #endregion } }

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧