実現したいこと
リッチテキストボックス配置時に、テキストが表示範囲を超えた場合スクロールバーが表示されますが、そのスクロールバーの表示位置を左にする方法はありますでしょうか。
言語:C#
環境:visualstudio、Windowsフォーム
.NET Framework 4,7,2
TN8001が👍を押しています
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。

回答1件
0
ベストアンサー
TextBox
での話ですが、RichTextBox
でも動作しました。
c# - TextBox with vertical scrollbar on the left side - Stack Overflow
cs
1using System; 2using System.Runtime.InteropServices; 3using System.Windows.Forms; 4 5 6namespace Qklheq94bhpzvyp 7{ 8 public partial class Form1 : Form 9 { 10 public Form1() 11 { 12 InitializeComponent(); 13 14 new LeftScrollBarRichTextBox 15 { 16 Dock = DockStyle.Fill, 17 Parent = this, 18 //ScrollBars = RichTextBoxScrollBars.Vertical, 19 Text = $"aaa{new string('\n', 26)}あああ", 20 }; 21 } 22 } 23 24 // [c# - TextBox with vertical scrollbar on the left side - Stack Overflow](https://stackoverflow.com/questions/14402428/textbox-with-vertical-scrollbar-on-the-left-side) 25 public class LeftScrollBarRichTextBox : RichTextBox 26 { 27 private const int GWL_EXSTYLE = -20; 28 private const int WS_EX_LEFTSCROLLBAR = 16384; 29 [DllImport("user32")] private extern static int GetWindowLong(IntPtr hWnd, int nIndex); 30 [DllImport("user32")] private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); 31 protected override void OnHandleCreated(EventArgs e) 32 { 33 base.OnHandleCreated(e); 34 var style = GetWindowLong(Handle, GWL_EXSTYLE); 35 style |= WS_EX_LEFTSCROLLBAR; 36 SetWindowLong(Handle, GWL_EXSTYLE, style); 37 } 38 } 39}
不特定多数が使うなら、あまりイレギュラーなことをするべきではありません(個人で使う分には好きにすればいいとは思いますが)
追記
既存のリッチテキストボックスのスクロールバーを左にしたり、右にしたりと変更可能にしたい
こんなんでしょうか(細かく確認していないので、なにか不具合等あるかもしれませんが)
cs
1using System; 2using System.Runtime.InteropServices; 3using System.Windows.Forms; 4 5 6namespace Qklheq94bhpzvyp 7{ 8 public partial class Form1 : Form 9 { 10 private LeftScrollBarRichTextBox leftScrollBarRichTextBox; 11 12 public Form1() 13 { 14 InitializeComponent(); 15 16 leftScrollBarRichTextBox = new LeftScrollBarRichTextBox 17 { 18 Dock = DockStyle.Fill, 19 Parent = this, 20 Text = $"aaa{new string('\n', 26)}あああ", 21 }; 22 23 new CheckBox 24 { 25 Dock = DockStyle.Top, 26 Parent = this, 27 Text = "LeftScrollBar", 28 }.CheckedChanged += CheckedChanged; 29 } 30 31 private void CheckedChanged(object sender, EventArgs e) 32 { 33 if (sender is CheckBox checkBox) 34 leftScrollBarRichTextBox.LeftScrollBar = checkBox.Checked; 35 } 36 } 37 38 // [c# - TextBox with vertical scrollbar on the left side - Stack Overflow](https://stackoverflow.com/questions/14402428/textbox-with-vertical-scrollbar-on-the-left-side) 39 public class LeftScrollBarRichTextBox : RichTextBox 40 { 41 public bool LeftScrollBar 42 { 43 get => (GetWindowLong(Handle, GWL_EXSTYLE) & WS_EX_LEFTSCROLLBAR) != 0; 44 set 45 { 46 var style = GetWindowLong(Handle, GWL_EXSTYLE); 47 if (value) style |= WS_EX_LEFTSCROLLBAR; 48 else style &= ~WS_EX_LEFTSCROLLBAR; 49 SetWindowLong(Handle, GWL_EXSTYLE, style); 50 Refresh(); 51 } 52 } 53 54 private const int GWL_EXSTYLE = -20; 55 private const int WS_EX_LEFTSCROLLBAR = 16384; 56 [DllImport("user32")] private extern static int GetWindowLong(IntPtr hWnd, int nIndex); 57 [DllImport("user32")] private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); 58 } 59}
投稿2023/12/15 14:03
編集2023/12/17 01:42総合スコア10024
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/12/16 06:21
2023/12/16 08:22
2023/12/17 05:14