winforms 如何在toolstripmenuitem中查看水平滚动

sq1bmfud  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(118)

我创建了一个toolstripmenuitem,在其中添加了许多toolstripmenuitem作为子菜单。然后我限制了toolstripmenuitem的最大大小,结果如下:

垂直滚动将自动启用,如图所示。
但是我需要一个水平滚动。有什么方法可以实现这个吗?

4ktjp1zp

4ktjp1zp1#

工作溶液:
1.在表单的Load事件中注册ToolStripDropDownMouseWheelOpenedClosed事件

dropDown.Opened+= new EventHandler(dropDown_Opened);
    dropDown.Closed+= new ToolStripDropDownClosedEventHandler(dropDown_Closed);
    dropDown.MouseWheel+= new MouseEventHandler(dropDown_MouseWheel);

1.添加模拟按键的Keyboard类的代码

public static class Keyboard
    {            
         [System.Runtime.InteropServices.DllImport("user32.dll")]
         static extern uint keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
         const byte VK_UP = 0x26; // Arrow Up key
         const byte VK_DOWN = 0x28; // Arrow Down key
         const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag, the key is going to be pressed
         const int KEYEVENTF_KEYUP = 0x0002; //Key up flag, the key is going to be released
         public static void KeyDown()
         {
             keybd_event(VK_DOWN, 0, KEYEVENTF_EXTENDEDKEY, 0);
             keybd_event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0);
         }
         public static void KeyUp()
         {
            keybd_event(VK_UP, 0, KEYEVENTF_EXTENDEDKEY, 0);
            keybd_event(VK_UP, 0, KEYEVENTF_KEYUP, 0);
         }
    }

1.添加OpenedClosedMouseWheel事件的代码:

bool IsMenuStripOpen  = false;
    void dropDown_MouseWheel(object sender, MouseEventArgs e)
    {
        if (IsMenuStripOpen)
        {
            if (e.Delta > 0)
            {
                Keyboard.KeyUp();
            }
            else
            {
                Keyboard.KeyDown();
            }
        }
    }
    void dropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e)
    {
        IsMenuStripOpen = false;
    }
    void dropDown_Opened(object sender, EventArgs e)
    {
        IsMenuStripOpen = true;
    }

1.为项目1的所有系统创建全局功能

void dropDownMenuScrollWheel(ToolStripDropDown dropDown)
    {
        dropDown.Opened +=new EventHandler(dropDown_Opened);
        dropDown.Closed +=new ToolStripDropDownClosedEventHandler(dropDown_Closed);
        dropDown.MouseWheel += new MouseEventHandler(dropDown_MouseWheel);
   }

工具条下拉菜单为ToolStripMenuItem.DropDown
希望对你有帮助。

相关问题