'AddMenuImg     增加菜单项目，并且设置资源里图片部分内容加到菜单中
'AddMenuImgTo   设置资源里图片部分内容加到菜单中
'uFlags  默认为 MF_BYCOMMAND  表示 cID 为菜单ID
'                MF_BYPOSITION   表示 cID 为 菜单项从零开始的相对位置。

Dim Shared Add_MenuImg_Save() As Add_MenuImg_type
Sub AddMenuImg(yMenu As HMENU, cID As ULong, Text As String, ResImgName As String = "", X As Long = 0, Y As Long = 0, W As Long = 0, H As Long = 0)
  '增加菜单项，且增加选择位图，将资源里图像部分图显示在菜单上。
  Dim A As ZString * 50
  a = Text
  AppendMenu yMenu, MF_STRING, cid, a
  AddMenuImgTo yMenu , cID , ResImgName , X , Y , W , H 
End Sub
Type Add_MenuImg_type '保存菜单位图句柄，给相同的用，避免重复增加位图
    ResImgName As ZString *50
    X As Long 
    Y As Long 
    Bmp As HBITMAP
End Type 
'--------------------------------------------------------------------------
Sub AddMenuImgTo(yMenu As HMENU, cID As ULong, ResImgName As String = "", X As Long = 0, Y As Long = 0, W As Long = 0, H As Long = 0,uFlags As UINT=MF_BYCOMMAND)
  Dim As Long u,i
  If Len(ResImgName) > 0 Then
      '先检查，原来有没有
      u=UBound(Add_MenuImg_Save)
      If u>-1 Then
          For i=0 To u
             If Add_MenuImg_Save(i).ResImgName = ResImgName And Add_MenuImg_Save(i).x =x And Add_MenuImg_Save(i).y =y  Then
'             Print "设置菜单位图",Add_MenuImg_Save(i).Bmp

                SetMenuItemBitmaps(yMenu, cID, uFlags, Add_MenuImg_Save(i).Bmp, Add_MenuImg_Save(i).Bmp) '设置菜单位图  
                Exit Sub          
             End If
          Next         
      End If
      
      '从资源里获取位图
      Dim ResBmp As HICON = AfxGdipIconFromRes(App.hInstance , ResImgName) 'AfxGdipBitmapFromRes(Null, ResImgName, 0, False, GetSysColor(COLOR_MENU))

      If ResBmp Then
          Dim m_nDC As hDC = GetDC(Null)
          Dim Dc As hDC = CreateCompatibleDC(m_nDC)
          Dim Bmp As HBITMAP = CreateCompatibleBitmap(m_nDC, x + w, y + h) '创建菜单图像内存BMP
          SelectObject(Dc, Bmp)  '将位图压入DC
          DrawFrame(Dc, 0, 0, x + w + 1, y + h + 1, GetSysColor(COLOR_MENU))
          DrawIconEx Dc, 0, 0, ResBmp, 0, 0, Null, Null, DI_NORMAL
          DestroyIcon ResBmp
          Dim As Long aw = GetSystemMetrics(SM_CXMENUCHECK), ah = GetSystemMetrics(SM_CYMENUCHECK) '获取菜单图像尺寸
          '          If aw>16 Then aw=16
          '          If ah>16 Then ah=16
          Dim m_Bmp As HBITMAP = CreateCompatibleBitmap(m_nDC, aw, ah) '创建菜单图像内存BMP
          Dim m_Dc As hDC = CreateCompatibleDC(m_nDC)
          SelectObject(m_Dc, m_Bmp)  '将位图压入DC
          SetStretchBltMode m_Dc, HALFTONE  'COLORONCOLOR
          StretchBlt(m_Dc, 0, 0, aw, ah, Dc, x, y, w, h, SRCCOPY) '复制部分图像
          SetMenuItemBitmaps(yMenu, cID, uFlags, m_Bmp, m_Bmp) '设置菜单位图
          DeleteDC m_Dc '销毁临时内容，必须的。
          DeleteDC Dc
'          DeleteObject m_Bmp
          DeleteObject Bmp
          ReleaseDC  Null, m_nDC
          u +=1
          ReDim Preserve Add_MenuImg_Save(u) 
          Add_MenuImg_Save(u).ResImgName = ResImgName
          Add_MenuImg_Save(u).x =x
          Add_MenuImg_Save(u).y =y
          Add_MenuImg_Save(u).Bmp = m_Bmp
      End If
  End If

End Sub