C#.NET Compact Framework,使用触摸屏的鼠标按下事件问题

时间:2019-05-02 15:24:43

标签: c# .net compact-framework onmousedown .net-cf-3.5

我目前正在使用Visual Studio 2008 C#开发用于智能设备的软件。设备具有触摸屏作为输入方法。我无法使用MouseDown事件。我创建了一个自定义按钮,该按钮继承自System.Windows.Forms.Button类。

尝试覆盖OnMouseDownOnMouseUp事件。但是这些事件并没有触发。我搜索了互联网,并找到了WndProc的解决方案,但是它不能与.NET Compact Framework一起使用。

请帮助我。谢谢大家示例代码非常简单,您可以在下面找到它。

protected override void OnMouseUp(MouseEventArgs e)
{
   Text = "Mouse Up";
   base.OnMouseUp(e);
}

编辑:

正如我在下面对@josef的评论中提到的那样,我创建了自定义控件。控件是带面板的面板。首先,我尝试为按钮文本添加标签,实际上不需要这样做。通过OnPaint事件的重写方法可以轻松完成此操作。该控件可能有一些不必要的代码,但是您可以对其进行优化。如果需要,可以在下面找到我的解决方案:

class ButonatedPanel : Panel
    {
        public int ShadeWidth { get; set; }
        public bool isMouseDown { get; set; }


        protected override void OnMouseDown(MouseEventArgs e)
        {
            Invalidate();
            isMouseDown = true;
            Text = "Mouse Down";
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            Invalidate();
            isMouseDown = false;
            Text = "Mouse Up";
            base.OnMouseUp(e);
        }
        public override string Text
        {
            get
            {
                return Lb.Text;
            }
            set
            {
                Lb.Text = value;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Lb.Size = new Size(Width - ShadeWidth * 2, Height - 2 * ShadeWidth);
            Lb.Location = new Point(ShadeWidth, ShadeWidth);

            SizeF iSizeF = new SizeF(e.Graphics.MeasureString(Text, Lb.Font).Width,
                e.Graphics.MeasureString(Text, Lb.Font).Height);

            e.Graphics.DrawString(Text, Lb.Font, new SolidBrush(Color.FromArgb(0, 0, 0)),
                (Width - 2 * ShadeWidth - iSizeF.Width) / 2, (Height - 2 * ShadeWidth - iSizeF.Height) / 2);
            //gray 128,128,128
            Rectangle Rect1 = new Rectangle(0, 0, ShadeWidth, Height - ShadeWidth);
            Rectangle Rect2 = new Rectangle(0, 0, Width - ShadeWidth, ShadeWidth);
            Rectangle Rect3 = new Rectangle(Width - 2 * ShadeWidth, 0, ShadeWidth, Height - ShadeWidth);
            Rectangle Rect4 = new Rectangle(0, Height - 2 * ShadeWidth, Width - ShadeWidth, ShadeWidth);
            //white 228,228,228
            Rectangle Rect5 = new Rectangle(ShadeWidth, ShadeWidth, ShadeWidth, Height - 4 * ShadeWidth);
            Rectangle Rect6 = new Rectangle(ShadeWidth, ShadeWidth, Width - 4 * ShadeWidth, ShadeWidth);
            //black 0,0,0
            Rectangle Rect7 = new Rectangle(Width - ShadeWidth, 0, ShadeWidth, Height);
            Rectangle Rect8 = new Rectangle(0, Height - ShadeWidth, Width, ShadeWidth);

            if (!isMouseDown)
            {
                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect1);
                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect2);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect1);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect2);
                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect3);
                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect4);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect3);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect4);

                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(228, 228, 228)), Rect5);
                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(228, 228, 228)), Rect6);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(228, 228, 228)), Rect5);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(228, 228, 228)), Rect6);

                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(0, 0, 0)), Rect7);
                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(0, 0, 0)), Rect8);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, 0, 0)), Rect7);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, 0, 0)), Rect8);
            }
            else
            {
                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(0, 0, 0)), Rect1);
                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(0, 0, 0)), Rect2);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, 0, 0)), Rect1);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, 0, 0)), Rect2);

                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(228, 228, 228)), Rect3);
                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(228, 228, 228)), Rect4);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(228, 228, 228)), Rect3);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(228, 228, 228)), Rect4);

                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect5);
                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect6);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect5);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect6);

                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect7);
                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(128, 128, 128)), Rect8);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect7);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128)), Rect8);
            }

        }
        Label Lb = new Label();
        public ButonatedPanel()
            : base()
        {
            Invalidate();
            ShadeWidth = 1;
            Lb.Size = new Size(Width - ShadeWidth * 2, Height - 2 * ShadeWidth);
            Lb.Location = new Point(ShadeWidth, ShadeWidth);
            Lb.Visible = false;
            Controls.Add(Lb);
        }

1 个答案:

答案 0 :(得分:0)

我曾经构建过Wince应用程序(CF 3.5),该应用程序需要该应用程序捕获自定义事件,例如RFID卡读取事件。概念是相同的,即获取控件句柄,使用SetWindowLong来使焦点控件通过事件触发获取消息。只需记住撤消您对控件所做的更改即可。 *我主要在Windows窗体上使用它,但是它应该适用于Panel控件。

public delegate void MouseDownEventHandler(object sender, MouseDownEventArgs e);

public class WndProcTest
{
    [DllImport("coredll.dll", SetLastError = true)]
    private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("coredll.dll", SetLastError = true)]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, WndProcDelegate dwNewLong);

    [DllImport("coredll.dll", SetLastError = true)]
    static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("coredll.dll", SetLastError = true)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowsName);

    private delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

    public event MouseDownEventHandler mouseDownTriggered;

    private WndProcDelegate _newproc;
    private int result;
    private IntPtr _oldproc = IntPtr.Zero;
    internal static IntPtr _hwnd = IntPtr.Zero;

    // Mouse down message value
    const int WM_LBUTTONDOWN = 0x0201;

    // Get window/control handle using name
    public WndProcTest(string windowsName)
    {
        _hwnd = FindWindow(null, windowsName);
    }

    // Initialization to allow a control to receive message
    public void InitWndProcModification()
    {
        _newproc = new WndProcDelegate(MyWndProc);
        _oldproc = GetWindowLong(_hwnd, -4);
        result = SetWindowLong(_hwnd, -4, Marshal.GetFunctionPointerForDelegate(_newproc));
    }

    // Intercept the message
    private IntPtr MyWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
    {
        switch (msg)
        {
            case WM_LBUTTONDOWN:
                MouseDownEventArgs mouseDownEventArgs = new FingerPrintSensorEventArgs();
                OnMouseDownTriggered(mouseDownEventArgs);
                break;

            default:
                break;
        }

        return CallWindowProc(_oldproc, hWnd, msg, wParam, lParam);
    }


    public void RemoveWndProcModification(IntPtr currentHandle)
    {
        result = SetWindowLong(currentHandle, -4, _oldproc);
        _newproc = null;
        _oldproc = IntPtr.Zero;
        _hwnd = IntPtr.Zero;
    }

    // Fire the event
    protected virtual void OnMouseDownTriggered(MouseDownEventArgs e)
    {
        if (mouseDownTriggered != null)
        {
            mouseDownTriggered(this, e);
        }
    }
}

// Event arguments, to pass the X & Y location?
public class MouseDownEventArgs : EventArgs
{

}

// To use the class above 
WndProcTest wp = new WndProcTest("Form1"); 
wp.InitWndProcModification(); 
wp.mouseDownTriggered += ReceivedMsg;

public void ReceivedMsg(Object sender, MouseDownEventArgs e) {   }