加快该代码的速度,该代码用于使控件绘制“正确"的图形.透明背景? [英] Speed up this code used to have a control draw a "proper" transparent background?

查看:94
本文介绍了加快该代码的速度,该代码用于使控件绘制“正确"的图形.透明背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码(源自此旧的CodeProject文章)以绘制更合适的透明效果 通过在用户控件后面创建每个控件的位图,然后将每个位图绘制到背景上来实现背景.

I have this code (originating from this old CodeProject article) to draw a more proper transparent background on a usercontrol by creating a bitmap of each control behind it and then drawing each bitmap onto the background.

问题是我在使用用户控件制作的游戏中使用了此代码,并且每个控件都使用此代码,因此当玩家经过其中的某些控件并且它们彼此重叠时,它们都将在同一位置重绘导致游戏落后的时间.

The problem is that I'm using this for a game made out of usercontrols, and each control uses this code so when the player walks over some of them and they're overlapping each other, they will all redraw at the same time which causes the game to lag.

有人知道我如何改进代码,或者有人知道更好的方法吗?

Does anyone know how I can improve the code, or does anyone know a better way to do this?

Protected Overrides Sub OnPaintBackground(e As System.Windows.Forms.PaintEventArgs)
    MyBase.OnPaintBackground(e)

    If Parent IsNot Nothing Then
        Dim index As Integer = Parent.Controls.GetChildIndex(Me)

        For i As Integer = Parent.Controls.Count - 1 To index + 1 Step -1
            Dim c As Control = Parent.Controls(i)
            If c.Bounds.IntersectsWith(Bounds) AndAlso c.Visible = True Then
                Dim bmp As New Bitmap(c.Width, c.Height, e.Graphics)
                c.DrawToBitmap(bmp, c.ClientRectangle)
                e.Graphics.TranslateTransform(c.Left - Left, c.Top - Top)
                e.Graphics.DrawImageUnscaled(bmp, Point.Empty)
                e.Graphics.TranslateTransform(Left - c.Left, Top - c.Top)
                bmp.Dispose()
            End If
        Next
    End If
End Sub


谢谢!


Thanks!

我希望你的日子比昨天更好,但是今天比明天更糟...
如果我解决了您的问题,请标记为答案. :)

I hope your day has been better than yesterday, but that it's worse than tomorrow...
Please mark as answer if I solved your problem. :)

推荐答案

 游戏不应将控件用作角色和对象.这就是为什么它落后.在VB.Net中合适的游戏应该将所有内容直接绘制到表单上,小组,甚至是Picturbox的图形对象 使用图形绘制方法".您将对包含所有有关它们的信息的字符和对象使用Class,例如, 名称,位置,尺寸以及绘制角色和对象所需的任何其他信息 在正确的位置.

 A game should not be using controls as the characters and objects.  That is why it is lagging.  A proper game in VB.Net should be drawing everything directly onto the Form,  a Panel,  or even a Picturbox`s  Graphics object using Graphics Drawing Methods.  You would use a Class for the characters and objects that hold all the information about them such as,  Names,  Locations,  Sizes,  and any other info that is needed to draw the characters and objects in their correct location.

 不幸的是,使用控件作为字符和对象,除了使用更少的控件外,实际上没有其他方法可以加快它的速度.这是人们在VB.Net中尝试创建游戏时常犯的错误.

 Unfortunately,  using controls as the characters and objects,  there really is no way to speed it up except using fewer controls.  This is a common mistake that people make when trying to create games in VB.Net.

 您可以从下面由Reed Kimble撰写的第一个链接中获得有关此信息的更多信息.第二个链接包含Reed Kimble展示的游戏的开始代码.它应该给你一个基本的概念 我的意思是关于对字符和对象使用类.

 You can get a little more info about this from the first link below that was written by Reed Kimble a while back.  The second link contains the beginning code of a game that Reed Kimble has shown.  It should give you a basic idea of what i mean about using Classes for the characters and objects.

VB.Net:如何创建视频游戏(Windows窗体)

具有Reed游戏代码示例的代码

这篇关于加快该代码的速度,该代码用于使控件绘制“正确"的图形.透明背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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