积极答复者
C#中如何使用Win32 API中的EM_GETLINE获取一个Edit控件中的内容

问题
-
请问在C#中如何使用Win32 API中的EM_GETLINE获取一个Edit控件中的内容。以下是我的代码
1、定义一个缓冲区
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct STRINGBUFFER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 512)]
public string szText;
}
2、将编辑控件某一行的内容拷贝到缓冲区中
int EM_GETLINE = 0x00C4;
STRINGBUFFER contentEditBoxLine; // 存放编辑控件中行的内容
SendMessage(hWnd, EM_GETLINE, (IntPtr)0, out contentEditBoxLine); // hWnd获取到的编辑控件的句柄,这条语句将编辑控件中的第一行内容拷贝到contentEditBoxLine中
问题是:我得到的contentEditBoxLine的内容永远是空的MSDN上对EM_GETLINE的参数lParam是这样解释的:Long pointer to the buffer that receives a copy of the line. The first word of the buffer specifies the maximum number of characters that can be copied to the buffer. 我想请问的是我要如何设置缓冲区contentEditBoxLine的first word
Spark
答案
-
你好
GetWindowText指定窗口的标题条文本(如果存在)拷贝到一个缓存区内。如果指定的窗口是一个控件,则拷贝控件的文本。但是,GetWindowText不能接收在其他应用程序中的控件的文本。
因为你指定的记事本是一个窗体,所以只能获取到标题的文本。
所以建议你还是用EM_GETLINE。我用下面的代码可以读取文本里面的文字。
[DllImport("user32.dll", EntryPoint = "SendMessageA")] private static extern int SendMessage_Ex(IntPtr hwnd, int wMsg, int wParam, StringBuilder lParam); private const int EM_GETLINE = 0xc4; public void test(){ IntPtr hWnd = new IntPtr(0x000C0DD6);//获取到的edit控件的句柄 StringBuilder contentEditBoxLine = new StringBuilder(); //用StringBuilder SendMessage_Ex(hWnd, EM_GETLINE, 0, contentEditBoxLine); }
然后可以读取到文本里面的第一行的数据。
希望对你有帮助。
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Cookie Luo 2011年6月9日 7:52
全部回复
-
你好
GetWindowText指定窗口的标题条文本(如果存在)拷贝到一个缓存区内。如果指定的窗口是一个控件,则拷贝控件的文本。但是,GetWindowText不能接收在其他应用程序中的控件的文本。
因为你指定的记事本是一个窗体,所以只能获取到标题的文本。
所以建议你还是用EM_GETLINE。我用下面的代码可以读取文本里面的文字。
[DllImport("user32.dll", EntryPoint = "SendMessageA")] private static extern int SendMessage_Ex(IntPtr hwnd, int wMsg, int wParam, StringBuilder lParam); private const int EM_GETLINE = 0xc4; public void test(){ IntPtr hWnd = new IntPtr(0x000C0DD6);//获取到的edit控件的句柄 StringBuilder contentEditBoxLine = new StringBuilder(); //用StringBuilder SendMessage_Ex(hWnd, EM_GETLINE, 0, contentEditBoxLine); }
然后可以读取到文本里面的第一行的数据。
希望对你有帮助。
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Cookie Luo 2011年6月9日 7:52