我将Windows窗体控件添加到WPF窗口,并在其图形上绘图。一切正常,直到我调整窗口大小为止。调整大小后,绘制的图形消失。我在那里做错了吗?
public MainWindow()
{
InitializeComponent();
host.Child = img;
this.Content = host;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
generatePoints();
AddNewPointOnGraphics((int)points[index].X, (int)points[index].Y, 0);
}
private void rendered()
{
if (index % 2 == 0)
_firstPointInLine = true;
if (index < 10000)
AddNewPointOnGraphics((int)points[index].X, (int)points[index].Y, 0);
index++;
}
public void AddNewPointOnGraphics(int pX, int pY, uint time)
{
if (_firstPointInline)
{
firstpoint = img.PointToClient(new System.Drawing.Point(pX, pY));
_firstPointInLine = false;
}
secondpoint = img.PointToClient(new System.Drawing.Point(pX, pY));
g = img.CreateGraphics();
g.DrawLine(new System.Drawing.Pen(System.Drawing.Brushes.Blue, 2), (float)firstpoint.X, (float)firstpoint.Y, (float)secondpoint.X, (float)secondpoint.Y);
firstpoint = img.PointToClient(new System.Drawing.Point(pX, pY));
Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(delegate
{
rendered();
}));
}
public class myImage : System.Windows.Forms.Control
{
public myImage()
{
this.SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint,
true);
}
}