View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default use of arrow keys in Excel

I haven't looked into it in great detail, but I think moving an Image
control with the arrow keys may prove to be somewhat hard to do. However, I
do have a routine that will allow you to move the Image control around with
your mouse. Here is that code for that if you can make use of it. Copy/Paste
the code below into the UserForm's code window (if you have any existing
MouseDown or MouseMove event code, you will have to integrate it into the
event procedures below). As coded, you need to press the Shift key when you
left click the mouse on the Image control (and keep them depressed) while
you drag the picture around. If you want to use the left mouse button all by
itself, then change this line in the MouseMove event....

If Button = 1 And Shift = 1 Then

to this...

If Button = 1 Then

Note... I used the Shift key coupled with the left mouse button for its
definitional description (Shift meaning *shift* the picture).

'*************** START OF CODE ***************
Private Type POINTAPI
X As Long
Y As Long
End Type

Private Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long

Private Declare Function GetDC Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function GetDeviceCaps Lib "gdi32" _
(ByVal hDC As Long, _
ByVal nIndex As Long) As Long

Private Declare Function ReleaseDC Lib "user32" _
(ByVal hwnd As Long, _
ByVal hDC As Long) As Long

Private Const LOGPIXELSX = 88 'Pixels/inch in X

'A point is defined as 1/72 inches
Private Const POINTS_PER_INCH As Long = 72

Dim Initialized As Boolean

'The size of a pixel, in points
Private Function PointsPerPixel() As Double
Dim hDC As Long
Dim lDotsPerInch As Long
hDC = GetDC(0)
lDotsPerInch = GetDeviceCaps(hDC, LOGPIXELSX)
PointsPerPixel = POINTS_PER_INCH / lDotsPerInch
ReleaseDC 0, hDC
End Function

Private Sub Image1_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
Static LastPoint As POINTAPI
Dim CurrentPoint As POINTAPI
If Button = 1 And Shift = 1 Then
GetCursorPos CurrentPoint
If Not Initialized Then
LastPoint.X = CurrentPoint.X
LastPoint.Y = CurrentPoint.Y
Initialized = True
End If
With Image1
.Move .Left + PointsPerPixel * (CurrentPoint.X - LastPoint.X), _
.Top + PointsPerPixel * (CurrentPoint.Y - LastPoint.Y)
End With
LastPoint = CurrentPoint
End If
End Sub

Private Sub Image1_MouseUp(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
Initialized = False
End Sub
'*************** END OF CODE ***************

--
Rick (MVP - Excel)


"Dean" wrote in message
...
On Jan 11, 7:44 pm, "Rick Rothstein"
wrote:
What object are you trying to move? Where is the object at (the worksheet
or
a UserForm)?

--
Rick (MVP - Excel)

"Dean" wrote in message

...



Hey, I am woundering for a simple game that I am making, wheather it
will be possible to use the arrow keys to move an object, right now I
have been using a command button, and using the acceleratior option,
but it is annoying to put alt + another key,
if you could tell me this I will be truly greatful- Hide quoted text -


- Show quoted text -


I'm using a userform, control toolbox and I just want to move a
picture left and right
at the moment I am using this code

Dim i As Integer

Private Sub Cmdright_Click()
i = i + 1
Image1.Left = i
End Sub

thanks