【问题标题】:Set horizontal scrollbar for 32 bit scrolling in a C# RichTextBox在 C# RichTextBox 中为 32 位滚动设置水平滚动条
【发布时间】:2014-03-31 04:22:17
【问题描述】:

感谢之前的帖子:https://stackoverflow.com/a/5611856/848344,我已经成功控制了 RichTextBox 中的垂直滚动条。但是如何控制水平滚动条呢?

方法为setVerticalScroll()填写。我只需要为 setHorizo​​ntalScroll() 填写“在此处插入 gubbins”。

// 32 bit scrolling of pane slider
// https://stackoverflow.com/questions/1380104/cc-setscrollpos-user32-dll
[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
[DllImport("User32.dll")]
private extern static int GetScrollPos(IntPtr hWnd, int nBar);
private enum ScrollBarType : uint { SbHorz = 0, SbVert = 1, SbCtl = 2, SbBoth = 3 }

public void setVerticalScroll(IntPtr hWnd, int pos) {
    SetScrollPos(hWnd, 0x1, pos, true);
    PostMessage(hWnd, 0x115, 4 + 0x10000 * pos, 0);
}
public void setHorizontalScroll(IntPtr hWnd, int pos) {
    /////////////////////////////////////
    //////////////// Insert gubbins here.
    /////////////////////////////////////
}
public int getVerticalScroll(IntPtr hWnd) {
    int n = GetScrollPos(hWnd, (int)ScrollBarType.SbVert);
    return n;
}
public int getHorizontalScroll(IntPtr hWnd) {
    int n = GetScrollPos(hWnd, (int)ScrollBarType.SbHorz);
    return n;
}

【问题讨论】:

  • 这里使用正确的符号,它是WM_VSCROLL而不是0x115,SB_VERT而不是0x1。然后,您将无法帮助自己陷入 WM_HSCROLL 和 SB_HORZ 的成功陷阱。请改用 SendMessage()。
  • SendMessage() 比 PostMessage 好多少?
  • 它总是被发送,无论你破解什么程序都可能不希望 GetMessage() 返回该消息,因此无法正确处理它。

标签: c# winapi scrollbar richtextbox


【解决方案1】:

通过反复试验以及纯粹的运气,我想我找到了解决方案。我只是从 0x115 值中减去 1 得到 0x114(并且还将 0x1 更改为 0x0):

public void setHorizontalScroll(IntPtr hWnd, int pos)
{
    SetScrollPos(hWnd, 0x0, pos, true);
    PostMessage(hWnd, 0x114, 4 + 0x10000 * pos, 0);
}

如果有人可以检查一下,我将不胜感激。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多