Answered by:
Sendmessage Retain existing Text

Question
-
I'm sending some text to another process via SendMessage to a WF textBox.
Is there anyway to retain the text that is already in the control and append it with the new SendMessage it recieves?
SendMessge now just overwrites the existing text with the new string.
Answers
-
Hello,
Since you have successes in sending the message to a WinForm application, I think that you are using WM_SETTEXT (equals 0x000C) messages to the TextBox in another application. If you want to append the text in the TextBox, and not just set the new string value, I think we could override the message loop function of the WinForm application. Just like the following codes:
Sender:
private
const int WM_SETTEXT = 0x000C;
[
DllImport("User32.dll")]
public static extern IntPtr FindWindow(
string lpClassName,
string lpWindowName);
[DllImport("User32.dll")]
public static extern IntPtr FindWindowEx(
IntPtr hwndParent,
IntPtr hwndChildAfter,
string lpszClass,
string lpszWindos);[
DllImport("User32.dll")]
public static extern Int32 SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
IntPtr wParam, // first message parameter
String lParam); // second message parameter
string lpszParentClass = "WindowsForms10.Window.8.app.0.3b95145";
string lpszParentWindow = "GetMessageForm";
IntPtr ParenthWnd = new IntPtr(0);
string text = "test";
ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow);
if (!ParenthWnd.Equals(IntPtr.Zero))
{
SendMessage(ParenthWnd, WM_SETTEXT, (IntPtr)1, text);
}
Receicer:private
const int WM_SETTEXT = 0x000C;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SETTEXT)
{
if (m.WParam == (IntPtr)1)
this.Text += Marshal.PtrToStringAuto(m.LParam);
}
else
base.WndProc(ref m);
}
If the WinForm application is not an application you can modify, I thin you could use the following codes instead. Send three messages, WM_GETTEXTLENGTH, WM_GETTEXT and WM_SETTEXT. Important to mention, we should define two SendMessage here. Then you could get the text in another application's TextBox, and send the new string value to the TextBox. For detail, please check the codes here:
private const int WM_GETTEXT = 0x000D;
private const int WM_SETTEXT = 0x000C;
private const int WM_GETTEXTLENGTH = 0x000E; [DllImport("User32.dll")]
public static extern Int32 SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
IntPtr wParam, // first message parameter
StringBuilder lParam);[
DllImport("User32.dll")]
public static extern Int32 SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam); // second message parameter string lpszParentClass = "WindowsForms10.Window.8.app.0.3b95145";
string lpszParentWindow = "GetMessageForm";
string lpszClass = "WindowsForms10.EDIT.app.0.3b95145";
IntPtr ParenthWnd = new IntPtr(0);
IntPtr EdithWnd = new IntPtr(0); string text = "test";
ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow);
if (!ParenthWnd.Equals(IntPtr.Zero))
{
EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, "");
if (!EdithWnd.Equals(IntPtr.Zero))
{
int length = SendMessage(EdithWnd, WM_GETTEXTLENGTH, (IntPtr)0, (IntPtr)0);
if (length > 0)
{
StringBuilder sb = new StringBuilder(length);
int numChars = SendMessage(EdithWnd, WM_GETTEXT, (IntPtr)(length + 1), sb);
if (numChars > 0)
{
sb.Append(text);
SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, sb);\
}
}
else
SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, new StringBuilder(text));
}
else
MessageBox.Show("No child form found!");
}
else
MessageBox.Show("No parent form found!");
Thanks,
Best Regards,
Lingzhi
Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6- Edited by Michael Sun [MSFT]Microsoft employee Wednesday, September 10, 2008 11:02 AM add another method
- Marked as answer by Michael Sun [MSFT]Microsoft employee Thursday, September 11, 2008 10:42 AM