我有一套方法,使用pin语打WM_。GETTEXT on another program s text Box - and it work fair well, but取回其末尾所附的黄麻全文。 (原案文始终未变)
这是随机的,但我无法按要求复制,但经常需要停职。
这里是获取信息的文本。
System.Text.StringBuilder strBuffer = new System.Text.StringBuilder();
int nLen = 0;
bool nUpdated = false;
try
{
this.isOpen = false;
if (ptrHandle == null)
return;
if (ptrHandle == IntPtr.Zero)
return;
nLen =
Converter.SendMessage(ptrHandle, Converter.WM_GETTEXTLENGTH, 0, 0);
if (nLen <= 0)
return;
if (nPreviousLen != nLen)
nUpdated = true;
if (nUpdated)
{
System.Diagnostics.Debug.WriteLine("nLen: {0}", nLen);
strBuffer = new System.Text.StringBuilder(null, nLen + 1);
System.Diagnostics.Debug.WriteLine("strBuffer: {0}", strBuffer.ToString());
int sLen = Converter.SendMessageByString(ptrHandle, Converter.WM_GETTEXT, nLen
, strBuffer);
System.Diagnostics.Debug.WriteLine("sLen: {0}", sLen);
System.Diagnostics.Debug.WriteLine("
strBuffern
{0}", strBuffer.ToString());
strBuffer = new System.Text.StringBuilder(strBuffer.ToString().Left(sLen));
System.Diagnostics.Debug.WriteLine("
sLenBuffer
{0}", strBuffer.ToString());
source = new Special.IO.TextReader(
new System.IO.MemoryStream( System.Text.Encoding.Default.GetBytes(strBuffer.ToString() ) ), nUpdated );
}
}
}
/// <summary>
/// Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.
/// <br />
/// To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function. To post a message to a thread s message queue and return immediately, use the PostMessage or PostThreadMessage function.
/// </summary>
/// <param name="hWnd">
/// Handle to the window whose window procedure will receive the message.
/// If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
/// </param>
/// <param name="Msg">
/// [in] Specifies the message to be sent.
/// </param>
/// <param name="wParam">
/// [in] Specifies additional message-specific information.
/// </param>
/// <param name="lParam">
/// [in] Specifies additional message-specific information.
/// </param>
/// <returns>
/// The return value specifies the result of the message processing; it depends on the message sent.
/// </returns>
[DllImport("user32.dll", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = false)]
internal static extern int SendMessageByString(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
/// <summary>
/// Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.
/// <br />
/// To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function. To post a message to a thread s message queue and return immediately, use the PostMessage or PostThreadMessage function.
/// </summary>
/// <param name="hWnd">
/// Handle to the window whose window procedure will receive the message.
/// If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
/// </param>
/// <param name="Msg">
/// [in] Specifies the message to be sent.
/// </param>
/// <param name="wParam">
/// [in] Specifies additional message-specific information.
/// </param>
/// <param name="lParam">
/// [in] Specifies additional message-specific information.
/// </param>
/// <returns>
/// The return value specifies the result of the message processing; it depends on the message sent.
/// </returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);