[英]How to Use Visual Studio Resource Selector Dialog in Custom Designer
將.NET 4.0與Visual Studio 2017和Visual Basic .NET結合使用(也可以在C#中完成),我創建了一個WinForms應用程序。 作為應用程序的一部分,我通過添加一個新類並從System.Windows.Forms.Control繼承來創建了一個自定義控件。
Public Class MyControl
Inherits System.Windows.Forms.Control
End Class
如果將自定義控件添加到窗體,則可以使用“屬性窗口”添加BackgroundImage。 在“屬性”窗口中,如果單擊BackgroundImage屬性,它將顯示一個省略號按鈕。 單擊該按鈕將打開“選擇資源對話框”窗口。
現在,我通過繼承System.Windows.Forms.Design.ControlDesigner為控件創建了一個自定義設計器。 我還創建了設計器表單,當在設計視圖中雙擊控件時,該表單就會彈出。 在設計器表單上,我希望能夠使用上面顯示的來自Visual Studio的相同“選擇資源”對話框來選擇背景圖像。 我一直找不到“選擇資源”對話框的位置。我懷疑是在以下程序集中,但沒有找到它。
Microsoft.VisualStudio.Design.dll
有人可以告訴我Visual Studio使用的“選擇資源”對話框的全限定名稱空間以及它存在於哪個程序集中嗎?
您可以在設計時使用代碼顯示所有屬性的屬性編輯器。 為此,您可以在System.Design
程序EditorServiceContext
找到內部的EditorServiceContext
,然后通過傳遞控件設計器,要編輯的控件和要編輯的屬性名稱來調用其EditValue
方法。
例
您可以在此存儲庫中找到示例的完整源代碼:
該示例的核心部分是設計器類:
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.