C# 中滚动条的两个问题

发布于 2024-10-04 00:35:35 字数 219 浏览 0 评论 0原文

我讨厌两个 richTextBox

我有两个问题:

  1. 无论我如何定义滚动条 - 它都不会出现。如果我写的内容超过了行的长度 - 光标会转到下面的行 - 我希望所有内容都写在同一行中,并且用户可以使用滚动条从右向左移动。

  2. 我想要一个滚动条来控制两个富文本框。 一个水平的两个,一个垂直的两个。

I hate two richTextBoxs.

I have two problems:

  1. no matter how I define the scroll bar - it doesn't apear. if I write more than the length of the line - the cursor goes to the line below - I want everything to be writen in the same row and the user could move right-left with the scroll bar.

  2. I want one scroll bar to control both richtextboxes.
    one horizontal for both, and one vertical for both.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开半夏魅人心 2024-10-11 00:35:35
  1. 您需要设置 MultiLine如果您只需要一行,请将 属性设置为 false(RichTextBox 的默认值为 true),否则设置 WordWrap 属性设置为 false(这也默认为 true)。
  2. 我想您需要处理 HScroll每个 RichTextBox 的 VScroll 事件,并在事件处理程序中以相同的量滚动其他文本框。但不确定这有多容易......
  1. You'll need to set the MultiLine property to false (the default is true for RichTextBoxes) if you just want one line, or else set the WordWrap property to false (this also defaults to true).
  2. I guess you'll need to handle the HScroll and VScroll events of each RichTextBox, and in the event handlers scroll the other textbox by the same amount. Not sure how easy this would be, though...
や莫失莫忘 2024-10-11 00:35:35

解决第二个问题的最佳方法是使用 GetScrollInfo 和 SetScrollInfo。我认为您应该能够处理 HScrollVScroll 事件,当它们触发时,您必须在第二个 RichTextBox 中设置新的 ScrollInfo。

您还需要 WM_SendMessage 来完成这项工作。 (所有都可以通过导入User32.dll使用)

通常要做的工作:

当用户滚动(Scrollevents)时获得通知,并将新的ScrollInfo设置为第二个RichTextBox。设置 ScrollInfo 后,您需要向滚动条/滚动控件发送一条消息。

首先,您需要“自己的”SCROLLINFO struct

[StructLayout(LayoutKind.Sequential)]
    struct SCROLLINFO
    {
        public uint cbSize;
        public uint fMask;
        public int nMin;
        public int nMax;
        public uint nPage;
        public int nPos;
        public int nTrackPos;
    }

然后在第一个 TextBox 中的 ScrollingEvent 处获取 Scrollinfo:(

GetScrollInfo(this.Handle, SB_VERT, ref _si);

其中 _si 是您的 SCROLLINFO 实例)。然后将消息发送到滚动条,

    _si.nPos = 0815; //Your wanted new ScrollbarPosition (I think you can use the value of the first Scrollbar)
                //// Reposition scroller
                SetScrollInfo(Handle, SB_VERT, ref _si, true);

                //// Send a WM_VSCROLL scroll message using SB_THUMBTRACK as wParam
                //// SB_THUMBTRACK: low-order word of wParam, si.nPos high-order word of  wParam

                IntPtr ptrWparam = new IntPtr(SB_THUMBTRACK + 0x10000 * _si.nPos);
                IntPtr ptrLparam = new IntPtr(0);
                SendMessage(Handle, WM_VSCROLL, ptrWparam, ptrLparam);     

其中 HANDLE 是必须滚动的 RichTextBox 的句柄 - 您可以随时使用该句柄

RichTextBox.Handle

您应该真正看看 http://msdn.microsoft.com/en-us/library/bb787537%28VS. 85%29.aspxhttp:// /msdn.microsoft.com/en-us/library/ms644950%28VS.85%29.aspx

pinvoke.net 上也有一些很好的示例(例如 GetScrollInfo)

Best way to solve your second Problem is to use GetScrollInfo and SetScrollInfo. I think you should be able to handle the HScrolland VScrollevents, when they fire you'll have to set the new ScrollInfo in the second RichTextBox.

You'll need WM_SendMessage too for this job. (All usuable with imports of User32.dll)

Usual work to do:

Getting informed when the user scrolls (Scrollevents), and setting the new ScrollInfo to the second RichTextBox. After setting the ScrollInfo you'll need to send a Message to the Scrollbar / Control to Scroll.

First of all you need your "own" SCROLLINFO struct

[StructLayout(LayoutKind.Sequential)]
    struct SCROLLINFO
    {
        public uint cbSize;
        public uint fMask;
        public int nMin;
        public int nMax;
        public uint nPage;
        public int nPos;
        public int nTrackPos;
    }

Then get the Scrollinfo at ScrollingEvent in the first TextBox:

GetScrollInfo(this.Handle, SB_VERT, ref _si);

(where _si is your SCROLLINFO-instance). Then send the message to the scrollbar

    _si.nPos = 0815; //Your wanted new ScrollbarPosition (I think you can use the value of the first Scrollbar)
                //// Reposition scroller
                SetScrollInfo(Handle, SB_VERT, ref _si, true);

                //// Send a WM_VSCROLL scroll message using SB_THUMBTRACK as wParam
                //// SB_THUMBTRACK: low-order word of wParam, si.nPos high-order word of  wParam

                IntPtr ptrWparam = new IntPtr(SB_THUMBTRACK + 0x10000 * _si.nPos);
                IntPtr ptrLparam = new IntPtr(0);
                SendMessage(Handle, WM_VSCROLL, ptrWparam, ptrLparam);     

Where HANDLE is the Handle of your RichTextBox that has to be scrolled - you can get the handle at anytime using

RichTextBox.Handle

You should really have a look at http://msdn.microsoft.com/en-us/library/bb787537%28VS.85%29.aspx and http://msdn.microsoft.com/en-us/library/ms644950%28VS.85%29.aspx

There are also some good examples at pinvoke.net (for example GetScrollInfo)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文