יום שבת, 24 במאי 2014

Remote Control Application - Mouse




תוכנות לשליטה מרחוק הן כלי חיוני בניהול מחשבים, זה כלי תמיכה מעולה ולגיטמי ברוב המקרים אבל יש מקרים שזה עלול להיות כלי מסוכן שיכול לעלות לנו ביוקר, בסדרת המאמרים הקרובה אתמקד כיצד לבנות תוכנית שתאפשר לנו לשלוט במחשבים מבוססי Windows מרחוק, התוכנית מחולקת ל 2 חלקים כיאה לעבודה עם Sockets אבל לפני שנתחיל לכתוב את צד השרת והלקוח קודם נגדיר על איזה מנגנונים נאפשר שליטה.

המנגנון הראשון הוא העכבר, אנחנו רוצים להזיז וללחוץ עליו מרחוק, נגדיר מחלקה שדרכה ניגשים למשאבים של העכבר, בעזרת קריאה לפונקציה mouse_event נוכל לדמות אירועים של כפתורים, נוסיף פונקציות נוספות שדרכן נזיז את העכבר לנקודה במסך ונקבל את מיקום הסמן.

 mouseControl.cs

    public class mouseControl
    {
        /// <summary>
        /// mouse virtual buttons
        /// </summary>
        public enum mouseButtons
        {
            LEFT,
            RIGHT
        }

        //mouse event signature
        [DllImport("user32.dll", CharSet = CharSet.Auto, 
            CallingConvention = CallingConvention.StdCall)]

        public static extern void mouse_event(long dwFlags, long dx,
            long dy, long cButtons, long dwExtraInfo);

        //mouse event types, for more events visit this link
        //http://msdn.microsoft.com/en-us/library/windows/desktop/ms646260(v=vs.85).aspx
        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

        /// <summary>
        /// raise mouse click event
        /// </summary>
        /// <param name="btn">left / right buttons</param>
        public void mouseClick(mouseButtons btn)
        {
            switch (btn)
            {
                case mouseButtons.LEFT:
                    mouse_event(MOUSEEVENTF_LEFTDOWN | 
                        MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                    break;
                case mouseButtons.RIGHT:
                    mouse_event(MOUSEEVENTF_RIGHTDOWN |
                        MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// move the mouse to new location
        /// </summary>
        /// <param name="x">x position</param>
        /// <param name="y">y position</param>
        public void mouseMove(int x, int y)
        {
            Cursor.Position = new Point(x, y); 
        }

        /// <summary>
        /// get the cursor location on screen
        /// </summary>
        /// <returns>cursor location</returns>
        public Point screenLocation()
        {
            return Cursor.Position;
        }

    }


על מנת שנוכל לבדוק את המחלקה נבנה תוכנית מאוד פשוטה, מדובר על טופס שמכיל מספר כפתורים ושדות שדרכם נשלוט בעכבר, בנוסף נגדיר שעון שדוגם כל 100ms את המיקום הנוכחי של העכבר ע"ג המסך, המטרה של התוכנית היא קודם כל לבדוק את הקוד מקומית ואח"כ מרחוק כפי שנראה במאמרים הבאים.




   mouseForm.cs

    public partial class mouseForm : Form
    {
        mouseControl c;

        public mouseForm()
        {
            InitializeComponent();
        }

        private void mouseForm_Load(object sender, EventArgs e)
        {
            txb_x.Text = "0";
            txb_y.Text = "0";
            c = new mouseControl();
        }

        /// <summary>
        /// timer for polling the location every 100ms
        /// </summary>
        private void timer1_Tick(object sender, EventArgs e)
        {
            Point p = c.screenLocation();

            lbl_x.Text = "X:" + p.X.ToString();
            lbl_y.Text = "Y:" + p.Y.ToString();
        }

        /// <summary>
        /// getting the location of the cursor
        /// </summary>
        private void setLocation()
        {
            int x = 0, y = 0;

            int.TryParse(txb_x.Text, out x);
            int.TryParse(txb_y.Text, out y);

            c.mouseMove(x, y);
        }

        /// <summary>
        /// move the cursor by x and y
        /// </summary>
        private void btn_move_Click(object sender, EventArgs e)
        {
            //set the cursor location
            setLocation();
        }

        /// <summary>
        /// move the cursor and make double click on the left button
        /// </summary>
        private void btn_left_Click(object sender, EventArgs e)
        {
            //set the cursor location
            setLocation();

            //single click
            c.mouseClick(mouseControl.mouseButtons.LEFT);
            //add other request for double click
            c.mouseClick(mouseControl.mouseButtons.LEFT);
        }

        /// <summary>
        /// move the cursor and make double click on right button
        /// </summary>
        private void btn_right_Click(object sender, EventArgs e)
        {
            //set the cursor location
            setLocation();

            //single click
            c.mouseClick(mouseControl.mouseButtons.RIGHT);
            //add other request for double click
            c.mouseClick(mouseControl.mouseButtons.RIGHT);
        }
    }


סיכום

אז אחרי שיש שליטה על העכבר נעבור למנגנונים נוספים כמו המקלדת ולכידת מסך אח"כ נכתוב את צד השרת ואת צד הלקוח ונשלב בהם את המחלקות שבנינו יחד עם אובייקט מרכזי שיעבור בניהם.

תחילת של דלת סתרים....

אין תגובות:

הוסף רשומת תגובה