Mouse Rollover images using the mouse

Logistix

Board Regular
Joined
Aug 23, 2006
Messages
77
Hi there,

Is there anyway of setting up mouse rollover images for my userforms' buttons?

I'm trying to make my new created project look a little fancy too.

Thanks,

Steve
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
In the properties window, add text to the controltiptext field.

Gene, "The Mortgage Man", Klein
 
Upvote 0
Thanks Mortgageman, but i want to change the whole buttons' image to another image when I rollover it.
 
Upvote 0
I think I understood you correctly. In the buttons moumove event add line of code to change the picture of the button.

Regards,
abhiram
 
Upvote 0
I think I understood you correctly. In the buttons moumove event add line of code to change the picture of the button.

Regards,
abhiram

How do you do this. I have never heard of this before.

Gene, "The Mortgage Man", Klein
 
Upvote 0
I don't know how to change the actual image, but I do know the syntax for the mousemove events

This 1st one is for your button

Code:
Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

'do whatever

End Sub

Now a second one so when you move away from the button, it will change the picture back or something.

Code:
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

'do whatever

End Sub

You can also change it to a click event to make it go back to default whenever you click on the form instead of a mousemove

I did something similiar, but I used the .Visible = True/False properties to make objects appear and disappear which was good enough for me.
 
Upvote 0
I have had better luck using frames in place of buttons to spice up the look of a userform. A commandbutton is a virtual (windowless) control whereas a frame is a windowed control. We can get the windows handle of the frame and do "stuff" with it. :)

See the example and post back if you have any questions...

SexyUserformControls2.zip

Uses 4 frames and 1 imagelist control containing 12 pictures. The frame's individual tag properties determine which picture is going to be shown. You do not need the pictures to use the example above but here they are if you want to try and create this from scratch for your own learning...

Hot Tracking Pics.zip

