Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98-VBForums
Results 1 to 26 of 26

Thread: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

  1. #1

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,992

    Arrow Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    After searching a long time for code that would align the text and the image in a standard command button
    without using the graphcal style or a picture of a button I decided to write my own.

    I got the text alignment working with the help of Merrion here in this post and this post.

    Next I needed to get the image to align also, but that seemed impossible so I figured out a workaround.

    If you use a picture box control and nest it inside a command button (not just drawn on the top of it)
    you can place an image anywhere you want on the button in relation to the text. I also found that this
    would keep the button (in standard style) compatible with XP and its Visual Styles which are ever so popular.

    Note: The XP Visual Style is achieved by using a manifest file and running on Windows XP.

    I think its actually quite simple once you think about it.

    One of the issues I had to figure out was when it was running under Windows 2000 or below the button click simulates
    a 3D effect of a push button. So I had to find the correct offset, in pixels, to the right and down. Then if its running
    under Windows XP there is no offset. So I added code to determine the OS and offset the image when pressed accordingly.



    1. is the MouseOver effect in XP
    2. is when the button has the focus in XP
    3. is when the button is in the up position under Windows 2000 or below
    4. is when the button is depressed under Windows 2000 or below


    I attached a zip file of the complete working demo too.

    Let me know what you think. I hope you enjoy this code.


    VB Code:
    1. 'BEHIND A FORM (FORM1)
    2. Option Explicit
    3. '<10/21/2004 ROBDOG888 VB/OUTLOOK GURU>
    4. Private mlX As Long
    5. Private mlY As Long
    6.  
    7. Private Sub Command1_Click()
    8.     Picture1.Refresh
    9.     MsgBox "Look Manavo11, a standard command button with an aligned image and text in it!", vbOKOnly + vbInformation, _
    10.     "RobDog888's Picture Button Demo" '
    11. End Sub
    12.  
    13. Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    14.     Picture1.Move Picture1.Left + mlX, Picture1.Top + mlY
    15.     Picture1.Refresh
    16. End Sub
    17.  
    18. Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    19.     Picture1.Move Picture1.Left - mlX, Picture1.Top - mlY
    20.     Picture1.Refresh
    21. End Sub
    22.  
    23. Private Sub Form_Load()
    24.     Select Case GetFullVersion
    25.         Case "2.5.1", "2.5.2" 'XP, SERVER 2003, +
    26.             'OFF - NO OFFSET WHEN CLICKED
    27.             mlX = 0
    28.             mlY = 0
    29.         Case Else
    30.             'ON - OFFSET WHEN CLICKED
    31.             mlX = 15
    32.             mlY = 15
    33.     End Select
    34.     'DISABLE THE PICTUREBOX TO AVOID THE PICTUREBOX CLICK EVENT CAUSING ISSUES - THANKS JOACIM :)
    35.     [color=red]Picture1.Enabled = False[/color] 'ADDED IN THIS POSTED CODE BUT ATTACHMENT CODE NOT UPDATED YET
    36.     'CHANGE THE LAST PARAMETER TO CHANGE THE ALIGNMENT
    37.     MakeGraphicalOutOfStandardButton Command1, Picture1, BS_LEFT
    38.     'ALIGN THE TEXT OPPOSITE OF THE PICTURE ALIGNMENT
    39.     AlignButtonText Command1, BS_RIGHT
    40. End Sub
    41.  
    42. 'NO LONGER NEEDED
    43. 'Private Sub Picture1_Click()
    44. '    Command1_Click
    45. 'End Sub
    46.  
    47.  
    48. 'INSIDE A STANDARD MODUE (MODULE1)
    49. Option Explicit
    50. '<10/21/2004 ROBDOG888 VB/OUTLOOK GURU>
    51. Private Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    52.  
    53. Public Const BS_LEFT    As Long = &H100&
    54. Public Const BS_RIGHT   As Long = &H200&
    55. Public Const BS_CENTER  As Long = &H300&
    56. Public Const BS_TOP     As Long = &H400&
    57. Public Const BS_BOTTOM  As Long = &H800&
    58. Public Const BS_vCENTER As Long = &HC00
    59.  
    60. Private Const GWL_STYLE  As Long = (-16)
    61.  
    62. Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    63.  
    64. Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, _
    65. ByVal dwNewLong As Long) As Long
    66.  
    67. Private Type OSVERSIONINFO
    68.   dwOSVersionInfoSize   As Long
    69.   dwMajorVersion        As Long
    70.   dwMinorVersion        As Long
    71.   dwBuildNumber         As Long
    72.   dwPlatformId          As Long '1 = Windows 95. '2 = Windows NT
    73.   szCSDVersion          As String * 128
    74. End Type
    75.  
    76. Private Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Integer
    77.  
    78. Public Function AlignButtonText(ByRef oCmdBtn As CommandButton, ByVal lStyle As Long)
    79. '<10/21/2004 ROBDOG888 VB/OUTLOOK GURU>
    80. 'Note: Align text to make room for the image. Depends on type of alignment passed
    81.     Dim lRet As Long
    82.     If lStyle = BS_RIGHT Then
    83.         lRet = GetWindowLong(oCmdBtn.hwnd, GWL_STYLE) And Not lStyle
    84.         oCmdBtn.Caption = oCmdBtn.Caption & Chr(32)
    85.     ElseIf lStyle = BS_LEFT Then
    86.         lRet = GetWindowLong(oCmdBtn.hwnd, GWL_STYLE) And Not lStyle
    87.         oCmdBtn.Caption = Chr(32) & oCmdBtn.Caption
    88.     ElseIf lStyle = BS_CENTER Then
    89.         lRet = GetWindowLong(oCmdBtn.hwnd, GWL_STYLE) And Not BS_RIGHT Or BS_LEFT
    90.     End If
    91.     lRet = SetWindowLong(oCmdBtn.hwnd, GWL_STYLE, lRet Or lStyle)
    92.     oCmdBtn.Refresh
    93. End Function
    94.  
    95. Public Function MakeGraphicalOutOfStandardButton(ByRef oCmdBtn As CommandButton, ByRef oPic As PictureBox, ByVal lStyle As Long)
    96. '<10/21/2004 ROBDOG888 VB/OUTLOOK GURU>
    97. 'Note: If running XP+ then dont need to adjust pic left and down when the button is clicked
    98. '      In the Button_Click, Button_MouseDown, and the Button_MouseUp events place a refresh method for the pic
    99.     SetParent oPic.hwnd, oCmdBtn.hwnd
    100.     If lStyle = BS_RIGHT Then
    101.         '95 IS THE BUFFER SPACING WHEN RIGHT ALIGNED
    102.         oPic.Move (oCmdBtn.Width - oPic.Width) - 95, (oCmdBtn.Height / 2) - (oPic.Height / 2)
    103.     ElseIf lStyle = BS_LEFT Then
    104.         '95 IS THE BUFFER SPACING WHEN LEFT ALIGNED
    105.         oPic.Move 95, (oCmdBtn.Height / 2) - (oPic.Height / 2)
    106.     ElseIf lStyle = BS_CENTER Then
    107.         oPic.Move (oCmdBtn.Width / 2) - (oPic.Width / 2), (oCmdBtn.Height / 2) - (oPic.Height / 2)
    108.     End If
    109.     oPic.Refresh
    110. End Function
    111.  
    112. Public Function GetFullVersion() As String
    113. '<07/07/2004 ROBDOG888 VB/OUTLOOK GURU>
    114.     Dim osinfo  As OSVERSIONINFO
    115.     Dim lRetVal As Integer
    116.     osinfo.dwOSVersionInfoSize = 148
    117.     osinfo.szCSDVersion = Space$(128)
    118.     lRetVal = GetVersionExA(osinfo)
    119.     GetFullVersion = osinfo.dwPlatformId & "." & osinfo.dwMajorVersion & "." & osinfo.dwMinorVersion
    120. End Function
    VB/Office Guru™

    Attached Images Attached Images   
    Attached Files Attached Files
    Last edited by RobDog888; Jan 16th, 2007 at 11:33 AM. Reason: Updated after 2 years to include the picturebox disabled issue
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Other side of town from si_the_geek
    Posts
    7,171

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    So the XP style is actually generated from the manifest file, right? I guess you forgot to mention it?

    And it's manavo, not Manovo


    Has someone helped you? Then you can Rate their helpful post.

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Other side of town from si_the_geek
    Posts
    7,171

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    1) While testing I noticed something... You can click on the picturebox
    2) The SetParent declaration isn't needed in the form
    Last edited by manavo11; Feb 6th, 2005 at 06:11 PM.


    Has someone helped you? Then you can Rate their helpful post.

  4. #4

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,992

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Arrrg! I forgot to remove it because I had started ouyt all in one form. I also forgot to include the picture box
    events for when they are clicked, but you just need to acll the command1_click event in the
    Picture1_Clickevent. Nothing involved there.

    The XP style is set by the manifest file but this depends if you are running on XP or not. Plus if you have any
    visual style applied, then there is nothing more to do. Best results are when your using an image with a
    transparent background.

    I will make the changes and upload a new attachment.

    Sorry about the typ-o
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  5. #5

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,992

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Changes applied Manavo11 or is Manavo your alter ego?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  6. #6
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    RobDog888, this is exactly what I was looking for however I noticed that if you click on the command button and then move your mouse off the button (while still holding down left mouse button) the image disappears.

  7. #7

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,992

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Yes, I have that issue too. I think the only way to get rid of it is to subclass the button and do it a more proper way. But this way is very simple for most programmers.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  8. #8
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    why dont you lockwindowupdate it so it cant dissapear?

  9. #9

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,992

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Because it needs to update to show the depressed state and then again the raised.

    Did you see I updated my Shortcut Target Path CodeBank thread?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  10. #10
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    yes i saw it, i meant to thank you..so thank you

    Well on mouse down you could lwu it then undo it on the mouseup, am i right?

  11. #11

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,992

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Not sure since there seems to be some internal animation when the button is pushed down. This is the part that creates the issue since if you didnt update the button window you may loose the animated effect.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  12. #12
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Other side of town from si_the_geek
    Posts
    7,171

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    I still don't like the fact that when you press the image you don't get the "pressed" down effect of the button


    Has someone helped you? Then you can Rate their helpful post.

  13. #13

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,992

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    You could make it elaborate and write some code to programmatically animate the push down and up effects if someone clicks on the image, but I'll leave that up to you.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  14. #14
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Other side of town from si_the_geek
    Posts
    7,171

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    I did think about it, but I'm not up for it at the moment... Maybe at some point I will


    Has someone helped you? Then you can Rate their helpful post.

  15. #15

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,992

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Probably way too much work. It would be easier to use BadgerBoys XP button code to manually draw the entire thing.
    Mine is for the beginners or the lazy
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  16. #16
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Other side of town from si_the_geek
    Posts
    7,171

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Custom made especially for me. right?


    Has someone helped you? Then you can Rate their helpful post.

  17. #17
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,424

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Quote Originally Posted by RobDog888
    You could make it elaborate and write some code to programmatically animate the push down and up effects if someone clicks on the image, but I'll leave that up to you.
    Yes, you will need some very complicated code for that effect.... Or you could just set the Enabled property of the PicBox to False . Since a PictureBox doesn't change it's apperance when you disable it will not be noticable, but all mouse events will be delegated to the control under it, which would be the command button.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  18. #18

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,992

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Ok, but that still wont produce the animation push down and up effect, correct?
    But it will reduce the need for calling the click event of the button.


    Ps, man! Its been 2 years since I wrote this code.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  19. #19
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,424

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Of course you get the pushed down/up effect since the mouse messages will be sent to the button instead of to the picture box. You'll get the same effect as if the picture box wasn't there.
    Quote Originally Posted by RobDog888
    Ps, man! Its been 2 years since I wrote this code.
    It's only 1 year since you posted it here, and you linked to this thread from another today. I've totally missed this thread earlier.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  20. #20

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,992

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Ok, cool. I didnt read your post that way. Was thinking it was meant as a compromise. So guess that answers my question.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  21. #21
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,424

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    That I said that "you would need some very complicated code" was meant as a joke since you only need to disable the picture box .
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  22. #22

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,992

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Ok, you got me on that one. But you are always a day ahead of me.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  23. #23
    New Member
    Join Date
    Aug 2010
    Location
    Craiova, Romania
    Posts
    7

    Resolved Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Quote Originally Posted by lintz View Post
    RobDog888, this is exactly what I was looking for however I noticed that if you click on the command button and then move your mouse off the button (while still holding down left mouse button) the image disappears.
    Quote Originally Posted by RobDog888 View Post
    Yes, I have that issue too. I think the only way to get rid of it is to subclass the button and do it a more proper way. But this way is very simple for most programmers.
    Hello Folks,

    I am sorry for reviving this very old thread. But since there are many people that still use the good ol' VB6, I think it's worth that I should tell everybody how to solve the issue above.

    The "disappearing picture" issue is not encountered when using XP Visual Styles (with the manifest file).
    But the workaround should be applied anyway, because when the application is executed under Windows Vista or W7, the will be no Visual Styles even with the manifest file (actually, there is a hack for this, but I haven't tried it yet).

    This issue can be solved in two ways.
    The simpler way is this (no code modification is need): set the "DragMode" property of the "Command1" control to "1 - Automatic".

    But as a side efect, this will show a rectangle when you click the button and drag the mouse while the left mouse button is still pressed.

    The less simple way is described below.
    It relies on the following workaround: whe set the focus to another control (we can even use the picture as a dummy control), then we set the focus back to the Command1 button).

    However, as (yet another) side effect, the picture won't update its offeset properly because some events are disturbed this way (especially the MouseUp event), but I resolved this by saving the picture box absolute position in two variables and modified the code as needed (I tried to bold each modificatin):

    Code:
    Option Explicit
    '<10/21/2004 ROBDOG888 VB/OUTLOOK GURU>
    Private mlX As Long
    Private mlY As Long
    
    ' these two variables will keep the position of the picture after the execution of "MakeGraphicalOutOfStandardButton" sub
    Private pictop As Long
    Private picleft As Long
    
    Private Sub Command1_Click()
        Picture1.Move picleft, pictop
        Picture1.Refresh
        MsgBox "Look Manavo11, a standard command button with an aligned image and text in it!", vbOKOnly + vbInformation, _
        "RobDog888's Picture Button Demo" '
    End Sub
    
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Picture1.Move picleft + mlX, pictop + mlY
        Picture1.Refresh
    End Sub
    
    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      If X < 0 Or Y < 0 Or X > Command1.Width Or Y > Command1.Height Then
        Picture1.Enabled = True  ' if using Picture1 as a dummy control, we'll make sure it is enabled, otherwise the SetFocus method will trigger an error
        Picture1.SetFocus
        Command1.SetFocus
        Picture1.Enabled = False  ' disable the Picture1
        Picture1.Move picleft, pictop
        Picture1.Refresh
      End If
    End Sub
    
    Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Picture1.Move picleft, pictop
        Picture1.Refresh
    End Sub
    
    Private Sub Form_Load()
        Select Case GetFullVersion
            Case "2.5.1", "2.5.2" 'XP, SERVER 2003, +
                'OFF - NO OFFSET WHEN CLICKED  - oh no, wait... let's create an effect, though
                mlX = 1 * Screen.TwipsPerPixelX
                mlY = 1 * Screen.TwipsPerPixelY
            Case Else        Case Else
                'ON - OFFSET WHEN CLICKED
                mlX = 15
                mlY = 15
        End Select
        'CHANGE THE LAST PARAMETER TO CHANGE THE ALIGNMENT
        MakeGraphicalOutOfStandardButton Command1, Picture1, BS_LEFT
        ' Saving picture position
        picleft = Picture1.Left
        pictop = Picture1.Top
        'ALIGN THE TEXT OPPOSITE OF THE PICTURE ALIGNMENT
        AlignButtonText Command1, BS_RIGHT
    End Sub
    As a "bonus", I modified the code to create a "pressed" effect for XP and 2K3.
    Please note that I assumed that the "Enabled" property of the Picture1 control is set to false.

    I hope this may be useful to someone some day.

    Yes, I know there are custom controls that mimic XP visual style. But those controls are immune to any other style/theme made for XP. Moreover, applications using those custom controls won't display Vista/W7 styles (providing the hack in the link above will work).
    Last edited by silkworm; May 7th, 2012 at 02:00 AM. Reason: a few typos

  24. #24
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,424

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Or you can simply do as I suggested 6 years ago and disable the picture box.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  25. #25
    New Member
    Join Date
    Aug 2010
    Location
    Craiova, Romania
    Posts
    7

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    Disabling the picture box doesn't prevent it from beign hidden when the user clicks the button and moves the mouse.

    But the picture box must be disabled indeed, because otherwise any click above it will be interpreted as a click on the picture box itself and not on the command button.

  26. #26
    Addicted Member
    Join Date
    Jun 2012
    Posts
    218

    Re: Image & Text On A Standard Command Button (Not Graphical Style!)- XP/2K/98

    I would like to share some improvements I have made. (changes in bold)

    Code:
    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If X < 0 Or Y < 0 Or X > EventsCommandButton.Width Or Y > EventsCommandButton.Height Then
        If Button = vbLeftButton Then
            Picture1.Enabled = True  ' if using Picture1 as a dummy control, we'll make sure it is enabled, otherwise the SetFocus method will trigger an error
            Picture1.SetFocus
            Command1.SetFocus
            Picture1.Enabled = False  ' disable the Picture1
            Picture1.Move picleft, pictop
            Picture1.Refresh
        End If
    End If
    End Sub
    Code:
    Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbLeftButton Then
            Picture1.Move picleft, pictop
            Picture1.Refresh
        End If
    End Sub
    Code:
    Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbLeftButton Then
            Picture1.Move picleft, pictop
            Picture1.Refresh
        End If
    End Sub
    This I have added to react when hitting the space key on the command button (like mouse clicking):
    Code:
    Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeySpace Then Picture1.Move picleft + 15, pictop + 15
    End Sub
    
    Private Sub Command1_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeySpace Then Picture1.Refresh
    End Sub
    
    Private Sub Command1_KeyUp(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeySpace Then Picture1.Move picleft, pictop
    End Sub
    Last edited by Krool; Jun 8th, 2012 at 02:01 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.