From c7992b0410d7e51832e41b849c87b414969edc95 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 28 Sep 2019 22:22:53 +0300 Subject: [PATCH] Added #32 --- Crypto Notepad/ExRichTextBox.cs | 56 ++++++++++++++++++++++++++++++++- Crypto Notepad/MainForm.cs | 1 + 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/Crypto Notepad/ExRichTextBox.cs b/Crypto Notepad/ExRichTextBox.cs index f9c2d2d..eae0dd2 100644 --- a/Crypto Notepad/ExRichTextBox.cs +++ b/Crypto Notepad/ExRichTextBox.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Runtime.InteropServices; using System.Windows.Forms; @@ -34,5 +34,59 @@ protected override void WndProc(ref Message m) base.WndProc(ref m); } + public static class RichTextBoxPadding + { + public static void SetInnerMargins(this TextBoxBase textBox, int left, int top, int right, int bottom) + { + var rect = textBox.GetFormattingRect(); + + var newRect = new Rectangle(left, top, rect.Width - left - right, rect.Height - top - bottom); + textBox.SetFormattingRect(newRect); + } + + [StructLayout(LayoutKind.Sequential)] + private struct RECT + { + public readonly int Left; + public readonly int Top; + public readonly int Right; + public readonly int Bottom; + + private RECT(int left, int top, int right, int bottom) + { + Left = left; + Top = top; + Right = right; + Bottom = bottom; + } + + public RECT(Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom) + { + } + } + + [DllImport(@"User32.dll", EntryPoint = @"SendMessage", CharSet = CharSet.Auto)] + private static extern int SendMessageRefRect(IntPtr hWnd, uint msg, int wParam, ref RECT rect); + + [DllImport(@"user32.dll", EntryPoint = @"SendMessage", CharSet = CharSet.Auto)] + private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam); + + private const int EmGetrect = 0xB2; + private const int EmSetrect = 0xB3; + + private static void SetFormattingRect(this TextBoxBase textbox, Rectangle rect) + { + var rc = new RECT(rect); + SendMessageRefRect(textbox.Handle, EmSetrect, 0, ref rc); + } + + private static Rectangle GetFormattingRect(this TextBoxBase textbox) + { + var rect = new Rectangle(); + SendMessage(textbox.Handle, EmGetrect, (IntPtr)0, ref rect); + return rect; + } + } +} } } diff --git a/Crypto Notepad/MainForm.cs b/Crypto Notepad/MainForm.cs index 9a23a9f..70902e1 100644 --- a/Crypto Notepad/MainForm.cs +++ b/Crypto Notepad/MainForm.cs @@ -722,6 +722,7 @@ private void MainWindow_FormClosing(object sender, FormClosingEventArgs e) } } + richTextBox.SetInnerMargins(Convert.ToInt32(settings.editorPaddingLeft), 0, 0, 0); private void MainWindow_Load(object sender, EventArgs e) {