2012-10-24 54 views
0

可以說我有窗口的處理程序包含選定的文本..使用SendMessage函數

獲取選定的文本,我需要的是從這個窗口中所選文本抓取。

我知道我們可以做到這一點使用的SendMessage()APIEM_GETSEL恆定的,但我真的很困惑該怎麼做!

回答

0

WM_GETTEXT信息將幫助你 -

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

namespace TextFocusedns 
{ 
    public partial class TextFocusedFrm : Form 
    { 
     #region APIs 

     [DllImport("user32.dll")] 
     public static extern bool GetCursorPos(out Point pt); 

     [DllImport("user32.dll", EntryPoint = "WindowFromPoint", CharSet = CharSet.Auto, ExactSpelling = true)] 
     public static extern IntPtr WindowFromPoint(Point pt); 

     [DllImport("user32.dll", EntryPoint = "SendMessageW")] 
     public static extern int SendMessageW([InAttribute] System.IntPtr hWnd, int Msg, int wParam, IntPtr lParam); 
     public const int WM_GETTEXT = 13; 

     [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] 
     internal static extern IntPtr GetForegroundWindow(); 

     [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] 
     internal static extern IntPtr GetFocus(); 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    internal static extern int GetWindowThreadProcessId(int handle, out int processId); 

    [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] 
    internal static extern int AttachThreadInput(int idAttach, int idAttachTo, bool fAttach); 
    [DllImport("kernel32.dll")] 
    internal static extern int GetCurrentThreadId(); 

    [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] 
    internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount); 

    #endregion 

    private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer() { Interval = 100, Enabled = true }; 

    public TextFocusedFrm() 
    { 
     InitializeComponent(); 
    } 

    private void TextFocusedFrm_Load(object sender, EventArgs e) 
    { 
     timer.Tick += new EventHandler(timer_Tick); 
     timer.Start(); 
    } 

    void timer_Tick(object sender, EventArgs e) 
    { 
     try 
     { 
      MultiLineTextBox.Text = GetTextFromFocusedControl(); 
     } 
     catch (Exception exp) 
     { 
      MultiLineTextBox.Text += exp.Message; 
     } 
    } 

    //Get the text of the focused control 
    private string GetTextFromFocusedControl() 
    { 
     try 
     { 
      int activeWinPtr = GetForegroundWindow().ToInt32(); 
      int activeThreadId = 0, processId; 
      activeThreadId = GetWindowThreadProcessId(activeWinPtr, out processId); 
      int currentThreadId = GetCurrentThreadId(); 
      if (activeThreadId != currentThreadId) 
       AttachThreadInput(activeThreadId, currentThreadId, true); 
      IntPtr activeCtrlId = GetFocus(); 

      return GetText(activeCtrlId); 
     } 
     catch (Exception exp) 
     { 
      return exp.Message; 
     } 
    } 

    //Get the text of the control at the mouse position 
    private string GetTextFromControlAtMousePosition() 
    { 
     try 
     { 
      Point p; 
      if (GetCursorPos(out p)) 
      { 
       IntPtr ptr = WindowFromPoint(p); 
       if (ptr != IntPtr.Zero) 
       { 
        return GetText(ptr); 
       } 
      } 
      return ""; 
     } 
     catch (Exception exp) 
     { 
      return exp.Message; 
     } 
    } 

    //Get the text of a control with its handle 
    private string GetText(IntPtr handle) 
    { 
     int maxLength = 512; 
     IntPtr buffer = Marshal.AllocHGlobal((maxLength + 1) * 2); 
     SendMessageW(handle, WM_GETTEXT, maxLength, buffer); 
     string w = Marshal.PtrToStringUni(buffer); 


     Marshal.FreeHGlobal(buffer); 
      return w; 
     } 
    } 
} 
+0

謝謝你,但你是什麼意思由 「aText =空間(lTextlen);」? 因爲編譯器不知道Space()是。 和該行: [的DllImport( 「USER32」)]公共靜態外部INT的SendMessage(BYVAL HWND作爲整數,BYVAL WMSG作爲整數,BYVAL wParam參數作爲整數,爲ByRef lParam的作爲整數) ------- -------------------------- 由於變量類型導致編譯錯誤 – blackgh

+0

我已經包含應用程序的代碼示例,它將查找在老鼠位置下的文本。請參閱上面的申請樣本。 –

+0

'AttachThreadInput' - 你不需要用'false'參數第二次調用它嗎? – Qwertiy