[英]Zooming in ComboBox not working as expected
I have an application that scans an image to display on the application. 我有一个扫描图像以显示在应用程序上的应用程序。 After the image is scanned, I have the option to zoom the image. 扫描图像后,我可以选择缩放图像。 There's a combo box on the application that display the zoom percentage as well. 应用程序上有一个组合框,可同时显示缩放百分比。
I can zoom fine using my mouse wheel and the combo box % changes accordingly which is fine. 我可以使用鼠标滚轮进行精细缩放,并且组合框%会相应更改,这很好。 The problem happens if I manually select the combo box and select a zoom percentage, say 50%, then there's no changes at all. 如果我手动选择组合框并选择缩放百分比(例如50%),则会发生问题,那么根本没有任何更改。
Code: 码:
private void ImageBox_ZoomLevelsChanged(object sender, EventArgs e)
{
this.FillZoomLevels();
}
private void ZoomComboBox_Click(object sender, EventArgs e)
{
}
private void FillZoomLevels()
{
ZoomComboBox.Items.Clear();
foreach (int zoom in ImageBox.ZoomLevels)
ZoomComboBox.Items.Add(string.Format("{0}%", zoom));
}
Am I doing anything wrong? 我做错什么了吗? Appreciate any help. 感谢任何帮助。
When there is more than one control on the Panel
, and the ScrollBars
are shown, the MouseWheel
event of the Panel
would be raised, but if there's only a PictureBox
on the Panel
whose Image
large enough to make the panel's ScrollBars
visible, then the MouseWheel
event of the Panel
won't be raised, instead, the Form's MouseWheel
event fires. 当存在一个以上的控制Panel
,和ScrollBars
显示,该MouseWheel
的事件Panel
会提高,但如果只有一个PictureBox
的上Panel
,其Image
足够大,使面板的ScrollBars
可见,那么MouseWheel
不会引发Panel
事件,而是触发Form的MouseWheel
事件。
In the following sample, I just add a PictureBox
onto the Panel
without any other controls added, set a large Image
to the PictureBox
which make the Panel
's ScrollBars
invisible, since there's only one control on the panel, the MouseWheel
event of the Panel
won't be raised, but the Form's MouseWheel
event be raised, we handle this event instead, handle the Form's KeyDown
and KeyUp events as well. 在下面的示例中,我只需添加一个PictureBox
到Panel
不加任何其他控件,设置一个大Image
的PictureBox
,其使Panel
的ScrollBars
不可见的,因为只有一个在面板上的控制, MouseWheel
的事件Panel
不会引发,但是会引发Form的MouseWheel
事件,我们将改为处理此事件,并同时处理Form的KeyDown
和KeyUp事件。
Furthermore, set the SizeMode of the PictureBox
to StretchImage instead of AutoSize
, thus when we change the size of the PictureBox
, the Image
in the PictureBox
will resize to fit the PictureBox
. 此外,设置的SizeMode PictureBox
来StretchImage而不是AutoSize
,因此当我们改变大小PictureBox
时, Image
的PictureBox
将调整以适应PictureBox
。
The PictureBox.Scale()
method won't help you in this screnario, change the size of the PictureBox
instead. 在这种情况下, PictureBox.Scale()
方法将无济于事,请改用PictureBox
的大小。
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
bool ctrlKeyDown;
bool shiftKeyDown;
private void Form4_Load(object sender, EventArgs e)
{
this.ctrlKeyDown = false;
this.shiftKeyDown = false;
//If there's only PictureBox control on the panel, the MouseWheel event of the form raised
//instead of the MouseWheel event of the Panel.
this.MouseWheel += new MouseEventHandler(Form4_MouseWheel);
this.KeyDown += new KeyEventHandler(Form4_KeyDown);
this.KeyUp += new KeyEventHandler(Form4_KeyUp);
//this is important for zooming the image
this.pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
}
void Form4_KeyUp(object sender, KeyEventArgs e)
{
this.ctrlKeyDown = e.Control;
this.shiftKeyDown = e.Shift;
}
void Form4_KeyDown(object sender, KeyEventArgs e)
{
this.ctrlKeyDown = e.Control;
this.shiftKeyDown = e.Shift;
}
void Form4_MouseWheel(object sender, MouseEventArgs e)
{
bool IsGoUp = e.Delta > 0 ? true : false;
if (this.ctrlKeyDown)
{
if (IsGoUp && this.panel1.HorizontalScroll.Value > 5)
{
this.panel1.HorizontalScroll.Value -= 5;
}
if (!IsGoUp && this.panel1.HorizontalScroll.Value < this.panel1.HorizontalScroll.Maximum - 5)
{
this.panel1.HorizontalScroll.Value += 5;
}
}
else if (this.shiftKeyDown)
{
int hStep = (int)(this.pictureBox2.Image.Width * 0.02);
int vStep = (int)(this.pictureBox2.Image.Height * 0.02);
if (IsGoUp)
{
this.pictureBox2.Width += hStep;
this.pictureBox2.Height += vStep;
}
else
{
this.pictureBox2.Width -= hStep;
this.pictureBox2.Height -= vStep;
}
}
else
{
if (IsGoUp && this.panel1.VerticalScroll.Value > 5)
{
this.panel1.VerticalScroll.Value -= 5;
}
if (!IsGoUp && this.panel1.VerticalScroll.Value < this.panel1.VerticalScroll.Maximum - 5)
{
this.panel1.VerticalScroll.Value += 5;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.