Pregunta

Tengo un diseñador de formularios informe escrito hace mucho tiempo para un proyecto de base de datos. Se utiliza una gran cantidad de magia winapi por lo tanto, me vi obligado a reescribir algunas partes 'en forma correcta'.

Gracias a algunos artículos de la revista de MSDN ( aquí y < a href = "http://msdn.microsoft.com/en-us/magazine/cc163634.aspx" rel = "nofollow noreferrer"> aquí ) y CodeProject yo era capaz de implementar superficie del diseñador, caja de herramientas y deshacer motor / rehacer.

  1. Cada recurso que descubrí sobre el tema hasta el momento es un poco anticuado. Puede señalar al artículo fresca / integral?

  2. artículo mencionado más arriba no parece trabajar .

    MenuCommandService.ShowContextMenu se llama, pero no aparece nada ya que no hay ningún DesignerVerbs en la colección globalVerbs. Debo añadir otras 'estándar', correspondieron a las acciones de diseño, tales como cortar / pegar, de forma manual? En caso afirmativo, ¿cómo puedo lograr esto?

¿Fue útil?

Solución

Gracias a fuentes SharpDevelop i era capaz de averiguar la solución

Esta implementación mínima (algunos comandos standart, no más) que funcionó para mí

using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;

namespace DesignerHost
{
    class MenuCommandServiceImpl : MenuCommandService
    {
        DesignerVerbCollection m_globalVerbs = new DesignerVerbCollection();

        public MenuCommandServiceImpl(IServiceProvider serviceProvider)
            : base(serviceProvider)
        {
            m_globalVerbs.Add(StandartVerb("Cut", StandardCommands.Cut));
            m_globalVerbs.Add(StandartVerb("Copy", StandardCommands.Copy));
            m_globalVerbs.Add(StandartVerb("Paste", StandardCommands.Paste));
            m_globalVerbs.Add(StandartVerb("Delete", StandardCommands.Delete));
            m_globalVerbs.Add(StandartVerb("Select All", StandardCommands.SelectAll));

        }

        private DesignerVerb StandartVerb(string text, CommandID commandID)
        {
            return new DesignerVerb(text,
                delegate(object o, EventArgs e) 
                {
                    IMenuCommandService ms = 
                        GetService(typeof(IMenuCommandService)) as IMenuCommandService;
                    Debug.Assert(ms != null);
                    ms.GlobalInvoke(commandID); 
                }
            );
        }

        class MenuItem : ToolStripMenuItem
        {
            DesignerVerb verb;

            public MenuItem(DesignerVerb verb)
                : base(verb.Text)
            {
                Enabled = verb.Enabled;
                this.verb = verb;
                Click += InvokeCommand;
            }

            void InvokeCommand(object sender, EventArgs e)
            {
                try
                {
                    verb.Invoke();
                }
                catch (Exception ex)
                {
                    Trace.Write("MenuCommandServiceImpl: " + ex.ToString());
                }
            }
        }

        private ToolStripItem[] BuildMenuItems()
        {
            List<ToolStripItem> items = new List<ToolStripItem>();

            foreach (DesignerVerb verb in m_globalVerbs) 
            {
                items.Add(new MenuItem(verb));
            }
            return items.ToArray();
        }

        #region IMenuCommandService Members

        /// This is called whenever the user right-clicks on a designer.
        public override void ShowContextMenu(CommandID menuID, int x, int y)
        {
            // Display our ContextMenu! Note that the coordinate parameters to this method
            // are in screen coordinates, so we've got to translate them into client coordinates.

            ContextMenuStrip cm = new ContextMenuStrip();
            cm.Items.AddRange(BuildMenuItems());

            ISelectionService ss = GetService(typeof (ISelectionService)) as ISelectionService;
            Debug.Assert(ss != null);

            Control ps = ss.PrimarySelection as Control;
            Debug.Assert(ps != null);

            Point s = ps.PointToScreen(new Point(0, 0));
            cm.Show(ps, new Point(x - s.X, y - s.Y));
        }

        #endregion

    }
}

Actualizar

encontrado solución similar

Otros consejos

Me encontré con el mismo problema hace un mes y tengo algo muy intersting y útiles de CodePlex aquí . He leído acerca de esto en Blog de Brad Abram post. Estos incluyen muchos ejemplos en la extensibilidad marco y la interfaz de formularios de Windows personalizada es uno de ellos.

Txt 1: http://mef.codeplex.com/

Txt 2: rel http://blogs.msdn.com/brada/archive/2009/04/13/managed-extensibility-framework-preview-5-released.aspx

Gracias.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top