vb.net more performance for moving objects - vb.net

I have the mission to make a small game for a school project. Pictures boxes, moved by a timer for walking enemies.If there are around 5 or 6 moving picture boxes at the form, my application get troubles and lags. After I kill some enemies (remove them from the Controls Collection of the Form/Panel) It come back smooth.
I think the loop of the enemy movement is too complicated but I don't know how to make that simpler.
Private Sub TimerEnemyMovement_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerEnemyMovement.Tick
For Each Enemy As Control In PanelBackground.Controls
If Enemy.Name.Substring(0, 5) = "Enemy" Then
_enemy.MoveEnemy(Enemy, 2)
End If
Next
End Sub
I also thought about Multithreading but not sure this would solve the problem and there is also the problem that I can't access the Controls of my mainform.
You see, I don't have much knowledge about vb.net
Any ideas how to fix that lag?

Try this:
Private Sub TimerEnemyMovement_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerEnemyMovement.Tick
SuspendLayout()
For Each Enemy As Control In PanelBackground.Controls
If Enemy.Name.Substring(0, 5) = "Enemy" Then
_enemy.MoveEnemy(Enemy, 2)
End If
Next
ResumeLayout()
End Sub

VB.NET WinForm apps aren't the ideal setup to create games or moving objects. The Painting of the form uses to much performance as you have found out.
Try moving to a VB.NET WPF (Windows Presentation Framework) application it handles graphics a lot better.

Just a couple of suggestions, that may improve speed
a) Rather than going through through all the controls, how about storing them in an array / list
b) old game trick is to draw the scene in memory and then copy it to screen .. so why not have a memory bitmap (or graphic) draw the "enemies" to this and then copy to entire bmp to screen once all is done

Do all the enemies need to move at the same time? Do some move faster?
if there are 6 enemies, you could move 1,3,5 on the first tick, then 2.4.6 on the next tick etc.
?

I've been using XNA for creating a simple game, it was really cool. Though VB.NET is not officially supported, you can make it work. It's optimized for scenery and large number of animated objects.

Related

vb.net desktop app - how to show the drag cursor as an image for a touchscreen