<table width="100%" border="1" bgcolor="White" style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New>  <font color="#0000A0">Option</font> <font color="#0000A0">Explicit</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> GetCursorPos <font color="#0000A0">Lib</font> "user32" (lpPoint <font color="#0000A0">As</font> POINTAPI) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> WindowFromPoint <font color="#0000A0">Lib</font> "user32" (ByVal xPoint <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> yPoint <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> SetCapture <font color="#0000A0">Lib</font> "user32" (ByVal Hwnd <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> ReleaseCapture <font color="#0000A0">Lib</font> "user32" () <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> GetCapture <font color="#0000A0">Lib</font> "user32" () <font color="#0000A0">As</font> <font color="#0000A0">Long</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Type</font> POINTAPI
           x <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
           y <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Type</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Frame1_MouseDown(ByVal Button <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> Shift <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> x <font color="#0000A0">As</font> Single, <font color="#0000A0">ByVal</font> y <font color="#0000A0">As</font> Single)
       MouseDown Frame1
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Frame2_MouseDown(ByVal Button <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> Shift <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> x <font color="#0000A0">As</font> Single, <font color="#0000A0">ByVal</font> y <font color="#0000A0">As</font> Single)
       MouseDown Frame2
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Frame3_MouseDown(ByVal Button <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> Shift <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> x <font color="#0000A0">As</font> Single, <font color="#0000A0">ByVal</font> y <font color="#0000A0">As</font> Single)
       MouseDown Frame3
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Frame4_MouseDown(ByVal Button <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> Shift <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> x <font color="#0000A0">As</font> Single, <font color="#0000A0">ByVal</font> y <font color="#0000A0">As</font> Single)
       MouseDown Frame4
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Frame1_MouseMove(ByVal Button <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> Shift <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> x <font color="#0000A0">As</font> Single, <font color="#0000A0">ByVal</font> y <font color="#0000A0">As</font> Single)
       <font color="#0000A0">Static</font> Hwnd <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       <font color="#0000A0">If</font> Hwnd = 0 <font color="#0000A0">Then</font> Hwnd = GetFrameHwnd()
       HotTrack Frame1, Hwnd, x, y
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Frame2_MouseMove(ByVal Button <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> Shift <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> x <font color="#0000A0">As</font> Single, <font color="#0000A0">ByVal</font> y <font color="#0000A0">As</font> Single)
       <font color="#0000A0">Static</font> Hwnd <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       <font color="#0000A0">If</font> Hwnd = 0 <font color="#0000A0">Then</font> Hwnd = GetFrameHwnd()
       HotTrack Frame2, Hwnd, x, y
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Frame3_MouseMove(ByVal Button <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> Shift <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> x <font color="#0000A0">As</font> Single, <font color="#0000A0">ByVal</font> y <font color="#0000A0">As</font> Single)
       <font color="#0000A0">Static</font> Hwnd <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       <font color="#0000A0">If</font> Hwnd = 0 <font color="#0000A0">Then</font> Hwnd = GetFrameHwnd()
       HotTrack Frame3, Hwnd, x, y
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Frame4_MouseMove(ByVal Button <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> Shift <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> x <font color="#0000A0">As</font> Single, <font color="#0000A0">ByVal</font> y <font color="#0000A0">As</font> Single)
       <font color="#0000A0">Static</font> Hwnd <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       <font color="#0000A0">If</font> Hwnd = 0 <font color="#0000A0">Then</font> Hwnd = GetFrameHwnd()
       HotTrack Frame4, Hwnd, x, y
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> MouseDown(f <font color="#0000A0">As</font> Frame)
       f.Picture = ImgList.ListImages(f.Tag & "_down").Picture
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> HotTrack(f <font color="#0000A0">As</font> Frame, Hwnd <font color="#0000A0">As</font> Long, x <font color="#0000A0">As</font> Single, y <font color="#0000A0">As</font> Single)
       <font color="#0000A0">If</font> (x < 0) <font color="#0000A0">Or</font> (y < 0) <font color="#0000A0">Or</font> (x > f.Width) <font color="#0000A0">Or</font> (y > f.Height) <font color="#0000A0">Then</font>
           ReleaseCapture
           f.Picture = ImgList.ListImages(f.Tag).Picture
       <font color="#0000A0">ElseIf</font> GetCapture() <> Hwnd <font color="#0000A0">Then</font>
           SetCapture Hwnd
           f.Picture = ImgList.ListImages(f.Tag & "_over").Picture
       <font color="#0000A0">End</font> <font color="#0000A0">If</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Function</font> GetFrameHwnd() <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       <font color="#0000A0">Dim</font> PT <font color="#0000A0">As</font> POINTAPI
       GetCursorPos PT
       GetFrameHwnd = WindowFromPoint(PT.x, PT.y)
  <font color="#0000A0">End</font> <font color="#0000A0">Function</font>
</FONT></td></tr></table><button onclick='document.all("96200631916484").value=document.all("96200631916484").value.replace(/<br \/>\s\s/g,"");document.all("96200631916484").value=document.all("96200631916484").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("96200631916484").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="96200631916484" wrap="virtual">
Option Explicit

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function SetCapture Lib "user32" (ByVal Hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function GetCapture Lib "user32" () As Long

Private Type POINTAPI
x As Long
y As Long
End Type

Private Sub Frame1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
MouseDown Frame1
End Sub
Private Sub Frame2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
MouseDown Frame2
End Sub
Private Sub Frame3_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
MouseDown Frame3
End Sub
Private Sub Frame4_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
MouseDown Frame4
End Sub

Private Sub Frame1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
Static Hwnd As Long
If Hwnd = 0 Then Hwnd = GetFrameHwnd()
HotTrack Frame1, Hwnd, x, y
End Sub
Private Sub Frame2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
Static Hwnd As Long
If Hwnd = 0 Then Hwnd = GetFrameHwnd()
HotTrack Frame2, Hwnd, x, y
End Sub
Private Sub Frame3_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
Static Hwnd As Long
If Hwnd = 0 Then Hwnd = GetFrameHwnd()
HotTrack Frame3, Hwnd, x, y
End Sub
Private Sub Frame4_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
Static Hwnd As Long
If Hwnd = 0 Then Hwnd = GetFrameHwnd()
HotTrack Frame4, Hwnd, x, y
End Sub

Private Sub MouseDown(f As Frame)
f.Picture = ImgList.ListImages(f.Tag & "_down").Picture
End Sub

Private Sub HotTrack(f As Frame, Hwnd As Long, x As Single, y As Single)
If (x < 0) Or (y < 0) Or (x > f.Width) Or (y > f.Height) Then
ReleaseCapture
f.Picture = ImgList.ListImages(f.Tag).Picture
ElseIf GetCapture() <> Hwnd Then
SetCapture Hwnd
f.Picture = ImgList.ListImages(f.Tag & "_over").Picture
End If
End Sub

Private Function GetFrameHwnd() As Long
Dim PT As POINTAPI
GetCursorPos PT
GetFrameHwnd = WindowFromPoint(PT.x, PT.y)
End Function</textarea>
 
Upvote 0
Hi Tom,

I'm trying to get an understanding of the above code but do not know how to setup a image list. Is there some sort of trick getting the images in there??

Thanks
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,238
Members
448,555
Latest member
RobertJones1986

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top