在.NET Compact Framework中单击触发按钮 [英] Trigger button click in .NET Compact Framework

查看:244
本文介绍了在.NET Compact Framework中单击触发按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Say I have a Panel with many Button and PictureBox controls. Each control has an associated Click event. 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.

许多其他答案建议使用 PerformClick事件,但Compact Framework不支持此事件。还有其他选择吗?我的代码:

A number of other answers suggest using the "PerformClick" event, but this is not supported in the 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?
            }
        }
    }
}


推荐答案

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

First off, try subclassing button and calling the click event from your own PerformClick. 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. I believe it is the SendMessage function. 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

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

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

Here's the full code to simulate a click by sending a mousedown and mouseup message to the control:

// 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;
        }
    }
}

这篇关于在.NET Compact Framework中单击触发按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