Picturebox在运行时重塑 [英] Picturebox Reshape at runtime

查看:49
本文介绍了Picturebox在运行时重塑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在玩了下面的代码几天后,我很难知道什么是错误的

。由于这是一个较大项目的一部分,我将代码分解为一小部分,在测试表单上运行以调试问题。



它是一个非常基本的形式用图片框164乘130像素。我使用纯色盒子来更好地了解发生了什么。



如果您尝试使用代码,它会缩小盒子,就像我想要的那样。但是当我改变这个过程时,一切都错了。



任何建议都会非常感激...



 公开  Form1 

公开 PBH 作为 整数
公开 PBW 作为 整数
公开作为 整数
公开 locx 作为 整数
公开 locy 作为 整数

私有 Sub Form1_Load(sender As Object ,e As EventArgs)句柄 MyBase .Load

Timer1.Enabled = True ' 打开计时器勾选

PBH = 130 ' 将Picturebox高度设置为130

PBW = 164 ' 将Picturebox宽度设置为164

次= 10 ' 退出前运行例程的计时器数

locx = 60

locy = 60

< span class =code-keyword> End Sub

Private Sub Timer1_Tick(发件人作为 对象 ,e As EventArgs)句柄 Timer1.Tick

执行 ' 启动主循环以收缩PB1

PictureBox1.Height = PBH ' 重置Picturebox 1高度
PictureBox1.Width = PBW ' 重置Picturebox 1宽度

PictureBox1.Location = 点(locx,locy)' 重置xy pos

PBH = PBH - 1 :PBW = PBW - 1 :locx = locx + 1 :locy = locy + 1 ' 减少PB的大小增加位置x,y

调用延迟()' 减慢图形效果

如果 PBH> 0 然后 继续 执行 ' 如果未达到限制,请继续缩小

循环直到PBH = 0 ' 如果达到限制,则转到下一个循环

PBH = 1 :PBW = 35 ' 重置Picturebox 1的起点

' 开始使PB1再次变大

PictureBox1.Height = PBH ' 重置Pict urebox 1高度
PictureBox1.Width = PBW ' 重置Picturebox 1宽度

PictureBox1.Location = 点(locx,locy)' 增加位置x,y

PBH = PBH + 1 :PBW = PBW + 1 :locx = locx - 1 :locy = locy - 1 ' 增加PB1的大小

调用延迟()' 降低图形效果

如果 PBH< 130 然后 继续 执行 ' 继续前行直至达到限制

循环直到PBH = 130 :Timer1.Enabled = False ' 达到限制后,关闭计时器勾选

结束 Sub

Public Sub 延迟()

对于 t = 1 2500000 下一步 t

结束 Sub

结束

解决方案

您好



添加对Update的调用( )强制图片框重绘其内容的方法。



http://msdn.microsoft.com/en-us/library/system.windows.forms.control.update.aspx [ ^ ]



 PictureBox1.Height = PBH ' 重置Picturebox 1高度 
PictureBox1.Width = PBW ' 重置Picturebox 1宽度

PictureBox1.Location = 点(locx,lo cy)' 增加位置x,y
PictureBox1.Update()











Valery

你不应该使用 PictureBox 来获取任何动态,交互式,动画等等。这是可能的,但完全没有意义。很明显,这样做。请看我过去的答案:

如何从旧图纸中清除面板 [ ^ ],

在C#中绘制一个矩形 [ ^ ],

在图片框中附加图片 [ ^ ]。



-SA

After playing with the code below for a few days I stumped at whats going wrong
with the routine. Since this is a part of a larger project I broke the code down to a small section to run on a test form to debug the problem.

Its very basic a form with a picture box 164 by 130 pixels. I use a solid color box to get a better view of what going on.

If you try the code it will shrink the box just like I wanted. But then when I reverse the process it goes all wrong.

Any suggestions would be a greatly appreciated...

Public Class Form1

    Public PBH As Integer
    Public PBW As Integer
    Public times As Integer
    Public locx As Integer
    Public locy As Integer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Timer1.Enabled = True ' Turn On Timer Tick

        PBH = 130 ' Set Picturebox height to 130

        PBW = 164 ' Set Picturebox width to 164

        times = 10 ' Number of timer to run routine before exit

        locx = 60

        locy = 60

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        Do                           ' start main loop to shrink PB1

            PictureBox1.Height = PBH ' Reset Picturebox 1 Height
            PictureBox1.Width = PBW '  Reset Picturebox 1 Width

            PictureBox1.Location = New Point(locx, locy) ' Reset x.y pos

            PBH = PBH - 1 : PBW = PBW - 1 : locx = locx + 1 : locy = locy + 1 ' Decrease size of PB increase location x,y

            Call Delay() ' Slow down graphics for effect

            If PBH > 0 Then Continue Do ' Keep making it smaller if limit not reached

        Loop Until PBH = 0  ' If limit is reached then move to next loop

        PBH = 1 : PBW = 35 ' Reset starting points for Picturebox 1

        Do                           ' Start to make PB1 Larger again

            PictureBox1.Height = PBH ' Reset Picturebox 1 Height
            PictureBox1.Width = PBW '  Reset Picturebox 1 Width

            PictureBox1.Location = New Point(locx, locy) ' Increase location x,y

            PBH = PBH + 1 : PBW = PBW + 1 : locx = locx - 1 : locy = locy - 1 ' Increase size of PB1

            Call Delay() ' Slow down for graphics effect

            If PBH < 130 Then Continue Do ' Keep going until limit is reached

        Loop Until PBH = 130 : Timer1.Enabled = False ' Once limits is reached turnoff timer tick

    End Sub

    Public Sub Delay()

        For t = 1 To 2500000 : Next t

    End Sub

End Class

解决方案

Hello

Add a call to the Update() method to force the picturebox to redraw it''s content.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.update.aspx[^]

PictureBox1.Height = PBH ' Reset Picturebox 1 Height
PictureBox1.Width = PBW '  Reset Picturebox 1 Width

PictureBox1.Location = New Point(locx, locy) ' Increase location x,y
PictureBox1.Update()






Valery


You should never use PictureBox for anything dynamic, interactive, animated, etc. It''s possible, but makes no sense at all. It''s pretty apparent that to do instead. Please see my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].

—SA


这篇关于Picturebox在运行时重塑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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