回答編集履歴

2

トグルじゃまずいやw

2023/12/17 01:42

投稿

TN8001
TN8001

スコア10026

test CHANGED
@@ -101,7 +101,8 @@
101
101
  set
102
102
  {
103
103
  var style = GetWindowLong(Handle, GWL_EXSTYLE);
104
+ if (value) style |= WS_EX_LEFTSCROLLBAR;
104
- style ^= WS_EX_LEFTSCROLLBAR;
105
+ else style &= ~WS_EX_LEFTSCROLLBAR;
105
106
  SetWindowLong(Handle, GWL_EXSTYLE, style);
106
107
  Refresh();
107
108
  }

1

追記 トグル動作

2023/12/16 08:21

投稿

TN8001
TN8001

スコア10026

test CHANGED
@@ -47,3 +47,71 @@
47
47
  ---
48
48
 
49
49
  不特定多数が使うなら、あまりイレギュラーなことをするべきではありません(個人で使う分には好きにすればいいとは思いますが)
50
+
51
+ ---
52
+
53
+ 追記
54
+ > 既存のリッチテキストボックスのスクロールバーを左にしたり、右にしたりと変更可能にしたい
55
+
56
+ こんなんでしょうか(細かく確認していないので、なにか不具合等あるかもしれませんが)
57
+ ```cs
58
+ using System;
59
+ using System.Runtime.InteropServices;
60
+ using System.Windows.Forms;
61
+
62
+
63
+ namespace Qklheq94bhpzvyp
64
+ {
65
+ public partial class Form1 : Form
66
+ {
67
+ private LeftScrollBarRichTextBox leftScrollBarRichTextBox;
68
+
69
+ public Form1()
70
+ {
71
+ InitializeComponent();
72
+
73
+ leftScrollBarRichTextBox = new LeftScrollBarRichTextBox
74
+ {
75
+ Dock = DockStyle.Fill,
76
+ Parent = this,
77
+ Text = $"aaa{new string('\n', 26)}あああ",
78
+ };
79
+
80
+ new CheckBox
81
+ {
82
+ Dock = DockStyle.Top,
83
+ Parent = this,
84
+ Text = "LeftScrollBar",
85
+ }.CheckedChanged += CheckedChanged;
86
+ }
87
+
88
+ private void CheckedChanged(object sender, EventArgs e)
89
+ {
90
+ if (sender is CheckBox checkBox)
91
+ leftScrollBarRichTextBox.LeftScrollBar = checkBox.Checked;
92
+ }
93
+ }
94
+
95
+ // [c# - TextBox with vertical scrollbar on the left side - Stack Overflow](https://stackoverflow.com/questions/14402428/textbox-with-vertical-scrollbar-on-the-left-side)
96
+ public class LeftScrollBarRichTextBox : RichTextBox
97
+ {
98
+ public bool LeftScrollBar
99
+ {
100
+ get => (GetWindowLong(Handle, GWL_EXSTYLE) & WS_EX_LEFTSCROLLBAR) != 0;
101
+ set
102
+ {
103
+ var style = GetWindowLong(Handle, GWL_EXSTYLE);
104
+ style ^= WS_EX_LEFTSCROLLBAR;
105
+ SetWindowLong(Handle, GWL_EXSTYLE, style);
106
+ Refresh();
107
+ }
108
+ }
109
+
110
+ private const int GWL_EXSTYLE = -20;
111
+ private const int WS_EX_LEFTSCROLLBAR = 16384;
112
+ [DllImport("user32")] private extern static int GetWindowLong(IntPtr hWnd, int nIndex);
113
+ [DllImport("user32")] private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
114
+ }
115
+ }
116
+ ```
117
+ ![トグル動作](https://ddjkaamml8q8x.cloudfront.net/questions/2023-12-16/a3c55f58-9757-4ea8-8430-b1dd104d452d.gif)