Click here to Skip to main content
15,885,244 members
Home / Discussions / C#
   

C#

 
AnswerRe: http://www.sourceforge.net/ Pin
Yusuf6-Jun-09 11:19
Yusuf6-Jun-09 11:19 
GeneralRe: http://www.sourceforge.net/ Pin
I Believe In GOD6-Jun-09 11:55
I Believe In GOD6-Jun-09 11:55 
QuestionPlease please please please ... Pin
IceWater426-Jun-09 9:55
IceWater426-Jun-09 9:55 
AnswerRe: Please please please please ... Pin
Dave Kreskowiak6-Jun-09 10:15
mveDave Kreskowiak6-Jun-09 10:15 
GeneralRe: Please please please please ... Pin
IceWater426-Jun-09 22:02
IceWater426-Jun-09 22:02 
GeneralRe: Please please please please ... Pin
Dave Kreskowiak7-Jun-09 7:15
mveDave Kreskowiak7-Jun-09 7:15 
AnswerRe: Please please please please ... Pin
EliottA6-Jun-09 12:59
EliottA6-Jun-09 12:59 
AnswerRe: Please please please please ... Pin
FocusedWolf6-Jun-09 18:05
FocusedWolf6-Jun-09 18:05 
hmm i think i tried using sendinput and found it sucked. I like using send message because you dont need to actually move the mouse about to click... you can send the coordinates of where a click is to occur... much more sexy right. Their might be problems or something with some windows and i think i had to have the window focused to get it to actually work.... unfortunatelly its not as nice as when doing sendmessage with keystrokes because in that case theirs no need to have the window focused... good for my bot... had it in the background doing stuff while i used the computer lol...

anyway here's what i had for using sendmessage (i think i had code for sendinput and deleted it because while it worked, it required both focus of the window and ownership of the moues cursor):

