944,124 Members | Top Members by Rank

Ad:
Mar 26th, 2005
0

Setting the command buttons Forecolor

Expand Post »
I have VB 5 learning edition which has suited me fine thus far believe it or not!
One thing has really been annoying me for quite some time. I can't find how to set the command buttons forecolor (at least with the VB version I have). Backcolor is no problem, and the forecolor property seems o.k for the other controls. Any clues, or is this one of the limitations with the "learning edition".
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
MrConfused is offline Offline
32 posts
since Mar 2005
Mar 26th, 2005
0

Re: Setting the command buttons Forecolor

The forecolor, do you mean the color of the text (font), or the face of the button?
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 26th, 2005
1

Re: Setting the command buttons Forecolor

you can't do it there no easy to set command button forecolor. but you can use CheckBox instead, set Style = Graphical (it exactly look like button) , and copy this code in

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Check1_Click()
  2. Check1.Value = 0
  3. End Sub

there are hard way to do it using API

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Option Explicit
  2. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  3. ' Copyright ©1996-2005 VBnet, Randy Birch, All Rights Reserved.
  4. ' Some pages may also contain other copyrights by the author.
  5. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  6. ' Distribution: You can freely use this code in your own
  7. ' applications, but you may not reproduce
  8. ' or publish this code on any web site,
  9. ' online service, or distribute as source
  10. ' on any media without express permission.
  11. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  12. '***********************************************************************
  13. '* *
  14. '* Copyright 1999 by Steve Derderian - The National Software Company *
  15. '* *
  16. '* Overview: *
  17. '* *
  18. '* This module allows you to colour the button text in a Visual Basic *
  19. '* application. The module can only be used from VB program code *
  20. '* (probably in the Form_Load event). *
  21. '* *
  22. '* Button text will appear black in the development environment. It *
  23. '* will only be coloured while the program is running. All other *
  24. '* button properties, methods and events will work normally. *
  25. '* *
  26. '* *
  27. '* Only three steps are required to use this module. *
  28. '* 1. Include this module in your VB project. *
  29. '* 2. When you add a button to the form set the style property *
  30. '* to Graphical. *
  31. '* 3. Call "RegisterButton" for each button you want to colour. *
  32. '* *
  33. '* *
  34. '* *
  35. '* *
  36. '* RegisterButton: *
  37. '* *
  38. '* Used to start colouring a button's text. *
  39. '* *
  40. '* Syntax -- RegisterButton(<Button>, <Forecolor>) *
  41. '* *
  42. '* Part Description *
  43. '* ------------------------------------------------------------ *
  44. '* Button The command button to register *
  45. '* Forecolor The colour for the button text *
  46. '* *
  47. '* Returned Value -- Returns a Boolean value. True if the *
  48. '* registration succeeded and false if it failed. *
  49. '* *
  50. '* Remarks -- To change the button text colour, call RegisterButton *
  51. '* again with the new colour. This will not register the button *
  52. '* twice. It will only change the colour of an already registered *
  53. '* button. *
  54. '* *
  55. '* *
  56. '* *
  57. '* *
  58. '* UnregisterButton: *
  59. '* *
  60. '* Used to stop colouring a button's text. *
  61. '* *
  62. '* Syntax -- UnregisterButton(<Button>) *
  63. '* *
  64. '* Part Description *
  65. '* ------------------------------------------------------------ *
  66. '* Button The command button to unregister *
  67. '* *
  68. '* Return Value -- Returns a Boolean value. True if the *
  69. '* unregistration succeeded and false if it failed. *
  70. '* *
  71. '* Remarks -- You don't need to unregister all button that were *
  72. '* registered. This will automatically be done when a form is *
  73. '* closed. This function is only provided so that a VB program may *
  74. '* stop colouring a button before the form is closed. *
  75. '* *
  76. '***********************************************************************
  77.  
  78. Private colButtons As New Collection
  79. Private Const KeyConst = "K"
  80. Private Const PROP_COLOR = "SMDColor"
  81. Private Const PROP_HWNDPARENT = "SMDhWndParent"
  82. Private Const PROP_LPWNDPROC = "SMDlpWndProc"
  83. Private Const GWL_WNDPROC As Long = (-4)
  84. Private Const ODA_SELECT As Long = &H2
  85. Private Const ODS_SELECTED As Long = &H1
  86. Private Const ODS_FOCUS As Long = &H10
  87. Private Const ODS_BUTTONDOWN As Long = ODS_FOCUS Or ODS_SELECTED
  88. Private Const WM_DESTROY As Long = &H2
  89. Private Const WM_DRAWITEM As Long = &H2B
  90. Private Const VER_PLATFORM_WIN32_NT As Long = 2
  91.  
  92. Private Type RECT
  93. Left As Long
  94. Top As Long
  95. Right As Long
  96. Bottom As Long
  97. End Type
  98.  
  99. Private Type SIZE
  100. cx As Long
  101. cy As Long
  102. End Type
  103.  
  104. Private Type DRAWITEMSTRUCT
  105. CtlType As Long
  106. CtlID As Long
  107. itemID As Long
  108. itemAction As Long
  109. itemState As Long
  110. hWndItem As Long
  111. hDC As Long
  112. rcItem As RECT
  113. itemData As Long
  114. End Type
  115.  
  116. Private Type OSVERSIONINFO
  117. OSVSize As Long
  118. dwVerMajor As Long
  119. dwVerMinor As Long
  120. dwBuildNumber As Long
  121. PlatformID As Long
  122. szCSDVersion As String * 128
  123. End Type
  124.  
  125. Private Declare Function CallWindowProc Lib "user32" _
  126. Alias "CallWindowProcA" _
  127. (ByVal lpPrevWndFunc As Long, _
  128. ByVal hWnd As Long, _
  129. ByVal msg As Long, _
  130. ByVal wParam As Long, _
  131. lParam As DRAWITEMSTRUCT) As Long
  132.  
  133. Private Declare Function GetParent Lib "user32" _
  134. (ByVal hWnd As Long) As Long
  135.  
  136. Private Declare Function GetProp Lib "user32" _
  137. Alias "GetPropA" _
  138. (ByVal hWnd As Long, _
  139. ByVal lpString As String) As Long
  140.  
  141. Private Declare Function GetTextExtentPoint32 Lib "gdi32" _
  142. Alias "GetTextExtentPoint32A" _
  143. (ByVal hDC As Long, _
  144. ByVal lpSz As String, _
  145. ByVal cbString As Long, _
  146. lpSize As SIZE) As Long
  147.  
  148. Private Declare Function RemoveProp Lib "user32" _
  149. Alias "RemovePropA" _
  150. (ByVal hWnd As Long, _
  151. ByVal lpString As String) As Long
  152.  
  153. Private Declare Function SetProp Lib "user32" _
  154. Alias "SetPropA" _
  155. (ByVal hWnd As Long, _
  156. ByVal lpString As String, _
  157. ByVal hData As Long) As Long
  158.  
  159. Private Declare Function SetTextColor Lib "gdi32" _
  160. (ByVal hDC As Long, _
  161. ByVal crColor As Long) As Long
  162.  
  163. Private Declare Function SetWindowLong Lib "user32" _
  164. Alias "SetWindowLongA" _
  165. (ByVal hWnd As Long, _
  166. ByVal nIndex As Long, _
  167. ByVal dwNewLong As Long) As Long
  168.  
  169. Private Declare Function TextOut Lib "gdi32" _
  170. Alias "TextOutA" _
  171. (ByVal hDC As Long, _
  172. ByVal x As Long, _
  173. ByVal y As Long, _
  174. ByVal lpString As String, _
  175. ByVal nCount As Long) As Long
  176.  
  177. Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
  178. (lpVersionInformation As Any) As Long
  179.  
  180.  
  181.  
  182. Private Function FindButton(sKey As String) As Boolean
  183.  
  184. Dim cmdButton As CommandButton
  185.  
  186. On Error Resume Next
  187. Set cmdButton = colButtons.Item(sKey)
  188. FindButton = (Err.Number = 0)
  189.  
  190. End Function
  191.  
  192.  
  193. Private Function GetKey(hWnd As Long) As String
  194.  
  195. GetKey = KeyConst & hWnd
  196.  
  197. End Function
  198.  
  199.  
  200. Private Function ProcessButton(ByVal hWnd As Long, _
  201. ByVal uMsg As Long, _
  202. ByVal wParam As Long, _
  203. lParam As DRAWITEMSTRUCT, _
  204. sKey As String) As Long
  205.  
  206. Dim cmdButton As CommandButton
  207. Dim bRC As Boolean
  208. Dim lRC As Long
  209. Dim x As Long
  210. Dim y As Long
  211. Dim lpWndProC As Long
  212. Dim lButtonWidth As Long
  213. Dim lButtonHeight As Long
  214. Dim lPrevColor As Long
  215. Dim lColor As Long
  216. Dim TextSize As SIZE
  217. Dim sCaption As String
  218.  
  219. Const PushOffset = 2
  220.  
  221. Set cmdButton = colButtons.Item(sKey)
  222. sCaption = cmdButton.Caption
  223.  
  224. lColor = GetProp(cmdButton.hWnd, PROP_COLOR)
  225. lPrevColor = SetTextColor(lParam.hDC, lColor)
  226.  
  227. 'in Pixels/Logical Units
  228. lRC = GetTextExtentPoint32(lParam.hDC, sCaption, Len(sCaption), TextSize)
  229.  
  230. 'in Pixels/Logical Units
  231. lButtonHeight = lParam.rcItem.Bottom - lParam.rcItem.Top
  232. lButtonWidth = lParam.rcItem.Right - lParam.rcItem.Left
  233.  
  234. 'the button is pressed! Offset the text
  235. 'so it looks like the button is pushed
  236. If ((lParam.itemState And ODS_BUTTONDOWN) = ODS_BUTTONDOWN) Then
  237. cmdButton.SetFocus
  238. DoEvents 'unneeded on XP - could use If Not IsWinXPPlus() Then DoEvents
  239. x = (lButtonWidth - TextSize.cx + PushOffset) \ 2
  240. y = (lButtonHeight - TextSize.cy + PushOffset) \ 2
  241. Else
  242. x = (lButtonWidth - TextSize.cx) \ 2
  243. y = (lButtonHeight - TextSize.cy) \ 2
  244. End If
  245.  
  246. 'get the default WndProc address
  247. lpWndProC = GetProp(hWnd, PROP_LPWNDPROC)
  248.  
  249. 'do the default button processing
  250. ProcessButton = CallWindowProc(lpWndProC, hWnd, uMsg, wParam, lParam)
  251.  
  252. 'put our text on the button
  253. bRC = TextOut(lParam.hDC, x, y, sCaption, Len(sCaption))
  254.  
  255. 'Restore the device context to the original color
  256. lRC = SetTextColor(lParam.hDC, lPrevColor)
  257.  
  258. ProcessButton_Exit:
  259. Set cmdButton = Nothing
  260.  
  261. End Function
  262.  
  263.  
  264. Private Sub RemoveForm(hWndParent As Long)
  265.  
  266. Dim hWndButton As Long
  267. Dim cnt As Integer
  268.  
  269. UnsubclassForm hWndParent
  270.  
  271. On Error GoTo RemoveForm_Exit
  272.  
  273. For cnt = colButtons.Count - 1 To 0 Step -1
  274.  
  275. hWndButton = colButtons(cnt).hWnd
  276.  
  277. If GetProp(hWndButton, PROP_HWNDPARENT) = hWndParent Then
  278. RemoveProp hWndButton, PROP_COLOR
  279. RemoveProp hWndButton, PROP_HWNDPARENT
  280. colButtons.Remove cnt
  281. End If
  282.  
  283. Next cnt
  284.  
  285. RemoveForm_Exit:
  286.  
  287. End Sub
  288.  
  289.  
  290. Private Function UnsubclassForm(hWnd As Long) As Boolean
  291.  
  292. Dim lpWndProC As Long
  293.  
  294. lpWndProC = GetProp(hWnd, PROP_LPWNDPROC)
  295.  
  296. If lpWndProC = 0 Then
  297.  
  298. UnsubclassForm = False
  299.  
  300. Else
  301.  
  302. Call SetWindowLong(hWnd, GWL_WNDPROC, lpWndProC)
  303. RemoveProp hWnd, PROP_LPWNDPROC
  304. UnsubclassForm = True
  305.  
  306. End If
  307.  
  308. End Function
  309.  
  310.  
  311. Private Function ButtonColorProc(ByVal hWnd As Long, _
  312. ByVal uMsg As Long, _
  313. ByVal wParam As Long, _
  314. lParam As DRAWITEMSTRUCT) As Long
  315.  
  316. Dim lpWndProC As Long
  317. Dim bProcessButton As Boolean
  318. Dim sButtonKey As String
  319.  
  320. bProcessButton = False 'Assume default processing
  321.  
  322. If (uMsg = WM_DRAWITEM) Then
  323.  
  324. 'Do we have this button? To find out, just
  325. 'try to reference the item in the collection.
  326. 'If it's there, we own the button. If it's
  327. 'not there, we'll get an error.
  328. sButtonKey = GetKey(lParam.hWndItem)
  329. bProcessButton = FindButton(sButtonKey)
  330.  
  331. End If
  332.  
  333.  
  334. If bProcessButton Then
  335.  
  336. ProcessButton hWnd, uMsg, wParam, lParam, sButtonKey
  337.  
  338. Else
  339.  
  340. lpWndProC = GetProp(hWnd, PROP_LPWNDPROC)
  341. ButtonColorProc = CallWindowProc(lpWndProC, hWnd, uMsg, wParam, lParam)
  342.  
  343. If uMsg = WM_DESTROY Then RemoveForm hWnd
  344.  
  345. End If
  346.  
  347. End Function
  348.  
  349.  
  350. Public Function RegisterButton(Button As CommandButton, _
  351. Forecolor As Long) As Boolean
  352.  
  353. Dim hWndParent As Long
  354. Dim lpWndProC As Long
  355. Dim sButtonKey As String
  356.  
  357. 'Make the colButtons key for the button
  358. sButtonKey = GetKey(Button.hWnd)
  359.  
  360. 'If we already own the button, just change the
  361. 'color otherwise we need to process the whole thing.
  362. If FindButton(sButtonKey) Then
  363.  
  364. SetProp Button.hWnd, PROP_COLOR, Forecolor
  365. Button.Refresh
  366.  
  367. Else
  368.  
  369. 'Get the handle to the buttons parent form.
  370. hWndParent = GetParent(Button.hWnd)
  371.  
  372. 'If we can't find a parent form, report a
  373. 'problem and get out.
  374. If (hWndParent = 0) Then
  375. RegisterButton = False
  376. Exit Function
  377. End If
  378.  
  379. 'found the parent, gather all of the necessary
  380. 'button values and add it to the collection.
  381. colButtons.Add Button, sButtonKey
  382. SetProp Button.hWnd, PROP_COLOR, Forecolor
  383. SetProp Button.hWnd, PROP_HWNDPARENT, hWndParent
  384.  
  385. 'Determine if we've already subclassed this form.
  386. lpWndProC = GetProp(hWndParent, PROP_LPWNDPROC)
  387.  
  388. 'It's a new form. Subclass it and add the
  389. 'Window proc address to the collection.
  390. If (lpWndProC = 0) Then
  391. lpWndProC = SetWindowLong(hWndParent, _
  392. GWL_WNDPROC, AddressOf ButtonColorProc)
  393. SetProp hWndParent, PROP_LPWNDPROC, lpWndProC
  394. End If
  395.  
  396. End If
  397.  
  398. RegisterButton = True
  399.  
  400. End Function
  401.  
  402.  
  403. Public Function UnregisterButton(Button As CommandButton) As Boolean
  404.  
  405. Dim hWndParent As Long
  406. Dim sKeyButton As String
  407.  
  408. sKeyButton = GetKey(Button.hWnd)
  409.  
  410. If (FindButton(sKeyButton) = False) Then
  411. UnregisterButton = False
  412. Exit Function
  413. End If
  414.  
  415. hWndParent = GetProp(Button.hWnd, PROP_HWNDPARENT)
  416. UnregisterButton = UnsubclassForm(hWndParent)
  417.  
  418. colButtons.Remove sKeyButton
  419. RemoveProp Button.hWnd, PROP_COLOR
  420. RemoveProp Button.hWnd, PROP_HWNDPARENT
  421.  
  422. End Function
  423.  
  424.  
  425. Private Function IsWinXPPlus() As Boolean
  426.  
  427. 'returns True if running WinXP (NT5.1) or later
  428. Dim osv As OSVERSIONINFO
  429.  
  430. osv.OSVSize = Len(osv)
  431.  
  432. If GetVersionEx(osv) = 1 Then
  433.  
  434. IsWinXPPlus = (osv.PlatformID = VER_PLATFORM_WIN32_NT) And _
  435. (osv.dwVerMajor >= 5 And osv.dwVerMinor >= 1)
  436.  
  437. End If
  438.  
  439. End Function

