2012-04-27 13 views
16

क्या किसी अन्य विंडो में प्रोग्रामेटिक रूप से उस स्थान पर माउस को स्थानांतरित किए बिना किसी स्थान पर क्लिक करना संभव है और यहां तक ​​कि यदि विंडो शीर्ष पर नहीं है? मैं किसी स्थान पर माउस क्लिक को अनुकरण करने के लिए किसी अन्य विंडो पर एक प्रकार का संदेश भेजना चाहता हूं।प्रोग्रामेटिक माउस एक और विंडो में क्लिक करें

मैं PostMessage साथ यह पूरा करने की कोशिश की:

private static IntPtr CreateLParam(int LoWord, int HiWord) 
{ 
    return (IntPtr)((HiWord << 16) | (LoWord & 0xffff)); 
} 

समस्या यह है कि खिड़की उसके स्थान पर बंद कर दिया जाता है:

PostMessage(WindowHandle, 0x201, IntPtr.Zero, CreateLParam(300,300)); 
PostMessage(WindowHandle, 0x202, IntPtr.Zero, CreateLParam(300,300)); 

मैं CreateLParam समारोह इस तरह से बनाया है। मुझे लगता है कि मेरा आवेदन (1,1) समन्वय पर क्लिक करता है। क्या कुछ इस समस्या के साथ मेरी मदद कर सकते हैं?

संपादित करें:

[return: MarshalAs(UnmanagedType.Bool)] 
[DllImport("user32.dll")] 
public static extern bool PostMessage(IntPtr WindowHandle, int Msg, IntPtr wParam, IntPtr lParam); 

और 0x201 और 0x202 WM_LBUTTONDOWN और WM_LBUTTONUP प्राधिकरण हैं: यह PostMessage है।

+0

क्या यह एक और विंडो है जिसे आप नियंत्रित करते हैं? यदि नहीं, तो यह एक बहुत ही अजीब अनुरोध की तरह लगता है। – Tejs

+0

आप किस प्रोग्राम पर क्लिक करने की कोशिश कर रहे हैं? कुछ कार्यक्रम (ज्यादातर गेम) में आप जो करने की कोशिश कर रहे हैं उसे अनदेखा करने के लिए तंत्र हैं और इसे अनदेखा करते हैं। उस मामले में आपका सबसे अच्छा मौका, गेम को सबसे ज्यादा बनाने के लिए WinApi का उपयोग कर रहा है, माउस को ले जाएं, क्लिक करें, माउस को वापस ले जाएं, गेम को पिछले जेड ऑर्डर पर वापस ले जाएं। – SimpleVar

+0

इसके अलावा, 'HiWord' को' uint' 'करने से पहले '<< 16' – SimpleVar

उत्तर

22

आप संदेश भेजकर ऐसा नहीं कर सकते हैं, इसके बजाय SendInput Windows API का उपयोग करें।

कॉल विधि ClickOnPoint, यह फॉर्म क्लिक ईवेंट से एक उदाहरण है, इसलिए this.handle फॉर्म हैंडल है, ध्यान दें कि ये खिड़की चुड़ैल हैंडल पर क्लाइंट निर्देशांक भेजते हैं, आप आसानी से इसे बदल सकते हैं और स्क्रीन निर्देशांक भेज सकते हैं, और उस मामले में आपको नीचे हैंडल या क्लाइंटटस्क्रीन कॉल की आवश्यकता नहीं है।

ClickOnPoint(this.Handle, new Point(375, 340)); 

अद्यतन: अब SendInput का उपयोग करके, टीएनएक्स टॉम।

बीटीडब्ल्यू। मैं केवल इस नमूने के लिए आवश्यक घोषणाओं का इस्तेमाल किया है, और अधिक कुछ भी करने के लिए वहाँ एक अच्छा पुस्तकालय है: Windows Input Simulator (C# SendInput Wrapper - Simulate Keyboard and Mouse)

public class ClickOnPointTool 
    { 

    [DllImport("user32.dll")] 
    static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint); 

    [DllImport("user32.dll")] 
    internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize); 

#pragma warning disable 649 
    internal struct INPUT 
    { 
     public UInt32 Type; 
     public MOUSEKEYBDHARDWAREINPUT Data; 
    } 

    [StructLayout(LayoutKind.Explicit)] 
    internal struct MOUSEKEYBDHARDWAREINPUT 
    { 
     [FieldOffset(0)] 
     public MOUSEINPUT Mouse; 
    } 

    internal struct MOUSEINPUT 
    { 
     public Int32 X; 
     public Int32 Y; 
     public UInt32 MouseData; 
     public UInt32 Flags; 
     public UInt32 Time; 
     public IntPtr ExtraInfo; 
    } 

