Skip to content

Cursor Position

J

Asked May 27, 2007, 5:57 AM EDT2 answers
Share
LinkedInX


I want that left click will draw rectangle and right click will locate the cursor into it.
Instead it I get the cursor in other place. What's wrong?

The code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
       
        Rectangle rect;

        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(new Pen(Color.BlueViolet), rect);
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                rect = new Rectangle(e.X, e.Y, 100, 100);
                panel1.Invalidate();
            }
            if (e.Button == MouseButtons.Right)
            {
                Cursor.Position = new Point(rect.X + 50, rect.Y + 50);
            }
        }
    }
}

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Just change this if:

    if (e.Button == MouseButtons.Right)
        {
             Cursor.Position = new Point(rect.X + 50, rect.Y + 50);
        }

    to this one:

    if (e.Button == MouseButtons.Right)
    {
           Rectangle rect1 = this.RectangleToScreen(rect);
           Cursor.Position = new Point(rect1.X + 50, rect1.Y + 50);
    }

    0
  • 0
    I just saw the reply.
    It works!

    Thanks a lot, my friend.
    0