使用键盘滚动带有图像的面板(PageDown/PageUp) [英] Scroll panel with image using keyboard (PageDown / PageUp)

查看:148
本文介绍了使用键盘滚动带有图像的面板(PageDown/PageUp)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在具有滚动条和键盘滚动支持的Winforms应用程序中显示大图像的正确方法是什么? 当前,我使用带有嵌套的PitureBox(SizeMod = AutoSize)的Panel(AutoScroll = True).

What is right way to show large image in Winforms app with scroll-bars and keyboard scroll support? Currenty i'm use Panel(AutoScroll=True) with nested PitureBox (SizeMod = AutoSize).

我有2个问题:

1)选择哪个控件来绘制图像? 使用Tab键无法选择(聚焦)Panel和PitureBox.将Button与AutoSize = true和FlatStyle = Flat一起使用是正确的解决方案吗?

1) What control select for drawing image? Panel and PitureBox cant be selected (focused) using Tab key. Using Button with AutoSize = true and FlatStyle = Flat is right solution?

2)如何使用键盘在面板中滚动图像.需要处理哪些键盘事件-窗体,面板或图片框.也许我应该将Panel AutoScroll = false设置为我应该处理的事件,然后将它们添加到HScroll和VScroll中?

2)How to scroll image in panel using keyboard. What keyboard events a need handle - Form, Panel or PictureBox. May be I should set for Panel AutoScroll=false and add to them HScroll and VScroll, that events I should handle?

实施此基本应用程序的正确方法是什么?

What is right way for implement this elementary app?

(仅供参考,主窗体具有包含某些控件的其他Panel(Dock = Top).)

(Just for info, main form have other Panel(Dock=Top) that contain some controls.)

推荐答案

  • 关于第一个问题:没有任何控件非常适合使用,并且仍然可以获得Focus.不过,在下面的链接中,您可以看到如何制作一个可选面板.

    • As for the first question: There is no Control which is really well suited to draw on and still can get the Focus. In the link below you can see how to make a selectable panel, though.

      现在出现了真正的问题:如何通过键盘滚动AutoScroll Panel ??

      Now for the real problem: How to scroll an AutoScroll Panel by keyboard..?

      这很难做到.这是一个开始的示例:

      This is amazingly hard to do. Here is an example for a start:

      private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
      {
        if (panel1.Bounds.Contains( this.PointToClient( Cursor.Position )  ))
        {
          if (e.KeyValue == 33) panel1.AutoScrollPosition = 
            new Point(panel1.AutoScrollPosition.X, Math.Abs(panel1.AutoScrollPosition.Y) - 10);
      
          if (e.KeyValue == 34) panel1.AutoScrollPosition = 
            new Point(panel1.AutoScrollPosition.X, Math.Abs(panel1.AutoScrollPosition.Y) + 10);
        }
      }
      

      我测试Panel是否包含鼠标.您可能想玩一下滚动量.还要用适当的Keys替换KeyValues ;-)将Form设置为KeyPreview = true;

      I test for the Panel to contain the mouse. You may want to play around with the scroll amount.. Also replace the KeyValues with proper Keys ;-) Set KeyPreview = true; for the Form!

      注意:仅当表单上没有可获取焦点的控件时,此选项才有效.

      由于您很可能拥有某些Controls,例如ButtonsListBoxes等.这是另一种解决方案,在任何情况下都可以使用,我相信..

      Since most likely you have some such Controls, like Buttons, ListBoxes etc.. here is another solution, which works in any case, I believe..:

      protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
      {
          if (panel1.Bounds.Contains(this.PointToClient(Cursor.Position)))
          switch (keyData)
          {
              case Keys.PageDown: { scroll(10);  return true; }
              case Keys.PageUp:   { scroll(-10); return true; }
             // maybe code for some more keys..?
          }
      
          return base.ProcessCmdKey(ref msg, keyData);
      }
      
      void scroll(int delta)
      {
          panel1.AutoScrollPosition =  new Point(
                 panel1.AutoScrollPosition.X, Math.Abs(panel1.AutoScrollPosition.Y) + delta);
      }
      

      这不需要表格具有KeyPreview = true;.

      这是AutoScrollPosition的MSDN 解释.

      Here is the MSDN explanation for AutoScrollPosition.

      此处是建议使用Panel和/或PictureBox的子类,应该允许它们获取Focus.我还是无法使它正常工作..(最后似乎也没有更简单..)

      Here is a post that suggests using a subclass for Panel and/or PictureBox, which is supposed to allow them to get Focus. I couldn't get that to work though..(Nor does it seem to be any simpler in the end..)

      这篇关于使用键盘滚动带有图像的面板(PageDown/PageUp)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