#pragma warning restore 649 


    public static void ClickOnPoint(IntPtr wndHandle , Point clientPoint) 
    { 
     var oldPos = Cursor.Position; 

     /// get screen coordinates 
     ClientToScreen(wndHandle, ref clientPoint); 

     /// set cursor on coords, and press mouse 
     Cursor.Position = new Point(clientPoint.X, clientPoint.Y); 

     var inputMouseDown = new INPUT(); 
     inputMouseDown.Type = 0; /// input type mouse 
     inputMouseDown.Data.Mouse.Flags = 0x0002; /// left button down 

     var inputMouseUp = new INPUT(); 
     inputMouseUp.Type = 0; /// input type mouse 
     inputMouseUp.Data.Mouse.Flags = 0x0004; /// left button up 

     var inputs = new INPUT[] { inputMouseDown, inputMouseUp }; 
     SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT))); 

     /// return mouse 
     Cursor.Position = oldPos; 
    } 

    } 
+0

धन्यवाद करने का प्रयास करें, यह बहुत अच्छा काम करता है। मैंने यह भी कहा कि क्लिक होने पर विंडो सक्रिय हो जाती है। – Dagob

+0

'प्वाइंट' (क्या नामस्थान) क्या है? –

+1

सिस्टम। ड्रॉइंग (http://msdn.microsoft.com/en-us/library/system.drawing.point.aspx) –

13

मैंने पहले भी पाया, विंडोज मीडिया को संदेश भेजने के लिए प्लेयर एक तरह से तो मैं का उपयोग किया जाता अनुकरण करने के लिए मैं चाहता था आवेदन में क्लिक करें!

विंडो ढूंढने के लिए this class (नीचे कोड) का उपयोग करके और इच्छित संदेश भेजने के लिए!

using System; 
using System.Runtime.InteropServices; 

namespace Mouse_Click_Simulator 
{ 
    /// <summary> 
    /// Summary description for Win32. 
    /// </summary> 
    public class Win32 
    { 
     // The WM_COMMAND message is sent when the user selects a command item from 
     // a menu, when a control sends a notification message to its parent window, 
     // or when an accelerator keystroke is translated. 
     public const int WM_KEYDOWN = 0x100; 
     public const int WM_KEYUP = 0x101; 
     public const int WM_COMMAND = 0x111; 
     public const int WM_LBUTTONDOWN = 0x201; 
     public const int WM_LBUTTONUP = 0x202; 
     public const int WM_LBUTTONDBLCLK = 0x203; 
     public const int WM_RBUTTONDOWN = 0x204; 
     public const int WM_RBUTTONUP = 0x205; 
     public const int WM_RBUTTONDBLCLK = 0x206; 

     // The FindWindow function retrieves a handle to the top-level window whose 
     // class name and window name match the specified strings. 
     // This function does not search child windows. 
     // This function does not perform a case-sensitive search. 
     [DllImport("User32.dll")] 
     public static extern int FindWindow(string strClassName, string strWindowName); 

     // The FindWindowEx function retrieves a handle to a window whose class name 
     // and window name match the specified strings. 
     // The function searches child windows, beginning with the one following the 
     // specified child window. 
     // This function does not perform a case-sensitive search. 
     [DllImport("User32.dll")] 
     public static extern int FindWindowEx(
      int hwndParent, 
      int hwndChildAfter, 
      string strClassName, 
      string strWindowName); 


     // The SendMessage function sends the specified message to a window or windows. 
     // It calls the window procedure for the specified window and does not return 
     // until the window procedure has processed the message. 
     [DllImport("User32.dll")] 
     public static extern Int32 SendMessage(
      int hWnd,    // handle to destination window 
      int Msg,    // message 
      int wParam,    // first message parameter 
      [MarshalAs(UnmanagedType.LPStr)] string lParam); // second message parameter 

     [DllImport("User32.dll")] 
     public static extern Int32 SendMessage(
      int hWnd,    // handle to destination window 
      int Msg,    // message 
      int wParam,    // first message parameter 
      int lParam);   // second message parameter 
    } 
} 

उदाहरण के लिए:

Win32.SendMessage(iHandle, Win32.WM_LBUTTONDOWN, 0x00000001, 0x1E5025B); 

Here's My Application Source Code कि मैं एक विशिष्ट अंतराल में "BlueStacks" आवेदन में ऑटो क्लिक करने के लिए बनाया गया!

FindWindow, wParam, lParam आदि के लिए, आप मुझसे पूछ सकते हैं कि यह कैसे करना है! यह बहुत कठिन नहीं है :); आशा है कि इससे मदद मिलेगी! :)

+0

जैसा लगता है, माउस क्लिक पोस्ट किए जाते हैं, नहीं भेजे जाते हैं। PostMessage एक बेहतर समाधान होगा। कम से कम Spy ++ मेरे लिए दावा करता है कि क्लिक पोस्ट किए गए हैं। – Tom

+0

यहां ब्लूस्टैक्स पर क्लिक भेजने की कोशिश कर रहा था, निराश नहीं था! – RW4

+1

क्लिक के लिए कॉर्ड कैसे प्रदान करें? मुझे किसी स्थिति पर क्लिक करने की आवश्यकता है, न केवल अज्ञात पर क्लिक करें। – Kosmos

संबंधित मुद्दे