C# 如何使用列表框中的箭头键移动picturebox?
在1+图片框中,一切正常。我可以使用以下代码:C# 如何使用列表框中的箭头键移动picturebox?,c#,.net,visual-studio,multithreading,C#,.net,Visual Studio,Multithreading,在1+图片框中,一切正常。我可以使用以下代码: public Form1() { InitializeComponent(); KeyDown += new KeyEventHandler(Form1_KeyDown); } private void Form1_KeyDown(object sender, KeyEventArgs e) { int
public Form1()
{
InitializeComponent();
KeyDown += new KeyEventHandler(Form1_KeyDown);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.Right) x += 5;
else if (e.KeyCode == Keys.Left) x -= 5;
else if (e.KeyCode == Keys.Up) y -= 5;
else if (e.KeyCode == Keys.Down) y += 5;
pictureBox1.Location = new Point(x, y);
}
但是;
如果我添加listbox来写入位置picturebox,我不能用箭头键移动picturebox,我该怎么做?您需要这样做,它将重新绘制您的UI;以便查看您所做的更改
您可以选择使用重载Invalidate(Region-Region)
重新绘制某些区域,也可以通过执行control.Invalidate()
使整个控件无效
尝试添加:this.Invalidate()代码>创建新的点后
您还可以使用将使控件无效并强制控件重新绘制的
下面是一篇MSDN文章,您可能希望阅读该文章并将其与当前代码进行比较。您可能希望将KeyDown改为KeyPress。而且,正如我在评论中所说的,它确实应该可以工作,而不必使用Invalidate()
或Refresh()
强制表单重新绘制。如果列表框获得焦点,而不是表单,那么您可能需要监听listboxs的keydown事件
编辑:
使用此可聚焦PictureBox并收听其previewkeydown事件:
public class PictureBoxEx : PictureBox
{
public PictureBoxEx()
{
this.SetStyle(ControlStyles.Selectable, true);
}
protected override void OnClick(EventArgs e)
{
this.Focus();
base.OnClick(e);
}
}
编辑:
或者使用以下命令。使用此选项,您不必移动表单中的控件,只需移动控件中的图像即可
public class PictureBoxEx : Control
{
public PictureBoxEx()
{
this.SetStyle(ControlStyles.Selectable | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnClick(EventArgs e)
{
this.Select();
base.OnClick(e);
}
private Image _Image;
public Image Image
{
get
{
return _Image;
}
set
{
_Image = value;
this.Invalidate();
}
}
private Point _ImageLocation = new Point(0,0);
public Point ImageLocation
{
get
{
return _ImageLocation;
}
set
{
_ImageLocation = value;
this.Invalidate();
}
}
private int _ImageLocationLeft = 0;
public int ImageLocationLeft
{
get
{
return _ImageLocationLeft;
}
set
{
_ImageLocationLeft = value;
ImageLocation = new Point(value, ImageLocationTop);
}
}
private int _ImageLocationTop = 0;
public int ImageLocationTop
{
get
{
return _ImageLocationTop;
}
set
{
_ImageLocationTop = value;
ImageLocation = new Point(ImageLocationLeft, value);
}
}
protected override void OnPaint(PaintEventArgs pe)
{
if (Image != null)
{
pe.Graphics.DrawImage(Image, ImageLocation);
}
base.OnPaint(pe);
}
protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
{
if (e.KeyData == Keys.Up || e.KeyData == Keys.Down || e.KeyData == Keys.Left || e.KeyData == Keys.Right)
e.IsInputKey = true;
base.OnPreviewKeyDown(e);
}
}
只是好奇,你为什么要标记这个多线程?你能解释一下关于列表框的部分吗?这还不太清楚。谢谢但不起作用:pictureBox1.Location=新点(x,y);这个。使无效();这个。刷新()@菲西卡,什么都没发生?您确定事件已触发吗?即使没有失效/刷新,它也应该可以工作,但可以肯定的是,现在就把它们放在那里。在那个事件中放置一个断点,看看它是否会触发。@Phsika,你不明白哪一部分?我会尽力澄清的;它与线程相关,因为它们是在表单加载中加载的,但如果我单击箭头键移动我的框,则所有内容都将被加载locked@Phsika,你能放一个MessageBox.Show(@“键盘按下!”)吗
在表单1\u键下
查看它是否实际被调用?否则它可能会像@Wowa所说的那样,你的ListBox
从表单中获取焦点,因此,你的击键不会被捕获。