IDesignerEventService 介面
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在以下情況下提供事件通知:加入和移除主要設計工具時、變更選取的元件時,以及目前的主要計工具變更時。
public interface class IDesignerEventService
public interface IDesignerEventService
type IDesignerEventService = interface
Public Interface IDesignerEventService
範例
此範例示範設計工具,該設計工具會使用 IDesignerEventService 在控件上顯示事件通知。 若要使用此範例,請將程式代碼編譯為類別庫。 將連結庫的參考新增至新的 Windows Forms 專案,並將連結庫內的控件新增至 [工具箱]。 將控件的 DesignerMonitor
實例新增至設計模式中的表單。 以滑鼠右鍵按下 控件,然後按下 Start monitoring
命令,以顯示 所 IDesignerEventService引發事件的通知。
#using <system.dll>
#using <system.design.dll>
#using <system.windows.forms.dll>
#using <system.drawing.dll>
using namespace System;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Collections;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::Design;
namespace DesignerEventServiceExample
{
ref class DesignerMonitorDesigner;
// DesignerMonitor provides a display for designer event notifications.
[Designer(DesignerEventServiceExample::DesignerMonitorDesigner::typeid)]
public ref class DesignerMonitor: public UserControl
{
public:
// List to contain strings that describe designer events.
ArrayList^ updates;
bool monitoring_events;
DesignerMonitor()
{
monitoring_events = false;
updates = gcnew ArrayList;
this->BackColor = Color::White;
this->Size = System::Drawing::Size( 450, 300 );
}
protected:
// Display the message for the current mode, and any event messages if event monitoring is active.
virtual void OnPaint( PaintEventArgs^ e ) override
{
e->Graphics->DrawString( "IDesignerEventService DesignerMonitor Control", gcnew System::Drawing::Font( FontFamily::GenericMonospace,10 ), gcnew SolidBrush( Color::Blue ), 5, 4 );
int yoffset = 10;
if ( !monitoring_events )
{
yoffset += 10;
e->Graphics->DrawString( "Currently not monitoring designer events.", gcnew System::Drawing::Font( FontFamily::GenericMonospace,10 ), gcnew SolidBrush( Color::Black ), 5.f, yoffset + 10.f );
e->Graphics->DrawString( "Use the shortcut menu commands", gcnew System::Drawing::Font( FontFamily::GenericMonospace,10 ), gcnew SolidBrush( Color::Black ), 5.f, yoffset + 30.f );
e->Graphics->DrawString( "provided by an associated DesignerMonitorDesigner", gcnew System::Drawing::Font( FontFamily::GenericMonospace,10 ), gcnew SolidBrush( Color::Black ), 5.f, yoffset + 40.f );
e->Graphics->DrawString( "to start or stop monitoring.", gcnew System::Drawing::Font( FontFamily::GenericMonospace,10 ), gcnew SolidBrush( Color::Black ), 5.f, yoffset + 50.f );
}
else
{
e->Graphics->DrawString( "Currently monitoring designer events.", gcnew System::Drawing::Font( FontFamily::GenericMonospace,10 ), gcnew SolidBrush( Color::DarkBlue ), 5.f, yoffset + 10.f );
e->Graphics->DrawString( "Designer created, changed and disposed events:", gcnew System::Drawing::Font( FontFamily::GenericMonospace,9 ), gcnew SolidBrush( Color::Brown ), 5.f, yoffset + 35.f );
for ( int i = 0; i < updates->Count; i++ )
{
e->Graphics->DrawString( static_cast<String^>(updates[ i ]), gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Black ), 5.f, yoffset + 55.f + (10 * i) );
yoffset += 10;
}
}
}
};
// DesignerMonitorDesigner uses the IDesignerEventService to send event information
// to an associated DesignerMonitor control's updates collection.
public ref class DesignerMonitorDesigner: public ControlDesigner
{
private:
DesignerMonitor^ dm;
DesignerVerbCollection^ dvc;
int eventcount;
void StopMonitoring( Object^ /*sender*/, EventArgs^ /*e*/ )
{
IDesignerEventService^ des = dynamic_cast<IDesignerEventService^>(this->Control->Site->GetService( IDesignerEventService::typeid ));
if ( des != nullptr )
{
// Remove event handlers for event notification methods.
des->DesignerCreated -= gcnew DesignerEventHandler( this, &DesignerMonitorDesigner::DesignerCreated );
des->DesignerDisposed -= gcnew DesignerEventHandler( this, &DesignerMonitorDesigner::DesignerDisposed );
des->ActiveDesignerChanged -= gcnew ActiveDesignerEventHandler( this, &DesignerMonitorDesigner::DesignerChanged );
des->SelectionChanged -= gcnew EventHandler( this, &DesignerMonitorDesigner::SelectionChanged );
dm->monitoring_events = false;
// Rebuild menu with "Start monitoring" command.
array<DesignerVerb^>^myArray = {gcnew DesignerVerb( "Start monitoring",gcnew EventHandler( this, &DesignerMonitorDesigner::StartMonitoring ) )};
dvc = gcnew DesignerVerbCollection( myArray );
dm->Refresh();
}
}
void StartMonitoring( Object^ /*sender*/, EventArgs^ /*e*/ )
{
IDesignerEventService^ des = dynamic_cast<IDesignerEventService^>(this->Control->Site->GetService( IDesignerEventService::typeid ));
if ( des != nullptr )
{
// Add event handlers for event notification methods.
des->DesignerCreated += gcnew DesignerEventHandler( this, &DesignerMonitorDesigner::DesignerCreated );
des->DesignerDisposed += gcnew DesignerEventHandler( this, &DesignerMonitorDesigner::DesignerDisposed );
des->ActiveDesignerChanged += gcnew ActiveDesignerEventHandler( this, &DesignerMonitorDesigner::DesignerChanged );
des->SelectionChanged += gcnew EventHandler( this, &DesignerMonitorDesigner::SelectionChanged );
dm->monitoring_events = false;
// Rebuild menu with "Stop monitoring" command.
array<DesignerVerb^>^myArray = {gcnew DesignerVerb( "Stop monitoring",gcnew EventHandler( this, &DesignerMonitorDesigner::StopMonitoring ) )};
dvc = gcnew DesignerVerbCollection( myArray );
dm->Refresh();
}
}
void DesignerCreated( Object^ /*sender*/, DesignerEventArgs^ e )
{
UpdateStatus( "Designer for " + e->Designer->RootComponent->Site->Name + " was created." );
}
void DesignerDisposed( Object^ /*sender*/, DesignerEventArgs^ e )
{
UpdateStatus( "Designer for " + e->Designer->RootComponent->Site->Name + " was disposed." );
}
void DesignerChanged( Object^ /*sender*/, ActiveDesignerEventArgs^ e )
{
UpdateStatus( "Active designer moved from " + e->OldDesigner->RootComponent->Site->Name + " to " + e->NewDesigner->RootComponent->Site->Name + "." );
}
void SelectionChanged( Object^ /*sender*/, EventArgs^ /*e*/ )
{
UpdateStatus("A component selection was changed.");
}
// Update message buffer on DesignerMonitor control.
void UpdateStatus( String^ newmsg )
{
if ( dm->updates->Count > 10 )
dm->updates->RemoveAt( 10 );
dm->updates->Insert( 0, "Event #" + eventcount.ToString() + ": " + newmsg );
eventcount++;
dm->Refresh();
}
};
}
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Namespace DesignerEventServiceExample
' DesignerMonitor provides a display for designer event notifications.
<Designer(GetType(DesignerMonitorDesigner))> _
Public Class DesignerMonitor
Inherits System.Windows.Forms.UserControl
' List to contain strings that describe designer events.
Public updates As ArrayList
Public monitoring_events As Boolean = False
Public Sub New()
updates = New ArrayList()
Me.BackColor = Color.Beige
Me.Size = New Size(450, 300)
End Sub
' Display the message for the current mode, and any event messages if monitoring events
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.DrawString("IDesignerEventService DesignerMonitor control", New Font(FontFamily.GenericMonospace, 10), New SolidBrush(Color.Red), 5, 4)
Dim yoffset As Integer = 10
If Not monitoring_events Then
yoffset += 10
e.Graphics.DrawString("Currently not monitoring designer events.", New Font(FontFamily.GenericMonospace, 10), New SolidBrush(Color.Black), 5, yoffset + 10)
e.Graphics.DrawString("Use the shortcut menu commands", New Font(FontFamily.GenericMonospace, 10), New SolidBrush(Color.Black), 5, yoffset + 30)
e.Graphics.DrawString("provided by an associated DesignerMonitorDesigner", New Font(FontFamily.GenericMonospace, 10), New SolidBrush(Color.Black), 5, yoffset + 40)
e.Graphics.DrawString("to start or stop monitoring.", New Font(FontFamily.GenericMonospace, 10), New SolidBrush(Color.Black), 5, yoffset + 50)
Else
e.Graphics.DrawString("Currently monitoring designer events.", New Font(FontFamily.GenericMonospace, 10), New SolidBrush(Color.DarkBlue), 5, yoffset + 10)
e.Graphics.DrawString("Designer created, changed and disposed events:", New Font(FontFamily.GenericMonospace, 9), New SolidBrush(Color.Brown), 5, yoffset + 35)
Dim i As Integer
For i = 0 To updates.Count - 1
e.Graphics.DrawString(CStr(updates(i)), New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Black), 5, yoffset + 55 + 10 * i)
yoffset += 10
Next i
End If
End Sub
End Class
' DesignerMonitorDesigner uses the IDesignerEventService to send event information
' to an associated DesignerMonitor control's updates collection.
Public Class DesignerMonitorDesigner
Inherits System.Windows.Forms.Design.ControlDesigner
Private dm As DesignerMonitor = Nothing
Private dvc As DesignerVerbCollection = Nothing
Private eventcount As Integer = 0
Public Sub New()
' Initializes the designer's shortcut menu with a "Start monitoring" command.
dvc = New DesignerVerbCollection(New DesignerVerb() {New DesignerVerb("Start monitoring", AddressOf Me.StartMonitoring)})
End Sub
Public Overrides Sub Initialize(ByVal component As System.ComponentModel.IComponent)
MyBase.Initialize(component)
Dim o As Object
o = component
If o.GetType() IsNot GetType(DesignerMonitor) Then
Throw New Exception("This designer requires a DesignerMonitor control.")
End If
dm = CType(component, DesignerMonitor)
End Sub
Public Overrides ReadOnly Property Verbs() As System.ComponentModel.Design.DesignerVerbCollection
Get
Return dvc
End Get
End Property
Private Sub StopMonitoring(ByVal sender As Object, ByVal e As EventArgs)
Dim des As IDesignerEventService = CType(Me.Control.Site.GetService(GetType(IDesignerEventService)), IDesignerEventService)
If des Is Nothing Then
Return
End If
' Remove event handlers for event notification methods.
RemoveHandler des.DesignerCreated, AddressOf Me.DesignerCreated
RemoveHandler des.DesignerDisposed, AddressOf Me.DesignerDisposed
RemoveHandler des.ActiveDesignerChanged, AddressOf Me.DesignerChanged
RemoveHandler des.SelectionChanged, AddressOf Me.SelectionChanged
dm.monitoring_events = False
' Rebuild menu with "Start monitoring" command.
dvc = New DesignerVerbCollection(New DesignerVerb() {New DesignerVerb("Start monitoring", AddressOf Me.StartMonitoring)})
dm.Refresh()
End Sub
Private Sub StartMonitoring(ByVal sender As Object, ByVal e As EventArgs)
Dim des As IDesignerEventService = CType(Me.Control.Site.GetService(GetType(IDesignerEventService)), IDesignerEventService)
If des Is Nothing Then
Return
End If
' Add event handlers for event notification methods.
AddHandler des.DesignerCreated, AddressOf Me.DesignerCreated
AddHandler des.DesignerDisposed, AddressOf Me.DesignerDisposed
AddHandler des.ActiveDesignerChanged, AddressOf Me.DesignerChanged
AddHandler des.SelectionChanged, AddressOf Me.SelectionChanged
dm.monitoring_events = True
' Rebuild menu with "Stop monitoring" command.
dvc = New DesignerVerbCollection(New DesignerVerb() {New DesignerVerb("Stop monitoring", AddressOf Me.StopMonitoring)})
dm.Refresh()
End Sub
Private Sub DesignerCreated(ByVal sender As Object, ByVal e As DesignerEventArgs)
UpdateStatus(("Designer for " + e.Designer.RootComponent.Site.Name + " was created."))
End Sub
Private Sub DesignerDisposed(ByVal sender As Object, ByVal e As DesignerEventArgs)
UpdateStatus(("Designer for " + e.Designer.RootComponent.Site.Name + " was disposed."))
End Sub
Private Sub DesignerChanged(ByVal sender As Object, ByVal e As ActiveDesignerEventArgs)
UpdateStatus(("Active designer moved from " + e.OldDesigner.RootComponent.Site.Name + " to " + e.NewDesigner.RootComponent.Site.Name + "."))
End Sub
Private Sub SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
UpdateStatus("A component selection was changed.")
End Sub
' Update message buffer on DesignerMonitor control.
Private Sub UpdateStatus(ByVal newmsg As String)
If dm.updates.Count > 10 Then
dm.updates.RemoveAt(10)
End If
dm.updates.Insert(0, "Event #" + eventcount.ToString() + ": " + newmsg)
eventcount += 1
dm.Refresh()
End Sub
End Class
End Namespace
備註
IDesignerEventService 會在建立、變更或處置新的根設計工具時提供通知。 根設計工具提供設計時間支援設計工具檢視中檔的基底元件。 事件 SelectionChanged 會在目前元件選取範圍變更時提供通知。
屬性
ActiveDesigner |
取得目前主動式文件的根設計工具。 |
Designers |
取得根設計工具的集合,用來設計目前在開發環境中的主動式文件。 |
事件
ActiveDesignerChanged |
發生於目前的根設計工具變更時。 |
DesignerCreated |
發生於根設計工具建立時。 |
DesignerDisposed |
發生於文件的根設計工具被捨棄時。 |
SelectionChanged |
發生於目前的設計檢視選取變更時。 |