I develop educational desktop (not browser-based) game applications for my wife to use teaching reading to special needs children. She runs the applications on a Microsoft Surface so they can use the touchscreen, getting some kind of physical and visual feedback from the game.
One of their favorite games includes a drag-and-drop feature for moving an image from one part of the screen to another. When using the mouse or the touchpad, the image will show as the cursor while dragging. But when using the touchscreen, the image disappears and what we see is a "chemtrail" following the fingertip. Then the image reappears in the target area when the touch is released.
I would like to make the image stick to the cursor while they are using the touchscreen, but I can't figure out how to do that, and I can't find any answers on the web.
Here's the code that I have:
Private Sub picTurkey1_MouseDown (sender As Object, e As MouseEventArgs) Handles picTurkey1_MouseDown
Dim l_objBitmap As New Bitmap(intBitmapWidth, intBitmapHeight)
sender.DrawToBitmap(l_objBitmap, New Rectangle(0, 0, sender.Width, sender.Height))
CustomCursor = New Cursor(imgTurkeyFlying.GetHicon)
sender.DoDragDrop(l_objBitmap, DragDropEffects.Move)
End Sub
Then:
Private Sub picTurkey1_GiveFeedback(sender As Object, e As GiveFeedbackEventArgs) Handles picTurkey1.GiveFeedback
e.UseDefaultCursors = False
Cursor.Current = Me.CustomCursor
End Sub
imgTurkeyFlying is the image I would like to show as the cursor (it's a Thanksgiving themed game). As I said, this works fine with the mouse or the touchpad, but not with the touchscreen.
Can someone help me see what I am overlooking?
Thanks!

Opening a UserControl using multithreading to avoid UI Freeze VB.NET

I'm a beginner. I've done a lot of research about Backgroundworker/Multithreading, but I still don't know what to do. My problem is the following:
I have a MENU (Which contains just a collection of colored buttons). When one of them is called (Btn), the button opens a new "UserControl". But it takes a long time to load, so the UI freezes for many seconds. I just want to avoid that. I want to show a Loading CircularProgressBar on top of my MENU while the UserControl loads and keeping the UI responsive during the loading.
The button's code :
Private Sub Btn_Click(sender As Object, e As EventArgs) Handles Btn.Click
MainForm.ShowModalPanel(New UserControl1, DevComponents.DotNetBar.Controls.eSlideSide.Bottom)
End Sub
Thank you in advance.

Windows button appearance in a VB 2008 application

I am creating a Win32-application in Visual Basic 2008. I would like to have a button in the form, with custom color (BackColor), on MouseEnter event. This works fine, but as you can see below, this custom color doesn't cover the whole area of the button. The button border remains as standard (Windows 7). Can I somehow have this color for the whole button? I don't want to use Flat button style, I prefer this Standard style, which has the normal Windows look.
What I typically do is this I first gather two buttons that look similar, such as this Pic A and B.
Then I will put in those two picture into my Resources.
After I begin coding the function such as the Example I have made down below.
Now their are two ways of going about this. There is a hover option as seen below:
Private Sub PictureBox1_MouseHover(sender As Object, e As EventArgs) Handles PictureBox1.MouseHover
PictureBox1.Image = My.Resources.Button1
End Sub
or there is this option which has way better response times when moving your mouse:
Private Sub PictureBox1_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox1.MouseEnter
PictureBox1.Image = My.Resources.Button1
End Sub
Then you need this to set things back to normal:
Private Sub PictureBox1_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox1.MouseLeave
PictureBox1.Image = My.Resources.Button
End Sub
Real simple code but in a realistics thats all you need! I typically hate the buttons! I find myself messing with the button look more then I actually code, really simple to just go to Photoshop and make a easy button that looks good!
I read your Question don't worry but to be honest that style button looks old fashion the buttons I made are more up to date and Modern!

Make form always stick to desktop like Win 7 gadget (VB.net)

Ok, I'm aware there's a lots of topis about this, but I haven't found anything that works for me.
My program generates a small picture that i would like to keep in a WinForm that's always at the bottom, "with the desktop". Something like the gadgets in windows 7.
How do i tell my form to always stay here, and just can't be visible over any other form/window?
Should'nt this be doable like in the form_load function for this window?
Like this
Private Sub Sticky_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeepMeAtTheBottomUntillIAmClosed
End Sub
If you create a form that has no border and no controlbox, the user will not be able to resize or move it. Then add your own button to allow it to close, tell it where you want it to be and tada!
You can even add some mouse over functionality to make the tools appear when you are over the form.

Visual Basic 2008 - New Form on a different thread

My “form1” is just a simple page with buttons that launch different forms which do all the work, the "form1" code for the first four buttons is below.
What I want is for each form to run in a separate thread.
Public Class Main
Private Sub btnDownLoadStockPrices_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownLoadStockPrices.Click
LoadStocksFromDownloadSite.Visible = True
End Sub
Private Sub btnLoadOptionsIntoDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadOptionsIntoDatabase.Click
LoadOptionsIntoDatabase.Visible = True
End Sub
Private Sub btnVerifyDatabases_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerifyDatabases.Click
VerifyDatabase.Visible = True
End Sub
Private Sub btnAnalyzeStock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyzeStock.Click
AnalyzeSingleStock.visible = True
End Sub
End Class
I’ve found plenty of code examples to have different buttons on a single form run a subroutine in a separate thread, but for some reason I can’t seem to apply it to an entire form.
I think it’s something simple, like I need to tie each new thread as a handle to each forms “load” routine, but I just can’t get it to work. I don’t care about “synchronizing” threads at all, as each form is really a completely different functional program.
Any help would be much appriciated!
This isn't very common; generally it's best to limit all UI stuff to a single thread. But if you're convinced that you need each form to run on a separate thread, you must take into account the Windows API event handling model. The [over]-simplified version is that each form must have its own message loop to remove event messages from the queue and process them, so if you want to open a form on a new thread, you need to create that message pump.
The easiest way to do that is using the Application.Run method, and let the .NET Framework handle creating that message pump for you. For example:
Dim frm As Form1 = New Form1()
Application.Run(frm)
From looking at the code shown in your question, I can't discern any possible reason why those forms would need to run on separate threads. You can call the Show method of multiple forms so that they will be displayed on the screen at the same time. They won't block each other as long as you don't use the ShowDialog method, which displays each as a modal dialog. This is the way so many applications display multiple toolbox windows and other kinds of forms on the screen at the same time.
If you need to do some type of processor-intensive calculation, you still don't need to run each on a separate thread. Spin up a background thread (the BackgroundWorker class makes this very simple) and update the appropriate form's UI using the Invoke method.
You can certainly do this on Win32 but I don't know how well this maps over to .net.
The essential issue is that window handles have thread affinity. So you really need all interaction with them to happen in that thread. Essentially this means that you create all the window handles associated with that form in its thread. You also need to run a message loop in the thread.
The reason that people usually run all the UI out of the main thread and handle long-running actions in separate threads is that it is easier that way. You should ask yourself again why you want to do it this non-standard way.
I suspect you are not quite seeing the full picture. The need for threads in a desktop app principally arises when you have long running actions. Usually you want to keep your UI responsive and providing feedback for the long running action. Doing so leads to threads.
However, in your proposed solution you now have a multitude of extra threads and complexity, and you are still faced with the original problem! A long running action on one of your forms will hang it unless you perform that action in a separate thread, and once again we reach the standard solution to the problem.