I have a Windows Form with a Rich Textbox control on the form. What I want to do is make it so that each line only accepts 32 characters of text. After 32 characters, I want the text to flow to the next line (I do NOT want to insert any carriage returns). The WordWrap property almost does this, except that it moves all the text entered, up to the last space in the text and moves it all. I just want to neatly wrap the text right after 32 characters. How can I do this? I'm using C#.
我有一个Windows窗体,窗体上有一个富文本框控件。我想做的是让每一行只接受32个字符的文本。32个字符之后,我希望文本流到下一行(我不想插入任何回车)。WordWrap属性几乎可以做到这一点,除了它将输入的所有文本移动到文本中的最后一个空格并将其全部移动。我只是想在32个字符后简洁地包装文本。我该怎么做呢?我使用c#。
3 个解决方案
#1
4
Ok, I have found a way to do this (after a lot of googling and Windows API referencing) and I am posting a solution here in case anyone else ever needs to figure this out. There is no clean .NET solution to this, but fortunately, the Windows API allows you to override the default procedure that gets called when handling word wrapping. First you need to import the following DLL:
好的,我找到了一种方法(在google和Windows API引用了很多次之后),我在这里发布了一个解决方案,以防有人需要解决这个问题。对此没有干净的。net解决方案,但幸运的是,Windows API允许您重写在处理字包装时调用的默认过程。首先需要导入以下DLL:
[DllImport("user32.dll")]
extern static IntPtr SendMessage(IntPtr hwnd, uint message, IntPtr wParam, IntPtr lParam);
Then you need to define this constant:
然后你需要定义这个常数:
const uint EM_SETWORDBREAKPROC = 0x00D0;
Then create a delegate and event:
然后创建一个委托和事件:
delegate int EditWordBreakProc(IntPtr text, int pos_in_text, int bCharSet, int action);
event EditWordBreakProc myCallBackEvent;
Then create our new function to handle word wrap (which in this case I don't want it to do anything):
然后创建我们的新函数来处理word wrap(在这种情况下,我不希望它做任何事情):
private int myCallBack(IntPtr text, int pos_in_text, int bCharSet, int action)
{
return 0;
}
Finally, in the Shown event of your form:
最后,在您的表单显示事件中:
myCallBackEvent = new EditWordBreakProc(myCallBack);
IntPtr ptr_func = Marshal.GetFunctionPointerForDelegate(myCallBackEvent);
SendMessage(txtDataEntry.Handle, EM_SETWORDBREAKPROC, IntPtr.Zero, ptr_func);
#2
2
If you are using only (one) fixed width font for text within rich text box then you can use MeasureString to count the width needed for 32 characters and then adjust rich text box width accordingly. Thats should do wrapping within 32 characters.
如果在富文本框中只使用(1)固定宽度字体,那么可以使用MeasureString来计算32个字符所需的宽度,然后相应地调整富文本框的宽度。它应该在32个字符内进行包装。
#3
0
I've been looking into this problem myself and have found a really easy way to get around this. You need to place a small piece of code in the Key_Down Event on your TextBox or RichTextBox control.
我自己也一直在研究这个问题,并且找到了一个很容易解决的方法。您需要在文本框或RichTextBox控件的Key_Down事件中放置一小段代码。
Ensure that both Word Wrap and Multiline properties are still set to true, then, you just need to check for the Key Press of KeyCode.Return & Keycode.Enter.
确保单词包装和多行属性仍然被设置为true,那么,您只需要检查KeyCode的按键。& Keycode.Enter返回。
Like Below :-
如以下:-
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
{
e.Handled = true;
}
}
Setting the handled value you here to true, sends back the message that we have dealt with the Key Press event, and nothing happens. The Text Control continues to use Word Wrap and blocks the carriage return.
将这里的处理值设置为true,返回我们处理过的键按事件的消息,然后什么也没有发生。文本控件继续使用Word Wrap,并阻止回车。
Hope this helps.
希望这个有帮助。
#1
4
Ok, I have found a way to do this (after a lot of googling and Windows API referencing) and I am posting a solution here in case anyone else ever needs to figure this out. There is no clean .NET solution to this, but fortunately, the Windows API allows you to override the default procedure that gets called when handling word wrapping. First you need to import the following DLL:
好的,我找到了一种方法(在google和Windows API引用了很多次之后),我在这里发布了一个解决方案,以防有人需要解决这个问题。对此没有干净的。net解决方案,但幸运的是,Windows API允许您重写在处理字包装时调用的默认过程。首先需要导入以下DLL:
[DllImport("user32.dll")]
extern static IntPtr SendMessage(IntPtr hwnd, uint message, IntPtr wParam, IntPtr lParam);
Then you need to define this constant:
然后你需要定义这个常数:
const uint EM_SETWORDBREAKPROC = 0x00D0;
Then create a delegate and event:
然后创建一个委托和事件:
delegate int EditWordBreakProc(IntPtr text, int pos_in_text, int bCharSet, int action);
event EditWordBreakProc myCallBackEvent;
Then create our new function to handle word wrap (which in this case I don't want it to do anything):
然后创建我们的新函数来处理word wrap(在这种情况下,我不希望它做任何事情):
private int myCallBack(IntPtr text, int pos_in_text, int bCharSet, int action)
{
return 0;
}
Finally, in the Shown event of your form:
最后,在您的表单显示事件中:
myCallBackEvent = new EditWordBreakProc(myCallBack);
IntPtr ptr_func = Marshal.GetFunctionPointerForDelegate(myCallBackEvent);
SendMessage(txtDataEntry.Handle, EM_SETWORDBREAKPROC, IntPtr.Zero, ptr_func);
#2
2
If you are using only (one) fixed width font for text within rich text box then you can use MeasureString to count the width needed for 32 characters and then adjust rich text box width accordingly. Thats should do wrapping within 32 characters.
如果在富文本框中只使用(1)固定宽度字体,那么可以使用MeasureString来计算32个字符所需的宽度,然后相应地调整富文本框的宽度。它应该在32个字符内进行包装。
#3
0
I've been looking into this problem myself and have found a really easy way to get around this. You need to place a small piece of code in the Key_Down Event on your TextBox or RichTextBox control.
我自己也一直在研究这个问题,并且找到了一个很容易解决的方法。您需要在文本框或RichTextBox控件的Key_Down事件中放置一小段代码。
Ensure that both Word Wrap and Multiline properties are still set to true, then, you just need to check for the Key Press of KeyCode.Return & Keycode.Enter.
确保单词包装和多行属性仍然被设置为true,那么,您只需要检查KeyCode的按键。& Keycode.Enter返回。
Like Below :-
如以下:-
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
{
e.Handled = true;
}
}
Setting the handled value you here to true, sends back the message that we have dealt with the Key Press event, and nothing happens. The Text Control continues to use Word Wrap and blocks the carriage return.
将这里的处理值设置为true,返回我们处理过的键按事件的消息,然后什么也没有发生。文本控件继续使用Word Wrap,并阻止回车。
Hope this helps.
希望这个有帮助。