NguyenThai 0 Newbie Poster

Hi everyone.

Right now I'm working with my project. This project will be the program to stimulate mouse click event. I already finished the basic of program, like using mouse_event() but I need to improve it more than it had. Basically what I mean is I need to use the mouse click with the inactive windows, example like you have 2 application opened, I want to click at a button of program B without minimizing the program A (front of B). So I came with some API, and I found 2 API can help me to solve, 1 is PostMessage, and 1 is SendMessage. I tried with SendMessage, but nothing happened. Here my code sample. I tried to move the basic program which I wrote in VB into C# but it hard to translate this so I created new one which same structure but using different techniques

public partial class Form1 : Form
    {
        [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);

        [DllImport("USER32.DLL")]
        public static extern IntPtr FindWindow(string lpClassName,
            string lpWindowName);

        // Activate an application window.
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

        //private const int WM_RBUTTONUP = &H205; --> This line and the line after I tried to convert into C# but I don't know how
        //private const int WM_RBUTTONDOWN = &H204;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Get a handle
            IntPtr handle = FindWindow("ClassName", "Program");

            if (handle == IntPtr.Zero)
            {
                MessageBox.Show("Program is not running.");
                return;
            }

            SetForegroundWindow(handle);

            //Call the imported function with the cursor's current position
            //int X = Cursor.Position.X;
            //int Y = Cursor.Position.Y;
             //mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, X, Y, 0, 0); --> This work perfect but it has to use active windows so I use SendMessage
            SendMessage(handle, MOUSEEVENTF_RIGHTDOWN,(IntPtr)0, MakeLParam(700, 350));
            SendMessage(handle, MOUSEEVENTF_RIGHTUP, (IntPtr)0, MakeLParam(700, 350));
        }

        public static IntPtr MakeLParam(int wLow, int wHigh)
        {
            return (IntPtr)(((short)wHigh << 16) | (wLow & 0xffff));
        }
    }