本文介绍了使用按键移动图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于按键来移动图片.
你能告诉我一种优雅的方法吗?


建议一种在应用程序运行时选择控件(例如Picturebox)的方法.
例如-我想使用鼠标拖动图片.

I want to move my pictures in the form based on key pressing .
Can you tell me an elegant way of doing it.


Suggest a way of selecting a control (eg Picturebox) when application is running.
For eg.-- i want to drag the picture in the form using mouse.

推荐答案

MyControl.KeyDown += (sender, eventArgs) => {
    switch switch (eventArgs.KeyCode) {
        case Keys.Left: Image.Left -= step; break;
        case Keys.Right: Image.Left += step; break;
        case Keys.Up: Image.Top -= step; break;
        case Keys.Down: Image.Top += step; break;
    }
}



检查eventArgs.Modifiers并根据是否按下AltControl或Shift选择不同的step也是很好的选择.

—SA



It''s also can be good to check up eventArgs.Modifiers and choose different step depending on if Alt, Control or Shift is pressed.

—SA


private void onKey(object sender, KeyEventArgs e)
{
  switch(e.KeyCode)
  {
     case Keys.Left:
        img.Left++;
        break;
     case Keys.Up:
       img.Top++;
       break;

    ...
  }
}


这篇关于使用按键移动图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 10:59