CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VB >  API

ACCP?改变MDI窗体中菜单的颜色

楼主010258(天堂鸟sun yi)2003-11-01 23:17:05 在 VB / API 提问

改变菜单的颜色应调用什么AIP函数  
  请举说出函数的用法  
  能步能用图片替换菜单背景,请举例说明 问题点数:50、回复次数:3Top

1 楼Gelim(Gelim)回复于 2003-11-02 01:13:19 得分 20

AppendMenu    
   
  VB声明    
  Declare   Function   AppendMenu   Lib   "user32"   Alias   "AppendMenuA"   (ByVal   hMenu   As   Long,   ByVal   wFlags   As   Long,   ByVal   wIDNewItem   As   Long,   ByVal   lpNewItem   As   Any)   As   Long    
  说明    
  在指定的菜单里添加一个菜单项    
  返回值    
  Long,非零表示成功,零表示失败。会设置GetLastError    
  参数表    
  参数   类型及说明    
  hMenu   Long,菜单句柄    
  wFlags   Long,参考ModifyMenu函数中的菜单常数标志定义表,其中列出了允许使用的所有常数    
  wIDNewItem   Long,指定菜单条目的新命令ID。如果在wFlags参数中指定了MF_POPUP字段,那么这应该是指向一个弹出式菜单的句柄    
  lpNewItem   String(相应的vb声明见注解),如果在wFlags参数中指定了MF_STRING标志,这就代表在菜单中设置的字串。如设置了MF_BITMAP标志,这就代表一个Long型变量,其中包含了一个位图句柄。如设置了MF_OWNERDRAW,这个值就会包括在DRAWITEMSTRUCT和MEASUREITEMSTRUCT结构中,在条目需要重画的时候由windows发送出去    
  注解    
  Declare   Function   AppendMenu&   Lib   "user32"   Alias   "AppendMenuA"   (ByVal   hMenu   As   Long,   ByVal   wFlags   As   Long,   ByVal   wIDNewItem   As   Long,   ByVal   lpNewItem   As   String)  
     
  CreateMenu    
   
  VB声明    
  Declare   Function   CreateMenu   Lib   "user32"   Alias   "CreateMenu"   ()   As   Long    
  说明    
  创建新菜单    
  返回值    
  Long,如成功则返回新的顶级菜单的句柄;零意味着错误    
  注解    
  最开始创建时,菜单是空的。可用菜单api函数插入菜单条目。一旦菜单不再需要,记住用DestroyMenu将其删除  
     
   
  DrawMenuBar    
   
  VB声明    
  Declare   Function   DrawMenuBar   Lib   "user32"   Alias   "DrawMenuBar"   (ByVal   hwnd   As   Long)   As   Long    
  说明    
  为指定的窗口重画菜单。用api函数改变一个窗口菜单的内容时,就要用到这个函数    
  返回值    
  Long,非零表示成功,零表示失败。会设置GetLastError    
  参数表    
  参数   类型及说明    
  hwnd   Long,要重画菜单栏的窗口的句柄    
  注解    
  在vb里很少有必要用到这个函数,因为不应该用api函数来改变一个窗口的顶级菜单栏  
     
   
  SetMenu    
   
  VB声明    
  Declare   Function   SetMenu   Lib   "user32"   Alias   "SetMenu"   (ByVal   hwnd   As   Long,   ByVal   hMenu   As   Long)   As   Long    
  说明    
  设置窗口菜单    
  返回值    
  Long,非零表示成功,零表示失败。会设置GetLastError    
  参数表    
  参数   类型及说明    
  hwnd   Long,窗口的句柄    
  hMenu   Long,窗口的新菜单的句柄    
  注解    
  不建议在vb里使用这个函数。如坚持使用,务必留意新菜单中的命令ID并不兼容于原始的vb窗口。只有窗体窗口才应通过这个函数指定。窗口的前一个菜单不会由这个函数删除  
     
  ModifyMenu,ModifyMenuBynum    
   
  VB声明    
  Declare   Function   ModifyMenu&   Lib   "user32"   Alias   "ModifyMenuA"   (ByVal   hMenu   As   Long,   ByVal   nPosition   As   Long,   ByVal   wFlags   As   Long,   ByVal   wIDNewItem   As   Long,   ByVal   lpString   As   String)  
  Declare   Function   ModifyMenuBynum&   Lib   "user32"   Alias   "ModifyMenuA"   (ByVal   hMenu   As   Long,   ByVal   nPosition   As   Long,   ByVal   wFlags   As   Long,   ByVal   wIDNewItem   As   Long,   ByVal   lpString   As   Long)    
  说明    
  改变菜单条目。在vb里这个函数做出的许多改变都会有效的执行,但不能由vb菜单对象反映出来    
  返回值    
  Long,非零表示成功,零表示失败。会设置GetLastError    
  参数表    
  参数   类型及说明    
  hMenu   Long,菜单句柄    
  nPosition   Long,欲改变的菜单条目的标识符。如在wFlags参数中指定了MF_BYCOMMAND,这个参数就代表欲改变的菜单条目的命令ID。如设置的是MF_BYPOSITION,这个参数就代表菜单条目在菜单中的位置(第一个条目的位置为零)    
  wFlags   Long,一系列常数标志的组合。详见菜单常数标志表    
  wIDNewItem   Long,指定菜单条目的新命令ID。如在wFlags参数中指定了MF_POPUP标志,就应是一个弹出式菜单的句柄    
  lpString   String或Long,如在wFlags参数中指定了MF_STRING标志,就代表欲设置到菜单的字串。如设置的是MF_BITMAP,就代表一个Long变量,其中包含了一个位图句柄。如设置的是MF_OWNERDRAW,那么这个值就会包括到DRAWITEMSTRUCT和MEASUREITEMSTRUCT结构中,并由windows在条目需要重画的时候发出    
  注解    
  标志的下述组合形式是不允许的:MF_BYCOMMAND和MF_BYPOSITION;MF_CHECKED和MF_UNCHECKED;MF_MENUBARBREAK和MF_MENUBREAK;MF_DISABLED,MF_ENABLED和MF_GRAYED;MF_BITMAP,MF_STRING,MF_OWNERDRAW和MF_SEPARATOR  
     
  菜单常数标志表    
  MF_BITMAP   菜单条目是一幅位图。一旦设入菜单,这幅位图就绝对不能删除。所以不应该使用由vb的image属性返回的值    
  MF_BYCOMMAND   菜单条目由菜单的命令ID指定    
  MF_BYPOSITION   菜单条目由条目在菜单中的位置决定。零代表菜单中的第一个条目    
  MF_CHECKED   检查指定的菜单条目。不能与vb的checked属性兼容    
  MF_DISABLED   禁止指定的菜单条目。不与vb的enabled属性兼容    
  MF_ENABLED   允许指定的菜单条目。不与vb的enabled属性兼容    
  MF_GRAYED   禁止指定的菜单条目,并用浅灰色描述它。不与vb的enabled属性兼容    
  MF_MENUBARBREAK   在弹出式菜单中,将指定的条目放置于一个新列,并用一条垂直线分隔不同的列    
  MF_MENUBREAK   在弹出式菜单中,将指定的条目放置于一个新列。在顶级菜单中,将条目放置到一个新行    
  MF_OWNERDRAW   创建一个物主绘图菜单(由您设计的程序负责描绘每个菜单条目)    
  MF_POPUP   将一个弹出式菜单置于指定的条目。可用于创建子菜单及弹出式菜单    
  MF_SEPARATOR   在指定的条目处显示一条分隔线    
  MF_STRING   在指定的条目处放置一个字串。不与vb的caption属性兼容    
  MF_UNCHECKED   检查指定的条目。不能与vb的checked属性兼容    
   
  LoadMenu    
   
  VB声明    
  Declare   Function   LoadMenu   Lib   "user32"   Alias   "LoadMenuA"   (ByVal   hInstance   As   Long,   ByVal   lpString   As   String)   As   Long    
  说明    
  从指定的模块或应用程序实例中载入一个菜单    
  返回值    
  Long,已装载的菜单的句柄;零意味着出错。会设置GetLastError    
  参数表    
  参数   类型及说明    
  hInstance   Long,一个动态链接库的模块句柄。或指定了特定可执行文件的一个实例句柄,那个可执行文件中包含了菜单资源    
  lpString   String,作为字串使用时,指定欲载入的菜单资源的名字;作为Long值使用时,指定欲载入的菜单ID。    
  注解    
  在vb里使用:由于vb不能控制那些不与现有vb菜单兼容的菜单,所以不建议使用这个函数  
     
  Top

2 楼changziyu(振翅雄鹰 (哈)ACCP010182)回复于 2004-01-07 19:14:56 得分 10

我不会,对不起。接分Top

3 楼jlctt(金)回复于 2004-01-07 20:00:00 得分 20

看个经典的吧  
   
   
  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   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   Function  
  '--end   block--'  
  Top

相关问题

  • 关于MDI窗体菜单的问题
  • 关于mdi窗体的菜单
  • MDI窗体的菜单问题
  • 关于MDI窗体菜单的问题?
  • MDI窗体能加图形菜单吗
  • MDI子窗体菜单问题
  • mdi菜单问题:如何更改子窗体菜单?
  • 怎么在MDI主窗体菜单中在运行时列出其子窗体?
  • 有关MDI主窗体和子窗体菜单的问题,请指点!
  • mdi子窗体怎么自动把主窗体的菜单条屏蔽了?

关键词

  • 函数
  • vb
  • 兼容
  • 属性
  • 菜单
  • 条目
  • mf
  • 句柄
  • 指定
  • wflags

得分解答快速导航

  • 帖主:010258
  • Gelim
  • changziyu
  • jlctt

相关链接

  • Visual Basic类图书
  • Visual Basic类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo