在.NET Compact Framework中单击“触发器”按钮

时间:2015-05-19 15:03:34

标签: c# .net compact-framework

假设我有一个包含许多Button和PictureBox控件的Panel。每个控件都有一个关联的Click事件。这是一个触摸屏应用程序,因此用户点击可能会有点不精确(或者触摸屏校准可能不完美)。因此,我想在面板上处理点击事件,然后如果点击接近按钮/图片,则以编程方式调用Button或PictureBox的Click事件。

许多其他答案建议使用" PerformClick"事件,但Compact Framework不支持此功能。任何替代品?我的代码:

private void pnlButtons_Click(object sender, EventArgs e)
{
    Point ptClick = Control.MousePosition;
    foreach (Control cntrl in pnlButtons.Controls)
    {
        // Make sure the control is visible!
        if (cntrl.Visible)
        {
            // Click close to control?
            if ((ptClick.X > (cntrl.Left - 5)) &&
                (ptClick.X < (cntrl.Right + 5)) &&
                (ptClick.Y > (cntrl.Top - 5)) &&
                (ptClick.Y < (cntrl.Bottom + 5)))
            {
                // Click Button or PictureBox without cntrl.PerformClick?
            }
        }
    }
}

3 个答案:

答案 0 :(得分:4)

由于您无法使用PerformClick,因此在这种情况下,您无法依靠控件的Click事件处理程序自动触发。相反,只需创建一个方法来控制并从该控件中找出要采取的操作。例如:

private void pnlButtons_Click(object sender, EventArgs e)
{
    Point ptClick = Control.MousePosition;
    foreach (Control cntrl in pnlButtons.Controls)
    {
        // Make sure the control is visible!
        if (cntrl.Visible)
        {
            // Click close to control?
            if ((ptClick.X > (cntrl.Left - 5)) &&
                (ptClick.X < (cntrl.Right + 5)) &&
                (ptClick.Y > (cntrl.Top - 5)) &&
                (ptClick.Y < (cntrl.Bottom + 5)))
            {
                handleClick (cntrl);
            }
        }
    }
}

private void handleClick(Control c)
{
    if (c == button1)
    {
        // handle button1 click, e.g. by calling its `Click` handler
    }
    else if (c == picureBox1)
    {
        // handle pictureBox1 click
    }
    // et cetera
}

答案 1 :(得分:2)

首先,尝试子类化按钮并从您自己的PerformClick调用click事件。否则,您可以编写一个按钮并执行单击的方法。首先获取控件的句柄,然后p /调用windows API函数向它发送mousedown然后mouseup事件。我相信它是SendMessage函数。然后,您只需编写逻辑以找到最近的按钮并将其传递给函数。或者,将其作为Button

的扩展方法编写

https://msdn.microsoft.com/en-us/library/windows/desktop/ms644950%28v=vs.85%29.aspx

编辑: 这是通过向控件发送mousedown和mouseup消息来模拟点击的完整代码:

// Windows constants for mouse messages
private const int WM_LBUTTONDOWN        = 0x0201;
private const int WM_LBUTTONUP          = 0x0202;

// P/Invoke for SendMessage
[DllImport("coredll.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int nMsg, IntPtr wParam, IntPtr lParam);

// Method to click a control
public void ClickControl(IntPtr hWnd)
{
    // Send a MOUSE_DOWN and MOUSE_UP message to the control to simulate a click
    SendMessage(hWnd, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
    SendMessage(hWnd, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
}

// Method to handle click event on parent Panel control
private void pnlButtons_Click(object sender, EventArgs e)
{
    // See if the click point is close to a (visible) button and if so, click the button.
    // The user was probably a little imprecise or the screen might need re-calibration.
    Point pt = pnlButtons.PointToClient(Cursor.Position);

    // Now look for any Button / PictureBox controls nearby
    foreach (Control cntrl in pnlButtons.Controls)
    {
        Rectangle inflated = cntrl.Bounds;
        inflated.Inflate(4, 5);
        if (cntrl.Visible && inflated.Contains(pt))
        {
            // Simulate a click on the control
            ClickControl(cntrl.Handle);
            break;
        }
    }
}

答案 2 :(得分:1)

将按钮点击处理程序中的所有内容放到单独的函数中,然后调用该函数而不是触发按钮单击。

private void pnlButtons_Click(object sender, EventArgs e)
{
    Point ptClick = Control.MousePosition;
    foreach (Control cntrl in pnlButtons.Controls)
    {
        // Make sure the control is visible!
        if (cntrl.Visible)
        {
            // Click close to control?
            if ((ptClick.X > (cntrl.Left - 5)) &&
                (ptClick.X < (cntrl.Right + 5)) &&
                (ptClick.Y > (cntrl.Top - 5)) &&
                (ptClick.Y < (cntrl.Bottom + 5)))
            {
                PerformActionsOnClick();
            }
        }
    }
}
private void MyButton_Click(object sender, EventArgs e)
{
    PerformActionsOnClick();
}
void PerformActionsOnClick()
{
    //do your stuff here
}