<br />
using System;<br />
using System.Text;<br />
using System.Runtime.InteropServices;<br />
using System.Drawing;<br />
using System.Windows.Forms;<br />
using System.Threading;<br />
<br />
namespace MouseKeyboardLibrary<br />
{<br />
    /// <summary><br />
    /// Operations that simulate mouse events<br />
    /// </summary><br />
    public class MouseSimulatorMessenger<br />
    {<br />
<br />
        [DllImport("user32.dll")]<br />
        static extern IntPtr GetForegroundWindow();<br />
<br />
        [DllImport("user32.dll")]<br />
        private static extern int SetForegroundWindow(IntPtr hWnd);<br />
<br />
<br />
<br />
<br />
        //const int MK_LBUTTON = 0x1;<br />
        //const int MK_RBUTTON = 0x2;<br />
        //const int MK_SHIFT = 0x4;<br />
        //const int MK_CONTROL = 0x8;<br />
        //const int MK_MBUTTON = 0x10;<br />
        //const int MK_XBUTTON1 = 0x20;<br />
        //const int MK_XBUTTON2 = 0x40;<br />
<br />
        static IntPtr? handle = IntPtr.Zero;<br />
        public IntPtr? Handle<br />
        {<br />
            get { return handle; }<br />
            set { handle = value; }<br />
        }<br />
<br />
        #region Windows API Code<br />
<br />
        //x -> loword<br />
        //y -> hiword<br />
        private static IntPtr MakeLParam(int LoWord, int HiWord)<br />
        {<br />
            return (IntPtr)((HiWord << 16) | (LoWord & 0xffff)); //Int32 lParam = x + y * 0x00010000<br />
            //return (IntPtr)(LoWord + HiWord * 0x00010000);<br />
        }<br />
<br />
        const int WM_LBUTTONDOWN = 0x0201;<br />
        const int WM_LBUTTONUP = 0x0202;<br />
<br />
        const int WM_MBUTTONDOWN = 0x207;<br />
        const int WM_MBUTTONUP = 0x208;<br />
<br />
        const int WM_RBUTTONDOWN = 0x0204;<br />
        const int WM_RBUTTONUP = 0x0205;<br />
<br />
        [return: MarshalAs(UnmanagedType.Bool)]<br />
        [DllImport("user32.dll", SetLastError = true)]<br />
        static extern bool SendMessageA(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);<br />
<br />
<br />
//PostMessage(hWndButton, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(1,0));<br />
//PostMessage(hWndButton, WM_LBUTTONUP, 0, MAKELPARAM(1,0));<br />
<br />
        private static int MouseButtonDownInteger(MouseButton button)<br />
        {<br />
            switch (button)<br />
            {<br />
                case MouseButton.Left:<br />
                    return WM_LBUTTONDOWN;<br />
<br />
                case MouseButton.Middle:<br />
                    return WM_MBUTTONDOWN;<br />
<br />
                case MouseButton.Right:<br />
                    return WM_RBUTTONDOWN;<br />
            }<br />
<br />
            throw new ArgumentException("button");<br />
        }<br />
<br />
        private static int MouseButtonUpInteger(MouseButton button)<br />
        {<br />
            switch (button)<br />
            {<br />
                case MouseButton.Left:<br />
                    return WM_LBUTTONUP;<br />
<br />
                case MouseButton.Middle:<br />
                    return WM_MBUTTONUP;<br />
<br />
                case MouseButton.Right:<br />
                    return WM_RBUTTONUP;<br />
            }<br />
<br />
            throw new ArgumentException("button");<br />
        }<br />
<br />
        #endregion<br />
<br />
        #region Methods<br />
<br />
        /// <summary><br />
        /// Press a mouse button down<br />
        /// </summary><br />
        /// <param name="button"></param><br />
        public void MouseDown(MouseButton button, int x, int y)<br />
        {<br />
            //IntPtr oldFocus = GetForegroundWindow();<br />
<br />
            if(handle.HasValue)<br />
                SendMessageA(handle.Value, MouseButtonDownInteger(button), 0, MakeLParam(x, y));<br />
            else<br />
                throw new ArgumentNullException("handle");<br />
<br />
            //SetForegroundWindow(oldFocus);<br />
        }<br />
<br />
        /// <summary><br />
        /// Let a mouse button up<br />
        /// </summary><br />
        /// <param name="button"></param><br />
        public void MouseUp(MouseButton button, int x, int y)<br />
        {<br />
            //IntPtr oldFocus = GetForegroundWindow();<br />
<br />
            if (handle.HasValue)<br />
                SendMessageA(handle.Value, MouseButtonUpInteger(button), 0, MakeLParam(x, y));<br />
            else<br />
                throw new ArgumentNullException("handle");<br />
<br />
            //SetForegroundWindow(oldFocus);<br />
        }<br />
<br />
        /// <summary><br />
        /// Press a mouse button down<br />
        /// </summary><br />
        /// <param name="button"></param><br />
        public void MouseDown(MouseButtons button, int x, int y)<br />
        {<br />
            switch (button)<br />
            {<br />
                case MouseButtons.Left:<br />
                    MouseDown(MouseButton.Left, x, y);<br />
                    break;<br />
                case MouseButtons.Middle:<br />
                    MouseDown(MouseButton.Middle, x, y);<br />
                    break;<br />
                case MouseButtons.Right:<br />
                    MouseDown(MouseButton.Right, x, y);<br />
                    break;<br />
            }<br />
        }<br />
<br />
        /// <summary><br />
        /// Let a mouse button up<br />
        /// </summary><br />
        /// <param name="button"></param><br />
        public void MouseUp(MouseButtons button, int x, int y)<br />
        {<br />
            switch (button)<br />
            {<br />
                case MouseButtons.Left:<br />
                    MouseUp(MouseButton.Left, x, y);<br />
                    break;<br />
                case MouseButtons.Middle:<br />
                    MouseUp(MouseButton.Middle, x, y);<br />
                    break;<br />
                case MouseButtons.Right:<br />
                    MouseUp(MouseButton.Right, x, y);<br />
                    break;<br />
            }<br />
        }<br />
<br />
        /// <summary><br />
        /// Click a mouse button (down then up)<br />
        /// </summary><br />
        /// <param name="button"></param><br />
        public void Click(MouseButton button, int x, int y)<br />
        {<br />
            IntPtr oldFocus = GetForegroundWindow();<br />
<br />
            MouseDown(button, x, y);<br />
<br />
            Thread.Sleep(200);<br />
<br />
            MouseUp(button, x, y);<br />
<br />
            SetForegroundWindow(oldFocus);<br />
        }<br />
<br />
        /// <summary><br />
        /// Click a mouse button (down then up)<br />
        /// </summary><br />
        /// <param name="button"></param><br />
        public void Click(MouseButtons button, int x, int y)<br />
        {<br />
            switch (button)<br />
            {<br />
                case MouseButtons.Left:<br />
                    Click(MouseButton.Left, x, y);<br />
                    break;<br />
                case MouseButtons.Middle:<br />
                    Click(MouseButton.Middle, x, y);<br />
                    break;<br />
                case MouseButtons.Right:<br />
                    Click(MouseButton.Right, x, y);<br />
                    break;<br />
            }<br />
        }<br />
<br />
        /// <summary><br />
        /// Double click a mouse button (down then up twice)<br />
        /// </summary><br />
        /// <param name="button"></param><br />
        public void DoubleClick(MouseButton button, int x, int y)<br />
        {<br />
            Click(button, x, y);<br />
<br />
            Thread.Sleep(200);<br />
<br />
            Click(button, x, y);<br />
        }<br />
<br />
        /// <summary><br />
        /// Double click a mouse button (down then up twice)<br />
        /// </summary><br />
        /// <param name="button"></param><br />
        public void DoubleClick(MouseButtons button, int x, int y)<br />
        {<br />
            switch (button)<br />
            {<br />
                case MouseButtons.Left:<br />
                    DoubleClick(MouseButton.Left, x, y);<br />
                    break;<br />
                case MouseButtons.Middle:<br />
                    DoubleClick(MouseButton.Middle, x, y);<br />
                    break;<br />
                case MouseButtons.Right:<br />
                    DoubleClick(MouseButton.Right, x, y);<br />
                    break;<br />
            }<br />
        }<br />
<br />
        #endregion<br />
    }<br />
}<br />

