5 Replies - 1414 Views - Last Post: 18 September 2009 - 03:29 PM Rate Topic: -----

#1 Iamazn  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 17-June 09

Keeping mouse on color?

Posted 17 September 2009 - 07:29 PM

Edit: would there be a faster method?
Whats Wrong:
If I put this code in the form, the mouse will be moved once. I want it to "freeze" the mouse at the point that its moved at. If I put code in timer (which it is now), the mouse will keep searching for the color, so the mouse moves around like crazy. How do I change my code so that it will "freeze" the mouse once it finds the color?
	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		Dim img As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
		Dim gr As Graphics = Graphics.FromImage(img)
		gr.CopyFromScreen(New Point(0, 0), New Point(0, 0), New Size(img.Width, img.Height))
		For c As Integer = 0 To img.Width - 1
			For r As Integer = 0 To img.Height - 1
				If img.GetPixel(c, r).ToArgb = Color.FromArgb(255, 255, 255).ToArgb Then
					Cursor.Position = New Point(c, r)
					Return
				End If
			Next
			Dim newMousePosition As New System.Drawing.Point
			newMousePosition.X = c
			System.Windows.Forms.Cursor.Position = newMousePosition
		Next
	End Sub
End Class

This post has been edited by Iamazn: 17 September 2009 - 07:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Keeping mouse on color?

#2 StealthTools  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 11-March 08

Re: Keeping mouse on color?

Posted 17 September 2009 - 10:22 PM

If you want the mouse to not move after it finds the color, it seems like what I would try would be to make a timer and Right after the color is found by the cursor, get the current cursors position, and in the timer set the cursor position to the current position.. so after the mouse finds the color the new timer starts and it will not move from that position unless the timer is turned off.. To turn off the timer at a given time interval you could use something like, in another timer with a specific interval:
Timer2(textbox1.text = val(textbox1.text)+1 - if textbox1.text > 5 then timer1.stop)...

Needed:
1) Code to get current mouse position
2) Code to set mouse position
2) Timer

the question is though why would you want to lock the mouse at that position anyway? If I were making a clicking program I would just, instead of locking the mouse on the target, have the mouse move to the target right before the function is activated.. So the user can move the mouse around but right before the mouse clicks (or whatever it does once it reaches the color) the mouse moves to the point... This would be better because it should make the program have a less chance of crashing and not responding and free up system resources that would be used up by the timer's short interval...

This post has been edited by StealthTools: 17 September 2009 - 10:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Keeping mouse on color?

Posted 18 September 2009 - 07:24 AM

Just stop the timer when it finds the color. The user will still be able to move the mouse.
Freezing it in one place would be a very strange thing to do. You could do it if you really wanted, with ClipCursor, but it sounds like a very very bad idea.

GetPixel is very slow. Either:
1) save the bitmap to a memorystream, skip the bitmap header and access the pixel data directly
2) use Bitmap.LockBits to lock the bitmap in memory allowing fast pixel access
3) lock a correctly size array in memory using a GCHandle, create a bitmap from the array, create a graphics object for the bitmap, copy the screen to the bitmap, access the pixels in the array.


If we have some context we might understand the situation better.
Was This Post Helpful? 0
  • +
  • -

#4 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Keeping mouse on color?

Posted 18 September 2009 - 07:43 AM

View PostIamazn, on 17 Sep, 2009 - 06:29 PM, said:

If I put this code in the form, the mouse will be moved once. I want it to "freeze" the mouse at the point that its moved at. If I put code in timer (which it is now), the mouse will keep searching for the color, so the mouse moves around like crazy. How do I change my code so that it will "freeze" the mouse once it finds the color?


Hi,

What kind of application you want to write here?

Why would you need to freeze the mouse so that the user can't move it anymore?

Do you want to HACK others PC's that way?

Please explain more about the purpose of your application.
Was This Post Helpful? 0
  • +
  • -

#5 Iamazn  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 17-June 09

Re: Keeping mouse on color?

Posted 18 September 2009 - 02:54 PM

View Postcrepitus, on 18 Sep, 2009 - 06:24 AM, said:

Just stop the timer when it finds the color. The user will still be able to move the mouse.
Freezing it in one place would be a very strange thing to do. You could do it if you really wanted, with ClipCursor, but it sounds like a very very bad idea.

GetPixel is very slow. Either:
1) save the bitmap to a memorystream, skip the bitmap header and access the pixel data directly
2) use Bitmap.LockBits to lock the bitmap in memory allowing fast pixel access
3) lock a correctly size array in memory using a GCHandle, create a bitmap from the array, create a graphics object for the bitmap, copy the screen to the bitmap, access the pixels in the array.


If we have some context we might understand the situation better.

What would be faster? I want to scan my whole
Was This Post Helpful? 0
  • +
  • -

#6 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Keeping mouse on color?

Posted 18 September 2009 - 03:29 PM

Look at JohnWein's post here:
http://social.msdn.m...2f-ce98599adaf2

It uses the third technique I mentioned.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1