'把文本转换为菜单小图标
'yMenu    菜单句柄
'cID      ID 或位置 
'tFont    字体句柄
'nText    宽字符文字的CHR码
'hColor   颜色 
'uFlags   默认 MF_BYCOMMAND    cID 是命令ID 
'              MF_BYPOSITION   cID 是位置，从0开始
'
'例题：
'          Dim tFont As HFONT
'          tFont = AfxCreateFont("ICONFONT", 14, -1)
'          MenuTextOutImag CaiDan.hMenu, 3000, tFont, &He78e, &HCD0000
'          MenuTextOutImag CaiDan.hMenu, 3001, tFont, &He783, &H006400
'          MenuTextOutImag CaiDan.hMenu, 3002, tFont, &He77e, &H800080
'          MenuTextOutImag CaiDan.hMenu, 3003, tFont, &Hec86, &HCD0000
'          MenuTextOutImag CaiDan.hMenu, 3004, tFont, &Hebe3, &H808080
'          DeleteObject tFont
Dim Shared MenuTextOutImag_Str() As String,MenuTextOutImag_B() As HBITMAP '避免重复加载，而没销毁句柄
Sub MenuTextOutImag(yMenu As HMENU, cID As UInteger, tFont As HFONT, nText As Long ,hColor As Long , ByVal uFlags As UINT = MF_BYCOMMAND)  '
  Dim U As Long, i As Long, ai As Long, confu As String
  Dim As Long w, h
  confu = nText & Hex(hColor)
  u = UBound(MenuTextOutImag_Str)
  ai = -1
  If u > -1 Then
      For i = 0 To u
          If MenuTextOutImag_Str(i) = confu Then
              ai = i
              Exit For
          End If
      Next
  End If
  If ai = -1 Then
      ai = u + 1
      ReDim Preserve MenuTextOutImag_Str(ai), MenuTextOutImag_B(ai)
      MenuTextOutImag_Str(ai) = confu
      
      w = GetSystemMetrics(SM_CXMENUCHECK)
      h = GetSystemMetrics(SM_CYMENUCHECK)
      Dim nDC As hDC, memDC As hDC, memBM As HBITMAP,rc As Rect,oFont As  HFONT

      nDC = GetDC(0)
      ' // 创建一个兼容的位图
      memBM = CreateCompatibleBitmap(nDC, w, h)
      ' // 创建兼容的设备上下文
      memDC = CreateCompatibleDC(nDC)
      ' // 将位图选择到兼容的设备上下文中
      SelectObject(memDC, memBM)      
      SetBkMode memDC, 1  '设置这个后，画上的字是透明的
      SetRect @rc, 0, 0, w, h 
      FillRect memDC, @rc, GetSysColorBrush(COLOR_MENU)     ' 画背景，填充底色         
      SetTextColor memDC, hColor      ' 文本颜色
      oFont = SelectObject(memDC, tFont)
      DrawTextW memDC,Cast(LPCWSTR, @nText), 1, @rc, DT_CENTER Or DT_VCENTER Or DT_SINGLELINE
      
      MenuTextOutImag_B(ai) = memBM ' CreatNewBitmap(GetSystemMetrics(SM_CXMENUCHECK), GetSystemMetrics(SM_CYMENUCHECK),bb.GetMemDC ,0,0,GetSysColor(COLOR_MENU))
      ReleaseDC  0, nDC
      DeleteDC memDC
      If oFont Then DeleteObject oFont
  End If
  SetMenuItemBitmaps( yMenu, cID, uFlags,MenuTextOutImag_B(ai), null)
  
End Sub
