【发布时间】:2019-06-04 19:42:31
【问题描述】:
我正在尝试使用SendMessage 以编程方式在记事本++ 中打开一个文件,但我没有运气。
我想,因为我可以将文件拖放到 Notepad++ 上并且它会打开它,所以 SendMessage 会起作用。
声明:
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
方法:
我使用 Process.Start 启动 Notepad++:
IntPtr cHwnd = FindWindowEx(pDocked.MainWindowHandle, IntPtr.Zero, "Scintilla", null);
SendMessage(cHwnd, WM_SETTEXT, 0, "C:\Users\nelsonj\Desktop\lic.txt");
SendMessage 执行时,它会将我的文本发送到 Notepad++ 的“编辑”部分,而不是打开文件。
任何帮助都会很棒。
【问题讨论】:
-
据我所知,notepad++ 作为单实例应用程序正常运行。因此,如果您使用
Process.Start启动 notepad++,应该会打开一个新选项卡。 -
几个猜测:老式的 DDE(不,我不记得它是如何工作的)。另一种方法是将文件对象放在剪贴板上,然后将拖放消息(或消息序列)发送到 Notepad++
-
如果您尝试@Pretasoc 的
Process.Start建议,我很确定您需要将UseShellExecute设置为true(在ProcessStartInfo中)。但是,话又说回来…… -
Process.Start 应该是正确的方法
标签: c# winapi notepad++ sendmessage