-1

我的应用程序有几个窗口。一旦鼠标在我的所有窗口之外指定的时间(比如半秒),我想执行某个操作。

对于单个窗口,我会在 MouseLeave 事件中启动一个计时器,并在 MouseEnter 事件中删除该时间,但是我将如何为多个窗口实现这个?

4

1 回答 1

0

所有 Windows 和页面都可以访问 App。只需启动并取消具有内置延迟的 BackGroundWorker。如果工人完成了,那么做你的事。我用两页测试了这个。

在所有页面上注册一个 MouseEnter 和 MouseLeave

    private void MainWindowsMouseLeave(object sender, MouseEventArgs e)
    {
        // MessageBox.Show("MouseLeave");
        tbMouseEnterLeave.Text = "MouseLeave";
        if (App.BackgroundWorkerApp.IsBusy) App.BackgroundWorkerApp.CancelAsync();
        else
        {
            Thread.Sleep(10);
            if (App.BackgroundWorkerApp.IsBusy)App.BackgroundWorkerApp.CancelAsync();
        }
        if (!App.BackgroundWorkerApp.IsBusy) App.BackgroundWorkerApp.RunWorkerAsync();
    }

    private void MainWindowsMouseEnter(object sender, MouseEventArgs e)
    {
        tbMouseEnterLeave.Text = "MouseEnter";
        App.BackgroundWorkerApp.CancelAsync();
    }   


public partial class App : Application
{
    private static System.ComponentModel.BackgroundWorker backgroundWorkerApp = new BackgroundWorker();

    public App()
    {
        backgroundWorkerApp.WorkerSupportsCancellation = true;
        backgroundWorkerApp.DoWork +=
            new DoWorkEventHandler(backgroundWorkerApp_DoWork);
        backgroundWorkerApp.RunWorkerCompleted +=
            new RunWorkerCompletedEventHandler(
        backgroundWorkerApp_RunWorkerCompleted);
    }

    public static System.ComponentModel.BackgroundWorker BackgroundWorkerApp { get { return backgroundWorkerApp; } }

    private void backgroundWorkerApp_DoWork(object sender,
        DoWorkEventArgs e)
    {
        // Get the BackgroundWorker that raised this event.
        BackgroundWorker worker = sender as BackgroundWorker;
        e.Result = ComputeApp(worker, e);
    }

    // This event handler deals with the results of the
    // background operation.
    private void backgroundWorkerApp_RunWorkerCompleted(
        object sender, RunWorkerCompletedEventArgs e)
    {
        // First, handle the case where an exception was thrown.
        if (e.Error != null)
        {
            MessageBox.Show(e.Error.Message);
        }
        else if (e.Cancelled)
        {
            // Next, handle the case where the user canceled 
            // the operation.
            // Note that due to a race condition in 
            // the DoWork event handler, the Cancelled
            // flag may not have been set, even though
            // CancelAsync was called.
            // MessageBox.Show("Cancel");
        }
        else
        {
            // Finally, handle the case where the operation 
            // succeeded.
            // this where you do that thing you want to do
            MessageBox.Show("Complete");            
        }
    }

    string ComputeApp(BackgroundWorker worker, DoWorkEventArgs e)
    {

        // Abort the operation if the user has canceled.
        // Note that a call to CancelAsync may have set 
        // CancellationPending to true just after the
        // last invocation of this method exits, so this 
        // code will not have the opportunity to set the 
        // DoWorkEventArgs.Cancel flag to true. This means
        // that RunWorkerCompletedEventArgs.Cancelled will
        // not be set to true in your RunWorkerCompleted
        // event handler. This is a race condition.

        if (worker.CancellationPending)
        {
            e.Cancel = true;
            return "cancelled";
        }
        for (int i=0; i < 10; i++)
        {
            Thread.Sleep(100);
            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return "cancelled";
            }
        }
        return "complete";
    }

} 
于 2012-04-11T21:09:15.640 回答