日期:2014-05-18  浏览次数:20789 次

熟悉VS SDK开发的请帮我解决一个问题
当前我新建了一个基于Component类,并声明为IRootDesigner

现在出现的效果就是在VS设计模式时,可以出现一个带点的面板(那些点都是自己用双缓冲+GDI画的)

如下图所示:




现在出现的情况就是我想在设计面板中点击鼠标右键,弹出菜单后出现“查看代码”“粘贴”“属性”等操作,类似于WinForm设计时右键菜单。请问该如何实现。

注:现在我已安装好VS SDK 2008 SP1

以下附基于Component类的代码:

1. DesignPanel

C# code
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;

namespace eWhereThink.Workflow.Design
{
    [Designer(typeof(DesignPanelDesigner), typeof(IRootDesigner))]
    [Designer(typeof(ComponentDesigner))]
    [ToolboxItem(false)]
    public class DesignPanel : Component
    {
        public DesignPanel()
        {

        }

    }
}



2. DesignPanelDesigner
C# code
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace eWhereThink.Workflow.Design
{
    public class DesignPanelDesigner : ComponentDocumentDesigner, IRootDesigner
    {
        DesignControl _rootViewControl;

        public DesignPanelDesigner()
        {
            
        }

        public object GetView(ViewTechnology technology)
        {
            _rootViewControl = new DesignControl(this);
            return _rootViewControl;
        }

        public ViewTechnology[] SupportedTechnologies
        {
            get
            {
                return new ViewTechnology[] { ViewTechnology.Default };
            }
        }

        protected override bool GetToolSupported(ToolboxItem tool)
        {
            MessageBox.Show(tool.TypeName);
            return true;
        }

        public void ToolPicked(ToolboxItem tool)
        {
            MessageBox.Show(tool.TypeName + "     ToolPicked");
        }
    }
}



3. DesignControl
C# code
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace eWhereThink.Workflow.Design
{
    [ToolboxItem(false)]
    public class DesignControl : ScrollableControl
    {
        private DesignPanelDesigner _rootDesigner;
        private IContainer components;
        private Hashtable _componentInfoTable;

        public DesignControl(DesignPanelDesigner rootDesigner)
        {
            InitializeComponent();
            _rootDesigner = rootDesigner;
            _componentInfoTable = new Hashtable();

            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);

            this.Invalidate();
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // DesignControl
            // 
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.DesignControl_Paint);
            this.ResumeLayout(false);

        }

        private void DesignControl_Paint(object sender, PaintEventArgs e)
        {
            float fZoom = 1F;

            Size size = new Size();
            size.Width = Math.Max(this.Width, this.AutoScrollMinSize.Width);
            size.Height = Math.Max(this.Height, this.AutoScrollMinSize.Height);

            Bit