(C#) 显示点击位置而不是自动点击

(C#) Showing where to click instead of automatic clicking

我已经建立了一个我发现的鼠标移动,但我不希望程序自动点击这些东西,而是告诉我点击哪里。我该怎么做?

(新手,大约四个月前开始使用 C# 和 C++!)

编辑: 需要澄清的是,该程序旨在单击另一个 window,而不是单击其自身(某种宏,这就是为什么我想要一个选项来显示,以便用户可以看到该程序的用途! )

假设您使用的是 WinForms,下面是一个简单示例。本例中的 "target" 恰好采用相同的形式,但您可以将 hWnd 传递给外部 window,它应该可以正常工作。您需要保留对覆盖层的引用,以便稍后关闭它;这就是第二个按钮的作用。显然,您必须在您的应用程序中以不同的方式执行此操作,但这是一个开始:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    OverlayForm overlay = null;

    private void button1_Click(object sender, EventArgs e)
    {
        if (overlay == null)
        {
            overlay = new OverlayForm(button2.Handle); // <-- pass in an hWnd to some external window
            overlay.Show();
        }  
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (overlay != null)
        {
            overlay.Close();
            overlay = null;
        }
    }

}

public class OverlayForm : Form
{

    private IntPtr ExternalhWnd;

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left;        // x position of upper-left corner
        public int Top;         // y position of upper-left corner
        public int Right;       // x position of lower-right corner
        public int Bottom;      // y position of lower-right corner
    }

    public OverlayForm(IntPtr ExternalhWnd)
    {
        this.ExternalhWnd = ExternalhWnd;

        this.TopMost = true;
        this.ShowInTaskbar = false;
        this.FormBorderStyle = FormBorderStyle.None;        
        this.StartPosition = FormStartPosition.Manual;

        this.Opacity = 0.7; // 70% opacity
        this.BackColor = Color.Yellow;
    }

    protected override CreateParams CreateParams 
    {
        get
        {
            CreateParams createParams = base.CreateParams;
            createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT - Ignore Mouse Clicks
            createParams.ExStyle |= 0x80; // WS_EX_TOOLWINDOW - Remove from Alt-Tab List
            return createParams;
        }
    }

    protected override void OnShown(EventArgs e)
    {
        base.OnShown(e);
        if (!this.ExternalhWnd.Equals(IntPtr.Zero))
        {
            // position our overlay on top of the external window
            RECT rct;
            GetWindowRect(this.ExternalhWnd, out rct);
            this.Location = new Point(rct.Left, rct.Top);
            this.Size = new Size(rct.Right - rct.Left, rct.Bottom - rct.Top);
        }
    }

}

这是单击 button1 前后的表单图片:

当黄色覆盖层出现时,按钮 2 仍对用户输入和点击做出反应。

请尽可能多地提问以理解代码并将其实施到您的项目中。我会尽力帮助你...