首页 > 解决方案 > 执行单击图片框

问题描述

到目前为止,我在单击另一个按钮后一直使用单击按钮的方法。(我单击按钮“A”并激活按钮“B”)

buttonDeleteFields.PerformClick();

现在,因为我创新了设计,我已经用图标(图片框)替换了这个按钮,但是带有图片框的 PerformClick 方法不起作用。你能给我一个解决这个问题的方法吗?(我单击按钮“A”并激活图片框“B”)

标签: c#visual-studio

解决方案


要完全模拟 Click 事件(就像用户点击了图片框一样),

将这些添加到您的表单类中:

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

private int WM_LBUTTONDOWN = 0x0201;
private int WM_LBUTTONUP = 0x0202;

并在您的按钮单击事件中这样调用:

private void button1_Click(object sender, EventArgs e)
{
    // OLD CODE: Used to perform click on button 2,
    // before it was changed to a picture box
    // buttonDeleteFields.PerformClick();

    // new code, emulating picture box click:
    SendMessage(pictureBoxDeleteFields.Handle, WM_LBUTTONDOWN, 0, 1);
    SendMessage(pictureBoxDeleteFields.Handle, WM_LBUTTONUP, 0, 0);
}

推荐阅读