如何使用箭头键在面板内的图片框中滚动图像 [英] How to scroll the image in picturebox inside the panel using arrow keys
问题描述
如何使用箭头键滚动面板内的图片框中的图像
我可以使用以下代码在picturebox keydown事件中滚动图片框中的图像
if(e.KeyCode == Keys.Left)
{
pictureboxImage.Left = pictureboxImage.Left + 10;
}
它正确滚动但问题是当我们连续按箭头键时它会在面板外流动。有时面板将是空白的,因为图片框将在面板之外。
但是当我们使用鼠标滚动它完美时,我想在箭头按键事件上执行相同的步骤。
请帮帮我。
谢谢
How to scroll the image in picture box inside the panel using arrow keys
I can scroll the images in picturebox using the below code on picturebox keydown event
if (e.KeyCode == Keys.Left)
{
pictureboxImage.Left = pictureboxImage.Left + 10;
}
it scrolls correctly but the problem is when we press arrow keys continuosly it flows outside the panel. Panel will be blank for sometimes because picturebox will be outside the panel.
but when we use mouse to scroll it works perfectly, I want same steps on arrow keypress event.
Please help me on this.
Thanks
推荐答案
基本上你需要检查你是不是超出了图片框。
1.添加一个面板
在表单中调用panel1
。
2.设置属性form1.PreviewKey = true
3.设置属性panel1.AutoScroll = true
4.设置propertypanel1.TabStop = true
(为了能够将焦点设置到控件上)
5.添加面板内的PictureBox
并调用它pictureBox1
6.设置属性pictureBox1.SizeMode = AutoSize
然后添加这样的代码。
步骤siz e当然取决于你自己的愿望。
Basically you need to check that you are not out of bounds of the picture box.
1. Add aPanel
calledpanel1
to the Form.
2. Set the propertyform1.PreviewKey = true
3. Set the propertypanel1.AutoScroll = true
4. Set the propertypanel1.TabStop = true
(in order to be able to set focus to the control)
5. Add thePictureBox
inside the panel and call itpictureBox1
6. Set the propertypictureBox1.SizeMode = AutoSize
Then add the code like this.
The step size is of course up to your own desire.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
if ((panel1.HorizontalScroll.Value + panel1.HorizontalScroll.SmallChange) >= panel1.HorizontalScroll.Maximum)
panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Maximum;
else
panel1.HorizontalScroll.Value += panel1.HorizontalScroll.SmallChange;
}
else if (e.KeyCode == Keys.Left)
{
if ((panel1.HorizontalScroll.Value - panel1.HorizontalScroll.SmallChange) < panel1.HorizontalScroll.Minimum)
panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Minimum;
else
panel1.HorizontalScroll.Value -= panel1.HorizontalScroll.SmallChange;
}
// Add similar code for the up and down arrows
}
也许这个CodeProject文章可以帮助你进一步。
C#中的快速图像滚动 [ ^ ]
这篇关于如何使用箭头键在面板内的图片框中滚动图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!