C# 创建一个无路径的记事本 并在编辑区添加字符串

Anod 2012-06-20 04:37:47
C# 创建一个无路径的记事本 并在编辑区添加字符串 调试半天找不到问题所在

//引用
using System.Runtime.InteropServices;
using System.Diagnostics;
//在类中声明外部函数
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
internal static extern IntPtr GetFocus();
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
internal static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll",EntryPoint="SendMessageA")]
internal static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wparam, string lparam);

// 声明消息标识
public const int WM_SETTEXT = 0x000C;
//在Button_click事件中添加以下代码
Process.Start("Notepad");
IntPtr myNotePad = IntPtr.Zero;
myNotePad = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "无标题 - 记事本", null);
IntPtr myNote = IntPtr.Zero;
myNote = FindWindowEx(myNotePad, IntPtr.Zero, "Edit", null);
//SendMessage(myNote, WM_SETTEXT, IntPtr.Zero, strData);
SendMessage(myNote, WM_SETTEXT, IntPtr.Zero, "display str");

// 现象是可以打开记事本,但不能添加字符串进去,调试时发现 myNotePad 始终=0,myNote 值是变化的六位数。
...全文
240 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dx_Happy 2013-10-10
  • 打赏
  • 举报
回复
这个始终是一行文字,写第二条就把第一条冲掉了
陈大欠 2013-03-25
  • 打赏
  • 举报
回复
引用 2 楼 Return_false 的回复:
click事件里内容修改如下C# code?123456789101112131415161718192021222324252627282930313233343536373839#region [ 启动记事本 ] System.Diagnostics.Process Proc; try ……
哈哈,谢谢这位哥们哈,帮了我大忙了.
Anod 2012-06-21
  • 打赏
  • 举报
回复
汗,修改的地方加红加不上。我没有用while,担心出错出不来。

不过有时候还是写入不进去,但没有任何 调试的时候可以写进去,猜测是Proc.Start()操作需要一定的时间开销,后面加上Sleep小时间后解决
Anod 2012-06-21
  • 打赏
  • 举报
回复
谢谢2L 不过还是不明白为什么FindwindowEx为什么找不到,之前试过使用MainWindowHandle异常时关闭记事本,就是没想到用来代替查找窗口,哎 钻牛角尖了,,,加点小修改
#region [ 启动记事本 ]

System.Diagnostics.Process Proc = new Process();

try
{
// 启动记事本
Proc = new System.Diagnostics.Process();
Proc.StartInfo.FileName = "notepad.exe";
Proc.StartInfo.UseShellExecute = false;
Proc.StartInfo.RedirectStandardInput = true;
Proc.StartInfo.RedirectStandardOutput = true;

Proc.Start();
}
catch
{
Proc.CloseMainWindow();
Proc = null;
}

#endregion

#region [ 传递数据给记事本 ]

if (Proc != null)
{
// 调用 API, 传递数据
while (Proc.MainWindowHandle == IntPtr.Zero)
{
Proc.Refresh();
}

IntPtr vHandle = FindWindowEx(Proc.MainWindowHandle, IntPtr.Zero, "Edit", null);

// 传递数据给记事本
SendMessage(vHandle, WM_SETTEXT, IntPtr.Zero, "display str");
}

#endregion
ParanoidKing 2012-06-20
  • 打赏
  • 举报
回复
换成FindWindow试试
myNotePad = FindWindow("无标题 - 记事本", null);
  • 打赏
  • 举报
回复
click事件里内容修改如下
#region [ 启动记事本 ]

System.Diagnostics.Process Proc;

try
{
// 启动记事本
Proc = new System.Diagnostics.Process();
Proc.StartInfo.FileName = "notepad.exe";
Proc.StartInfo.UseShellExecute = false;
Proc.StartInfo.RedirectStandardInput = true;
Proc.StartInfo.RedirectStandardOutput = true;

Proc.Start();
}
catch
{
Proc = null;
}

#endregion

#region [ 传递数据给记事本 ]

if (Proc != null)
{
// 调用 API, 传递数据
while (Proc.MainWindowHandle == IntPtr.Zero)
{
Proc.Refresh();
}

IntPtr vHandle = FindWindowEx(Proc.MainWindowHandle, IntPtr.Zero, "Edit", null);

// 传递数据给记事本
SendMessage(vHandle, WM_SETTEXT, IntPtr.Zero, "display str");
}

#endregion
Anod 2012-06-20
  • 打赏
  • 举报
回复
木有大神在啊?求解答啊

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