国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

VB打造超酷個性化菜單(三)

2024-07-21 02:20:58
字體:
來源:轉載
供稿:網友
  • 網站運營seo文章大全
  • 提供全面的站長運營經驗及seo技術!
  • vb打造超酷個性化菜單(三)



    現在到了最關鍵,最精彩,也是最復雜的部分了。我們最關心的就是怎樣“畫”菜單,怎樣處理菜單事件,在menuwndproc這個處理消息的函數里,我們要處理如下消息:wm_command(單擊菜單項),wm_measureitem(處理菜單高度和寬度),wm_menuselect(選擇菜單項),wm_drawitem(繪制菜單項)。

    打開上次建好的工程,添加一個標準模塊,并將其名稱設置為mmenu,代碼如下:

    '**************************************************************************************************************

    '* 本模塊配合 cmenu 菜單類模塊

    '*

    '* 版權: lpp軟件工作室

    '* 作者: 盧培培(goodname008)

    '* (******* 復制請保留以上信息 *******)

    '**************************************************************************************************************



    option explicit



    ' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- api 函數聲明 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-



    public declare function bitblt lib "gdi32" (byval hdestdc 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 dwrop as long) as long

    public declare function callwindowproc lib "user32" alias "callwindowproca" (byval lpprevwndfunc as long, byval hwnd as long, byval msg as long, byval wparam as long, byval lparam as long) as long

    public declare function createcompatibledc lib "gdi32" (byval hdc as long) as long

    public declare function createpen lib "gdi32" (byval npenstyle as long, byval nwidth as long, byval crcolor as long) as long

    public declare function createpopupmenu lib "user32" () as long

    public declare function createsolidbrush lib "gdi32" (byval crcolor as long) as long

    public declare function deletedc lib "gdi32" (byval hdc as long) as long

    public declare function deletemenu lib "user32" (byval hmenu as long, byval nposition as long, byval wflags as long) as long

    public declare function deleteobject lib "gdi32" (byval hobject as long) as long

    public declare function destroymenu lib "user32" (byval hmenu as long) as long

    public declare function drawedge lib "user32" (byval hdc as long, qrc as rect, byval edge as long, byval grfflags as long) as long

    public declare function drawiconex lib "user32" (byval hdc as long, byval xleft as long, byval ytop as long, byval hicon as long, byval cxwidth as long, byval cywidth as long, byval istepifanicur as long, byval hbrflickerfreedraw as long, byval diflags as long) as long

    public declare function drawstate lib "user32" alias "drawstatea" (byval hdc as long, byval hbrush as long, byval lpdrawstateproc as long, byval lparam as long, byval wparam as long, byval n1 as long, byval n2 as long, byval n3 as long, byval n4 as long, byval un as long) as long

    public declare function drawtext lib "user32" alias "drawtexta" (byval hdc as long, byval lpstr as string, byval ncount as long, lprect as rect, byval wformat as long) as long

    public declare function fillrect lib "user32" (byval hdc as long, lprect as rect, byval hbrush as long) as long

    public declare function getdc lib "user32" (byval hwnd as long) as long

    public declare function getmenuitemcount lib "user32" (byval hmenu as long) as long

    public declare function getmenuitemid lib "user32" (byval hmenu as long, byval npos as long) as long

    public declare function getmenuiteminfo lib "user32" alias "getmenuiteminfoa" (byval hmenu as long, byval un as long, byval b as long, lpmenuiteminfo as menuiteminfo) as long

    public declare function getsyscolor lib "user32" (byval nindex as long) as long

    public declare function getsystemmetrics lib "user32" (byval nindex as long) as long

    public declare function inflaterect lib "user32" (lprect as rect, byval x as long, byval y as long) as long

    public declare function insertmenuitem lib "user32" alias "insertmenuitema" (byval hmenu as long, byval un as long, byval bool as boolean, byref lpcmenuiteminfo as menuiteminfo) as long

    public declare function lineto lib "gdi32" (byval hdc as long, byval x as long, byval y as long) as long

    public declare function lstrlen lib "kernel32" alias "lstrlena" (byval lpstring as string) as long

    public declare function movetoex lib "gdi32" (byval hdc as long, byval x as long, byval y as long, lppoint as long) as long

    public declare function rectangle lib "gdi32" (byval hdc as long, byval x1 as long, byval y1 as long, byval x2 as long, byval y2 as long) as long

    public declare function releasedc lib "user32" (byval hwnd as long, byval hdc as long) as long

    public declare function selectobject lib "gdi32" (byval hdc as long, byval hobject as long) as long

    public declare function sendmessage lib "user32" alias "sendmessagea" (byval hwnd as long, byval wmsg as long, byval wparam as long, lparam as any) as long

    public declare function setbkmode lib "gdi32" (byval hdc as long, byval nbkmode as long) as long

    public declare function setmenuiteminfo lib "user32" alias "setmenuiteminfoa" (byval hmenu as long, byval un as long, byval bool as boolean, lpcmenuiteminfo as menuiteminfo) as long

    public declare function settextcolor lib "gdi32" (byval hdc as long, byval crcolor as long) as long

    public declare function setwindowlong lib "user32" alias "setwindowlonga" (byval hwnd as long, byval nindex as long, byval dwnewlong as long) as long



    public declare sub copymemory lib "kernel32" alias "rtlmovememory" (destination as any, source as any, byval length as long)





    ' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- api 常量聲明 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-



    public const gwl_wndproc = (-4) ' setwindowlong 設置窗口函數入口地址

    public const sm_cymenu = 15 ' getsystemmetrics 獲得系統菜單項高度



    public const wm_command = &h111 ' 消息: 單擊菜單項

    public const wm_drawitem = &h2b ' 消息: 繪制菜單項

    public const wm_exitmenuloop = &h212 ' 消息: 退出菜單消息循環

    public const wm_measureitem = &h2c ' 消息: 處理菜單高度和寬度

    public const wm_menuselect = &h11f ' 消息: 選擇菜單項



    ' odt

    public const odt_menu = 1 ' 菜單

    public const odt_listbox = 2 ' 列表框

    public const odt_combobox = 3 ' 組合框

    public const odt_button = 4 ' 按鈕



    ' ods

    public const ods_selected = &h1 ' 菜單被選擇

    public const ods_grayed = &h2 ' 灰色字

    public const ods_disabled = &h4 ' 禁用

    public const ods_checked = &h8 ' 選中

    public const ods_focus = &h10 ' 聚焦



    ' diflags to drawiconex

    public const di_mask = &h1 ' 繪圖時使用圖標的mask部分 (如單獨使用, 可獲得圖標的掩模)

    public const di_image = &h2 ' 繪圖時使用圖標的xor部分 (即圖標沒有透明區域)

    public const di_normal = di_mask or di_image ' 用常規方式繪圖 (合并 di_image 和 di_mask)



    ' nbkmode to setbkmode

    public const transparent = 1 ' 透明處理, 即不作上述填充

    public const opaque = 2 ' 用當前的背景色填充虛線畫筆、陰影刷子以及字符的空隙

    public const newtransparent = 3 ' 在有顏色的菜單上畫透明文字





    ' mf 菜單相關常數

    public const mf_bycommand = &h0& ' 菜單條目由菜單的命令id指定

    public const mf_byposition = &h400& ' 菜單條目由條目在菜單中的位置決定 (零代表菜單中的第一個條目)



    public const mf_checked = &h8& ' 檢查指定的菜單條目 (不能與vb的checked屬性兼容)

    public const mf_disabled = &h2& ' 禁止指定的菜單條目 (不與vb的enabled屬性兼容)

    public const mf_enabled = &h0& ' 允許指定的菜單條目 (不與vb的enabled屬性兼容)

    public const mf_grayed = &h1& ' 禁止指定的菜單條目, 并用淺灰色描述它. (不與vb的enabled屬性兼容)

    public const mf_hilite = &h80&

    public const mf_separator = &h800& ' 在指定的條目處顯示一條分隔線

    public const mf_string = &h0& ' 在指定的條目處放置一個字串 (不與vb的caption屬性兼容)

    public const mf_unchecked = &h0& ' 檢查指定的條目 (不能與vb的checked屬性兼容)

    public const mf_unhilite = &h0&



    public const mf_bitmap = &h4& ' 菜單條目是一幅位圖. 一旦設入菜單, 這幅位圖就絕對不能刪除, 所以不應該使用由vb的image屬性返回的值.

    public const mf_ownerdraw = &h100& ' 創建一個物主繪圖菜單 (由您設計的程序負責描繪每個菜單條目)

    public const mf_usecheckbitmaps = &h200&



    public const mf_menubarbreak = &h20& ' 在彈出式菜單中, 將指定的條目放置于一個新列, 并用一條垂直線分隔不同的列.

    public const mf_menubreak = &h40& ' 在彈出式菜單中, 將指定的條目放置于一個新列. 在頂級菜單中, 將條目放置到一個新行.



    public const mf_popup = &h10& ' 將一個彈出式菜單置于指定的條目, 可用于創建子菜單及彈出式菜單.

    public const mf_help = &h4000&



    public const mf_default = &h1000

    public const mf_rightjustify = &h4000



    ' fmask to insertmenuitem ' 指定 menuiteminfo 中哪些成員有效

    public const miim_state = &h1

    public const miim_id = &h2

    public const miim_submenu = &h4

    public const miim_checkmarks = &h8

    public const miim_type = &h10

    public const miim_data = &h20

    public const miim_string = &h40

    public const miim_bitmap = &h80

    public const miim_ftype = &h100



    ' ftype to insertmenuitem ' menuiteminfo 中菜單項類型

    public const mft_bitmap = &h4&

    public const mft_menubarbreak = &h20&

    public const mft_menubreak = &h40&

    public const mft_ownerdraw = &h100&

    public const mft_separator = &h800&

    public const mft_string = &h0&



    ' fstate to insertmenuitem ' menuiteminfo 中菜單項狀態

    public const mfs_checked = &h8&

    public const mfs_disabled = &h2&

    public const mfs_enabled = &h0&

    public const mfs_grayed = &h1&

    public const mfs_hilite = &h80&

    public const mfs_unchecked = &h0&

    public const mfs_unhilite = &h0&



    ' nformat to drawtext

    public const dt_left = &h0 ' 水平左對齊

    public const dt_center = &h1 ' 水平居中對齊

    public const dt_right = &h2 ' 水平右對齊



    public const dt_singleline = &h20 ' 單行



    public const dt_top = &h0 ' 垂直上對齊 (僅單行時有效)

    public const dt_vcenter = &h4 ' 垂直居中對齊 (僅單行時有效)

    public const dt_bottom = &h8 ' 垂直下對齊 (僅單行時有效)



    public const dt_calcrect = &h400 ' 多行繪圖時矩形的底邊根據需要進行延展, 以便容下所有文字; 單行繪圖時, 延展矩形的右側, 不描繪文字, 由lprect參數指定的矩形會載入計算出來的值.

    public const dt_wordbreak = &h10 ' 進行自動換行. 如用settextalign函數設置了ta_updatecp標志, 這里的設置則無效.



    public const dt_noclip = &h100 ' 描繪文字時不剪切到指定的矩形

    public const dt_noprefix = &h800 ' 通常, 函數認為 & 字符表示應為下一個字符加上下劃線, 該標志禁止這種行為.



    public const dt_expandtabs = &h40 ' 描繪文字的時候, 對制表站進行擴展. 默認的制表站間距是8個字符. 但是, 可用dt_tabstop標志改變這項設定.

    public const dt_tabstop = &h80 ' 指定新的制表站間距, 采用這個整數的高 8 位.

    public const dt_externalleading = &h200 ' 計算文本行高度的時候, 使用當前字體的外部間距屬性.



    ' nindex to getsyscolor 標準: 0--20

    public const color_activeborder = 10 ' 活動窗口的邊框

    public const color_activecaption = 2 ' 活動窗口的標題

    public const color_appworkspace = 12 ' mdi桌面的背景

    public const color_background = 1 ' windows 桌面

    public const color_btnface = 15 ' 按鈕

    public const color_btnhighlight = 20 ' 按鈕的3d加亮區

    public const color_btnshadow = 16 ' 按鈕的3d陰影

    public const color_btntext = 18 ' 按鈕文字

    public const color_captiontext = 9 ' 窗口標題中的文字

    public const color_graytext = 17 ' 灰色文字; 如使用了抖動技術則為零

    public const color_highlight = 13 ' 選定的項目背景

    public const color_highlighttext = 14 ' 選定的項目文字

    public const color_inactiveborder = 11 ' 不活動窗口的邊框

    public const color_inactivecaption = 3 ' 不活動窗口的標題

    public const color_inactivecaptiontext = 19 ' 不活動窗口的文字

    public const color_menu = 4 ' 菜單

    public const color_menutext = 7 ' 菜單文字

    public const color_scrollbar = 0 ' 滾動條

    public const color_window = 5 ' 窗口背景

    public const color_windowframe = 6 ' 窗框

    public const color_windowtext = 8 ' 窗口文字



    ' un to drawstate

    public const dst_complex = &h0 ' 繪圖在由lpdrawstateproc參數指定的回調函數期間執行, lparam和wparam會傳遞給回調事件.

    public const dst_text = &h1 ' lparam代表文字的地址(可使用一個字串別名),wparam代表字串的長度.

    public const dst_prefixtext = &h2 ' 與dst_text類似, 只是 & 字符指出為下各字符加上下劃線.

    public const dst_icon = &h3 ' lparam包括圖標的句柄

    public const dst_bitmap = &h4 ' lparam包括位圖的句柄

    public const dss_normal = &h0 ' 普通圖像

    public const dss_union = &h10 ' 圖像進行抖動處理

    public const dss_disabled = &h20 ' 圖象具有浮雕效果

    public const dss_mono = &h80 ' 用hbrush描繪圖像

    public const dss_right = &h8000 ' 無任何作用



    ' edge to drawedge

    public const bdr_raisedouter = &h1 ' 外層凸

    public const bdr_sunkenouter = &h2 ' 外層凹

    public const bdr_raisedinner = &h4 ' 內層凸

    public const bdr_sunkeninner = &h8 ' 內層凹

    public const bdr_outer = &h3

    public const bdr_raised = &h5

    public const bdr_sunken = &ha

    public const bdr_inner = &hc

    public const edge_bump = (bdr_raisedouter or bdr_sunkeninner)

    public const edge_etched = (bdr_sunkenouter or bdr_raisedinner)

    public const edge_raised = (bdr_raisedouter or bdr_raisedinner)

    public const edge_sunken = (bdr_sunkenouter or bdr_sunkeninner)



    ' grfflags to drawedge

    public const bf_left = &h1 ' 左邊緣

    public const bf_top = &h2 ' 上邊緣

    public const bf_right = &h4 ' 右邊緣

    public const bf_bottom = &h8 ' 下邊緣

    public const bf_diagonal = &h10 ' 對角線

    public const bf_middle = &h800 ' 填充矩形內部

    public const bf_soft = &h1000 ' msdn: soft buttons instead of tiles.

    public const bf_adjust = &h2000 ' 調整矩形, 預留客戶區

    public const bf_flat = &h4000 ' 平面邊緣

    public const bf_mono = &h8000 ' 一維邊緣



    public const bf_rect = (bf_left or bf_top or bf_right or bf_bottom)

    public const bf_topleft = (bf_top or bf_left)

    public const bf_topright = (bf_top or bf_right)

    public const bf_bottomleft = (bf_bottom or bf_left)

    public const bf_bottomright = (bf_bottom or bf_right)

    public const bf_diagonal_endtopleft = (bf_diagonal or bf_top or bf_left)

    public const bf_diagonal_endtopright = (bf_diagonal or bf_top or bf_right)

    public const bf_diagonal_endbottomleft = (bf_diagonal or bf_bottom or bf_left)

    public const bf_diagonal_endbottomright = (bf_diagonal or bf_bottom or bf_right)



    ' npenstyle to createpen

    public const ps_dash = 1 ' 畫筆類型:虛線 (nwidth必須是1) -------

    public const ps_dashdot = 3 ' 畫筆類型:點劃線 (nwidth必須是1) _._._._

    public const ps_dashdotdot = 4 ' 畫筆類型:點-點-劃線 (nwidth必須是1) _.._.._

    public const ps_dot = 2 ' 畫筆類型:點線 (nwidth必須是1) .......

    public const ps_solid = 0 ' 畫筆類型:實線 _______





    ' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- api 類型聲明 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-



    public type rect

    left as long

    top as long

    right as long

    bottom as long

    end type



    public type drawitemstruct

    ctltype as long

    ctlid as long

    itemid as long

    itemaction as long

    itemstate as long

    hwnditem as long

    hdc as long

    rcitem as rect

    itemdata as long

    end type



    public type menuiteminfo

    cbsize as long

    fmask as long

    ftype as long

    fstate as long

    wid as long

    hsubmenu as long

    hbmpchecked as long

    hbmpunchecked as long

    dwitemdata as long

    dwtypedata as string

    cch as long

    end type



    public type measureitemstruct

    ctltype as long

    ctlid as long

    itemid as long

    itemwidth as long

    itemheight as long

    itemdata as long

    end type



    public type size

    cx as long

    cy as long

    end type





    ' 自定義菜單項數據結構

    public type mymenuiteminfo

    itemicon as stdpicture

    itemalias as string

    itemtext as string

    itemtype as menuitemtype

    itemstate as menuitemstate

    end type



    ' 菜單相關結構

    private measureinfo as measureitemstruct

    private drawinfo as drawitemstruct



    public hmenu as long

    public premenuwndproc as long

    public myiteminfo() as mymenuiteminfo



    ' 菜單類屬性

    public barwidth as long ' 菜單附加條寬度

    public barstyle as menuleftbarstyle ' 菜單附加條風格

    public barimage as stdpicture ' 菜單附加條圖像

    public barstartcolor as long ' 菜單附加條過渡色起始顏色

    public barendcolor as long ' 菜單附加條過渡色終止顏色

    public selectscope as menuitemselectscope ' 菜單項高亮條的范圍

    public textenabledcolor as long ' 菜單項可用時文字顏色

    public textdisabledcolor as long ' 菜單項不可用時文字顏色

    public textselectcolor as long ' 菜單項選中時文字顏色

    public iconstyle as menuitemiconstyle ' 菜單項圖標風格

    public edgestyle as menuitemselectedgestyle ' 菜單項邊框風格

    public edgecolor as long ' 菜單項邊框顏色

    public fillstyle as menuitemselectfillstyle ' 菜單項背景填充風格

    public fillstartcolor as long ' 菜單項過渡色起始顏色

    public fillendcolor as long ' 菜單項過渡色終止顏色

    public bkcolor as long ' 菜單背景顏色

    public sepstyle as menuseparatorstyle ' 菜單分隔條風格

    public sepcolor as long ' 菜單分隔條顏色

    public menustyle as menuuserstyle ' 菜單總體風格



    ' 攔截菜單消息 (frmmenu 窗口入口函數)

    function menuwndproc(byval hwnd as long, byval msg as long, byval wparam as long, byval lparam as long) as long

    select case msg

    case wm_command ' 單擊菜單項

    if myiteminfo(wparam).itemtype = mit_checkbox then

    if myiteminfo(wparam).itemstate = mis_checked then

    myiteminfo(wparam).itemstate = mis_unchecked

    else

    myiteminfo(wparam).itemstate = mis_checked

    end if

    end if

    menuitemselected wparam

    case wm_exitmenuloop ' 退出菜單消息循環(保留)



    case wm_measureitem ' 處理菜單項高度和寬度

    measureitem hwnd, lparam

    case wm_menuselect ' 選擇菜單項

    dim itemid as long

    itemid = getmenuitemid(lparam, wparam and &hff)

    if itemid <> -1 then

    menuitemselecting itemid

    end if

    case wm_drawitem ' 繪制菜單項

    drawitem lparam

    end select

    menuwndproc = callwindowproc(premenuwndproc, hwnd, msg, wparam, lparam)

    end function



    ' 處理菜單高度和寬度

    private sub measureitem(byval hwnd as long, byval lparam as long)

    dim textsize as size, hdc as long

    hdc = getdc(hwnd)

    copymemory measureinfo, byval lparam, len(measureinfo)

    if measureinfo.ctltype and odt_menu then

    measureinfo.itemwidth = lstrlen(myiteminfo(measureinfo.itemid).itemtext) * (getsystemmetrics(sm_cymenu) / 2.5) + barwidth

    if myiteminfo(measureinfo.itemid).itemtype <> mit_separator then

    measureinfo.itemheight = getsystemmetrics(sm_cymenu)

    else

    measureinfo.itemheight = 6

    end if

    end if

    copymemory byval lparam, measureinfo, len(measureinfo)

    releasedc hwnd, hdc

    end sub



    ' 繪制菜單項

    private sub drawitem(byval lparam as long)

    dim hpen as long, hbrush as long

    dim itemrect as rect, barrect as rect, iconrect as rect, textrect as rect

    dim i as long

    copymemory drawinfo, byval lparam, len(drawinfo)

    if drawinfo.ctltype = odt_menu then

    setbkmode drawinfo.hdc, transparent



    ' 初始化菜單項矩形, 圖標矩形, 文字矩形

    itemrect = drawinfo.rcitem

    iconrect = drawinfo.rcitem

    textrect = drawinfo.rcitem



    ' 設置菜單附加條矩形

    with barrect

    .left = 0

    .top = 0

    .right = barwidth - 1

    for i = 0 to getmenuitemcount(hmenu) - 1

    if myiteminfo(i).itemtype = mit_separator then

    .bottom = .bottom + 6

    else

    .bottom = .bottom + measureinfo.itemheight

    end if

    next i

    .bottom = .bottom - 1

    end with



    ' 設置圖標矩形, 文字矩形

    if barstyle <> lbs_none then iconrect.left = barrect.right + 2

    iconrect.right = iconrect.left + 20

    textrect.left = iconrect.right + 3



    with drawinfo



    ' 畫菜單背景

    itemrect.left = barrect.right

    hbrush = createsolidbrush(bkcolor)

    fillrect .hdc, itemrect, hbrush

    deleteobject hbrush





    ' 畫菜單左邊的附加條

    dim redarea as long, greenarea as long, bluearea as long

    dim red as long, green as long, blue as long

    select case barstyle

    case lbs_none ' 無附加條



    case lbs_solidcolor ' 實色填充



    hbrush = createsolidbrush(barstartcolor)

    fillrect .hdc, barrect, hbrush

    deleteobject hbrush



    case lbs_horizontalcolor ' 水平過渡色



    bluearea = int(barendcolor / &h10000) - int(barstartcolor / &h10000)

    greenarea = (int(barendcolor / &h100) and &hff) - (int(barstartcolor / &h100) and &hff)

    redarea = (barendcolor and &hff) - (barstartcolor and &hff)



    for i = 0 to barwidth - 1

    red = int(barstartcolor and &hff) + int(i / barwidth * redarea)

    green = (int(barstartcolor / &h100) and &hff) + int(i / barwidth * greenarea)

    blue = int(barstartcolor / &h10000) + int(i / barwidth * bluearea)

    hpen = createpen(ps_solid, 1, rgb(red, green, blue))

    call selectobject(.hdc, hpen)

    call movetoex(.hdc, i, 0, 0)

    call lineto(.hdc, i, barrect.bottom)

    call deleteobject(hpen)

    next i



    case lbs_verticalcolor ' 垂直過渡色



    bluearea = int(barendcolor / &h10000) - int(barstartcolor / &h10000)

    greenarea = (int(barendcolor / &h100) and &hff) - (int(barstartcolor / &h100) and &hff)

    redarea = (barendcolor and &hff) - (barstartcolor and &hff)



    for i = 0 to barrect.bottom

    red = int(barstartcolor and &hff) + int(i / (barrect.bottom + 1) * redarea)

    green = (int(barstartcolor / &h100) and &hff) + int(i / (barrect.bottom + 1) * greenarea)

    blue = int(barstartcolor / &h10000) + int(i / (barrect.bottom + 1) * bluearea)

    hpen = createpen(ps_solid, 1, rgb(red, green, blue))

    call selectobject(.hdc, hpen)

    call movetoex(.hdc, 0, i, 0)

    call lineto(.hdc, barrect.right, i)

    call deleteobject(hpen)

    next i



    case lbs_image ' 圖像



    if barimage.handle <> 0 then

    dim barhdc as long

    barhdc = createcompatibledc(getdc(0))

    selectobject barhdc, barimage.handle

    bitblt .hdc, 0, 0, barwidth, barrect.bottom - barrect.top + 1, barhdc, 0, 0, vbsrccopy

    deletedc barhdc

    end if



    end select





    ' 畫菜單項

    if myiteminfo(.itemid).itemtype = mit_separator then

    ' 畫菜單分隔條(mit_separator)

    if myiteminfo(.itemid).itemtype = mit_separator then

    itemrect.top = itemrect.top + 2

    itemrect.bottom = itemrect.top + 1

    itemrect.left = barrect.right + 5

    select case sepstyle

    case mss_none ' 無分隔條



    case mss_default ' 默認樣式

    drawedge .hdc, itemrect, edge_etched, bf_top

    case else ' 其它

    hpen = createpen(sepstyle, 0, sepcolor)

    hbrush = createsolidbrush(bkcolor)

    selectobject .hdc, hpen

    selectobject .hdc, hbrush

    rectangle .hdc, itemrect.left, itemrect.top, itemrect.right, itemrect.bottom

    deleteobject hpen

    deleteobject hbrush

    end select

    end if

    else

    if not cbool(myiteminfo(.itemid).itemstate and mis_disabled) then ' 當菜單項可用時

    if .itemstate and ods_selected then ' 當鼠標移動到菜單項時



    ' 設置菜單項高亮范圍

    if selectscope and iss_icon_text then

    itemrect.left = iconrect.left

    elseif selectscope and iss_text then

    itemrect.left = textrect.left - 2

    else

    itemrect.left = .rcitem.left

    end if





    ' 處理菜單項無圖標或為checkbox時的情況

    if (myiteminfo(.itemid).itemtype = mit_checkbox or myiteminfo(.itemid).itemicon = 0) and selectscope <> iss_leftbar_icon_text then

    itemrect.left = iconrect.left

    end if





    ' 畫菜單項邊框

    select case edgestyle

    case ises_none ' 無邊框



    case ises_sunken ' 凹進

    drawedge .hdc, itemrect, bdr_sunkenouter, bf_rect

    case ises_raised ' 凸起

    drawedge .hdc, itemrect, bdr_raisedinner, bf_rect

    case else ' 其它

    hpen = createpen(edgestyle, 0, edgecolor)

    hbrush = createsolidbrush(bkcolor)

    selectobject .hdc, hpen

    selectobject .hdc, hbrush

    rectangle .hdc, itemrect.left, itemrect.top, itemrect.right, itemrect.bottom

    deleteobject hpen

    deleteobject hbrush

    end select





    ' 畫菜單項背景

    inflaterect itemrect, -1, -1

    select case fillstyle

    case isfs_none ' 無背景



    case isfs_horizontalcolor ' 水平漸變色



    bluearea = int(fillendcolor / &h10000) - int(fillstartcolor / &h10000)

    greenarea = (int(fillendcolor / &h100) and &hff) - (int(fillstartcolor / &h100) and &hff)

    redarea = (fillendcolor and &hff) - (fillstartcolor and &hff)



    for i = itemrect.left to itemrect.right - 1

    red = int(fillstartcolor and &hff) + int((i - itemrect.left) / (itemrect.right - itemrect.left + 1) * redarea)

    green = (int(fillstartcolor / &h100) and &hff) + int((i - itemrect.left) / (itemrect.right - itemrect.left + 1) * greenarea)

    blue = int(fillstartcolor / &h10000) + int((i - itemrect.left) / (itemrect.right - itemrect.left + 1) * bluearea)

    hpen = createpen(ps_solid, 1, rgb(red, green, blue))

    call selectobject(.hdc, hpen)

    call movetoex(.hdc, i, itemrect.top, 0)

    call lineto(.hdc, i, itemrect.bottom)

    call deleteobject(hpen)

    next i



    case isfs_verticalcolor ' 垂直漸變色



    bluearea = int(fillendcolor / &h10000) - int(fillstartcolor / &h10000)

    greenarea = (int(fillendcolor / &h100) and &hff) - (int(fillstartcolor / &h100) and &hff)

    redarea = (fillendcolor and &hff) - (fillstartcolor and &hff)



    for i = itemrect.top to itemrect.bottom - 1

    red = int(fillstartcolor and &hff) + int((i - itemrect.top) / (itemrect.bottom - itemrect.top + 1) * redarea)

    green = (int(fillstartcolor / &h100) and &hff) + int((i - itemrect.top) / (itemrect.bottom - itemrect.top + 1) * greenarea)

    blue = int(fillstartcolor / &h10000) + int((i - itemrect.top) / (itemrect.bottom - itemrect.top + 1) * bluearea)

    hpen = createpen(ps_solid, 1, rgb(red, green, blue))

    call selectobject(.hdc, hpen)

    call movetoex(.hdc, itemrect.left, i, 0)

    call lineto(.hdc, itemrect.right, i)

    call deleteobject(hpen)

    next i



    case isfs_solidcolor ' 實色填充



    hpen = createpen(ps_solid, 0, fillstartcolor)

    hbrush = createsolidbrush(fillstartcolor)

    selectobject .hdc, hpen

    selectobject .hdc, hbrush

    rectangle .hdc, itemrect.left, itemrect.top, itemrect.right, itemrect.bottom

    deleteobject hpen

    deleteobject hbrush



    end select





    ' 畫菜單項文字

    settextcolor .hdc, textselectcolor

    drawtext .hdc, myiteminfo(.itemid).itemtext, -1, textrect, dt_singleline or dt_left or dt_vcenter





    ' 畫菜單項圖標

    if myiteminfo(.itemid).itemtype <> mit_checkbox then

    drawiconex .hdc, iconrect.left + 2, iconrect.top + (iconrect.bottom - iconrect.top + 1 - 16) / 2, myiteminfo(.itemid).itemicon, 16, 16, 0, 0, di_normal

    select case iconstyle

    case iis_none ' 無效果



    case iis_sunken ' 凹進

    if myiteminfo(.itemid).itemicon <> 0 then

    drawedge .hdc, iconrect, bdr_sunkenouter, bf_rect

    end if

    case iis_raised ' 凸起

    if myiteminfo(.itemid).itemicon <> 0 then

    drawedge .hdc, iconrect, bdr_raisedinner, bf_rect

    end if

    case iis_shadow ' 陰影

    hbrush = createsolidbrush(rgb(128, 128, 128))

    drawstate .hdc, hbrush, 0, myiteminfo(.itemid).itemicon, 0, iconrect.left + 3, iconrect.top + (iconrect.bottom - iconrect.top + 1 - 16) / 2 + 1, 0, 0, dst_icon or dss_mono

    deleteobject hbrush

    drawiconex .hdc, iconrect.left + 1, iconrect.top + (iconrect.bottom - iconrect.top + 1 - 16) / 2 - 1, myiteminfo(.itemid).itemicon, 16, 16, 0, 0, di_normal

    end select

    else

    ' checkbox型菜單項圖標效果

    if myiteminfo(.itemid).itemstate and mis_checked then

    drawiconex .hdc, iconrect.left + 2, iconrect.top + (iconrect.bottom - iconrect.top + 1 - 16) / 2, myiteminfo(.itemid).itemicon, 16, 16, 0, 0, di_normal

    end if

    end if



    else ' 當鼠標移開菜單項時



    ' 畫菜單項邊框和背景(清除)

    if barstyle <> lbs_none then

    itemrect.left = barrect.right + 1

    else

    itemrect.left = 0

    end if

    hbrush = createsolidbrush(bkcolor)

    fillrect .hdc, itemrect, hbrush

    deleteobject hbrush





    ' 畫菜單項文字

    settextcolor .hdc, textenabledcolor

    drawtext .hdc, myiteminfo(.itemid).itemtext, -1, textrect, dt_singleline or dt_left or dt_vcenter





    ' 畫菜單項圖標

    if myiteminfo(.itemid).itemtype <> mit_checkbox then

    drawiconex .hdc, iconrect.left + 2, iconrect.top + (iconrect.bottom - iconrect.top + 1 - 16) / 2, myiteminfo(.itemid).itemicon, 16, 16, 0, 0, di_normal

    else

    if myiteminfo(.itemid).itemstate and mis_checked then

    drawiconex .hdc, iconrect.left + 2, iconrect.top + (iconrect.bottom - iconrect.top + 1 - 16) / 2, myiteminfo(.itemid).itemicon, 16, 16, 0, 0, di_normal

    end if

    end if



    end if

    else ' 當菜單項不可用時



    ' 畫菜單項文字

    settextcolor .hdc, textdisabledcolor

    drawtext .hdc, myiteminfo(.itemid).itemtext, -1, textrect, dt_singleline or dt_left or dt_vcenter



    ' 畫菜單項圖標

    if myiteminfo(.itemid).itemtype <> mit_checkbox then

    drawstate .hdc, 0, 0, myiteminfo(.itemid).itemicon, 0, iconrect.left + 2, iconrect.top + (iconrect.bottom - iconrect.top + 1 - 16) / 2, 0, 0, dst_icon or dss_disabled

    else

    if myiteminfo(.itemid).itemstate and mis_checked then

    drawstate .hdc, 0, 0, myiteminfo(.itemid).itemicon, 0, iconrect.left + 2, iconrect.top + (iconrect.bottom - iconrect.top + 1 - 16) / 2, 0, 0, dst_icon or dss_disabled

    end if

    end if



    end if

    end if



    end with

    end if

    end sub



    ' 菜單項事件響應(單擊菜單項)

    private sub menuitemselected(byval itemid as long)

    debug.print "鼠標單擊了:" & myiteminfo(itemid).itemtext

    select case myiteminfo(itemid).itemalias

    case "exit"

    dim frm as form

    for each frm in forms

    unload frm

    next

    end select

    end sub



    ' 菜單項事件響應(選擇菜單項)

    private sub menuitemselecting(byval itemid as long)

    debug.print "鼠標移動到:" & myiteminfo(itemid).itemtext

    end sub



    ok,到此為止,我們就徹底完成了菜單類的編寫,而且還包括一個測試窗體?,F在,完整的工程里應該包括兩個窗體:frmmain和frmmenu;一個標準模塊:mmenu;一個類模塊:cmenu。按f5編譯運行一下,在窗體空白處單擊鼠標右鍵。怎么樣,出現彈出式菜單了嗎?換個風格再試試。

    在看完這個系列的文章后,我想你應該已經對采用物主繪圖技術的自繪菜單有一定的了解了,回過頭來再看看ms office 2003的菜單,其實也沒什么難的嘛。以后,我們就可以在自己的任何程序中調用這個寫好的菜單類,為自己的程序添光加彩了。 :)

    該程序在windows xp、vb6下調試通過。

    源代碼下載地址:http://y365.com/ses518/soft/samplecsdn.zip

    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 彭水| 西乌珠穆沁旗| 青冈县| 壤塘县| 仁寿县| 华蓥市| 浮山县| 巴塘县| 新营市| 郑州市| 清远市| 丰城市| 宁武县| 宁蒗| 临沧市| 黑水县| 通山县| 同德县| 姜堰市| 乳山市| 连城县| 阜新| 石柱| 黑山县| 广宁县| 庆阳市| 镇赉县| 新宾| 湘潭市| 都昌县| 荃湾区| 敦煌市| 湖北省| 闽侯县| 崇明县| 喜德县| 台中县| 白银市| 镇江市| 兴隆县| 井陉县|