用vb写程序后,在添加菜单时,怎样在菜单项前添加图片??

用vb写程序后,在添加菜单时,怎样在菜单项前添加图片??,第1张

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

如:在打开菜单前有个打开的文件夹图片,在保存菜单前有个软盘的图片,怎么加上这些图的。我做的菜单就只有文字。

解析:

用VB6做这个很麻烦的,要调用API,一个例子:

Private Declare Function GetMenu Lib "user32" _

(ByVal hwnd As Long) As Long

Private Declare Function GetSubMenu Lib "user32" _

(ByVal hMenu As Long, ByVal nPos As Long) As Long

Private 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

Const MF_BYPOSITION = &H400&

Private Sub Form_Load()

Dim mHandle As Long, lRet As Long, sHandle As Long, sHandle2 As Long

'取得菜单的句柄并赋值给mHandle

mHandle = GetMenu(hwnd)

'取得mHandle句柄所指菜单的第一个d出式菜单(文件&F)的句柄并赋值给sHandle

sHandle = GetSubMenu(mHandle, 0)

'将d出式菜单的第0-4项加上图片,为什么跳过2呢?因为2是分割线

lRet = SetMenuItemBitmaps(sHandle, 0, MF_BYPOSITION, imOpen.Picture, imSave.Picture)

lRet = SetMenuItemBitmaps(sHandle, 1, MF_BYPOSITION, imSave.Picture, imSave.Picture)

lRet = SetMenuItemBitmaps(sHandle, 3, MF_BYPOSITION, imPrint.Picture, imPrint.Picture)

lRet = SetMenuItemBitmaps(sHandle, 4, MF_BYPOSITION, imPrintSetup.Picture, imPrintSetup.Picture)

'取得mHandle句柄所指菜单的第二个d出式菜单(编辑&E)的句柄并赋值给sHandle

sHandle = GetSubMenu(mHandle, 1)

'取得sHandle句柄所指菜单的第一个次级菜单(次级菜单&S)的句柄并赋值给sHandle2

sHandle2 = GetSubMenu(sHandle, 0)

'将次级菜单中的第1项加上图片

lRet = SetMenuItemBitmaps(sHandle2, 0, MF_BYPOSITION, imCopy.Picture, imCopy.Picture)

'提示:在SetMenuItemBitmaps()我们把后两项设为相同的图片,如果设为不同的两张图片会有什么效果呢?

' 原来这两张图片分别表示复选和撤消复选时的状态,你只须在菜单项被点击的函数中加入以下语句:

' Private Sub mnuOpen_Click()

' If mnuOpen.Checked = True Then

' mnuOpen.Checked = False

' Else: mnuOpen.Checked = True

' End If

' End Sub

' 然后在SetMenuItemBitmaps()我们把后两项设为不同的图片即可,有兴趣的话试一试。

End Sub

有三种方法:

1、添加一个picturebox或imagebox,在属性窗口中选择picture属性,然后就可以添加图片了,这是最简单的一种;

2、第二种是代码加入:

Option Explicit

Private Sub Form_Load()

Me.Picture = LoadPicture("c:\text.bmp") 'c:\text.bmp为图片路径

End Sub

3、第三种是通过剪贴板加入,这种比较复杂,如:

Option Explicit

Private Sub Form_Load()

Clipboard.SetData Picture1.Image

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11959531.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存