装扮你的程序菜单

Author: Date: 2001年 16期

?牐犗衷谛矶嗳砑牟说ヌ踔杏型夹危热鏦indows的开始菜单就是如此。现在就让我们用VB在自己的程序菜单中也加入图形吧(^16020502a^)。
  ?牐?1.新建一个工程,将Form1的Caption属性设为:“图形菜单”,其它不变。
  ?牐?2 .打开菜单设计器,为窗体添加两个菜单项:第1项的标题设置为&File名称设置为mnuFile其下添加3个子菜单项,它们的标题分别设置为:&New、&Open、&Save,名称为:mnuNew、mnuOpen、mnuSave。第2项的标题设置为&Edit,名称设置为mnuEdit,其下添加3个子菜单项,它们的标题分别设置为:&Copy、&Cut、&Paste,名称为:mnuCopy、mnuCut、mnuPaste。
  ?牐?3.在窗体上任意位置添加6个Image控件,利用属性框的Picture属性装入图片(这里选择Windows中默认的新建、打开、保存、复制、剪切、粘贴等图形,可在VB的安装目录下找到),并将它们的Name属性分别设置为:imgNew、imgOpen、imgSave、imgCopy、imgCut、imgPaste,然后将它们的Visible属性设置为False,即在程序运行过程中不可见。
  ?牐?4. 添加代码。
  ?牐犑紫龋颐且?3个API函数:GetMenu、GetSubMenu、SetMenuItemBitmaps,它们的作用分别是:获得菜单的句柄、获得子菜单项的句柄、设置菜单项图形。在程序中的声明部分添加如下代码:
  ?牐燩rivate Declare Function GetMenu Lib “user32”(ByVal hwnd As Long) As Long
  ?牐燩rivate Declare Function GetSubMenu Lib “user32” (ByVal hMenu As Long, ByVal nPos As Long) As Long
  ?牐燩rivate Declare Function SetMenuItemBitmaps Lib “user32” (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal hBitmapUnchecked As Long, ByVal hBitmapChecked As Long) As Long
  ?牐燙onst myflag = &H400&
  ?牐牻幼牛贔orm_Load事件中添加如下代码,为菜单加入图形:
  ?牐燩rivate Sub Form_Load()?煟?
  ?牐燚im mHandle As Long, lRet As Long, sHandle As Long
  ?牐爉Handle = GetMenu(hwnd)??
  ?牐爏Handle = GetSubMenu(mHandle, 0)??
  ?牐爈Ret = SetMenuItemBitmaps(sHandle, 2, myflag, imgnew.Picture,imgnew.Picture)??
  ?牐爈Ret = SetMenuItemBitmaps(sHandle, 3, myflag, imgopen.Picture,imgopen.Picture)??
  ?牐爈Ret = SetMenuItemBitmaps(sHandle,4,myflag, imgsave.Picture, imgsave.Picture)??
  ?牐爏Handle = GetSubMenu(mHandle, 1)??
  ?牐爈Ret = SetMenuItemBitmaps(sHandle,6,myflag,imgcopy.Picture, imgcopy.Picture)??
  ?牐爈Ret = SetMenuItemBitmaps(sHandle,7, myflag, imgcut.Picture, imgcut.Picture)??
  ?牐爈Ret = SetMenuItemBitmaps(sHandle,8, myflag,imgpaste.Picture, imgpaste.Picture)??
  ?牐燛nd Sub
  ?牐牶昧耍驼饷醇虻ィ聪翭5键,看看效果吧。