For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > May 2004 > Menu Bar Color









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Menu Bar Color
CF

2004-05-29, 12:30 am

Is there a way to change the background color of menus created with 'Menu
Editor'?

Thanks,
Chuck


Dan Cannon

2004-05-29, 2:30 am

Add a combobox (combo1) and a commondialog control (CommonDialog1) to a
Form1 and add the following code to the form:

Option ExplicitPrivate Const MIM_BACKGROUND As Long = &H2
Private Const MIM_APPLYTOSUBMENUS As Long = &H80000000

Private Type MENUINFO
cbSize As Long
fMask As Long
dwStyle As Long
cyMax As Long
hbrBack As Long
dwContextHelpID As Long
dwMenuData As Long
End Type

Private Declare Function DrawMenuBar Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function GetMenu Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function GetSystemMenu Lib "user32" _
(ByVal hwnd As Long, _
ByVal bRevert As Long) As Long

Private Declare Function SetMenuInfo Lib "user32" _
(ByVal hmenu As Long, _
mi As MENUINFO) As Long

Private Declare Function CreateSolidBrush Lib "gdi32" _
(ByVal crColor As Long) As Long

Private Declare Function OleTranslateColor Lib "olepro32.dll" _
(ByVal OLE_COLOR As Long, _
ByVal HPALETTE As Long, _
pccolorref As Long) As Long



Private Sub Form_Load()

Dim cnt As Long

With Combo1

.Move 3720, 240

.AddItem "Button Face (Default)"
.ItemData(.NewIndex) = vbButtonFace

.AddItem "Application Workspace"
.ItemData(.NewIndex) = vbApplicationWorkspace

.AddItem "Window Background"
.ItemData(.NewIndex) = vbWindowBackground

.AddItem "Active Title Bar"
.ItemData(.NewIndex) = vbActiveTitleBar

.AddItem "Tooltip Background"
.ItemData(.NewIndex) = vbInfoBackground

.AddItem "(Select colour...)"

.ListIndex = 0

End With

End Sub

Private Sub Combo1_Click()

Dim clrref As Long

With Combo1

If .ListIndex > -1 Then

Select Case .ListIndex

Case .ListCount - 1

clrref = GetColor()

If clrref > -1 Then

SetMenuColour Me.hwnd, clrref, True
SetSysMenuColour Me.hwnd, clrref
'Me.BackColor = clrref

End If

Case Else

SetMenuColour Me.hwnd, .ItemData(.ListIndex), True
SetSysMenuColour Me.hwnd, .ItemData(.ListIndex)
'Me.BackColor = .ItemData(.ListIndex)

End Select


End If
End With

End Sub


Private Function GetColor() As Long

On Local Error GoTo GetColor_error

With CommonDialog1
'trap cancel
.CancelError = True

'match colour if possible
.flags = cdlCCRGBInit

'get the actual color from
'the form's current background.
'This could also be passed as a
'parameter to this method for
'increased functionality
.Color = TranslateOLEtoRBG(Me.BackColor)

'get and return the colour selected
.ShowColor
GetColor = .Color

End With

GetColor_exit:
Exit Function

GetColor_error:
'return -1 to enable trapping
'and still allow black (value 0)
'to be selected
GetColor = -1
Resume GetColor_exit

End Function


Private Function SetMenuColour(ByVal hwndfrm As Long, _
ByVal dwColour As Long, _
ByVal bIncludeSubmenus As Boolean) As Boolean

'set application menu colour
Dim mi As MENUINFO
Dim flags As Long
Dim clrref As Long

'convert a Windows colour (OLE colour)
'to a valid RGB colour if required
clrref = TranslateOLEtoRBG(dwColour)

'we're changing the background,
'so at a minimum set this flag
flags = MIM_BACKGROUND

If bIncludeSubmenus Then
'MIM_BACKGROUND only changes
'the back colour of the main
'menu bar, unless this flag is set
flags = flags Or MIM_APPLYTOSUBMENUS
End If

'fill in struct, assign to menu,
'and force a redraw with the
'new attributes
With mi
.cbSize = Len(mi)
.fMask = flags
.hbrBack = CreateSolidBrush(clrref)
End With

SetMenuInfo GetMenu(hwndfrm), mi
DrawMenuBar hwndfrm

End Function


Private Function SetSysMenuColour(ByVal hwndfrm As Long, _
ByVal dwColour As Long) As Boolean

'set system menu colour
Dim mi As MENUINFO
Dim hSysMenu As Long
Dim clrref As Long

'convert a Windows colour (OLE colour)
'to a valid RGB colour if required
clrref = TranslateOLEtoRBG(dwColour)

'get handle to the system menu,
'fill in struct, assign to menu,
'and force a redraw with the
'new attributes
hSysMenu = GetSystemMenu(Me.hwnd, False)

With mi
.cbSize = Len(mi)
.fMask = MIM_BACKGROUND Or MIM_APPLYTOSUBMENUS
.hbrBack = CreateSolidBrush(clrref)
End With

SetMenuInfo hSysMenu, mi
DrawMenuBar hSysMenu

End Function


Private Function TranslateOLEtoRBG(ByVal dwOleColour As Long) As Long

'check to see if the passed colour
'value is and OLE or RGB colour, and
'if an OLE colour, translate it to
'a valid RGB color and return. If the
'colour is already a valid RGB colour,
'the function returns the colour without
'change
OleTranslateColor dwOleColour, 0, TranslateOLEtoRBG

End FunctionI got this from vbNet a while ago, but I don't have the link
handy. HTH

-Dan

"CF" <Nomal@1.biz> wrote in message
news:BPCdnUpdGa2JlSXdRVn-jA@comcast.com...
> Is there a way to change the background color of menus created with 'Menu
> Editor'?
>
> Thanks,
> Chuck
>
>



CF

2004-05-29, 9:30 am

Thanks Dan!!!

Chuck
"Dan Cannon" <dccannon@hotmail.com> wrote in message
news:eDOhz7TREHA.3608@TK2MSFTNGP10.phx.gbl...
> Add a combobox (combo1) and a commondialog control (CommonDialog1) to a
> Form1 and add the following code to the form:

< SNIP>


Randy Birch

2004-05-29, 11:30 am

http://vbnet.mvps.org/code/menu/menucolour.htm

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.


"CF" <Nomal@1.biz> wrote in message
news:BPCdnUpdGa2JlSXdRVn-jA@comcast.com...
: Is there a way to change the background color of menus created with 'Menu
: Editor'?
:
: Thanks,
: Chuck
:
:

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com