Click here to Skip to main content
15,940,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I'm developing a windows form application.
During the OnMouseMove I need to change the mouse position, but without trigger a new OnMouseMove event, simply change its position.
I've tried some ways, but no results.
Any idea?
Many thanks
Gian

What I have tried:

SetMousePos, Cursor.Position, handle onmousemove
Posted
Updated 28-Dec-22 20:44pm

A really crude way would be to have a global bool; ignoreMouseModeEvent

C#
ignoreMouseModeEvent = true;
SetMousePosition
ignoreMouseModeEvent = false;


then in the OnMouseMove
C#
OnMouseMove(.....)
{
   if (ignoreMouseMoveEvent)
      return;
}
 
Share this answer
 
Comments
Gian.girardelli 29-Dec-22 2:55am    
@Mike Hankey thanks for the answer, but the problem is a little bit more complicate.
I'll try to explain better what I'm doing.
I'm developing a software for rendering a room in 3D, in my form I need to drag an element using the mouse and during the dragging, in OnMouseMove, I need to change the position of the mouse cursor, but without generate the corresponding message WM_MOUSEMOVE and the coresponding OnMouseMove.
I thought to your solution, but the probelm is that Windows generate events that contain not only the one generated by my "new position", but also the others that are in progress.

This is a snap of the code to set the cursor position in OnMouseMove.

protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (Cursor.Position.X < 500)
{
Debug.WriteLine(string.Format("X {0} Y {1}", Cursor.Position.X, Cursor.Position.Y));
Cursor.Position = new Point(Cursor.Position.X + 10, Cursor.Position.Y + 10);
}
}
}

What I see in the console when I move the mouse is:
X 188 Y 231
X 198 Y 241
X 211 Y 254
X 224 Y 267
X 234 Y 277
X 247 Y 290
X 261 Y 302
X 271 Y 312
X 286 Y 325
X 300 Y 338
X 310 Y 348
X 325 Y 361
X 339 Y 373
X 349 Y 383
X 365 Y 397
X 379 Y 408
X 389 Y 418
X 403 Y 430
X 418 Y 442
X 428 Y 452
X 442 Y 464
X 456 Y 476
X 466 Y 486
X 479 Y 498
X 492 Y 511
There's no stepping 10 pixels by 10 pixels, but a continuos moving ... as should be
this code example (my own) shows how to limit mouse movement to a specific area (of a Button on a Form) and disable tab-key switching. an existing MouseMove handler is uninstalled, or reinstalled as necessary.

the user re-enables mouse movement by pressing the Escape key while the Button has focus.

review the documentation on Cursor to better understand what is going on.
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace MouseControlDemo_2022
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        DataTable dtbl = new DataTable("Customers");

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private bool MouseMoveSuppressed = false;

        private void SuppressMouseMove(Point pt)
        {
            // remove the current handler
            this.MouseMove -= Form1_MouseMove;

            // set the Cursor position
            Cursor.Position = this.PointToScreen(pt);

            // limit Cursor movement
            Cursor.Clip = RectangleToScreen(SuppressMouseMoveBtn.Bounds);

            MouseMoveSuppressed = true;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
        }

        private void SuppressMouseMoveBtn_Click(object sender, EventArgs e)
        {
            if (! MouseMoveSuppressed)
            {
                SuppressMouseMove(new Point(SuppressMouseMoveBtn.Right, SuppressMouseMoveBtn.Bottom));
            }
        }

        private void SuppressMouseMoveBtn_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (MouseMoveSuppressed)
                {
                    // restore handler
                    this.MouseMove += Form1_MouseMove;

                    // remove limit on Cursor movement
                    Cursor.Clip = Rectangle.Empty;
                }

                MouseMoveSuppressed = false;
            }
            else if (e.KeyCode == Keys.Tab)
            {
                // disable Tab key for button
                e.Handled = true;
            }
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900