Ogen Dogen 2 859 Właściciel Last activity: 1 hour ago Posted August 23, 2016 Właściciel Share Posted August 23, 2016 Posted August 23, 2016 Moja klasa w C# do symulacji różnych akcji myszki w systemach Windows, której użyłem do pewnego projektu. Udostępniam, bo nie widziałem w sieci takiego gotowego narzędzia. Wszystkie metody są statyczne tzn. nie trzeba tworzyć obiektu, aby ich użyć. Większość funkcji jest importowane z WinAPI, zatem: Zadziała nawet na Windows 2000 / XP. Można tej klasy używać w C++, po drobnych modyfikacjach składni, bez importowania funkcji. (nagłówek windows.h) using System.Windows.Forms; using System.Drawing; namespace my_program { public class MyMouse { [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool SetCursorPos(int x, int y); [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); private const int MOUSEEVENTF_LEFTDOWN = 0x02; private const int MOUSEEVENTF_LEFTUP = 0x04; private const int MOUSEEVENTF_RIGHTDOWN = 0x08; private const int MOUSEEVENTF_RIGHTUP = 0x10; public static void LeftMouseClick() { mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); } public static void LeftDoubleClick() { LeftMouseClick(); LeftMouseClick(); } public static void RightMouseClick() { mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); } public static void DragNDrop(int dragx, int dragy, int dropx, int dropy) { SetCursorPos(dragx, dragy); mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); SetCursorPos(dropx, dropy); mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); } public static void GetCursorPos(out int x, out int y) { x = Cursor.Position.X; y = Cursor.Position.Y; } public static Point GetCursorPos() { return Cursor.Position; } } } Zakładając, że klasa jest w tej samej przestrzeni nazw co plik źródłowy w którym chcemy jej użyć, możemy jej użyć w przykładowy sposób: MyMouse.SetCursorPos(300,300); MyMouse.LeftMouseClick(); MyMouse.DragNDrop(100,100,200,200); Link to comment https://cskatowice.com/topic/17925-cc-klasa-do-symulacji-akcji-myszki/ Share on other sites More sharing options...
The Shy 5 Last activity: October 12, 2016 Posted September 16, 2016 Share Posted September 16, 2016 Posted September 16, 2016 Hej! Nie widzisz zawartości tego postu? Sign In lub Create an account, aby korzystać ze wszystkich dostępnych funkcji! Link to comment https://cskatowice.com/topic/17925-cc-klasa-do-symulacji-akcji-myszki/#findComment-111754 Share on other sites More sharing options...
Recommended Posts