发布消息什么都不做

时间:2012-11-17 05:19:23

标签: c# timer postmessage

我正在使用代码(如下所示)向流程发送左键单击。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    namespace Cross_Click
    {
public partial class Form1 : Form
{
    [DllImport("User32.Dll", EntryPoint = "PostMessageA")]
    private static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

    const int WM_LBUTTONDOWN = 0x201;
    const int WM_LBUTTONUP = 0x202;

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    IntPtr hWnd = FindWindow(null, "iw5mp.exe");

    private Form form2instance;
    Boolean cross = false;
    Boolean click = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        cross = !cross; //Nice little toggle
        if (cross)
        {
            button1.Text = "Disable Crosshair";
            this.form2instance = new Form2();
            this.form2instance.Show();
        }

        else
        {
            button1.Text = "Enable Crosshair";

            this.form2instance.Close();
        }

    }

    private void button2_Click(object sender, EventArgs e)
    {
        click = !click; //Nice little toggle
        if (click)
        {


            button2.Text = "Disable Clicker";

        }
        else
        {
            button2.Text = "Enable Clicker";

        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {


            var hWnd = FindWindow("wordpad", null);
            PostMessage(hWnd, WM_LBUTTONDOWN, 0, 0);
            PostMessage(hWnd, WM_LBUTTONUP, 0, 0);

    }
}

然而,当我使用它时,没有任何反应。定时器启用,间隔为1毫秒。为什么会这样?

0 个答案:

没有答案