2010-05-04 3 views
5

का उपयोग कर किसी भी विंडो से हाइलाइट किए गए टेक्स्ट को कैप्चर करें C# का उपयोग करके किसी भी विंडो से हाइलाइट/चयनित टेक्स्ट को कैसे पढ़ा जाए।सी #

मैंने 2 दृष्टिकोणों की कोशिश की।

  1. जब भी उपयोगकर्ता कुछ चीज़ चुनता है तो "^ सी" भेजें। लेकिन इस मामले में मेरा क्लिपबोर्ड बहुत अनावश्यक डेटा के साथ बाढ़ आ गया है। कभी-कभी यह पासवर्ड भी कॉपी करता है।

इसलिए मैंने दूसरी विधि में अपना दृष्टिकोण बदल दिया, संदेश विधि भेज दी।

इस नमूना कोड

[DllImport("user32.dll")] 
    static extern int GetFocus(); 

    [DllImport("user32.dll")] 
    static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach); 

    [DllImport("kernel32.dll")] 
    static extern uint GetCurrentThreadId(); 

    [DllImport("user32.dll")] 
    static extern uint GetWindowThreadProcessId(int hWnd, int ProcessId);  

    [DllImport("user32.dll") ] 
    static extern int GetForegroundWindow(); 

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] 
    static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam);  

    // second overload of SendMessage 

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

    const int WM_SETTEXT = 12; 
    const int WM_GETTEXT = 13;  

private string PerformCopy() 
    { 
     try 
     { 
      //Wait 5 seconds to give us a chance to give focus to some edit window, 
      //notepad for example 
      System.Threading.Thread.Sleep(5000); 
      StringBuilder builder = new StringBuilder(500); 

      int foregroundWindowHandle = GetForegroundWindow(); 
      uint remoteThreadId = GetWindowThreadProcessId(foregroundWindowHandle, 0); 
      uint currentThreadId = GetCurrentThreadId(); 

      //AttachTrheadInput is needed so we can get the handle of a focused window in another app 
      AttachThreadInput(remoteThreadId, currentThreadId, true); 
      //Get the handle of a focused window 
      int focused = GetFocus(); 
      //Now detach since we got the focused handle 
      AttachThreadInput(remoteThreadId, currentThreadId, false); 

      //Get the text from the active window into the stringbuilder 
      SendMessage(focused, WM_GETTEXT, builder.Capacity, builder); 

      return builder.ToString(); 
     } 
     catch (System.Exception oException) 
     { 
      throw oException; 
     } 
    } 

इस कोड को नोटपैड में ठीक काम कर रहा है देखते हैं। लेकिन अगर मैं मोज़िला फ़ायरफ़ॉक्स, या विजुअल स्टूडियो आईडीई जैसे किसी अन्य एप्लिकेशन से कैप्चर करने का प्रयास करता हूं, तो यह टेक्स्ट वापस नहीं कर रहा है।

क्या कोई मेरी मदद कर सकता है, जहां मैं गलत कर रहा हूं? सबसे पहले, मैंने सही दृष्टिकोण चुना है?

उत्तर

3

ऐसा इसलिए है क्योंकि फ़ायरफ़ॉक्स और विजुअल स्टूडियो दोनों टेक्स्ट प्रदर्शित/संपादित करने के लिए अंतर्निहित Win32 नियंत्रणों का उपयोग नहीं करते हैं।

यह संभव नहीं है सामान्य में क्योंकि तथ्य यह है कि कार्यक्रमों कर सकते हैं Win32 के अपने स्वयं के संस्करण को फिर से लागू की, की "किसी भी" चयनित पाठ मूल्य प्राप्त करने में सक्षम होने के लिए किसी भी तरह से वे मनचाहे ढंग नियंत्रण करता है और आपका कार्यक्रम संभवतः उन सभी के साथ काम करने की उम्मीद नहीं कर सकता है। इस तरह के दृश्य स्टूडियो और Firefox के रूप में - - यूआई के साथ काम होने की संभावना होगी

हालांकि, अगर आप UI Automation एपीआई जो आप बहुमत 3 पक्ष के नियंत्रण के के साथ बातचीत करने (कम से कम, सभी अच्छे की अनुमति देगा का उपयोग कर सकते ऑटोमेशन एपीआई क्योंकि यह पहुंच के लिए एक आवश्यकता है)

+0

फ़ायरफ़ॉक्स यूआई ऑटोमेशन को लागू नहीं करता है, मैंने संस्करण 3.0 की कोशिश की थी और यह काम नहीं किया था, उम्मीद है कि यह भविष्य में समर्थन कर सकता है। –

+0

क्या हम अपना पहला दृष्टिकोण सुधारने के लिए कुछ कर सकते हैं। अनावश्यक प्रतिलिपि को छोड़कर यह बहुत अच्छा काम कर रहा है ........ – Dinesh