157 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuexiaodong2009 2011-05-11
  • 打赏
  • 举报
回复
一般的界面自画,你用OnPaint事件
Refresh,Update可以刷新一部分页面
sddsf34 2011-05-11
  • 打赏
  • 举报
回复
看过,MSDN我觉得很迷糊,
bdmh 2011-05-11
  • 打赏
  • 举报
回复
msdn不是写的很清楚了吗,你想在哪里用,一般的界面自画,你用OnPaint事件,Invalidate 触发即可
niaoked 2011-05-11
  • 打赏
  • 举报
回复
我们先来看看上面几个方法在Control.cs中的源代码定义:
1,Paint事件
 [SRCategory(SR.CatAppearance), SRDescription(SR.ControlOnPaintDescr)]
public event PaintEventHandler Paint {
add {
Events.AddHandler(EventPaint, value);
}
remove {
Events.RemoveHandler(EventPaint, value);
}
}


从上面的代码可以看出,控件只是把外部的事件处理委托保存在了事件列表中

2,接下来我们看OnPaint方法 :


[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void OnPaint(PaintEventArgs e) {
PaintEventHandler handler = (PaintEventHandler)Events[EventPaint];
if (handler != null) handler(this, e);
}

控件在OnPaint方法中肯定会触发Paint事件

3,接着看Invalidate方法

[ResourceExposure(ResourceScope.None)]
public void Invalidate(bool invalidateChildren) {
if (IsHandleCreated) {
if (invalidateChildren) {
SafeNativeMethods.RedrawWindow(new HandleRef(window, Handle),
null, NativeMethods.NullHandleRef,
NativeMethods.RDW_INVALIDATE |
NativeMethods.RDW_ERASE |
NativeMethods.RDW_ALLCHILDREN);
}
else {
// It's safe to invoke InvalidateRect from a separate thread.
using (new MultithreadSafeCallScope())
{
SafeNativeMethods.InvalidateRect(new HandleRef(window, Handle),
null,
(controlStyle & ControlStyles.Opaque) != ControlStyles.Opaque);
}
}

NotifyInvalidate(this.ClientRectangle);
}
}


Invalidate()方法默认调用的Invalidate(false);
从代码可以看出,控件在此方法主要通知系统对控件的所在区域进行绘制。
4,Update()方法:

public void Update() {
SafeNativeMethods.UpdateWindow(new HandleRef(window, InternalHandle));
}

Update()方法强制控件重新绘制无效区域(需要重绘的区域)
5,Refresh()方法:

public virtual void Refresh() {
Invalidate(true);
Update();
}

注意对照看代码,Refresh()方法会对控件及其子控制进行强制重绘。然后调用Update()重新绘制无效区域。
sddsf34 2011-05-11
  • 打赏
  • 举报
回复
还能说得更详细点的吗

110,534

社区成员

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

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

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