写文本记事本用C#/的Win32 [英] Write text to notepad with C#/Win32

查看:161
本文介绍了写文本记事本用C#/的Win32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Win32 API和Windows消息试图找出是如何工作的乱搞,我发现这个的的问题非常有帮助



我想在那里提供的解决方案来改善,使其追加文本,而不是通过WM_SETTEXT在记事本刚替换文本。



我的问题是,我怎么会用WM_GETTEXTLENGHT,其次是WM_GETTEXT,以获得在记事本窗口的当前文本所以我可以然后使用WM_SETTEXT之前追加新的文本呢?



使用是否在32位和64位的机器WM_XXXTEXT工作?如果在记事本中大量的文本将建议的get / set算法仍然可以工作,否则会养猪的大量资源?如果是这样,有另一种方式将文本追加到记事本窗口而不先在其复制的一切?



感谢您的帮助!



更新:



下面是我想出了根据大卫·赫弗南的帮助和谷歌/ SO削减ñ粘贴代码。由于我是新来的Win32API的复制和来自不同来源的多条线路我会很感激的任何和所有的反馈。

 函数[DllImport (User32.dll中,字符集= CharSet.Auto)] 
的extern静态的IntPtr FindWindowEx(IntPtr的hwndParent,IntPtr的hwndChildAfter,[IN]串lpClassName,[在]串lpWindowName);

函数[DllImport(User32.dll中,入口点=SendMessage函数)]
的extern静态INT SendMessageGetTextLength(IntPtr的的HWND,INT味精,IntPtr的的wParam,lParam的IntPtr的);

函数[DllImport(User32.dll中)]
公共静态外部INT SendMessage函数(IntPtr的的HWND,INT uMsg,诠释的wParam,lParam的字符串)

函数[DllImport(User32.dll中)]
公共静态外部INT SendMessage函数(IntPtr的的HWND,INT uMsg,诠释的wParam,lParam的INT);

const int的WM_GETTEXTLENGTH = 0x000E;

const int的EM_SETSEL = 0x00B1;

const int的EM_REPLACESEL = 0x00C2;

公共无效testAppendText(字符串文本)
{
过程[] =记事本Process.GetProcessesByName(记事本);
如果(notepads.Length == 0)回报;
如果(记事本[0]!= NULL)
{
IntPtr的编辑框= FindWindowEx(记事本[0] .MainWindowHandle,新的IntPtr(0),编辑,NULL);
INT长度= SendMessageGetTextLength(编辑框,WM_GETTEXTLENGTH,IntPtr.Zero,IntPtr.Zero);
SendMessage函数(编辑框,EM_SETSEL,长度,长度);
SendMessage函数(编辑框,EM_REPLACESEL,1,文本);
}

}


解决方案

发送 EM_SETSEL 来把插入符编辑窗口的结束。然后发送 EM_REPLACESEL 追加文本。



这是比读的全部内容,附加你的另外再要好得多设置的全部内容,如果编辑控件包含大量的文字。



这些方法可以跨32/64位进程边界没有困难。


I'm messing around with Win32 API and windows messaging trying to figure out how things work and I found this question very helpful.

I'd like to improve upon the solution provided there so that it appends the text instead of just replacing the text in notepad via WM_SETTEXT.

My question is, how would I use WM_GETTEXTLENGHT, followed by WM_GETTEXT, to get the current text in the notepad window so I could then append new text to it before using WM_SETTEXT?

Does using WM_XXXTEXT work on both 32 and 64-bit machines? If there is a lot of text in notepad would the proposed get/set algorithm still work or would it hog a bunch of resources? If so, is there another way to append text to the notepad window without copying everything in it first?

Thanks for you help!!

UPDATE:

Here is the code I came up with based on David Heffernan's help and Google/SO cut n pasting. As I'm new to the Win32API and copied many lines from different sources I'd appreciate any and all feedback.

[DllImport("User32.dll", CharSet = CharSet.Auto)]
        extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, [In] string lpClassName, [In] string lpWindowName);

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        extern static int SendMessageGetTextLength(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);

        const int WM_GETTEXTLENGTH = 0x000E;

        const int EM_SETSEL = 0x00B1;

        const int EM_REPLACESEL = 0x00C2;

        public void testAppendText(string text)
        {
            Process[] notepads = Process.GetProcessesByName("notepad");
            if (notepads.Length == 0) return;
            if (notepads[0] != null)
            {
                IntPtr editBox = FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
                int length = SendMessageGetTextLength(editBox, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
                SendMessage(editBox, EM_SETSEL, length, length);
                SendMessage(editBox, EM_REPLACESEL, 1, text);
            }

        } 

解决方案

Send EM_SETSEL to put the caret to the end of the edit window. Then send EM_REPLACESEL to append text.

This is much better than reading the entire contents, appending your addition and then setting the entire contents if the edit control contains a large amount of text.

These methods can cross 32/64 bit process boundaries without difficulty.

这篇关于写文本记事本用C#/的Win32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