有 BitMap 之Menu

2016-02-19 15:47 8 1 收藏

下面,图老师小编带您去了解一下有 BitMap 之Menu,生活就是不断的发现新事物,get新技能~

【 tulaoshi.com - 编程语言 】

  在Window API中,有一些名词要先清楚,假设有一功能表如下:

  档案  编辑 选项 -- hMenu  (功能表)

  +-------+

  |复制  |--------- hSubMenu (子功能表)

  |贴上  |

  |减下 ------------- MenuID  (功能表项目)

  |    |

  +-------+

  如果,我们使用vb的功能表编辑器做出上面的Menu,那 hMenu的取得使用GetMenu() API,而hSubMenu 的取得是 GetSubMenu,而GetSubMenu()的第二个叁数指的是功能表的第几个子功能表,以上例来说,编辑子功能表是第1个子功能表(以0为基准),所以编辑子功能表的取得应用以下的呼叫 :

  hMenu = GetMenu(Me.hwnd)

  hSubMenu = GetSubMenu(hMenu, 1) '取得编辑子功能表的hSubMenu

  而功能表项目则由以下的呼叫取得,第二叁数指的是该子功能表的第几个项目(以0开始),故复制 功能表项目 = 0 减下 = 2

  MenuId = GetMenuItemID(hSubMenu, 0) '取得复制 的hMenuId接着便是以ModifyMenu来更动MenuId成BitMap的方式Set Pic1 = LoadPicture("E:cli.bmp")ModifyMenu hSubMenu, 0, MF_BITMAP Or MF_BYPOSITION, MenuId, pic1.HandleModifyMenu 第二个叁数 表示更动hSubMenu所指的子功能表中第几个功能表项目

  第三个叁数 MF_BITMAP 表示用BitMap的方式显示

  MF_STRING 表示用字串方式显示

  MF_BYPOSITION 表示第二个叁数的值代表是依位置来算

  第四个叁数 MenuId

  第五个叁数 显示图的hBitMap

  另外,如何做到MenuItem的左方有一小Bitmap,右方仍是字串呢,使用以下的API

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  

SetMenuItemBitmaps(  hSubMenu as Long ,   // handle of 子功能表  uItem  as Long ,   // 更动第几个Menu Item  fuFlags as Long,   // menu item flags  hbmUnchecked as Long, // handle of unchecked bitmap  hbmChecked as Long   // handle of checked bitmap  )Set Pic2 = LoadPicture("e:cli2.BitMap")Call SetMenuItemBitmaps(hSubMenu, 1, MF_BYPOSITION,pic2.Handle, Pic2.Handle)

  这里有一个地方要特别注意,到底hbmUnchecked/hbmchecked 所指的BitMap图有多大呢,

  如果pic2所放入的BitMap太大,那不会出现我们想要的图,那得自己想办法缩图;而使

  用以下的API可以取得Menu Item左边Bitmap图的大小(By Pixels)

  i = GetMenuCheckMarkDimensions

  wd5 = i Mod 2 ^ 16 '宽

  hi5 = i / 2 ^ 16  '高

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  而我们Load进来的图之宽 Me.ScaleX(pic2.Width, vbHimetric, vbPixels)

  高 Me.ScaleY(pic2.Height, vbHimetric, vbPixels)

  於是呢,我写了一个GetBitMapHandle 来取得hbmUnchecked/hbmchecked所需的BitMap

  Handle,而且该hBitMap所指的图,大小刚好是系统内定的大小,而不必在乎原始的图

  有多大,当然了,一定要使用BitMap图,不可使用icon/gif等之类的图,这是什麽原

  因呢,这是因为我使用StdPicture物件来开启图形档,如果图形档是BitMap图,那麽,

  stdPicture物件的Handle属性便是hBitmap。

  

以下在.basOption ExplicitPublic Const MF_BYCOMMAND = &H0&Public Const MF_BYPOSITION = &H400&Public Const MF_BITMAP = &H4&Public Const MF_STRING = &H0&Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As LongDeclare Function GetSubMenu Lib "user32" (ByVal hMenu As Long,_   ByVal nPos As Long) As LongDeclare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As LongDeclare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, _   ByVal nPos As Long) As LongDeclare 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 Any) As LongDeclare 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 LongDeclare Function GetMenuCheckMarkDimensions Lib "user32" () As LongDeclare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long,_   ByVal nWidth As Long, _     ByVal nHeight As Long) As LongDeclare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As LongDeclare Function SelectObject Lib "gdi32" (ByVal hdc As Long,_   ByVal hObject As Long) As LongDeclare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As LongDeclare Function StretchBlt Lib "gdi32" (ByVal hdc As Long,_   ByVal x As Long, ByVal y As Long, _   ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long,_   ByVal xSrc As Long,ByVal ySrc As Long, ByVal nSrcWidth As Long,_   ByVal nSrcHeight As Long, ByVal dwRop As Long) As LongConst SRCCOPY = &HCC0020Public TheForm As FormPublic Function GetBitMapHandle(ByVal FileName As String)Dim dstWidth As Long, dstHeight As LongDim srcWidth As Long, srcHeight As LongDim x As Long, y As LongDim pic As New StdPictureDim hDc5 As Long, i As LongDim hBitmap As LongDim hDstDc As LongSet pic = LoadPicture(FileName) 读取图形档hDc5 = CreateCompatibleDC(0) 建立Memory DCi = SelectObject(hDc5, pic.Handle) 在该memoryDC上放上bitmap图i = GetMenuCheckMarkDimensions 取得SetMenuItemBitmaps 所需Bitmap大小dstWidth = i Mod 2 ^ 16dstHeight = i / 2 ^ 16建一个大小为dstWidh * dstHeight大小的BitmaphBitmap = CreateCompatibleBitmap(TheForm.hdc, dstWidth, dstHeight)hDstDc = CreateCompatibleDC(TheForm.hdc) 建memory dc设该memory dc的绘图区大小=该bitmap大小,且在该memory dc上的绘图便是在该bitmap图上画图SelectObject hDstDc, hBitmapsrcHeight = TheForm.ScaleY(pic.Height, vbHimetric, vbPixels)srcWidth = TheForm.ScaleX(pic.Width, vbHimetric, vbPixels)Call StretchBlt(hDstDc, 0, 0, dstWidth, dstHeight, hDc5, 0, 0, _  srcWidth, srcHeight, SRCCOPY)GetBitMapHandle = hBitmapCall DeleteDC(hDc5)Call DeleteDC(hDstDc)End Function以下在FormOption ExplicitPrivate hMenu As LongPrivate hSubMenu As LongPrivate MenuId As LongPrivate pic1 As New StdPicturePrivate pic2 As New StdPictureDim hBitmap As LongPrivate Sub Form_Load()Set TheForm = MeSet pic1 = LoadPicture("e:cli.bmp")hMenu = GetMenu(Me.hwnd)hSubMenu = GetSubMenu(hMenu, 1)MenuId = GetMenuItemID(hSubMenu, 1)ModifyMenu hSubMenu, 0, MF_BITMAP Or MF_BYPOSITION, MenuId, pic1.HandlehBitmap = GetBitMapHandle("e:cli.bmp")Call SetMenuItemBitmaps(hSubMenu, 1, MF_BYPOSITION, hBitmap, hBitmap)End SubPrivate Sub Form_Unload(Cancel As Integer)DeleteObject hBitmapEnd Sub

来源:http://www.tulaoshi.com/n/20160219/1610427.html

延伸阅读
标签: flash教程
本文由 中国 cao4811 原创,转载请保留此信息! 本系列Flash教程由中国Flash互助课堂专为Flash新手制作,更多教程和练习请 点击这里 ,在这里有系列的教程、练习,并有老师对练习进行点评与指导,欢迎朋友们的光临!  在学习中遇到问题请到 论坛 发贴交流! 创建Bitmap类的实例 Bitmap类与位图操作有关,一般来说,Bitmap...
今天看到EOE问答里面有这“[Android 界面]NotificationManager 如何使用Bitmap做图标”这样一个问题,在论坛搜索也没有好的案例 特写一个简单的demo供大家参考 今天发布的是NotificationManager 使用Bitmap做图标 关键code 代码如下: public void notification(int flag) { Notification notification = new Notification(); //设...
标签: flash教程
最近在制作flash时需要制作一个可以随意设定宽度的按钮组件.为了保证按钮样式不变形,一般都会采用将按钮分成几个mc的方法来制作.但其实,如果对按钮的动态效果要求不多的话,完全可以使用Bitmap类来制作.下面是将按钮的一个状态分解开来.一般的思路是做为三个MC,然后在改变宽度是,动态改变中间mc的宽度就可以了。但是这样的话,对于元素的制作就比...
MainActivity如下: 代码如下: package cn.testmediametadataretriever; import java.io.File; import java.io.FileOutputStream; import android.media.MediaMetadataRetriever; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.graphics.Bitmap; import android.graphics....
测试过程如下: create table sales  as select * from sh.sales;     create table customers  as  select * from sh.customers;  create unique index CUST_ID_un on customers(CUST_ID);  &nbs...

经验教程

282

收藏

82
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部