积极答复者
WPF的重复绘制问题

问题
答案
-
在WPF里面是用DrawingContext去完成“画”图形的, 你可以参考:
http://msdn.microsoft.com/en-us/library/system.windows.media.drawingcontext.aspx
我给你写了一个sample:
public class MyVisualHost : FrameworkElement
{
private VisualCollection _children;
public MyVisualHost()
{
_children = new VisualCollection(this);
_children.Add(CreateDrawingVisualRectangle());
}
protected override int VisualChildrenCount
{
get { return _children.Count; }
}
protected override Visual GetVisualChild(int index)
{
if (index < 0 || index >= _children.Count)
{
throw new ArgumentOutOfRangeException();
}
return _children[index];
}
private DrawingVisual CreateDrawingVisualRectangle()
{
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
int position = 0;
for (int i = 0; i < 100; i++)
{
Rect rect = new Rect(new System.Windows.Point(position, position), new System.Windows.Size(10, 10));
drawingContext.DrawRectangle(System.Windows.Media.Brushes.Red, (System.Windows.Media.Pen)null, rect);
position += 10;
}
drawingContext.Close();
return drawingVisual;
}
}
然后你就可以只创建一个对象,添加到Canvas就可以了,比如:
MyVisualHost test = new MyVisualHost();
canvas.Children.Add(test);
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 黑心 2011年6月16日 1:05
全部回复
-
在WPF里面是用DrawingContext去完成“画”图形的, 你可以参考:
http://msdn.microsoft.com/en-us/library/system.windows.media.drawingcontext.aspx
我给你写了一个sample:
public class MyVisualHost : FrameworkElement
{
private VisualCollection _children;
public MyVisualHost()
{
_children = new VisualCollection(this);
_children.Add(CreateDrawingVisualRectangle());
}
protected override int VisualChildrenCount
{
get { return _children.Count; }
}
protected override Visual GetVisualChild(int index)
{
if (index < 0 || index >= _children.Count)
{
throw new ArgumentOutOfRangeException();
}
return _children[index];
}
private DrawingVisual CreateDrawingVisualRectangle()
{
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
int position = 0;
for (int i = 0; i < 100; i++)
{
Rect rect = new Rect(new System.Windows.Point(position, position), new System.Windows.Size(10, 10));
drawingContext.DrawRectangle(System.Windows.Media.Brushes.Red, (System.Windows.Media.Pen)null, rect);
position += 10;
}
drawingContext.Close();
return drawingVisual;
}
}
然后你就可以只创建一个对象,添加到Canvas就可以了,比如:
MyVisualHost test = new MyVisualHost();
canvas.Children.Add(test);
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 黑心 2011年6月16日 1:05