【027】TOCControl上實現右鍵


---------------------------------------------------------------------------------------------------------

●·● 目錄:

A1 ………… 第一種方法:contextMenuStrip
A2 ………… 第二種方法:IToolbarMenu
1A ………… ICommandSubType 接口
2A ………… IToolbarMenu 接口

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第A1個    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● 第一種方法:contextMenuStrip

第一步:新建另外一個窗體

  • 首先要定義一個全局變量 ILayer。
  • 窗體要帶參數,以便將 ILayer 傳遞過來。
  • 獲取屬性列表。
        ILayer pLayer;            //定義一個虛擬的ILayer
public Form2(ILayer layer)
{
InitializeComponent();
pLayer = layer; //將賦值的layer再賦值給pLayer,這樣pLayer就是實體了
}

private void Form2_Load(object sender, EventArgs e)
{
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
IFeature pFeature = pFeatureCursor.NextFeature();
IFields pFields = pFeatureClass.Fields;

DataTable pTable = new DataTable();
for (int i = 0; i < pFields.FieldCount; i++) //獲取所有列
{
DataColumn pColumn = new DataColumn(pFields.get_Field(i).Name);
pTable.Columns.Add(pColumn);
}
while (pFeature != null)
{
DataRow pRow = pTable.NewRow();
for (int i = 0; i < pFields.FieldCount; i++) //添加每一列的值
{
pRow[i] = pFeature.get_Value(i);
}
pTable.Rows.Add(pRow);
pFeature = pFeatureCursor.NextFeature();
}
dataGridView1.DataSource = pTable;
}

第二步:建立右鍵菜單項

  • 添加一個 ContextMenuStrip 控件,然后增加幾個菜單項。
  • 特別注意加入一個菜單項為“顯示屬性”。
  • 判斷在什么情況下顯示右鍵菜單。
        ILayer m_Layer;  //溝通圖層
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
if (e.button == 2)
{
esriTOCControlItem Item = esriTOCControlItem.esriTOCControlItemNone;
IBasicMap pBasicMap = null;
ILayer pLayer = null;
object other = null;
object index = null;
axTOCControl1.HitTest(e.x, e.y, ref Item, ref pBasicMap, ref pLayer, ref other, ref index); //實現賦值
m_Layer = pLayer;
if (Item == esriTOCControlItem.esriTOCControlItemLayer) //點擊的是圖層的話,就顯示右鍵菜單
{
contextMenuStrip2.Show(axTOCControl1, new System.Drawing.Point(e.x, e.y));
//顯示右鍵菜單,並定義其相對控件的位置,正好在鼠標出顯示
}
}

}

private void 顯示屬性ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(m_Layer); //定義窗體,注意窗體的參數
frm.Text = "Attributes of " + m_Layer.Name; //顯示標題
frm.ShowDialog(); //以對話框形式顯示窗體
}

效果如下所示:

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第A2個    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● 第二種方法:IToolbarMenu

第一步:新建菜單框架:

IToolbarMenu m_TOCMapMenu = new ToolbarMenu();
IToolbarMenu m_TOCLayerMenu = new ToolbarMenu();

第二步:在菜單框架中加入菜單項,用 AddItem 方法實現:

m_TOCLayerMenu.Caption = "Sub Menu";       //二級菜單設置名稱
m_TOCLayerMenu.AddItem(new ControlsMapZoomInToolClass(), 0, 0, false, esriCommandStyles.esriCommandStyleIconAndText);
m_TOCLayerMenu.AddItem(new ControlsMapZoomInToolClass(), 0, 1, true, esriCommandStyles.esriCommandStyleIconAndText);
m_TOCLayerMenu.AddItem(new ControlsMapZoomInToolClass(), 0, 2, false, esriCommandStyles.esriCommandStyleIconAndText);
m_TOCLayerMenu.SetHook(axMapControl1);         //作用在 MapControl 上面

m_TOCMapMenu.AddItem(new ControlsAddDataCommandClass(), 0, 0, false, esriCommandStyles.esriCommandStyleIconAndText);
m_TOCMapMenu.AddItem(new ControlsMapFullExtentCommand(), 0, 1, false, esriCommandStyles.esriCommandStyleIconAndText);
m_TOCMapMenu.AddItem(new ControlsMapZoomInToolClass(), 0, 2, true, esriCommandStyles.esriCommandStyleIconAndText);
m_TOCMapMenu.AddItem(new ControlsMapZoomOutToolClass(), 0, 3, false, esriCommandStyles.esriCommandStyleIconAndText);
m_TOCMapMenu.AddItem(new ControlsPagePanToolClass(), 0, 4, false, esriCommandStyles.esriCommandStyleIconAndText);
m_TOCMapMenu.AddSubMenu(m_TOCLayerMenu, 5, false);

m_TOCMapMenu.SetHook(axMapControl1);

 

第三步:TOCControl 的 MouseDown 事件:

private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
    esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
    IBasicMap map = null;
    ILayer layer = null;
    object other = null;
    object index = null;

    axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);

    if (e.button == 2)
    {
        if (item == esriTOCControlItem.esriTOCControlItemMap)
        {
            axTOCControl1.SelectItem(map, null);
            m_TOCMapMenu.PopupMenu(e.x, e.y, axTOCControl1.hWnd);
        }
    }
}

 

實現效果如下:

