在PictureBox控件中缩放图像会导致PictureBox在窗体上移动 [英] Zooming image in a PictureBox control causes the PictureBox to move on the form

查看:53
本文介绍了在PictureBox控件中缩放图像会导致PictureBox在窗体上移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。 当恐龙统治地球和Windows NT 4是"新技术"时,我是VB6中的MCSE和程序员。 我已经转向了生活中的其他事情(我在大约5年内没有做任何真正的编码,那是在VB6中)但决定为我自己写一个特定的程序
,哇,没有技术变了一堆?  Visual Studio Visual Basic 2017不是VB6。 我说我过去的所有内容都是为了解释我对这些东西的理解(形式宽度,高度,像素,变量,
subs等等),但却无法让一些简单的东西正常工作。


我在表单上放了一个PictureBox控件,并在这里抓取一些代码,根据MouseWheel事件缩放显示的图像。 但是,当我放大和缩小时,PictureBox的左侧位置会发生变化。 我用左右位置更新标题栏
,它表示"30-30"。每次更新缩放时,图像(PictureBox)在放大和缩小时从左向右移动。


我检查了窗体的宽度和高度;它们在缩放操作期间不会更改。 我已经检查了PictureBox的宽度和高度,它们确实发生了变化。 由于我们从左侧和顶部测量PictureBox的属性,我想
认为盒子会变得更高更宽,但是与表格的左侧和顶部保持相同的距离。 / p>

有人可以告诉我,如果我做了些蠢事吗? 谢谢。


这里是缩放代码:


Private Sub PictureBox1_MouseWheel(ByVal sender As System.Object,ByVal e As MouseEventArgs)处理PictureBox1.MouseWheel

       如果e.Delta< D> 0然后

           如果e.Delta< = 0那么
b $ b               如果PictureBox1.Width< 250然后退出子'最低50?

           否则b $ b               如果PictureBox1.Width> 1000然后退出子'最多500个?

           结束如果

            PictureBox1.Width + = CInt(PictureBox1.Width * e.Delta / 1000)

            PictureBox1.Height + = CInt(PictureBox1.Height * e.Delta / 1000)



          ;&NBSP;&NBSP; PictureBox1.Left = PictureBoxLeft

            PictureBox1.Top = PictureBoxTop



            Me.Text = CStr(PictureBox1.Left)& " - " &安培; CStr(PictureBox1.Top)

            'Me.Text = CStr(PictureBox1.Width)& " - " &安培; CStr(PictureBox1.Height)

            'Me.Text = CStr(Me.Width)& " - " &安培; CStr(Me.Height)



       结束如果

解决方案

在您的代码中设置(PictureBoxLeft)的位置。 这似乎是我在该代码中看到的唯一设置PictureBox1的Left位置的部分。 您是否尝试将其设置为固定值以查看它是否移动?

 PictureBox1.Left = PictureBoxLeft 

'也许只需尝试一个固定的值来测试它,看它是否移动...

PictureBox1.Left = 10'或任何数字


Hello, all.  When dinosaurs ruled the Earth and Windows NT 4 was "new technology", I was an MCSE and a programmer in VB6.  I've moved to other things in life (I've not done any real coding in about 5 years and that was in VB6) but decided to write a specific program for myself and, wow, hasn't the technology changed a bunch?  Visual Studio Visual Basic 2017 is not VB6.  I say all that about my past to explain how I understand a lot of this stuff (form width, height, pixels, variables, subs and so forth), and yet cannot get some simple stuff to work properly.

I put a PictureBox control on a form and grabbed some code here to zoom the displayed image based on the MouseWheel event.  What happens, though, is that as I zoom in and out, the Left position of the PictureBox changes.  I update the titlebar with the left and right positions and it indicates "30 - 30" at each update of the zoom, but the image (PictureBox) moves from left to right as I zoom in and out.

I've checked the Form width and height; they do not change during the zoom operations.  I've checked the PictureBox width and height and they do change.  Since we're measuring from the left and the top for the properties of the PictureBox, I would think the box would just get taller and wider, but keep the same distance from the left and the top of the form.

Can someone tell me if I'm doing something stupid, please?  Thank you.

Here's zoom code:

Private Sub PictureBox1_MouseWheel(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseWheel
        If e.Delta <> 0 Then
            If e.Delta <= 0 Then
                If PictureBox1.Width < 250 Then Exit Sub 'minimum 50?
            Else
                If PictureBox1.Width > 1000 Then Exit Sub 'maximum 500?
            End If
            PictureBox1.Width += CInt(PictureBox1.Width * e.Delta / 1000)
            PictureBox1.Height += CInt(PictureBox1.Height * e.Delta / 1000)

            PictureBox1.Left = PictureBoxLeft
            PictureBox1.Top = PictureBoxTop

            Me.Text = CStr(PictureBox1.Left) & " - " & CStr(PictureBox1.Top)
            'Me.Text = CStr(PictureBox1.Width) & " - " & CStr(PictureBox1.Height)
            'Me.Text = CStr(Me.Width) & " - " & CStr(Me.Height)

        End If

解决方案

Where does (PictureBoxLeft) get set in your code.  That seems to be the only part I see in that code that sets the Left location of PictureBox1.  Have you tried just setting that to a fixed value to see if it moves?

 PictureBox1.Left = PictureBoxLeft

'maybe just try a fixed value to test it and see if it moves...

 PictureBox1.Left = 10 'or whatever number


这篇关于在PictureBox控件中缩放图像会导致PictureBox在窗体上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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