GeneralRe: Please please please please ... Pin
IceWater426-Jun-09 21:42
IceWater426-Jun-09 21:42 
GeneralRe: Please please please please ... Pin
FocusedWolf7-Jun-09 9:09
FocusedWolf7-Jun-09 9:09 
GeneralRe: Please please please please ... Pin
David OBrien12-Oct-09 4:12
David OBrien12-Oct-09 4:12 
GeneralRe: Please please please please ... Pin
FocusedWolf5-Oct-09 11:45
FocusedWolf5-Oct-09 11:45 
Questionabout Array.Clone(), a shallow copy? Pin
Seraph_summer6-Jun-09 6:47
Seraph_summer6-Jun-09 6:47 
AnswerRe: about Array.Clone(), a shallow copy? Pin
Luc Pattyn6-Jun-09 6:57
sitebuilderLuc Pattyn6-Jun-09 6:57 
GeneralRe: about Array.Clone(), a shallow copy? Pin
Seraph_summer6-Jun-09 7:34
Seraph_summer6-Jun-09 7:34 
GeneralRe: about Array.Clone(), a shallow copy? Pin
Luc Pattyn6-Jun-09 8:11
sitebuilderLuc Pattyn6-Jun-09 8:11 
GeneralRe: about Array.Clone(), a shallow copy? Pin
Seraph_summer6-Jun-09 8:27
Seraph_summer6-Jun-09 8:27 
AnswerRe: about Array.Clone(), a shallow copy? Pin
Dave Kreskowiak6-Jun-09 10:12
mveDave Kreskowiak6-Jun-09 10:12 
GeneralRe: about Array.Clone(), a shallow copy? Pin
Seraph_summer6-Jun-09 10:37
Seraph_summer6-Jun-09 10:37 
QuestionError in some client machines with Microsoft.mshtml Pin
ManojKumar196-Jun-09 6:41
ManojKumar196-Jun-09 6:41 
AnswerRe: Error in some client machines with Microsoft.mshtml Pin
Dave Kreskowiak6-Jun-09 10:08
mveDave Kreskowiak6-Jun-09 10:08 
GeneralRe: Error in some client machines with Microsoft.mshtml Pin
ManojKumar197-Jun-09 5:46
ManojKumar197-Jun-09 5:46 
GeneralRe: Error in some client machines with Microsoft.mshtml Pin
Dave Kreskowiak7-Jun-09 7:16
mveDave Kreskowiak7-Jun-09 7:16 
QuestionRegarding Notepad application Pin
Devi526-Jun-09 5:55
Devi526-Jun-09 5:55 
AnswerRe: Regarding Notepad application Pin
0x3c06-Jun-09 6:04
0x3c06-Jun-09 6:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.