this code i take from http://vbnet.mvps.org/index.html?cod...ttoncolour.htm

and another way is using another Button Components, there are alot of free components.
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Mar 27th, 2005
0

Re: Setting the command buttons Forecolor

Yes. The color of the caption on the command button, believe it or not!
Reputation Points: 10
Solved Threads: 0
Light Poster
MrConfused is offline Offline
32 posts
since Mar 2005
Mar 27th, 2005
0

Re: Setting the command buttons Forecolor

Thanks. Is this a limitation put on VB5 learning edition only, or does the same apply for the other versions? Also do you know if VB6 is still available?

Thanks.
Reputation Points: 10
Solved Threads: 0
Light Poster
MrConfused is offline Offline
32 posts
since Mar 2005
Mar 27th, 2005
0

Re: Setting the command buttons Forecolor

This is a limitation of all the VB's that I know of. I'm trying to find a less complicated way to accomplish what Visal had posted... but it looks like his plan so far is the best. I've still got a couple of tricks up my sleeve, that I'm trying to work out right now, and I'll let you know what I come up with. I don't believe vendors still carry VB6, but I'm sure you can find a copy on Ebay.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 28th, 2005
0

Re: Setting the command buttons Forecolor

I think Visal Hit the nail on the head with that one..... it's easiest to use a checkbox with the style set to graphical...
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Feb 20th, 2011
0

this may be easier...

you could always use photshop or a similar program, edit the picture the way you wanted to, then import it as a picture..... it does take a little bit in order to get the sizing right, but may be easier then writing a bit of code for it especially for those who are just getting into the language


hope this helps

dan
Reputation Points: 10
Solved Threads: 0
Newbie Poster
danomac is offline Offline
1 posts
since Feb 2011

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: how to lock windows using vb6
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: check if another user has an xls file open?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC