Results 1 to 22 of 22

Thread: Mouse Event Inquires

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    79

    Question Mouse Event Inquires

    Dear gurus and members,
    I am new in VB. I have created some images to be used as buttons in VB. I would like some sample codes on creating mouse over and onClick event. I would like to use different images when user rollover the button and when click on the button. Any sample codes and suggestions are highly appreciated. Look forward to some prompt reply soon and have a pleasant day.

    Thanks in advance,
    Janice

  2. #2
    Registered User HellRaider's Avatar
    Join Date
    Sep 2003
    Posts
    70

    Re: Mouse Event Inquires

    Hello
    Why don't use the command button and set its style to graphic and just add pictures for when its up (picture) and when its pressed (downpicture). It would save you alot of time. One plus side to this is that you don't need to create events for the onclick etc, but if you want a round or any other shapped button then you'll have to change it using dlls.

  3. #3
    Addicted Member
    Join Date
    Dec 2004
    Posts
    129

    Re: Mouse Event Inquires

    I am curious if you are using this for skinning purposes? What i mean by this is are you just using it for lets say a couple buttons on a form or are you going to have a fully skinned application with a backgournd and multiple of buttons?

  4. #4
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: Mouse Event Inquires

    Quote Originally Posted by janice_2k
    Dear gurus and members,
    I am new in VB. I have created some images to be used as buttons in VB. I would like some sample codes on creating mouse over and onClick event. I would like to use different images when user rollover the button and when click on the button. Any sample codes and suggestions are highly appreciated. Look forward to some prompt reply soon and have a pleasant day.

    Thanks in advance,
    Janice
    HellRaider's advice would be what you need, BUT if you really want to know how to make a custom button - here goes:
    Put 3 image controls on the form. Give 2 Image Controls, Images
    for ex: 1 picture will be used for the button's Down state
    1 picture will be used for the button's up state.
    Set the visible properties for 2 Image controls(with the pictures) to false
    Don't do anything with the 3rd Image control.
    Use code similar to this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     imgMain.Picture = imgUp.Picture
    5. End Sub
    6.  
    7.  
    8. Private Sub imgMain_Click()
    9.     MsgBox "I am a custom button!"
    10. End Sub
    11.  
    12. Private Sub imgMain_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    13.     imgMain.Picture = imgDown.Picture
    14. End Sub
    15.  
    16.  
    17. Private Sub imgMain_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    18.     imgMain.Picture = imgUp.Picture
    19. End Sub

    Hope it helps!
    VB.NET MVP 2008 - Present

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    79

    Re: Mouse Event Inquires

    Thanks to all replies.

    Dear HellRaider: I once used command buttons for the earlier version. However the current enhancement requires buttons in various shapes and sizes. Therefore, I guess I should use image, right?

    Dear SllX: I am not using using this for skinning purposes. I just use it for 4 buttons with different events (default, mouseover and onClick) on a form. Currently, I am not going to have a fully skinned application. However, there are chances of enhancements to create a fully skinned application later on where the background/form may be an image.

    Dear HanneSThEGreaT: Thank you for your codes. I will look at it soon.

    Any suggestions are appreciated. Hope you have a pleasant day

    Thanks in advance,
    Janice

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    79

    Re: Mouse Event Inquires

    Dear HanneSThEGreaT,
    I wonder does the mouseDown and Up actually work similarly with the MouseMove event? I wish to create a button that when mouse over, it change to another color/image (lets say Image2). if it s not clicked and the mouse move to somewhere outside the button, it returns to default button(lets say Image1). If the button is clicked, it changed to another image (lets say Image3). After the button is clicked, it remains as Image3 even if the mouse move to elsewhere (including mouse over other buttons), UNLESS the other button is clicked. I wonder if VB6 is able to do this...Hope you could assist me as I am very new in this area.

    The attached is the VB code and interface I have. Only the Record button has both mouse over and click event for the moment. Would be glad if you could assist me in this. Look forward to your reply soon and hope you have a pleasant day.

    Thanks in advance,
    Janice
    Last edited by janice_2k; Jan 14th, 2005 at 02:45 AM.

  7. #7
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: Mouse Event Inquires

    Hello again Janice!

    Here's what I did..
    General Declarations
    Dim b As Boolean

    Form_Load
    b = True

    VB Code:
    1. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    2.     If a = True Then
    3.         a = False
    4.         Image1.Picture = LoadPicture(App.Path & "\RECORD.jpg")
    5.     If b = False Then Image1.Picture = LoadPicture(App.Path & "\onClick RECORD.jpg")
    6.     End If
    7. End Sub
    8.  
    9. Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    10.    
    11.     If a = False Then
    12.         a = True
    13.         Image1.Picture = LoadPicture(App.Path & "\mouse RECORD.jpg")
    14.     End If
    15.     If b = False Then Image1.Picture = LoadPicture(App.Path & "\onClick RECORD.jpg")
    16.  
    17. End Sub
    18.  
    19. Private Sub Image1_Click()
    20.     Dim settings As String
    21.     Dim Alignment As Integer
    22.      b = False
    23. ....
    24. End Sub

    And then in Image 2 3 4 Click() Events
    b = True


    Just follow the above steps exactly then it should work perfectly!
    Hope it helps!
    VB.NET MVP 2008 - Present

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    79

    Question Re: Mouse Event Inquires

    Dear HanneSThEGreaT,
    Thank you for your fast reply. I placed the codes as mentioned. It works fine for one button but I am clueless on how to make the other buttons having the similar events. Hope you could assist me... Sorry to trouble you again.

    Another question is whether can we 'embed' the images into the vb, instead of having them outside of the vb currently. Now, the jpg files have to be saved in the same folder as the .exe file on the user's computer. Is there another methods to 'embed' these jpgs so that we needn't copy all these images to every users' computers?

    Hope to hear from you soon and have a pleasant day.

    Thanks in advance,
    Janice

  9. #9
    Registered User HellRaider's Avatar
    Join Date
    Sep 2003
    Posts
    70

    Re: Mouse Event Inquires

    Hi again
    If you need it for different shapes then you can use
    VB Code:
    1. Declare Function CreatePolygonRgn Lib "gdi32" Alias "CreatePolygonRgn" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
    for wierd shaped buttons or
    VB Code:
    1. Declare Function CreateEllipticRgn Lib "gdi32" Alias "CreateEllipticRgn" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    for round buttons, then just set the command button rgn using
    VB Code:
    1. Declare Function SetWindowRgn Lib "user32" Alias "SetWindowRgn" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
    and set the buttons hWnd instead of a form.

    Only problem is when the button is selected and the button is shapped, the outline appears broken.

  10. #10
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: Mouse Event Inquires

    Quote Originally Posted by janice_2k
    Dear HanneSThEGreaT,
    Thank you for your fast reply. I placed the codes as mentioned. It works fine for one button but I am clueless on how to make the other buttons having the similar events. Hope you could assist me... Sorry to trouble you again.
    Hi Janice! I don't mind helping it keeps me out of trouble
    Have a look at this code and copy it exaclty to where it should be.
    VB Code:
    1. ‘General Declarations
    2. Dim c As Boolean 'img 2
    3. Dim d As Boolean 'img 3
    4. Dim e As Boolean 'img 4
    5.  
    6. Private Sub Form_Load()
    7.     b = True
    8.     c = True
    9.     d = True
    10.     e = True
    11.    
    12. End Sub
    13.  
    14. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    15.     If a = True Then
    16.         a = False
    17.         Image1.Picture = LoadPicture(App.Path & "\RECORD.jpg")
    18.         Image2.Picture = LoadPicture(App.Path & "\RESET.jpg")
    19.         Image3.Picture = LoadPicture(App.Path & "\STOP.jpg")
    20.         Image4.Picture = LoadPicture(App.Path & "\PLAY.jpg")
    21.        
    22.     If b = False Then Image1.Picture = LoadPicture(App.Path & "\onClick RECORD.jpg")
    23.     If c = False Then Image2.Picture = LoadPicture(App.Path & "\onClick RESET.jpg")
    24.     If d = False Then Image3.Picture = LoadPicture(App.Path & "\onClick STOP.jpg")
    25.     If e = False Then Image4.Picture = LoadPicture(App.Path & "\onClick PLAY.jpg")
    26.     End If
    27. End Sub
    28.  
    29. Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    30.    
    31.     If a = False Then
    32.         a = True
    33.         Image1.Picture = LoadPicture(App.Path & "\mouse RECORD.jpg")
    34.     End If
    35.     If b = False Then Image1.Picture = LoadPicture(App.Path & "\onClick RECORD.jpg")
    36.  
    37. End Sub
    38.  
    39. Private Sub Image1_Click()
    40.      b = False
    41.      c = True
    42.      d = True
    43.      e = True
    44.     Image1.Picture = LoadPicture(App.Path & "\onClick RECORD.jpg")
    45.    …
    46. End Sub
    47.  
    48.  
    49. Private Sub Image2_Click()
    50.      b = True
    51.      d = True
    52.      e = True
    53.     c = False
    54.     Image2.Picture = LoadPicture(App.Path & "\onClick RESET.jpg")
    55. End Sub
    56.  
    57.  
    58.  
    59. Private Sub Image2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    60.    
    61.     If a = False Then
    62.         a = True
    63.         Image2.Picture = LoadPicture(App.Path & "\mouse RESET.jpg")
    64.     End If
    65.     If c = False Then Image2.Picture = LoadPicture(App.Path & "\onClick RESET.jpg")
    66. End Sub
    67.  
    68. Private Sub Image3_Click()
    69.           b = True
    70.           c = True
    71.           e = True
    72.           d = False
    73.  
    74.         Image3.Picture = LoadPicture(App.Path & "\onClick STOP.jpg")
    75. End Sub
    76.  
    77. Private Sub Image3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    78.    
    79.     If a = False Then
    80.         a = True
    81.         Image3.Picture = LoadPicture(App.Path & "\mouse STOP.jpg")
    82.     End If
    83.     If d = False Then Image3.Picture = LoadPicture(App.Path & "\onClick STOP.jpg")
    84. End Sub
    85.  
    86. Private Sub Image4_Click()
    87.         Image4.Picture = LoadPicture(App.Path & "\onClick PLAY.jpg")
    88.          b = True
    89.          c = True
    90.          d = True
    91.          e = False
    92.  
    93. End Sub
    94.  
    95. Private Sub Image4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    96.    
    97.     If a = False Then
    98.         a = True
    99.         Image4.Picture = LoadPicture(App.Path & "\mouse PLAY.jpg")
    100.     End If
    101.     If e = False Then Image4.Picture = LoadPicture(App.Path & "\onClick PLAY.jpg")
    102. End Sub

    Sorry I couldn't send the attachment with, my firewall settings doesn't allow it.
    I could perhaps email you the full finished project¿
    VB.NET MVP 2008 - Present

  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    79

    Re: Mouse Event Inquires

    Dear HanneSThEGreaT,
    Thank you for your codes. It works now. Though, another request/inquiries... Is there a way to 'embed' these images into the Form? Currently, it seems that the user will have to download the exe as well as the jpg files onto their pc to be able to run the application. I wonder if we can reduce the files to be downloaded by 'embedding' all these jpeg files onto the application in order to let the user downloads only 1 file, that is the .exe file. Hope to hear from you soon and have a pleasant day.

    Thanks in advance,
    Janice


    ps:my email is [email protected]

  12. #12
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: Mouse Event Inquires

    Quote Originally Posted by janice_2k
    Dear HanneSThEGreaT,
    Thank you for your codes. It works now. Though, another request/inquiries... Is there a way to 'embed' these images into the Form? Currently, it seems that the user will have to download the exe as well as the jpg files onto their pc to be able to run the application. I wonder if we can reduce the files to be downloaded by 'embedding' all these jpeg files onto the application in order to let the user downloads only 1 file, that is the .exe file. Hope to hear from you soon and have a pleasant day.
    1) Downloading Zipped files would be safer than .exe
    2) Otherwise, Use Image Controls which are not Visible then Load the according pictures from there. Example

    Instead of saying Image1.Picture = LoadPicture App.Path & "\OnClick RECORD.jpg"
    Say something like: Image1.Picture = imgOnClickRecord.Picture

    I sincerely hope it's clear enough..

    Hannes
    VB.NET MVP 2008 - Present

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Mouse Event Inquires

    Quote Originally Posted by janice_2k
    Dear HanneSThEGreaT,
    Thank you for your codes. It works now. Though, another request/inquiries... Is there a way to 'embed' these images into the Form? Currently, it seems that the user will have to download the exe as well as the jpg files onto their pc to be able to run the application. I wonder if we can reduce the files to be downloaded by 'embedding' all these jpeg files onto the application in order to let the user downloads only 1 file, that is the .exe file. Hope to hear from you soon and have a pleasant day.

    Thanks in advance,
    Janice


    ps:my email is [email protected]
    Have you considered using a Resource File for storing your images?

  14. #14
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: Mouse Event Inquires

    Quote Originally Posted by Hack
    Have you considered using a Resource File for storing your images?
    I forgot about that one!

    To Add the Resource Editor to VB use the Add-Ins Menu.
    Once you have selected it in the Add In Manager dialog Box, you can find the Resource Editor on the Standard Toolbar.

    When you have added all the pictures, you can use something like:
    Example:
    Set Img1.Picture = LoadResPicture(101, vbResBitmap)

    Where 101 is the Resource ID and vbResBitmap is the type of bitmap.

    Hannes
    VB.NET MVP 2008 - Present

  15. #15

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    79

    Re: Mouse Event Inquires

    Dear all,
    Thank you for your replies and suggestions.

    Hi again HanneSThEGreaT,
    Does using the Resource Editor means that my images must be in .bmp format? Can I use other formats?

    Hope to hear from you soon and have a nice day

    Thanks in advance,
    Janice

  16. #16

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    79

    Question Re: Mouse Event Inquires

    Dear All,
    How can I disable the buttons created using image / resource editor. I tried using the normal method, but it seems to have some problems..in fact, the buttons are still clickable.
    You see, when the exe is loaded, only Record button is enable. The Stop, Play and Reset buttons should be disabled. However I notice my Play button is still clickable. After the user clicks Reset button, only Record button is enabled. Though, my Play buttons is still clickable. I am not sure where goes wrong. Wonder if any one could assist me in getting these solved. Attached is the codes. Hope to hear some replies soon and have a pleasant day.

    Thanks in advance,
    Janice
    Attached Files Attached Files

  17. #17
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: Mouse Event Inquires

    Hello again Janice!
    I have removed this:
    Image2.Enabled = False
    Image3.Enabled = False
    Image4.Enabled = False

    From Form_Load
    And set the Enabled Property for each Image Control on the Properties Window to False.

    It worked.

    Enjoy your day!
    Your project looks cool!
    VB.NET MVP 2008 - Present

  18. #18

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    79

    Re: Mouse Event Inquires

    Hi again HanneSThEGreaT,
    Thanks for the suggestions and compliments I tried your suggested method. The Stop and Reset button is disabled when the exe is loaded now, but I still have the Play button clickable. Wonder what went wrong...

    Anyway, thanks again and have a pleasant day.

    Thanks in advance,
    Janice

  19. #19
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Mouse Event Inquires

    you may want to write to the registry in a different location,as the one for VB programs is user-dependant. i just hanged my program tonight before any trouble arose. Nice program. I couldn't see the audio spectrum, as I don't have a microphone on my laptop.

  20. #20

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    79

    Re: Mouse Event Inquires

    Dear David,
    Thanks for your suggestions and compliments. I appreciate them. This is a simple application for user to record his audio, playback and view the spectrum. You'd be able to view the spectrum when you record your audio through a microphone. It is actually a small program for some e-learning purposes I am assigned to do.

    Do you mean I should save the wav file into some registry or the entire exe to be in the registry. Sorry, I am quite new in this area, would hope to gain more experience through trying and asking (silly questions, at times). Hope to hear from you soon and have a pleasant day.

    Thanks in advance,
    Janice

  21. #21
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Mouse Event Inquires

    No, I just saw that your settings are saved under the current user area of the registry. if you changed it to local user, then more than one person would be able to use the settings. if the user were to long on and use your program, the settings would get added a second time. if you use HKLM, then they are stored only once per machine. I just got hip to that, and don't use SaveSettings and GetSettings anymore. They are not really good. It isn't hard to write to other areas of the registry.
    Here's the module that I used:

    Too big to include.
    Attached Files Attached Files
    Last edited by dglienna; Jan 17th, 2005 at 03:55 AM.

  22. #22

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    79

    Re: Mouse Event Inquires

    Hi David,
    Thanks again for your advice. Have a pleasant day

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