Advertisement
uwekeim

RichTextEdit control — Set padding between text and border

Nov 5th, 2015
3,368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. namespace ZetaProducer
  2. {
  3.     // See http://stackoverflow.com/q/2914004/107625
  4.  
  5.     using System;
  6.     using System.Drawing;
  7.     using System.Runtime.InteropServices;
  8.     using System.Windows.Forms;
  9.  
  10.     public static class RichTextBoxExtensions
  11.     {
  12.         public static void SetInnerMargins(this TextBoxBase textBox, int left, int top, int right, int bottom)
  13.         {
  14.             var rect = textBox.GetFormattingRect();
  15.  
  16.             var newRect = new Rectangle(left, top, rect.Width - left - right, rect.Height - top - bottom);
  17.             textBox.SetFormattingRect(newRect);
  18.         }
  19.  
  20.         [StructLayout(LayoutKind.Sequential)]
  21.         private struct RECT
  22.         {
  23.             public readonly int Left;
  24.             public readonly int Top;
  25.             public readonly int Right;
  26.             public readonly int Bottom;
  27.  
  28.             private RECT(int left, int top, int right, int bottom)
  29.             {
  30.                 Left = left;
  31.                 Top = top;
  32.                 Right = right;
  33.                 Bottom = bottom;
  34.             }
  35.  
  36.             public RECT(Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom)
  37.             {
  38.             }
  39.         }
  40.  
  41.         [DllImport(@"User32.dll", EntryPoint = @"SendMessage", CharSet = CharSet.Auto)]
  42.         private static extern int SendMessageRefRect(IntPtr hWnd, uint msg, int wParam, ref RECT rect);
  43.  
  44.         [DllImport(@"user32.dll", EntryPoint = @"SendMessage", CharSet = CharSet.Auto)]
  45.         private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam);
  46.  
  47.         private const int EmGetrect = 0xB2;
  48.         private const int EmSetrect = 0xB3;
  49.  
  50.         private static void SetFormattingRect(this TextBoxBase textbox, Rectangle rect)
  51.         {
  52.             var rc = new RECT(rect);
  53.             SendMessageRefRect(textbox.Handle, EmSetrect, 0, ref rc);
  54.         }
  55.  
  56.         private static Rectangle GetFormattingRect(this TextBoxBase textbox)
  57.         {
  58.             var rect = new Rectangle();
  59.             SendMessage(textbox.Handle, EmGetrect, (IntPtr) 0, ref rect);
  60.             return rect;
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement