哦,简单。 [DllImport("user32.dll", EntryPoint="GetWindowText")] public static extern int GetWindowText ( int hwnd, string lpString, int cch ); 调用的时候这样: StringBuilder s = new StringBuilder(512); int i = GetWindowText(this.Handle, s, s.Capacity); MessageBox.Show(s.ToString());
是的,先WM_GETTEXT,取到的字符串,自己再追加吧,然后WM_SETTEXT
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] bs1 = converter.GetBytes("你好");
//---------------------------------------
//假设bs1是你GetWindowText得到的。前面的代码只是测试用的。
string textGot = converter.GetString(bs1);
string textSet = textGot + "吗?"; byte[] bytesToSent = converter.GetBytes(textSet);
//SetWindowText or SendMessage
[DllImport("user32.dll", EntryPoint="GetWindowText")]
public static extern int GetWindowText (
int hwnd,
string lpString,
int cch
);
调用的时候这样:
StringBuilder s = new StringBuilder(512);
int i = GetWindowText(this.Handle, s, s.Capacity);
MessageBox.Show(s.ToString());
private void lvContent_MouseDoubleClick(object sender, MouseEventArgs e)
{
string fileName = this.lvContent.SelectedItems[0].SubItems[2].Text;
int index = fileName.LastIndexOf(".");
string fileCategory = "";
if (index != -1)
fileCategory = fileName.Substring(index).ToLower();
if (fileCategory != ".txt")
{
return;
}
Process proc = new Process();
try
{
// 启动记事本
proc.StartInfo.FileName = "notepad.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
}
catch
{
proc = null;
}
// 传递数据给记事本
if (proc != null)
{
// 从数据库中查找
byte[] imgs;
try
{
int id = Convert.ToInt32(lvContent.SelectedItems[0].SubItems[0].Text);
string sql = string.Format("select documentContent from document where documentID ={0}", id);
DBHelper.conn.Open();
SqlCommand cmd = new SqlCommand(sql, DBHelper.conn);
cmd.CommandTimeout = 0;
imgs = (byte[])cmd.ExecuteScalar();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
finally
{
DBHelper.conn.Close();
} // 调用 API, 传递数据
while (proc.MainWindowHandle == IntPtr.Zero)
{
proc.Refresh();
}
IntPtr vHandle = FindWindowEx(proc.MainWindowHandle, IntPtr.Zero, "Edit", null);
string content = System.Text.Encoding.Default.GetString(imgs);
SendMessage(vHandle, WM_SETTEXT, 0, content);// 传递数据给记事本
}
} /// <summary>
/// 传递消息给记事本
/// </summary>
[DllImport("User32.DLL")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, string lParam); /// <summary>
/// 查找句柄
/// </summary>
[DllImport("User32.DLL")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); /// <summary>
/// 记事本需要的常量
/// </summary>
public const uint WM_SETTEXT = 0x000C;
#endregion