i want the mouse to freez (cant move) when mouse down thanks
-
7Why you want to do this? – Serkan Hekimoglu Oct 22 '10 at 11:37
-
8Superglue it to the table. – Winston Smith Oct 22 '10 at 11:41
-
8Leave it in the freezer – Luke Oct 22 '10 at 11:53
-
1@ all........... funny comments....... Cheers!!!!!!!!!! – Thorin Oakenshield Oct 22 '10 at 12:02
-
2I hate apps that grab/block the mouse :( – John Warlow Oct 22 '10 at 13:00
-
There would be considerable usefulness to an application which froze the pointer while the mouse was pressed, but processed delta movements. Such an app wouldn't work well with all input devices, but it would allow things like moving an object an arbitrary distance while scrolling the screen, in a fashion much better than most apps presently do. – supercat Oct 22 '10 at 13:38
-
nail it... along w/user's hand. – Luiscencio Oct 22 '10 at 13:39
-
To get the mouse moving again you can unfreeze it in a microwave oven. – pablosaraiva Oct 22 '10 at 18:30
-
1I think he made it with @Grozz's answer, and now he cant find the mouse to write reply :) – Serkan Hekimoglu Oct 22 '10 at 11:54
6 Answers
I used a tableLayoutPanel for your reference (Just remember to implement the code to the Control that is in the front):
OPTION1: Reset the mouse position:
Define two global variables:
bool mousemove = true;
Point currentp = new Point(0, 0);
Handel MouseDown Event to update mousemove
:
private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
int offsetX = (sender as Control).Location.X + this.Location.X;
int offsetY = (sender as Control).Location.Y + this.Location.Y;
mousemove = false;
currentp = new Point(e.X+offsetX, e.Y+offsetY); //or just use Cursor.Position
}
Handel MouseMove
to disable/enable move:
private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
{
if (!mousemove)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = currentp;
}
}
Reset mousemove
while Mouseup
private void tableLayoutPanel1_MouseUp(object sender, MouseEventArgs e)
{
mousemove = true;
}
OPTION2: Limit mouse clipping rectangle:
Limit it while MouseDown:
private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = Cursor.Position;
Cursor.Clip = new Rectangle(Cursor.Position, new Size(0, 0));
}
Release it after MouseUp:
private void tableLayoutPanel1_MouseUp(object sender, MouseEventArgs e)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = Cursor.Position;
Cursor.Clip = Screen.PrimaryScreen.Bounds;
}

- 8,696
- 4
- 38
- 70
-
Didn't tested it, but it was exactly what I had in mind. Keep resetting mouse position would give the desired effect. – Bruno Brant Oct 22 '10 at 12:54
-
@Bruno: You can freeze the mouse by setting it's clipping rectangle to 0 as well, and I've updated my answer. – Bolu Oct 22 '10 at 13:03
You can't.
Mouse acts in the OS Layer, not your app... even if you freeze your app, mouse will be able to run.
You can try to disconnect the mouse driver/port but you do need to ask the user what port the mouse is using as for the OS it's a Input device, just like a pen in a design board and you will not know the one to disconnect.

- 73,608
- 45
- 233
- 342
It's possible, Windows has a dedicated API for it, BlockInput(). Be sure to save all your work when you experiment with it, it is rather effective. You may need to reboot your machine, the thing your user will do when you use it in a program. Here's a sample Windows Forms form that uses it, it needs a button and a timer:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
timer1.Interval = 3000;
timer1.Tick += new EventHandler(timer1_Tick);
button1.Click += new EventHandler(button1_Click);
}
private void button1_Click(object sender, EventArgs e) {
timer1.Enabled = true;
BlockInput(true);
}
private void timer1_Tick(object sender, EventArgs e) {
timer1.Enabled = false;
BlockInput(false);
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool BlockInput(bool block);
}

- 922,412
- 146
- 1,693
- 2,536
-
this blocks input for 3 seconds, but not until the mouse button is released. Per definition the releasing of the MouseButton won't be received as long as user input is blocked. – chiccodoro Oct 22 '10 at 13:16
-
1@chicco - this is normal Button behavior, the Click event is raised on MouseUp. – Hans Passant Oct 22 '10 at 13:22
You can fake that behavior for your window in the following way:
Remember current cursor and its position.
Set
this.Cursor = Cursors.None;
Draw the remembered cursor at specified position and introduce
canExecute
flag for all your mouse handlers to disable them during "fake mouse freezing".

- 8,317
- 4
- 38
- 53
-
2You may still get other effects as the hidden mouse pointer moves over other controls. On the other hand, this whole question is probably a bad idea. – JasonFruit Oct 22 '10 at 12:03
Can't you move the mouse pointer somewhere? You could reset its position when moving (which may look ugly).

- 11,607
- 7
- 57
- 81
Setup a low level mouse hook with SetWindowsHookEx
and ignore all messages to the HOOKPROC
delegate you specified (means not to call CallNextHookEx
).

- 2,497
- 5
- 26
- 35