 |
 |

03-28-2008, 02:50 PM
|
 |
Freshman
|
|
Join Date: Mar 2008
Posts: 49
|
|
problem with gdi
|
|
hello, im new to gdi and im trying to make a game where you can move a tank that i made in paint. it works, however when i move the tank using the arrow keys the rectangle i made in gdi moves but the tank doesnt. its kind of hard to explain but its kind of like there is a tank wallpaper and you can only see it through the rectangle.
heres the code for the project:
Code:
Public Class Form1
Dim tank1 As Integer = 28
Dim tank2 As Integer = 45
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
If keyData = Keys.Up Then
tank2 -= 2
Me.Invalidate()
End If
If keyData = Keys.Down Then
tank2 += 2
Me.Invalidate()
End If
If keyData = Keys.Right Then
tank1 += 2
Me.Invalidate()
End If
If keyData = Keys.Left Then
tank1 -= 2
Me.Invalidate()
End If
End Function
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
Dim tank As Bitmap = New Bitmap("C:\Documents and Settings\Owner\My Documents\JP\project resources\tank\tank1.bmp")
Dim tankBrush As TextureBrush = New TextureBrush(tank)
Dim tankrect As Rectangle = New Rectangle(tank1, tank2, 28, 45)
g.FillRectangle(tankBrush, tankrect)
End Sub
End Class
|
Last edited by optox; 03-28-2008 at 03:49 PM.
|

03-28-2008, 03:19 PM
|
 |
Ultimate Contributor
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,598
|
|
There's a few problems here; I'll start with the most important and move to the least.
First, it's against the Posting Guidelines that you agreed to at registration to post an executable. Executables can contain malicious code. This is a programming forum, it's perfectly acceptable to post your project (with executables removed). We can build and run the application and thus be guaranteed that the program is not malicious. Please remove the current attachment and attach one that has no executable.
Second, you didn't include tank1.bmp so the application won't work.
Third, I believe the problem might be a need to specify a bounding rectangle for the texture brush, but I am not certain since I don't have a working copy of the program or a screenshot that demonstrates the problem.
Finally, it's not proper to override ProcessCmdKey to handle the arrow keys, and you've implemented it incorrectly:
Quote:
|
When overriding the ProcessCmdKey method in a derived class, a control should return true to indicate that it has processed the key. For keys that are not processed by the control, the result of calling the base class's ProcessCmdKey method should be returned. Controls will seldom, if ever, need to override this method.
|
Your code returns a default value, which means you're eating any input keys that come through the ProcessCmdKey method. The more appropriate way to do this is to override IsInputKey and return true for the arrow keys; then input will be handled by the standard keyboard events such as KeyUp.
|
|

03-28-2008, 03:45 PM
|
 |
Freshman
|
|
Join Date: Mar 2008
Posts: 49
|
|
|
|
sorry, i didnt know i wasnt supposed to attach an executable, it wont happen again.
|
|

03-28-2008, 03:49 PM
|
 |
Ultimate Contributor
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,598
|
|
Have you tried using the overload of the TextureBrush constructor that lets you specify a bounding rectangle for the image? Try using:
Code:
Dim tankBrush As TextureBrush = New TextureBrush(tank, tankrect)
Of course, this requires moving the line beneath where you declare tankrect.
|
|

03-28-2008, 03:55 PM
|
 |
Freshman
|
|
Join Date: Mar 2008
Posts: 49
|
|
hmm, that didn't work it says "out of memory exception was unhandled"
|
|

03-28-2008, 03:57 PM
|
 |
Freshman
|
|
Join Date: Mar 2008
Posts: 49
|
|
|

03-29-2008, 07:53 AM
|
 |
Ultimate Contributor
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,598
|
|
"That didn't work" is not sufficient to convince me that this is not the solution. It would have been nice if you would have posted your modified code, since an out of memory exception is generally something you cause by doing something incorrectly. I'm going to roll my own version of this form from scratch and I'll get back to you if I can recreate the exception, but in the meantime can you post what you tried?
|
|

03-29-2008, 10:51 AM
|
 |
Ultimate Contributor
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,598
|
|
It seems like it does cause an out of memory exception, which seems to be a bug. I'm going to experiment with it more, but while I was working on it I realized we're barking up the wrong tree.
TextureBrush is intended to be used with some tiled texture to make an area have a pattern. What you are doing isn't really an intended use for TextureBrush; you are trying to draw an image at a certain location, something that Graphics.DrawImage does. The following code works, has less flicker, and handles the arrow keys the right way; just change "error.png" to whatever the path to your image file is.
Code:
Public Class Form1
Dim tankImage As Image
Dim tankRect As Rectangle
Dim tank1 As Integer = 28
Dim tank2 As Integer = 45
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer, True)
' Add any initialization after the InitializeComponent() call.
tankRect = New Rectangle(28, 45, 28, 45)
tankImage = Image.FromFile("error.png")
End Sub
Protected Overrides Function IsInputKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select keyData
Case Keys.Up, Keys.Down, Keys.Left, Keys.Right
Return True
Case Else
Return MyBase.IsInputKey(keyData)
End Select
End Function
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
Select Case e.KeyData
Case Keys.Up
tankRect.Y -= 2
Case Keys.Down
tankRect.Y += 2
Case Keys.Left
tankRect.X -= 2
Case Keys.Right
tankRect.X += 2
Case Else
MyBase.OnKeyDown(e)
End Select
Me.Invalidate()
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
g.DrawImage(tankImage, tankRect)
End Sub
End Class
|
|

03-29-2008, 11:10 AM
|
 |
Ultimate Contributor
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,598
|
|
I at least understand why there was an out of memory exception when using the bounding rectangle. Suffice to say it wasn't going to do what I wanted; it looks like the bounding rectangle is more useful when you only want to tile part of an image. Nonetheless, you could have solved the problem via the TranslateTransform method, but it would have required calculating the appropriate offsets. It's much easier to just use DrawImage.
|
|

04-02-2008, 03:45 PM
|
 |
Freshman
|
|
Join Date: Mar 2008
Posts: 49
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
| |
|
|
|
 |
|