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

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
In the properties window, add text to the controltiptext field.

Gene, "The Mortgage Man", Klein
 

Logistix

Board Regular
Joined
Aug 23, 2006
Messages
77
Thanks Mortgageman, but i want to change the whole buttons' image to another image when I rollover it.
 

abiram01

New Member
Joined
Aug 30, 2006
Messages
30
I think I understood you correctly. In the buttons moumove event add line of code to change the picture of the button.

Regards,
abhiram
 

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015

ADVERTISEMENT

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
 

Logistix

Board Regular
Joined
Aug 23, 2006
Messages
77
Abhiram, yes that sounds correct. Does anyone know the line/s of code?? Tks
 

Big Monkey

Active Member
Joined
Nov 5, 2005
Messages
255

ADVERTISEMENT

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.
 
L

Legacy 98055

Guest
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>
 

Logistix

Board Regular
Joined
Aug 23, 2006
Messages
77
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
 

Forum statistics

Threads
1,141,730
Messages
5,708,147
Members
421,549
Latest member
Dtcfire

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
Top