关闭后 IWindowsFormsEditorService 延迟

IWindowsFormsEditorService delay after closing

我需要将自定义 属性 值编辑器集成到 PropertyGrid 控件中。代码如下

internal class VariableTypeEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.DropDown;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;

        Type type = context.GetType();
        PropertyInfo property = type.GetProperty("OwnerGrid");

        IPlugIn plug = context.Instance as IPlugIn;

        if (service != null && property != null
                            && plug != null)
        {
            PropertyGrid owner = property.GetValue(context) as PropertyGrid;

            if (owner != null)
            {
                Collection<VariableWrapper> variables = owner.Tag as Collection<VariableWrapper>;

                if (variables != null)
                {
                    VariableEditorForm editor = new VariableEditorForm();
                    editor.Value = plug.VariableName;
                    editor.Variables = variables.Select(o => o.Variable).Where(o => o.ValueType == plug.VariableType).ToArray();
                    editor.TopLevel = false;
                    editor.FormClosed += new FormClosedEventHandler((sender, args) =>
                    {
                        service.CloseDropDown();
                    });

                    service.DropDownControl(editor);
                    value = editor.Value;
                }
            }
        }

        return value;
    }
}

我希望它以这种方式工作:

现在可以了。但由于某种原因,EditValue 方法 return 其值需要 2-3 秒。

为什么 service.DropDownControl(editor) 不会在关闭后立即 return?

我设法找到了解决方案。我不喜欢,但它有效。代码如下。

            public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;

                Type type = context.GetType();
                PropertyInfo property = type.GetProperty("OwnerGrid");

                IPlugIn plug = context.Instance as IPlugIn;

                if (service != null && property != null
                                    && plug != null)
                {
                    PropertyGrid owner = property.GetValue(context) as PropertyGrid;

                    if (owner != null)
                    {
                        Collection<VariableWrapper> variables = owner.Tag as Collection<VariableWrapper>;

                        if (variables != null)
                        {
                            VariableEditorForm editor = new VariableEditorForm();
                            editor.Value = plug.VariableName;
                            editor.Variables = variables.Select(o => o.Variable).Where(o => o.ValueType == plug.VariableType).ToArray();
                            editor.TopLevel = false;
                            editor.FormClosed += new FormClosedEventHandler((sender, args) =>
                            {
                                // disable main form rendering
                                User32.PostMessage(Process.GetCurrentProcess().MainWindowHandle, Messages.WM_DISABLE_RENDER, IntPtr.Zero, IntPtr.Zero);
                                service.CloseDropDown();
                            });

                            service.DropDownControl(editor);

                            // enable main form rendering
                            User32.PostMessage(Process.GetCurrentProcess().MainWindowHandle, Messages.WM_ENABLE_RENDER, IntPtr.Zero, IntPtr.Zero);
                            value = editor.Value;
                            editor.Dispose();
                        }
                    }
                }

                return value;
            }

问题是我的主窗体上有一个面板,它使用计时器不断地呈现自己。我注意到每次禁用该计时器时我的问题都会消失。因此,没有更好的解决方案,我决定 post 向我的主表单发送消息给 enable/disable 提到的计时器。

如果有人能为我的问题提供适当的解决方案,我将不胜感激。

是否可以使用 const 的值:WM_DISABLE_RENDERWM_ENABLE_RENDER