C#自定义控件MouseDown - Y位置总是出错

时间:2011-12-14 16:59:30

标签: c# compact-framework custom-controls windows-mobile-6.5 compact-framework2.0

我在Windows Mobile 6.5上的项目中苦苦挣扎。我正在编写一个自定义控件,可以绘制用户点击自定义控件的位置。

我遇到的问题是OnMouseDown(MouseEventArgs e)没有返回正确的e.Y(点击位置的Y位置)。有人请帮忙!我在这个问题上花了几个小时,但仍然无法弄清楚是什么问题。 (我想我的方向错了)

这是应用程序的样子:

My application looks like

当我尝试在WM6.5模拟器中运行时,OnMouseDown(MouseEventArgs e)总是返回错误的Y位置(它返回Y位置减去某些值)。例如:我点击控件的中心进行第一次点击,但显然e.Y不在中心。

obviously the e.Y is not at center

以下是代码spinet:

protected override void OnPaint(PaintEventArgs pe)
    {

        Graphics g = pe.Graphics;

        Pen pen_black = new Pen(Color.Black);
        g.DrawLine(pen_black, 0, 0, this.Width, 0);
        g.DrawLine(pen_black, 0, this.Height - 1, this.Width, this.Height - 1);
        g.DrawLine(pen_black, 0, 0, 0, this.Height);
        g.DrawLine(pen_black, this.Width - 1, 0, this.Width - 1, this.Height);

        // draw center cross
        g.DrawLine(pen_black, this.Width / 2, this.Height / 2 + 10, this.Width / 2, this.Height / 2 - 10);
        g.DrawLine(pen_black, this.Width / 2 + 10, this.Height / 2, this.Width / 2 - 10, this.Height / 2);


        // draw lines between all mouse down point
        if (pointCount > 0)
        {
            Pen pen_red = new Pen(Color.Red);

            for (int i = 0; i < pointCount - 1; i++)
            {
                g.DrawLine(pen_red, lineList[i].X, lineList[i].Y, lineList[i + 1].X, lineList[i + 1].Y);
            }
        }

            base.OnPaint(pe);
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        // Put the last point to array            
        lineList[pointCount] = new Point(e.X, e.Y);

        pointCount++;
    }

以下是我的自定义控件的源代码: Download here 谢谢!

2 个答案:

答案 0 :(得分:2)

这可能听起来很疯狂,如果它实际上不是一个可能的解决方案,甚至可能更好作为评论:

进入系统设置并配置屏幕。

  

设置&gt; “系统标签”&gt;屏幕&gt;对齐屏幕

System Settings Screen Settings

答案 1 :(得分:1)

Y值很可能是屏幕坐标,而不是您正在绘制的矩形内的坐标。我认为您需要考虑任务栏的高度。

自从我使用WM以来已经很长时间了,但我记得在通过MouseEventArgs捕获点时遇到了类似的问题。