Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Winforms无效不';不要在油漆上点火_C#_Winforms_Onpaint_Invalidation - Fatal编程技术网

C# Winforms无效不';不要在油漆上点火

C# Winforms无效不';不要在油漆上点火,c#,winforms,onpaint,invalidation,C#,Winforms,Onpaint,Invalidation,我正在尝试使用问题中给出的代码平滑地移动表单 但是由于某些原因,我的this.Invalidate()调用永远不会触发OnPaint事件。表单上是否需要一些配置才能实现此功能 编辑: 涉及线程,因为它在具有自己的messageloop的backgroundworker中运行。代码如下: public class PopupWorker { public event PopupRelocateEventHandler RelocateEvent; private Backgrou

我正在尝试使用问题中给出的代码平滑地移动表单

但是由于某些原因,我的this.Invalidate()调用永远不会触发OnPaint事件。表单上是否需要一些配置才能实现此功能

编辑:

涉及线程,因为它在具有自己的messageloop的backgroundworker中运行。代码如下:

public class PopupWorker
{
    public event PopupRelocateEventHandler RelocateEvent;

    private BackgroundWorker worker;
    private MyPopup popupForm;

    public PopupWorker()
    {
        worker = new BackgroundWorker();
        worker.DoWork += worker_DoWork;
    }

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        popupForm = PopupCreator.CreatePopup("Title", "BodyText");
        this.RelocateEvent += popupForm.OnRelocate;
        popupForm.CustomShow();
        Application.Run();
    }

    public void Show()
    {
        worker.RunWorkerAsync();
    }

    public void PopupRelocate(object sender, Point newLocation)
    {
        if (popupForm.InvokeRequired)
            popupForm.Invoke(new PopupRelocateEventHandler(PopupRelocate), new object[] {sender, newLocation});
        else
            RelocateEvent(this, newLocation);
    }
}
表格:

public void OnRelocate(object sender, Point newLocation)
{
    targetLocation = newLocation;
    this.Invalidate();
}

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    if (Location.Y != targetLocation.Y)
    {
        Location = new Point(Location.X, Location.Y + 10);
        if (Location.Y > targetLocation.Y)
            Location = targetLocation;
        this.Invalidate();
    }
}

链接问题中的代码使用Application.DoEvents,这是让OnPaint发生的关键部分。
没有它,您可以使用Form.Refresh()而不是Invalidate

有关更多详细信息,请参阅

编辑: 您的代码确实显示了一些问题,但它并不完整。让我们从基础开始,为了进行表单移动,您只需启用计时器,如下所示:

private void timer1_Tick(object sender, EventArgs e)
{            
    this.Location = new Point(this.Location.X + 2, this.Location.Y + 1);
}

我们可以看看你的一些代码吗?你是在线程中调用它吗?刷新通常是个坏主意,因为它会强制绘制。这意味着他的UI动画可能会影响CPU。@弗兰克:唯一的区别是强制vs请求。都重新粉刷,弗兰克,警长想移动并重新粉刷。定时器是我的第一个策略,它确实起了作用。但是运动不是很平稳,我不想创建一个每1毫秒滴答响一次的计时器。