public int AddItem (
    object item,
    int SubType,
    int index,
    bool beginGroup,
    esriCommandStyles Style
);

第一個參數:菜單項的內容,功能實現。
第二個參數:對於一個工具定義多個 type 的時候,才會用到,每一個 int 代表一個新的實現。
第三個參數:索引值,在菜單項上面顯示的位置。默認為 -1,按書寫順序排序。
第四個參數:是否開始一個新組,就是在其上面有一個“——”的效果。
第五個參數:顯示樣式。

用 SubType 實現:

首先:建立工具,這個時候類不僅要繼承 BaseCommand 類,還要繼承 ICommandSubType 接口

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第1A個    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● ICommandSubType 接口

 

  Provides access to members that define a subtyped command.

  Members

 

    Description
Method GetCount The number of commands defined with this CLSID.
Method SetSubType The subtype of the command.

 

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.SystemUI;

namespace ESRI_01
{
    /// <summary>
    /// Summary description for ScaleThresholdCmd.
    /// </summary>
    [Guid("41b9d7ee-ba25-47d1-9644-f9eca4243c34")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("ESRI_01.ScaleThresholdCmd")]
    public sealed class ScaleThresholdCmd : BaseCommand, ICommandSubType  //要多繼承一個接口!
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;
        private IMapControl4 m_mapControl;
        private long m_subType;    //用來分類的

        public ScaleThresholdCmd()
        {
        }

        #region Overriden Class Methods  //重寫的方法

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        /// 

        public override string Caption  //重寫標題
        {
            get
            {
                if (m_subType == 1)
                {
                    return "設置最大顯示比例尺";
                }
                else if (m_subType == 2)
                {
                    return "設置最小顯示比例尺";
                }
                else
                {
                    return "取消比例尺顯示限制";
                }
            }
        }

        public override bool Enabled  //重寫 Enabled 屬性
        {
            get
            {
                bool enabled = true;    //默認情況下都是 true 的
                ILayer layer = (ILayer)m_mapControl.CustomProperty;  //獲取所指的 layer,在 MouseDown 事件中有獲取到這個 layer。

                if (m_subType == 3)  //如果選擇的是 3,且最大和最小比例尺都沒有設置,則為 false!
                {
                    if ((layer.MaximumScale == 0) && (layer.MinimumScale == 0))
                    {
                        enabled = false;
                    }
                }
                return enabled;
            }
        }

        public override void OnCreate(object hook)  //重寫創建方法
        {
            if (hook is IMapControl4)
                m_mapControl = (IMapControl4)hook;  //給 m_mapControl 賦值
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()  //重寫單擊方法
        {
            // TODO: Add ScaleThresholdCmd.OnClick implementation
            IMap map = m_mapControl.Map;
            ILayer layer = m_mapControl.CustomProperty as ILayer;
            if (m_subType == 1)
            {
                layer.MaximumScale = map.MapScale;  //為 1 的時候,實現最大顯示比例尺
            }
            else if (m_subType == 2)
            {
                layer.MinimumScale = map.MapScale;  //為 2 的時候,實現最小顯示比例尺
            }
            else if (m_subType == 3)
            {
                layer.MaximumScale = 0;    //為 3 的時候,去除這些
                layer.MinimumScale = 0;
            }
        }

        #endregion

        #region ICommandSubType 成員  //繼承接口的話,就一定要實現接口了!

        public int GetCount()    //重寫 ICommandSubType 的 GetCount 方法
        {
            return 3;
        }

        public void SetSubType(int SubType)  //重寫 ICommandSubType 的 SetSubType 方法
        {
            m_subType = SubType;
        }

        #endregion
    }
}

 

最后通過下面的 1、2、3 來實現即可!

m_TOCLayerMenu.AddItem(new ScaleThresholdCmd(), 1, 2, true, esriCommandStyles.esriCommandStyleTextOnly);
m_TOCLayerMenu.AddItem(new ScaleThresholdCmd(), 2, 3, false, esriCommandStyles.esriCommandStyleTextOnly);
m_TOCLayerMenu.AddItem(new ScaleThresholdCmd(), 3, 4, false, esriCommandStyles.esriCommandStyleTextOnly);
m_TOCLayerMenu.SetHook(axMapControl1);

 

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第2A個    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● IToolbarMenu 接口

    Description
Method AddItem Adds an item to the ToolbarMenu.
Method AddSubMenu Adds a sub-menu to the ToolbarMenu.
Read/write property Bitmap The bitmap that is used as the icon on this ToolbarMenu.
Read/write property Caption The caption used by the ToolbarMenu.
Read/write property CommandPool The CommandPool used by the ToolbarMenu.
Read-only property Count The number of items on the ToolbarMenu.
Method Find Returns the index of the first item containing the given command or menu. Returns -1 if it is not found.
Method GetItem Returns the item at the specified index from the ToolbarMenu.
Method GetMenuDef Returns the menu definition, this method is obsolete.
Read-only property Hook The object that is passed as a hook to the OnCreate event of each item's command.
Method MoveItem Moves an item from one index to another.
Method PopupMenu Pops up the menu at the position specified.
Method Remove Removes the item at the specified index from the ToolbarMenu.
Method RemoveAll Removes all items from the ToolbarMenu.
Method SetHook Sets the object that is passed as a hook to the OnCreate event of each item's command.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM