Wrapping Text in a rich textbox, but not word wrapping it

后端 未结 4 1275
醉梦人生
醉梦人生 2021-01-13 07:06

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

4条回答
  •  感动是毒
    2021-01-13 07:22

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

提交回复
热议问题