Link to home
Start Free TrialLog in
Avatar of falconew
falconew

asked on

Display a graphic in the Command Button

How can I make a command button which has a graphic beside the caption. I already set the style and picture property to graphical and an icon. I can't display the picture right beside the caption, but it is sitting right above the caption instead.
Any help would be appreciated.
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Hi falconew,

Just a suggestion:

Put the graphic in a frame, let the frame 'Bring To Front', put the frame in the left hand side of command button, and enter the command button's caption as '        Hello World'.

<regards and listening..>
Use a checkbox instead with style on Graphical. You can set caption on both sides of pictures.
NOTE: You must keep track of checked status of it with code like:

Private Sub Check1_Click()
If Check1.Value = 1 Then
    Check1.Value = 0
End If
End Sub
Ups! Forget my comment.
Align Text on a Command Button

If you?ve ever wanted to align text on a command button and found you can do it only by using spaces in the caption, there are a couple of constants that can help you do this with the SetWindowLong API Call. Add this code to a standard BAS module, then call it at will, passing the command button and the desired combination of vertical and horizontal alignment values:

Option Explicit
Private Declare Function GetWindowLong Lib _
"user32" Alias "GetWindowLongA" (ByVal hWnd _
As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib _
"user32" Alias "SetWindowLongA" (ByVal hWnd _
As Long, ByVal nIndex As Long, ByVal _
dwNewLong As Long) As Long
Private Const BS_LEFT As Long = &H100
Private Const BS_RIGHT As Long = &H200
Private Const BS_CENTER As Long = &H300
Private Const BS_TOP As Long = &H400
Private Const BS_BOTTOM As Long = &H800
Private Const BS_VCENTER As Long = &HC00
Private Const BS_ALLSTYLES = BS_LEFT Or BS_RIGHT _
Or BS_CENTER Or BS_TOP Or BS_BOTTOM Or _
BS_VCENTER
Private Const GWL_STYLE& = (-16)
Public Enum bsHorizontalAlignments
bsLeft = BS_LEFT
bsRight = BS_RIGHT
bsCenter = BS_CENTER
End Enum
Public Enum bsVerticalAlignments
bsTop = BS_TOP
bsBottom = BS_BOTTOM
bsVCenter = BS_VCENTER
End Enum
Public Sub AlignButtonText(cmd As CommandButton, _
Optional ByVal HStyle As _
bsHorizontalAlignments = bsCenter, _
Optional ByVal VStyle As _
bsVerticalAlignments = bsVCenter)
Dim oldStyle As Long
' get current style
oldStyle = GetWindowLong(cmd.hWnd, GWL_STYLE)
' clear existing alignment setting(s)
oldStyle = oldStyle And (Not BS_ALLSTYLES)
' set new style and refresh button
Call SetWindowLong(cmd.hWnd, GWL_STYLE, _
oldStyle Or HStyle Or VStyle)
cmd.Refresh
End Sub
Avatar of falconew
falconew

ASKER

When using frame the picture is not bound to the command button, hence when the button was click, the picture was not click-animated follow the button.

Richie, the code still don't work for me.
I need the picture sitting in the left of caption like we make a command button with Delphi languange.

Regards
hi
Since u want such a specific setting of a picture.wht u can do is make a new picture embedding both ur caption n icon u want in proper size that fits ur button.
If the icon is BITMAP u can do it using Paint Brush or else adobe photoshop is the best method.
What about displaying a label, below the Command button, or in the middle of the Command button with its color set to Transparent style?
Embedding both caption and icon is an alternative way I've ever think. It's not efficient because I have to change the text in the bitmap everytime I need to use it with different text.
Displaying a label in command button still get the same result which will not animate the caption when the button is clicked.
You could also put the label, below the command button, and make it visible in the MouseMove event of the command button. Or in the Command click event, or MouseDown event, you could change the label's font style also.

And in the MouseOver event of Form, or MouseUp event of Command button, you could reset the label's fonts, as you wish to get the animation done.

You could also have a look at www.vbaccelerator.com, for your needs, if you could get one from there.

Cheers.
sorry, typo, MouseOver event of Form, read as MouseMove event of Form.

I mean use the MouseMove on Command button and MouseMove on Form (OR)
MouseUp on Command button and MouseDown on Command button,

to do the reqd. animation of the label.
why dont u try setting size of ur icon.sometimes when the size is big enough the caption is displayed at some other place like its position changes.
Use GoldenButton OCX instead...it's much more elaborate and the picture is displayed on the left ;)

http://www.a1vbcode.com/app.asp?ID=348
The problem is, there are a lot of command button on my Application. I don't think It's efficient of doing that. Doesn't VB have a property like Delphi which can display image before the caption in the command button?
raybeam, My previous answer is not for you. The link you provided is kewl. But it has no picture yet until the next version. I add an image/picture control on OCX designer, the images still doesn't animate when the button is clicked. It's like the picture doesn't embed with the command button.
but the version I have include that feature
gimme your e-mail and I'll send it to you if you want
Ok. Thanks so much. Please email to macrodesk4@yahoo.com.
Soon I'm coming back here to accept your comment as answer.
Ok. Thanks so much. Please email to macrodesk4@yahoo.com.
Soon I'm coming back here to accept your comment as answer.
ASKER CERTIFIED SOLUTION
Avatar of raybeam
raybeam

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excelent....
For your efforts of helping me I give you extra points for 50.