C# 震动检测鼠标

C# 震动检测鼠标,c#,winforms,C#,Winforms,我的代码用于form1 private int X,Y; private void timer1_Tick(object sender, EventArgs e) { if (this.X != Cursor.Position.X || this.Y != Cursor.Position.Y) { this.Form1_Load(this, e); }

我的代码用于form1

        private int X,Y;

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (this.X != Cursor.Position.X ||
            this.Y != Cursor.Position.Y)
        {
            this.Form1_Load(this, e);
        }
        else
        {
            this.Text = (Convert.ToInt16(this.Text)+1).ToString();
        }

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Text = "0";
        this.X = Cursor.Position.X;
        this.Y = Cursor.Position.Y;
    }
其他表格不适用


如果form2已打开。上面的代码不起作用。

我不确定您到底想实现什么,但是如果您从from1或form2显示form2,请尝试设置form2.Owner=this。Owner=form1。

此屏幕保护程序代码:

//信息1

        [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
    private static extern IntPtr GetDesktopWindow();

    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

    //...

    private const int SC_SCREENSAVE = 0xF140;
    private const int WM_SYSCOMMAND = 0x0112;

    //...

    public static void SetScreenSaverRunning()
    {
        SendMessage

    (GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
    }


    public void Shakedetectionmouse(EventArgs e)
    {
        //CallTimer1_Tick
       timer1_Tick(this,e);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        //ShowScreenSaver
        timer1.Enabled = true;
        timer1.Interval = 1000;
        SetScreenSaverRunning();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        //ShowForm2
        using (var Form2 = new Form2())
        {
            Form2.ShowDialog();
        }
    }
//Inform2

       private void Form2_Load(object sender, EventArgs e)
    {
        timer1.Enabled = true;
        timer1.Interval = 2000;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
       using(var form1 = new Form1())
        {
            form1.Shakedetectionmouse(EventArgs.Empty);
        }
    }
应以任何形式使用定时器组件

这不是解决这个问题的办法吗


可以使用屏幕保护程序窗口吗?

这到底应该做什么?顺便说一下,不要从事件处理程序调用Form_Load-只有WinForms框架应该调用它。@Michael:为什么usercode不应该调用自写函数?如果你想调用与你想在再次调用FormLoad事件中调用的代码相同的代码,只需这样做,但这不会妨碍其他人进行更多的重构。@Oliver:这只是一种不好的做法,将来会导致误解。Form_Load的功能已经很好地理解了——当加载表单时,框架会与其他处理程序一起顺序调用它。弄乱它就是自找麻烦。用这些细节编辑你的问题;除非你在回答问题,否则不要创建答案。