Solved

Change background color of a Pop up menu

Posted on 2011-09-29
20
1,598 Views
Last Modified: 2012-06-27
I have written a vb6 code to change background color of a menu.
It works perfectly when the menu is set to visible, but wont work when menu is invisible.
How can i change the background color of a invisible menu?
I have attached my project. MDIback-color-change-notworking.zip
0
Comment
Question by:winman_soft
  • 7
  • 6
  • 4
  • +2
20 Comments
 
LVL 11

Expert Comment

by:kbirecki
ID: 36814030
I'm going to check out your project because it sounds like a cool idea to change the background color of a menu, but can you give an example of how you would use this feature of changing the color of an invisible menu?  And how would you tell the difference?  I'm thinking that since it is invisible, you could just tell people it can read minds and is magically their favorite color.  :)
0
 

Author Comment

by:winman_soft
ID: 36814239
I am using popupmenu to display that menu at a button click.. BOOYA. it is visible again:)
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 36815481
So change the color in the button click event.
0
 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 36815981
Yeah, do what MartinLiss says. Right after the PopupMenu command, set the color. It'll happen so fast the user won't notice the color change.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 36816110
Or you could have several menus that are identical except for the color and then popup the one with the color you want.
0
 

Author Comment

by:winman_soft
ID: 36881958
problem is I am not able to get the handle for that pop up menu.. In the project I change the color menu when it is invisible, and when I click the button to popup that menu the color is not changed.
0
 
LVL 6

Expert Comment

by:judgeking
ID: 36911513
If the menu is invisible when you change it, then when do you see the menu?  Are you trying to popup Form2's menu when it's not visible?  Where in your code would you do that, you don't have any other forms.  Are you trying to do it on the Parent form or a Form3?
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 36911802
        .cbSize = Len(mi)
        
        .fMask = MIM_BACKGROUND
        .hbrBack = CreateSolidBrush(vbYellow)
        'SetMenuInfo GetMenu(MDIForm1.hwnd), mi  'main menu bar
        
        .fMask = MIM_BACKGROUND Or MIM_APPLYTOSUBMENUS
        .hbrBack = CreateSolidBrush(vbCyan)
        SetMenuInfo GetSubMenu(GetMenu(MDIForm1.hwnd), 0), mi
        DrawMenuBar MDIForm1.hwnd

Open in new window

0
 
LVL 14

Accepted Solution

by:
VBClassicGuy earned 500 total points
ID: 36918279
This is the only way i could find to do it. Have a menu bar that has one or more visible menus, but your popup menu is invisible. The main trick is to set the invisible menu's caption to be blank. Here is the code, and attached is the source and compiled exe. i don't explain very well, just run the exe and see what it does.

BTW, exe files aren't allowed in zip attachments here, so I renamed Project1.exe to Project1.txt. Name it back then run it.

Private 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 GetMenu Lib "user32" (ByVal hwnd 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 GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long

Private Sub Form_Load()
   Dim mi As MENUINFO
   
   With mi
      .cbSize = Len(mi)
      .fMask = MIM_BACKGROUND
      .hbrBack = CreateSolidBrush(vbWhite)
       SetMenuInfo GetMenu(Me.hwnd), mi  'main menu bar
      .fMask = MIM_BACKGROUND Or MIM_APPLYTOSUBMENUS
    End With

End Sub

Private Sub Command1_Click(Index As Integer)
   Dim mi As MENUINFO
   
   With mi
      .cbSize = Len(mi)
      .fMask = MIM_BACKGROUND
      .hbrBack = CreateSolidBrush(vbWhite)
       SetMenuInfo GetMenu(Me.hwnd), mi  'main menu bar
      .fMask = MIM_BACKGROUND Or MIM_APPLYTOSUBMENUS
    End With

   Select Case Index
      Case 0
         Rem *** RED ***
         mi.hbrBack = CreateSolidBrush(vbCyan)
      Case 1
         Rem *** GRN ***
         mi.hbrBack = CreateSolidBrush(vbGreen)
      Case 2
         Rem *** BLU ***
         mi.hbrBack = CreateSolidBrush(vbRed)
   End Select
   
   mnuMisc.Visible = True
   
   SetMenuInfo GetSubMenu(GetMenu(Me.hwnd), 1), mi 'Misc menu (item 1)
   PopupMenu mnuMisc
   mnuMisc.Visible = False

End Sub

PopupMenu.zip
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 36918343
@VBClassicGuy: I see you incorporated my code, but if you just substitute my code for the With mi code in the author's Command1_Click code it works.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 36918861
Sorry Martin, I didn't even look at your code, didn't know I copied it. I grabbed an example I found from Googling, which did a lot more than change popup menus, it did system menus, buttons, listboxes, etc. Then I culled out all the stuff that didn't apply to this issue, played with it for a long time and made some changes, and posted what I wound up with.

Again, sorry. Didn't mean to step on any toes.

It's probably not what Winman_soft wants anyway, because you have to have a visible menu bar at the top of your form. If you do, then it's a piece of cake hiding the invisible submenu. But he probably wants NO menu bar to show, which is a sticky wicket.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 36918881
No worries, I wear steal-toed boots:)
0
 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 36918922
@MartinLiss: BOOOO...very punny!  :-)
0
 

Author Comment

by:winman_soft
ID: 36928775
@VBclassicguy
Even in ur code when I uncheck visible for file menu the pop up menu colors wont be changed.
Anyhow, Thank u for the comments.
I found the answer in a project called ODmenus.

http://www.xtremevbtalk.com/showpost.php?p=1011714&postcount=13
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 36928804
Did you simply try my code in your command button click event?
0
 

Author Comment

by:winman_soft
ID: 36929840
@MartinLiss

I did try ur code by replacing the code in with mi.. Even then the color wont be changed when the visible parameter in unchecked in menu editor
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 36932038
ColorMenu.zip

It works for me.
0
 

Author Comment

by:winman_soft
ID: 36934838
@Martin
Still.. when I hide menu "a" the popup menu will not be colored...
Let me explain my scenario again..
In our product we have a toolbar instead of a traditional menu and when we click on that we popup the submenu..
(So "a" must not be visible and the popup menu must be colored)
0
 
LVL 6

Expert Comment

by:judgeking
ID: 36934871
Is the toolbar on the parent MDI form?  If so, why not use its menu instead of Form2?
0
 

Author Closing Comment

by:winman_soft
ID: 36946560
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now