简体   繁体   English

如何在自定义设计器中使用Visual Studio资源选择器对话框

[英]How to Use Visual Studio Resource Selector Dialog in Custom Designer

Using .NET 4.0 with Visual Studio 2017 and Visual Basic .NET (could be done in C# just as well), I have created a WinForms application. 将.NET 4.0与Visual Studio 2017和Visual Basic .NET结合使用(也可以在C#中完成),我创建了一个WinForms应用程序。 As part of the application I have created a custom control by adding a new class and inheriting from System.Windows.Forms.Control 作为应用程序的一部分,我通过添加一个新类并从System.Windows.Forms.Control继承来创建了一个自定义控件。

Public Class MyControl
    Inherits System.Windows.Forms.Control
End Class

If I add the custom control to a form, I can add a BackgroundImage using the Property Window. 如果将自定义控件添加到窗体,则可以使用“属性窗口”添加BackgroundImage。 In the Property Window, if I click in the BackgroundImage property, it shows an ellipsis button. 在“属性”窗口中,如果单击BackgroundImage属性,它将显示一个省略号按钮。 Clicking on that button will open a Select Resource Dialog window. 单击该按钮将打开“选择资源对话框”窗口。

在此处输入图片说明

I have now created a custom designer for the control by inheriting from System.Windows.Forms.Design.ControlDesigner . 现在,我通过继承System.Windows.Forms.Design.ControlDesigner为控件创建了一个自定义设计器。 I have also created the designer form that pops up when the control is double clicked in design view. 我还创建了设计器表单,当在设计视图中双击控件时,该表单就会弹出。 On the designer form I want to be able to choose a Background image using the same Select Resource dialog from Visual Studio that is shown above. 在设计器表单上,我希望能够使用上面显示的来自Visual Studio的相同“选择资源”对话框来选择背景图像。 I have been unable to find where the Select Resource Dialog exists.My suspicion was in the following assembly, but I did not find it. 我一直找不到“选择资源”对话框的位置。我怀疑是在以下程序集中,但没有找到它。

Microsoft.VisualStudio.Design.dll

Can someone tell me the fully qualified namespace for the Select Resource Dialog used by Visual Studio and which assembly it exists in? 有人可以告诉我Visual Studio使用的“选择资源”对话框的全限定名称空间以及它存在于哪个程序集中吗?

You can show the property editor of all properties at design time using code. 您可以在设计时使用代码显示所有属性的属性编辑器。 To do so, you can find the internal EditorServiceContext which is in System.Design assembly, and then invoke its EditValue method by passing the your control designer, the control to edit and the property name to edit. 为此,您可以在System.Design程序EditorServiceContext找到内部的EditorServiceContext ,然后通过传递控件设计器,要编辑的控件和要编辑的属性名称来调用其EditValue方法。

Example

You can find the full source code for example here in this repository: 您可以在此存储库中找到示例的完整源代码:

The core part of the sample is the designer class: 该示例的核心部分是设计器类:

using System.ComponentModel.Design;
using System.Linq;
using System.Windows.Forms.Design;
public class MyControlDesigner : ControlDesigner
{
    DesignerVerbCollection verbs;
    public override DesignerVerbCollection Verbs
    {
        get
        {
            if (verbs == null)
            {
                verbs = base.Verbs;
                verbs.Add(new DesignerVerb("Show Select Resource", 
                    (s, e) => ShowSelectResource()));
            }
            return verbs;
        }
    }
    public void ShowSelectResource()
    {
        var editorServiceContext = typeof(ControlDesigner).Assembly.GetTypes()
            .Where(x => x.Name == "EditorServiceContext").FirstOrDefault();
        var editValue = editorServiceContext.GetMethod("EditValue",
            System.Reflection.BindingFlags.Static |
            System.Reflection.BindingFlags.Public);
        editValue.Invoke(null, new object[] { this, this.Component, "SomeProperty" });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM