Results 1 to 26 of 26

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

Threaded View

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 12:33 PM. 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!
    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 i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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