Send scroll event to form

百般思念 提交于 2019-12-13 06:36:21

问题


In my windows forms application it occurs frequently that a user scrolls through a panel and is then blocked by a richtextbox. I would like to catch the scroll event and send it to the panel when the richtextbox vertical scrollbar is not visible.

I already found the code to check if the vertical scrollbar is visible in this thread: https://social.msdn.microsoft.com/Forums/en-US/a3facad3-0eae-4610-9a63-1b6c7a718bf5/how-do-you-determine-if-vertical-scroll-bar-is-visible-in-richtextbox?forum=winforms

Also the VScroll event of a richtextbox is only triggered when the verticalscrollbar is already visible.

What would be the correct way to catch the mousescroll and send it to the correct panel?


回答1:


I think you can add a event to your rich text box in your Form1.Designer.cs like this code:

this.richTextBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.richTextBox1_MouseWheel);

and add this function to your form:

private void richTextBox1_MouseWheel(object sender, MouseEventArgs e)
{
    Control control = sender as Control;
    if (!NativeMethods.VerticalScrollBarVisible(control))
    {
        int numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines;
        int numberOfPixelsToMove = numberOfTextLinesToMove * Convert.ToInt32(control.Font.Size);
        if (panel1.VerticalScroll.Value - numberOfPixelsToMove < panel1.VerticalScroll.Minimum)
            panel1.VerticalScroll.Value = panel1.VerticalScroll.Minimum;
        else if (panel1.VerticalScroll.Value - numberOfPixelsToMove > panel1.VerticalScroll.Maximum)
            panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum;
        else
            panel1.VerticalScroll.Value -= numberOfPixelsToMove;
    }
}

The VerticalScrollBarVisible method is explaned in this MSDN thread.




回答2:


The event MouseWheel suggested by @Farshad does also trigger when the vertical scrollbar is not visible. I then added the following code to scroll panel1. Note that the MouseWheel event is not show in the visual studio designer and must be added to Form.Designer.cs manually.

private void richTextBox1_MouseWheel(object sender, MouseEventArgs e)
{
    Control control = sender as Control;
    if (!VerticalScrollBarVisible(control))
    {
        int numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines;
        int numberOfPixelsToMove = numberOfTextLinesToMove * Convert.ToInt32(control.Font.Size);
        if (panel1.VerticalScroll.Value - numberOfPixelsToMove < panel1.VerticalScroll.Minimum)
            panel1.VerticalScroll.Value = panel1.VerticalScroll.Minimum;
        else if (panel1.VerticalScroll.Value - numberOfPixelsToMove > panel1.VerticalScroll.Maximum)
            panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum;
        else
            panel1.VerticalScroll.Value -= numberOfPixelsToMove;
    }
}

[System.Runtime.InteropServices.DllImport("user32.dll")]
private extern static int GetWindowLong(IntPtr hWnd, int index);

public static bool VerticalScrollBarVisible(Control ctl) {
  int style = GetWindowLong(ctl.Handle, -16);
  return (style & 0x200000) != 0;
}


来源:https://stackoverflow.com/questions/39203749/send-scroll-event-to-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!