将连续文本中丰富的文本框,但不换行它但不、文本框、换行、丰富

2023-09-05 00:11:35 作者:氣質、惹眼

我有一个Windows窗体与窗体上的格式文本框控件。我想要做的就是让这个每行只接受文字的32个字符。经过32个字符,我希望文本流到下一行(我不想插入任何回车)。 wordWrap属性几乎做到这一点,但它会将所有输入的文本,最多在文本中的最后空间和移动这一切。我只是想后,32个字符整齐地自动换行权。我怎样才能做到这一点?我使用C#。

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#.

推荐答案

好吧,我已经找到一种方法(大量的使用Google和Windows API引用后)做到这一点,我在这里张贴,以防有人解决别人永远需要弄清楚了这一点。有没有干净的.NET解决方案,这一点,但幸运的是,Windows的API允许你覆盖处理自动换行的时候被调用的默认程序。首先,你需要导入以下DLL:

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:

[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):

     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);