Results 1 to 7 of 7

Thread: Moving an image

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Location
    Sealand
    Posts
    6

    Question

    How would i make an image move across the screen so that when you hold down a certain button it moves in that direction.

    Also if i take an image from a webpic how would I take out the background of that webpic, so It will match the background of the form, or the pic thats behind it...

    Sorry im teaching myself how to program

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Not too hard to move the image with keys:
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        With Image1
            Select Case KeyCode
                Case vbKeyUp
                    .Top = .Top - 100
                Case vbKeyDown
                    .Top = .Top + 100
                Case vbKeyLeft
                    .Left = .Left - 100
                Case vbKeyRight
                    .Left = .Left + 100
            End Select
        End With
    End Sub
    if you have several controls on the form, set keypreview to true and it will always fire the forms keydown event even if the controls have focus
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Guest
    If you want support for multipule key presses then use GtAsyncKeyState API instead.

    Add the following code to a Form with a Timer and a PictureBox.
    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Form_Load()
        Timer1.Interval = 1
    End Sub
    
    Private Sub Timer1_Timer()
        If GetAsyncKeyState(vbKeyLeft) Then Picture1.Left = Picture1.Left - 100
        If GetAsyncKeyState(vbKeyRight) Then Picture1.Left = Picture1.Left + 100
        If GetAsyncKeyState(vbKeyUp) Then Picture1.Top = Picture1.Top - 100
        If GetAsyncKeyState(vbKeyDown) Then Picture1.Top = Picture1.Top + 100
    End Sub

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Meg, the timer method won't execute more frequently than once in 53ms and Getasynckeystate could miss some of the keys, on the other hand you will catch all the keydown events.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    Meg and Keda, what's the thing about GetAsyncKeyState(). I'm looking forward to implement some code to create a software keyboard (for my touchscreen monitor).
    I tried to use GetAsyncKeyState(), but I frequently receive strange chars, while opening i.e. Notepad, typing some text, and returning to my app.
    Is there any given time table for this API, because chars are repeated while holding down a key...?

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Compie, have an array of all keys (I know i've done this with DirectInput) Then in the method you call frequently, compare all values in the array, this is a bit slow (maybe you could xor them with some apis) and then if an item has changed, then the button has been released or been pressed,check the original array.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    Yes, this is the code I had in mind.
    I found it in the API guide on http://www.allapi.net.

    TimerProc() does the equality comparison.

    But when I run the code and goto the start menu, run, "notepad" and typing some chars then returning to VB,
    some strange prefix chars are being displayed.

    Code:
    'In a module
    Public Const DT_CENTER = &H1
    Public Const DT_WORDBREAK = &H10
    Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Declare Function DrawTextEx Lib "user32" Alias "DrawTextExA" (ByVal hDC As Long, ByVal lpsz As String, ByVal n As Long, lpRect As RECT, ByVal un As Long, ByVal lpDrawTextParams As Any) As Long
    Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    Global Cnt As Long, sSave As String, sOld As String, Ret As String
    Dim Tel As Long
    Function GetPressedKey() As String
        For Cnt = 32 To 128
            'Get the keystate of a specified key
            If GetAsyncKeyState(Cnt) <> 0 Then
                GetPressedKey = Chr$(Cnt)
                Exit For
            End If
        Next Cnt
    End Function
    Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
        Ret = GetPressedKey
        If Ret <> sOld Then
            sOld = Ret
            sSave = sSave + sOld
        End If
    End Sub
    
    'In a form
    Private Sub Form_Load()
        'KPD-Team 1999
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Me.Caption = "Key Spy"
        'Create an API-timer
        SetTimer Me.hwnd, 0, 1, AddressOf TimerProc
    End Sub
    Private Sub Form_Paint()
        Dim R As RECT
        Const mStr = "Start this project, go to another application, type something, switch back to this application and unload the form. If you unload the form, a messagebox with all the typed keys will be shown."
        'Clear the form
        Me.Cls
        'API uses pixels
        Me.ScaleMode = vbPixels
        'Set the rectangle's values
        SetRect R, 0, 0, Me.ScaleWidth, Me.ScaleHeight
        'Draw the text on the form
        DrawTextEx Me.hDC, mStr, Len(mStr), R, DT_WORDBREAK Or DT_CENTER, ByVal 0&
    End Sub
    Private Sub Form_Resize()
        Form_Paint
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        'Kill our API-timer
        KillTimer Me.hwnd, 0
        'Show all the typed keys
        MsgBox sSave
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width