简体   繁体   中英

location of mouse to draw a image

here's code for interface of my project, but I can't determine location of mouse when I move mouse!when I draw image on screen, it not draw a right location!

public partial class Form1 : Form
{
    int _countRouter = 0;
    Point[] _posiRouter = new Point[100];
    readonly Image _imgRouter = Image.FromFile(@"C:\Router2.png");
    public Form1()
    {
        InitializeComponent();
    }

    private void btnRouter_MouseUp(object sender, MouseEventArgs e)
    {
        //panelMain.Cursor = new Cursor(Cursor.Current.Handle);

        //Point x = Cursor.Position;
        Point x = new Point(e.X, e.Y);
        if ((x.X > 10 || x.X < 660) && (x.Y > 30 || x.Y < 350))
        {
            _posiRouter[_countRouter].X = x.X;// -_imgRouter.Width;
            _posiRouter[_countRouter].Y = x.Y;// -_imgRouter.Height;
            _countRouter++;
        }
        this.panelOption.Invalidate();
        this.panelMain.Invalidate();
    }

    private void panelMain_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.FillRectangle(Brushes.White,  
                        new Rectangle ( 0, 0, this.ClientRectangle.Width , 
                                        this.ClientRectangle.Height));

        for (int x = 0; x < _countRouter; x++)
        {
            g.DrawImage(_imgRouter, _posiRouter[x]);

        }
    }
// ... ?
}

From MSDN :

The mouse coordinates vary based on the event being raised. For example, when the Control.MouseMove event is handled, the mouse coordinate values are relative to the coordinates of the control that raised the event. Some events related to drag-and-drop operations have associated mouse-coordinate values that are relative to the form origin or the screen origin.

Have a look there, and try to figure out what/where is your mouse by putting a debug point Point x = new Point(eX, eY); to see what you get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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