4

ScrollableControl在我的 C# 项目中使用 a ,我想知道如何将箭头键映射到 vertical/horizo​​ntal scrolling

编辑:我的图片框获得焦点,我设法将键映射到滚动。这里的问题是,当我按下箭头键时,它会滚动一次,然后失去焦点,将其提供给 Scrollviewer 旁边的按钮。我希望滚动查看器不要失去焦点

这是我的代码,第二段是这里有趣的一段。

private void drawMap(IGame game)
{
    System.Windows.Forms.PictureBox pictureBox = new System.Windows.Forms.PictureBox();
    pictureBox.Width = (int)Math.Sqrt((double)game.Map.grid.Count) * 50; pictureBox.Height = (int)Math.Sqrt((double)game.Map.grid.Count) * 50;
    pictureBox.Paint += new System.Windows.Forms.PaintEventHandler(game.Map.afficher);
    pictureBox.MouseEnter += pictureBoxFocus;
    pictureBox.Click += pictureBoxFocus;
    pictureBox.Click += mapClicked;

    System.Windows.Forms.ScrollableControl sc = new System.Windows.Forms.ScrollableControl();
    sc.Controls.Add(pictureBox);
    sc.AutoScroll = true;
    windowsFormsHost1.Child = sc;
}

先感谢您 :)

加:我的滚动算法有点脏,效果不太好,所以我想知道是否有一种“简单的方法”来做到这一点。

private void sc_PreviewKeyDown(object sender, System.Windows.Forms.PreviewKeyDownEventArgs e)
{
    System.Windows.Forms.ScrollableControl sc = (System.Windows.Forms.ScrollableControl)sender;
    switch (e.KeyValue)
    {
        case (int)System.Windows.Forms.Keys.Down:
            sc.VerticalScroll.Value += 50;
            break;
        case (int)System.Windows.Forms.Keys.Up:
            if(sc.VerticalScroll.Value - 50 < 0)
                sc.VerticalScroll.Value = 0;
            else sc.VerticalScroll.Value -= 50;
            break;
        case (int)System.Windows.Forms.Keys.Right:
            sc.HorizontalScroll.Value += 50;
            break;
        case (int)System.Windows.Forms.Keys.Left:
            if (sc.HorizontalScroll.Value - 50 < 0)
                sc.HorizontalScroll.Value = 0;
            else sc.HorizontalScroll.Value -= 50;
            break;
    }
    windowsFormsHost1.Child.Focus();
}
4

0 回答 0