简体   繁体   English

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

[英]Trigger button click in .NET Compact Framework

Say I have a Panel with many Button and PictureBox controls. 假设我有一个包含许多Button和PictureBox控件的Panel。 Each control has an associated Click event. 每个控件都有一个关联的Click事件。 This is a touchscreen application, so the user may be a little imprecise with their click (or the touchscreen calibration might not be perfect). 这是一个触摸屏应用程序,因此用户点击可能会有点不精确(或者触摸屏校准可能不完美)。 So, I'd like to handle the click event on the panel and then programmatically call the Button's or PictureBox's Click event if the click is close to a button/picture. 所以,我想处理面板上的click事件,然后如果点击接近按钮/图片,则以编程方式调用Button或PictureBox的Click事件。

A number of other answers suggest using the "PerformClick" event, but this is not supported in the Compact Framework. 许多其他答案建议使用“PerformClick”事件,但Compact Framework不支持此功能。 Any alternatives? 任何替代品? My code: 我的代码:

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?
            }
        }
    }
}

Since you can't use PerformClick , you won't be able to rely on the control's Click event handler firing automatically in this case. 由于您无法使用PerformClick ,因此在这种情况下您将无法依赖控件的Click事件处理程序自动触发。 Instead, just make a method that takes a control and figure out from that control which action to take. 相反,只需创建一个方法来控制并从该控件中找出要采取的操作。 Example: 例:

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
}

First off, try subclassing button and calling the click event from your own PerformClick. 首先,尝试子类化按钮并从您自己的PerformClick调用click事件。 Else, You can write a method which takes a button and performs a click. 否则,您可以编写一个按钮并执行单击的方法。 First get the handle of the control, then p/invoke the windows API functions to send a mousedown then mouseup event to it. 首先获取控件的句柄,然后p /调用windows API函数向它发送mousedown然后mouseup事件。 I believe it is the SendMessage function. 我相信它是SendMessage函数。 All you have to do then is write the logic to find the closest button and pass it to the function. 然后,您只需编写逻辑以找到最近的按钮并将其传递给函数。 Or, write it as an extension method to Button 或者,将其作为Button的扩展方法

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

EDIT: Here's the full code to simulate a click by sending a mousedown and mouseup message to the control: 编辑:这是通过向控件发送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;
        }
    }
}

put all the stuff from your button click handler to a separate function, and call that function instead of triggering a button 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)))
            {
                PerformActionsOnClick();
            }
        }
    }
}
private void MyButton_Click(object sender, EventArgs e)
{
    PerformActionsOnClick();
}
void PerformActionsOnClick()
{
    //do your stuff here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM