PDA

View Full Version : حرکت موس بدون دخالت کاربر



aida.gh
پنج شنبه 22 آذر 1386, 21:24 عصر
با سلام:
چه جوری می تونم یک برنامه بنویسم که موس بدون دخالت کاربر شروع به حرکت کنه و روی یک دکمه کلیک کنه و به سراغ دکمه بعدی بره.

sinpin
جمعه 23 آذر 1386, 00:02 صبح
با سلام:
چه جوری می تونم یک برنامه بنویسم که موس بدون دخالت کاربر شروع به حرکت کنه و روی یک دکمه کلیک کنه و به سراغ دکمه بعدی بره.

از طریق یکی از API های ویندوز بنام SetCursorPos شما میتونید موقعیت مکانی نشانگر ماوس رو عوض کنید.

1.) Create a Windows Form application
2.) Create a class as below:
public class Win32
{
[DllImport("User32.Dll")]
public static extern long SetCursorPos(int x, int y);
[DllImport("User32.Dll")]
public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point);
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}
}
3.) Paste the following code in the button's clik eventhandler:
Win32.POINT p = new Win32.POINT();
p.x = button1.Left + (button1.Width / 2);
p.y = button1.Top + (button1.Height / 2);
Win32.ClientToScreen(this.Handle, ref p);
Win32.SetCursorPos(p.x, p.y);
This will move the mouse pointer to the center of the button.
Now write you're handler to move the mouse around - Enjoy!


منبع : http://swigartconsulting.blogs.com/tech_blender/2005/08/how_to_move_the.html (http://swigartconsulting.blogs.com/tech_blender/2005/08/how_to_move_the.html)

sinpin
جمعه 23 آذر 1386, 01:19 صبح
اینهم منبع خوبیه :
http://www.codeproject.com/KB/cs/MouseActions.aspx

The following API moves the mouse:

[DllImport("user32")]
public static extern int SetCursorPos(int x, int y);
This is fairly easy to understand, it gets the coordinates and moves the mouse to that position, but how do I click then?

[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
Ok then, there's an API called mouse_event. you're probably wondering how it's used, so here's an example:
First, you make an enumeration (you might as well use the integers themselves, i just find this way to be more organized.

public enum MouseEventType : int
{
LeftDown = 0x02,
LeftUp = 0x04,
RightDown = 0x08,
RightUp = 0x10
}


Now, what exactly is a mouse click? it's a click down and then up. only sending LeftDown will be like a mouse hold, and only sending LeftUp...will just be weird, that's why in order to emulate a mouse click, you have to send both:

mouse_event((int)MouseEventType.LeftDown, Cursor.Position.X, Cursor.Position.Y, 0, 0);
mouse_event((int)MouseEventType.LeftUp, Cursor.Position.X, Cursor.Position.Y, 0, 0);<code>
Woohoo! we got ourselves a Mouseclick!
The full code:



[DllImport("user32.dll")]
public static extern void mouse_event(MouseEventType dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
[DllImport("user32")]
public static extern int SetCursorPos(int x, int y);
public enum MouseEventType : int
{
LeftDown = 0x02,
LeftUp = 0x04,
RightDown = 0x08,
RightUp = 0x10
}
private void button1_Click(object sender, EventArgs e)
{
SetCursorPos(0, 0);
mouse_event(MouseEventType.LeftDown, Cursor.Position.X, Cursor.Position.Y, 0, 0);
mouse_event(MouseEventType.LeftUp, Cursor.Position.X, Cursor.Position.Y, 0, 0);
}
By putting the click in a function and calling it with a timer (with a 1 millisecond interval, for example), you'll be making the exact same virus (but viruses are bad...so don't).
Other Functions

Well, there is 1 more thing I'd like to show you, which is a really weird function i found that for some unknown reason exists, and it is SwapMouseButton. It does exactly what the name suggests - swaps the mouse buttons ( left = right, right = left ).
How to use:

[DllImport("user32.dll")]
static extern bool SwapMouseButton(bool fSwap);
private void button1_Click(object sender, EventArgs e)
{
SwapMouseButton(true);
}Sending true to the function will swap the mouse buttons, sending false will turn them back to normal.
Final Notes

Well, this is my first article here (and at all), hope it helps.
Oh, and don't forget adding: using System.Runtime.InteropServices; to your using clause.

silentrise
جمعه 23 آذر 1386, 01:59 صبح
سلام
میشه بیشتر توضیح بدید...
من نتونستم این کارو انجام بدم.
با سپاس

sinpin
جمعه 23 آذر 1386, 02:27 صبح
سلام
میشه بیشتر توضیح بدید...
من نتونستم این کارو انجام بدم.
با سپاس

اینجا یه نمونه کد هست :
http://www.codeproject.com/KB/cs/all_ehllapi.aspx

aida.gh
جمعه 23 آذر 1386, 10:56 صبح
من dll رو بلد نیستم میخوام از تابع mouse event arga استفاده کنم.

newgoldenman
جمعه 23 آذر 1386, 13:08 عصر
من dll رو بلد نیستم میخوام از تابع mouse event arga استفاده کنم.

خیلی ساده است!
تنها کافیه تا طول و عرض مکان نما رو به دلخواه خودتون تغییر بدین.

MousePosition.X
MousePosition.Y
این ها برای تغییر دادن مکان نما و نیز دریافت موقعیت فعلی اون، لازم و کافی هستند.

mehrzad007
جمعه 23 آذر 1386, 22:40 عصر
یه سورس خیلی خوب تو این زمینه توی سایت لمیر احسانی هست روبات مین روب
ehsani.ir./jooti

Payman62
شنبه 24 آذر 1386, 15:00 عصر
سلام.
من شنیدم 99% API ها داخل کلاس های خود C# هست. میشه تغییر مکان موس رو به کمک خود C# انجام داد؟

newgoldenman
یک شنبه 25 آذر 1386, 14:04 عصر
سلام.
من شنیدم 99% API ها داخل کلاس های خود C# هست. میشه تغییر مکان موس رو به کمک خود C# انجام داد؟


خیلی ساده است!
تنها کافیه تا طول و عرض مکان نما رو به دلخواه خودتون تغییر بدین.

MousePosition.X
MousePosition.Yاین ها برای تغییر دادن مکان نما و نیز دریافت موقعیت فعلی اون، لازم و کافی هستند.

خیلی ساده هست ها!

rohullah
یک شنبه 25 آذر 1386, 15:10 عصر
خیلی ساده است!
تنها کافیه تا طول و عرض مکان نما رو به دلخواه خودتون تغییر بدین.

MousePosition.X
MousePosition.Yاین ها برای تغییر دادن مکان نما و نیز دریافت موقعیت فعلی اون، لازم و کافی هستند.
ببشیندا ولی میگم که رئیس به نظرم mouseposition نمیشه بهش مقدار داد به من این error رو میده:
Cannot modify the return value of 'System.Windows.Forms.Control.MousePosition' because it is not a variable
ولی من اینجوری میتونم موس رو حرکت بدم:

Cursor.Position = new Point(10, 100);

newgoldenman
یک شنبه 25 آذر 1386, 21:56 عصر
وای خدای من! D: حق با شماست. معذرت میخوام. با این کد فقط میشه موقعیت فعلی رو بدست آورد.