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.
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.
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?
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:
Option Explicit
Private Sub Form_Load()
imgMain.Picture = imgUp.Picture
End Sub
Private Sub imgMain_Click()
MsgBox "I am a custom button!"
End Sub
Private Sub imgMain_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
imgMain.Picture = imgDown.Picture
End Sub
Private Sub imgMain_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
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
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.
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.
Hi again
If you need it for different shapes then you can use
VB Code:
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:
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:
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.
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:
‘General Declarations
…
Dim c As Boolean 'img 2
Dim d As Boolean 'img 3
Dim e As Boolean 'img 4
Private Sub Form_Load()
…
b = True
c = True
d = True
e = True
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
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.
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
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.
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.
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.
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...
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.
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.
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.
Last edited by dglienna; Jan 17th, 2005 at 03:55 AM.