C# customcontrol OnMouseDown - always get wrong Y position
문제
I am struggling in a project on Windows Mobile 6.5. I am writing a custom-controls that can plot lines of where the user has clicked on the custom-controls.
I am facing a problem that the OnMouseDown(MouseEventArgs e) is not returning correct e.Y (Y position of clicked location). Anyone please help! I have spent few hours on this issue, but still cannot figure what's wrong. (I think I am in the wrong direction)
Here is what the application looks like:
When I tried to run in a WM6.5 emulator, the OnMouseDown(MouseEventArgs e) always return wrong Y position (It returns Y location minus some values). For example: I clicked on the center of the control for the first click, but obviously the e.Y is not at the center.
Here is the code 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++;
}
Here is the source code of my custom-controls: Download here Thanks!
해결책
This may sound crazy, and might even be better as a comment if it were not actually a possible solution:
Go into your system settings and configure your screen.
Settings > System Tab > Screen > Align Screen
다른 팁
The Y value is most likely the screen coordinates, not the coordinates within the rectangle you are drawing in. I think you'll need to take into account the taskbar height.
It's been a long time since I worked with WM, but I remember having similar issues when capturing points via MouseEventArgs.