• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            twzheng's cppblog

            『站在風口浪尖緊握住鼠標旋轉!』 http://www.cnblogs.com/twzheng

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              136 隨筆 :: 78 文章 :: 353 評論 :: 0 Trackbacks
            Flash ActionScript 3.0 實現的樹形菜單

            這是我初學ActionScript 3.0時實現的treeMenu類, 貼出來分享,或許對ActionScript 3.0的初學者有一定的幫助,但不建議在應用程序開發中使用。

            各位博友可以就此發表自己的觀點,謝謝各位指教。

            下面是treeMenu類的定義:
            /**
             *    treeMenu類
             *     
             *    構造一個樹形菜單
             *    
             *    @author    twzheng (twzheng@msn.cn)
             *    @date    20070903
             *    @version    1.0.070903
             *    
             
            */


            ////////////////////////////////////////////////////////////////////////////////////////////
            //
                在這里添加修改說明:
            //
            //
            //
            //////////////////////////////////////////////////////////////////////////////////////////

            package com.components
            {
                import flash.display.MovieClip;
                
                public class treeMenu extends MovieClip
                
            {
                    public 
            var rootMenu:menuItem;
                    
                    
            function treeMenu()
                    
            {
                    }

                    
                    public 
            function newTreeMenu()
                    
            {
                        
            if(rootMenu != null)
                        
            {
                            trace(
            " 錯誤:根菜單已存在,根菜單只能有一個!");
                            
            return;
                        }

                        rootMenu 
            = new menuItem("rootMenu","rootMenu");
                        
            if(rootMenu == null)
                        
            {
                            trace(
            " 創建根菜單失敗!");
                            
            return;
                        }

                        rootMenu.removeChild(rootMenu.menuLabel);
                        rootMenu.childMenu.x 
            = 0;
                        rootMenu.childMenu.y 
            = 0;
                        
            this.addChild(rootMenu);
                    }

                    
                    public 
            function addChildMenu(bMenu:menuItem, mName:String, mLabel:String)
                    
            {
                        
            var mItem = new menuItem(mName,mLabel);
                        
            if(mItem == nullreturn;
                        
            var index:int = bMenu.childItem.length;
                        
                        mItem.y 
            = menuItemLocalizer(bMenu.childItem);
                        
                        bMenu.childItem[index] 
            = mItem;
                        
                        bMenu.childMenu.addChild(mItem);
                    }

                    
                    private 
            function menuItemLocalizer(bMenu:Array):int
                    
            {
                        
            var num:int = 0;
                        
            for each (var item in bMenu)
                            num 
            = num + item.getHeight();
                        
            return num;
                    }

                }

            }


            /**
             * 菜單項節點類
             * 
             * @author    twzheng (http://www.ugocn.com)
             * @date    20070903
             * @version    1.0.070903
             
            */
                

            import flash.display.DisplayObjectContainer;
            import flash.display.MovieClip;
            import flash.text.TextField;
            import flash.events.MouseEvent;
            import flash.events.Event;
            import flash.ui.Mouse;

            internal class menuItem extends MovieClip
            {
                public 
            var menuName:String;            // 菜單項名稱
                public var menuLabel:TextField;        // 菜單項標簽(即顯示給用戶的菜單標簽)
                public var childMenu:DisplayObjectContainer; // 子菜單項容器
                public var childItem:Array;            // 子菜單項數組
                
                private 
            var h:Number;        // 菜單項高度屬性,記錄的是實際高度(包含隱藏菜單高度)
                
                
            function menuItem(mName:String, mLabel:String)
                
            {
                    
            if(mName == "" || mName == null || mLabel == null)
                    
            {
                        trace(
            " 菜單名或者菜單標簽為空,添加菜單項失敗!");
                        
            return;
                    }

                    menuName 
            = mName;
                    
                    menuLabel 
            = new TextField();
                    menuLabel.text 
            = mLabel;
                    menuLabel.height 
            = 22;
                    menuLabel.textColor 
            = 0x000000;
                    menuLabel.background 
            = false;
                    menuLabel.addEventListener(MouseEvent.MOUSE_MOVE,itemMouseMove);
                    menuLabel.addEventListener(MouseEvent.MOUSE_OUT,itemMouseOut);
                    
            this.addChild(menuLabel);
                    
                    childMenu 
            = new MovieClip();
                    childMenu.addEventListener(Event.ADDED,mcAddedEvent);
                    childMenu.addEventListener(Event.REMOVED,mcRemovedEvent);
                    
            this.childMenu.x = this.menuLabel.x + 8;
                    
            this.childMenu.y = this.menuLabel.y + this.menuLabel.height;
                    
            this.addChild(childMenu);
                    
                    childItem 
            = new Array();
                    
                    
            this.h = menuLabel.height;
                    
            // 菜單項單擊事件應留給外部使用者實現
                    //this.menuLabel.addEventListener(MouseEvent.CLICK,itemClick);
                }

                
                
            // 返回菜單項顯示的真實高度,即菜單項的實際高度減去隱藏菜單項的高度
                public function getHeight():Number
                
            {
                    
            return h - getHideMenu(this);
                }

                
                
            /**
                * 獲取item的childMenu中所有隱藏子菜單項的高度和
                *
                * @item                主菜單項,此函數即計算它的子菜單中隱藏菜單的高度
                * @return            返回item的childMenu中visible屬性為false的子菜單高度和
                * 
                * 注:如果item.childMenu的visible屬性為false即返回childMenu的高度,如果item.childMenu為空則返回0。
                
            */

                private 
            function getHideMenu(item:menuItem):Number
                
            {
                    
            var sumHeight = 0;
                    
            if(item.childMenu.visible)
                    
            {
                        
            if(item.childItem == null)
                            
            return 0;
                        
            for(var i = 0; i < item.childItem.length; i++)
                        
            {
                            
            // 對每個子菜單項遞歸
                            sumHeight = sumHeight + getHideMenu(item.childItem[i]);
                        }

                        
            return sumHeight;
                    }

                    
            else
                        
            return item.childMenu.height;
                }

                
                
            /**
                * 獲取名字為mName菜單項的對象
                *
                * @mName            菜單項名字字符串
                * @return            返回調用此函數的菜單項的子菜單中名字為mName的子菜單項對象
                
            */

                public 
            function getMenu(mName:String):menuItem
                
            {
                    
            for each(var item in childItem)
                    
            {
                        
            if(item.menuName == mName)
                            
            return item;
                    }

                    trace(
            " 錯誤:不存在名為 " + mName + " 的子菜單項!");
                    
            return null;
                }

                
                
            /**
                * 接收TextField的單擊事件,更改TextField對應item的子菜單顯示狀態
                *
                * @item            接收到單擊事件的menuLabel對應的菜單項(menuItem)
                
            */

                public 
            function chgChildItemVisible(item:menuItem)
                
            {
                    
            var chgHeight = 0;
                    
            if(item.childMenu.visible)
                    
            {
                        chgHeight 
            = item.getHeight() - item.menuLabel.height;
                        item.childMenu.visible 
            = false;
                        updateMenu(item,
            0 - chgHeight);
                    }

                    
            else
                    
            {
                        item.childMenu.visible 
            = true;
                        chgHeight 
            = item.childMenu.height - getHideMenu(item);
                        updateMenu(item,chgHeight);
                    }

                }

                
                
            /**
                * 更新各菜單項位置
                * 注:由于參數item的子菜單容器childMenu高度發生變化而需要改變其同級別的菜單項以及所有的父菜單項的y坐標
                *
                * @item            引發調用此函數的菜單項(即由于item的childMenu高度改變而需要調用此函數)
                * @chgHeight    需要改變的y坐標高度,正值即增加y坐標值,負值減小y坐標值
                
            */

                private 
            function updateMenu(item:menuItem, chgHeight:Number)
                
            {
                    
            if(item == nullreturn;
                    
            // item.parent為父菜單的子菜單容器,item.parent.paren才是對應的父菜單項
                    var parentItem = item.parent.parent;

                    
            var i,index:int = 0;
                    
                    
            if(parentItem == null || ! (parentItem is menuItem))
                        
            return;
                    
                    
            // 搜索item在父菜單的子菜單數組childItem中的索引
                    index = parentItem.childItem.indexOf(item);
                    
                    
            // 改變item同級別的并且位于其后的菜單項的顯示位置
                    for(i = index + 1; i < parentItem.childItem.length; i++)
                    
            {
                        parentItem.childItem[i].y 
            = parentItem.childItem[i].y + chgHeight;
                    }

                    
                    
            // 對父菜單項遞歸
                    updateMenu(parentItem,chgHeight);
                }

                
            //    private function itemClick(e:MouseEvent)
            //
                {
            //
                    var item = e.currentTarget;
            //
                    chgChildItemVisible(item.parent);
            //
                }
                
                private 
            function itemMouseMove(e:MouseEvent)
                
            {
                    
            var item = e.currentTarget;
                    item.background 
            = true;
                    item.backgroundColor 
            = 0x66ccFF;
                    item.textColor 
            = 0x0000FF;
                }

                
                private 
            function itemMouseOut(e:MouseEvent)
                
            {
                    
            var item = e.currentTarget;
                    item.background 
            = false;
                    item.backgroundColor 
            = 0xFFFFFF;
                    item.textColor 
            = 0x000000;
                }

                
                
            // 向子菜單容器加入子菜單項事件,增加當前菜單的高度
                private function mcAddedEvent(e:Event)
                
            {// 此事件響應函數還需要更改。。。
                    var mc = e.currentTarget;
                    h 
            = h + 22;//mc.height;///2
                }

                
                
            // 從子菜單容器移除子菜單項事件,減小當前菜單的高度
                private function mcRemovedEvent(e:Event)
                
            {// 此事件響應函數還需要更改。。。
                    var mc = e.currentTarget;
                    h 
            = h - 22;//mc.height/2;
                }

            }



            treeMenu類簡單應用
            package 
            {
                import flash.display.MovieClip;    
                import flash.events.
            *;

                import com.library.treeMenu;
                
                public class menu extends MovieClip
                
            {
                    
            function menu()
                    
            {
                        
            var tm = new treeMenu();
                        tm.newTreeMenu();            
            // 創建根菜單
                        
                        tm.addChildMenu(tm.rootMenu,
            "基菜單-01","基菜單-01");
                        tm.rootMenu.getMenu(
            "基菜單-01").menuLabel.addEventListener(MouseEvent.CLICK,eventClick);
                        
                        tm.addChildMenu(tm.rootMenu.getMenu(
            "基菜單-01"),"一級菜單-011","一級菜單-011");
                        
                        tm.addChildMenu(tm.rootMenu,
            "基菜單-02","基菜單-02");
                        tm.rootMenu.getMenu(
            "基菜單-02").menuLabel.addEventListener(MouseEvent.CLICK,eventClick);
                        
                        tm.addChildMenu(tm.rootMenu.getMenu(
            "基菜單-02"),"一級菜單-021","一級菜單-021");
                        tm.rootMenu.getMenu(
            "基菜單-02").getMenu("一級菜單-021").menuLabel.addEventListener(MouseEvent.CLICK,eventClick);
                        
                        tm.addChildMenu(tm.rootMenu.getMenu(
            "基菜單-02").getMenu("一級菜單-021"),"二級菜單-0211","二級菜單-0211");
                        tm.rootMenu.getMenu(
            "基菜單-02").getMenu("一級菜單-021").getMenu("二級菜單-0211").menuLabel.addEventListener(MouseEvent.CLICK,eventClick);
                        
                        tm.addChildMenu(tm.rootMenu.getMenu(
            "基菜單-02").getMenu("一級菜單-021").getMenu("二級菜單-0211"),"三級菜單-02111","三級菜單-02111");
                        tm.addChildMenu(tm.rootMenu,
            "基菜單-03","基菜單-03");
                        
                        
            // 菜單坐標 默認坐標(0,0)
                        //tm.x = 50;
                        //tm.y = 50;
                        this.addChild(tm);
                    }

                    
                    private 
            function eventClick(e:MouseEvent)
                    
            {
                        
            var item = e.currentTarget;
                        item.parent.chgChildItemVisible(item.parent);    
            // 隱藏或顯示子菜單項
                    }

                }

            }


            posted on 2007-10-19 00:51 譚文政 閱讀(3143) 評論(9)  編輯 收藏 引用 所屬分類: Flash AS 3.0

            評論

            # re: ActionScript 3.0 實現樹形菜單(TreeMenu) 2007-10-19 07:43 Minidx全文檢索
            非C++,屬于其它技術哦  回復  更多評論
              

            # re: ActionScript 3.0 實現樹形菜單(TreeMenu) 2007-10-19 07:43 Minidx全文檢索
            非C++,屬于其它技術哦  回復  更多評論
              

            # re: ActionScript 3.0 實現樹形菜單(TreeMenu) 2007-10-19 12:53 苦味酸
            對,這是用Flash ActionScript 3.0 實現的。

            由于項目的需要,前段時間改學Flash ActionScript 3.0了,覺得自己越學越雜,但計算機語言都是相通的,大同小異,重要的是設計思想,軟件的架構。我現在比較熟悉的語言是C++/C#和ActionScript 3.0。  回復  更多評論
              

            # re: ActionScript 3.0 實現樹形菜單(TreeMenu)[未登錄] 2008-02-26 21:15 小小菜鳥
            wangyan.work@qq.com

            發個源文件吧,謝謝  回復  更多評論
              

            # re: ActionScript 3.0 實現樹形菜單(TreeMenu)[未登錄] 2008-02-26 21:34 小小菜鳥
            能拖動節點換位置,給節點改名,添件刪除節點,就美了,呵呵
              回復  更多評論
              

            # re: ActionScript 3.0 實現樹形菜單(TreeMenu) 2008-10-17 17:08 hfydm
            編譯不能通過,相關信息:1046: Type was not found or was not a compile-time constant: menuItem.  回復  更多評論
              

            # re: ActionScript 3.0 實現樹形菜單(TreeMenu) 2009-02-18 09:09 needi
            這個怎么用啊。。有沒有實例啊!????  回復  更多評論
              

            # re: ActionScript 3.0 實現樹形菜單(TreeMenu) 2009-11-19 18:46 路過的人
            拜讀了,謝謝  回復  更多評論
              

            # re: ActionScript 3.0 實現樹形菜單(TreeMenu) 2012-04-05 09:10 fds
            灰常感謝  回復  更多評論
              

            久久国产精品99精品国产| 久久精品国产精品亚洲艾草网美妙 | 久久国产劲爆AV内射—百度| 亚洲精品乱码久久久久久久久久久久 | www.久久精品| 久久久久久A亚洲欧洲AV冫| 一本色道久久综合| 久久国产欧美日韩精品| 久久精品成人免费国产片小草| 久久久久久久久久久久久久 | 亚洲国产精品久久久天堂| 精品国产福利久久久| 久久夜色精品国产亚洲| 久久噜噜电影你懂的| 亚洲一区精品伊人久久伊人| 91久久精一区二区三区大全| 伊人久久大香线蕉精品不卡| 伊人久久免费视频| 欧美喷潮久久久XXXXx| 日本精品久久久久影院日本| 97精品国产91久久久久久| 久久综合亚洲色HEZYO社区 | 99精品国产99久久久久久97| 久久国产成人午夜aⅴ影院| 久久人人爽人人爽人人片av高请| 久久丝袜精品中文字幕| 国产精品永久久久久久久久久 | 久久久精品国产Sm最大网站| 久久久中文字幕| 久久91精品国产91久久小草| 久久精品国产亚洲av高清漫画| 精产国品久久一二三产区区别 | 久久亚洲精品中文字幕| 日本五月天婷久久网站| 亚洲人AV永久一区二区三区久久| 久久精品国产精品亚洲人人 | 亚洲精品国产综合久久一线| 日韩美女18网站久久精品| 久久国产精品视频| 久久精品国产亚洲AV不卡| 日韩欧美亚洲综合久久影院Ds|