Change the appearance of shape ports in Nevron Diagram

Applies to: Nevron Diagram for .NET

How to change the appearance of shape ports in Nevron Diagram?

If you want to change the appearance of the shape ports, you should extend the NPort class (or one of its derived classes) and override the Paint method implementing your drawing logic in it.



The following simple example will help you to create a ports that you draw by yourself and then, you can add your custom port to any shape:

[C#]
using System;
using System.Drawing;
using System.Windows.Forms;
using Nevron.Diagram;
using Nevron.GraphicsCore;
...
public class CustomDynamicPort : NDynamicPort
{
    public override void Paint(NPaintContext context)
    {
        // Compute the bounds of the port in device coordinates
        NPointF location = context.SceneToDevice.TransformPoint(Location);
        float width = NDR.BitmapPortSize.Width;
        float height = NDR.BitmapPortSize.Height;
        NRectangleF bounds = new NRectangleF(location.X - width / 2, location.Y - height / 2, width, height);
 
        // Clip test
        if (context.DevicePaintArea.IntersectsWith(bounds) == false)
            return;
 
        // Paint the port
        NRectanglePath rectangle = new NRectanglePath(bounds.Location, bounds.RightBottom);
        NStyle.SetFillStyle(rectangle, new NColorFillStyle(Color.FromArgb(125, Color.Green)));
        NStyle.SetStrokeStyle(rectangle, new NStrokeStyle(KnownArgbColorValue.Red));
        rectangle.Paint(context);
    }
}
 
public class CustomRotatedBoundsPort : NRotatedBoundsPort
{
    public override void Paint(NPaintContext context)
    {
        // Compute the bounds of the port in device coordinates
        NPointF location = context.SceneToDevice.TransformPoint(Location);
        float width = NDR.BitmapPortSize.Width;
        float height = NDR.BitmapPortSize.Height;
        NRectangleF bounds = new NRectangleF(location.X - width / 2, location.Y - height / 2, width, height);
 
        // Clip test
        if (context.DevicePaintArea.IntersectsWith(bounds) == false)
            return;
 
        // Paint the port
        NEllipsePath circle = new NEllipsePath(bounds.Location, bounds.RightBottom);
        NStyle.SetFillStyle(circle, new NColorFillStyle(Color.FromArgb(125, Color.Yellow)));
        NStyle.SetStrokeStyle(circle, new NStrokeStyle(KnownArgbColorValue.Red));
        circle.Paint(context);
    }
}
 
private void Form1_Load(object sender, EventArgs e)
{
    // Create a shape
    NShape shape = new NRectangleShape(300, 300, 100, 100);
    nDrawingDocument1.ActiveLayer.AddChild(shape);
     
    // Create the ports collection
    shape.CreateShapeElements(ShapeElementsMask.Ports);
     
    // Create an instance of the custom ports
    CustomDynamicPort myCenterPort = new CustomDynamicPort();
    CustomRotatedBoundsPort myTopPort = new CustomRotatedBoundsPort();
    CustomRotatedBoundsPort myBottomPort = new CustomRotatedBoundsPort();
    CustomRotatedBoundsPort myLeftPort = new CustomRotatedBoundsPort();
    CustomRotatedBoundsPort myRightPort = new CustomRotatedBoundsPort();
     
    // Set the port's alignment and glue
    myCenterPort.Alignment = new NContentAlignment(0, 0);
    myCenterPort.GlueMode = DynamicPortGlueMode.GlueToContour;
 
    myTopPort.Alignment = new NContentAlignment(0, -50);
    myBottomPort.Alignment = new NContentAlignment(0, 50);
    myLeftPort.Alignment = new NContentAlignment(-50, 0);
    myRightPort.Alignment = new NContentAlignment(50, 0);
     
    // Add the port to the port's collection of the shape
    shape.Ports.AddChild(myCenterPort);
    shape.Ports.AddChild(myTopPort);
    shape.Ports.AddChild(myBottomPort);
    shape.Ports.AddChild(myLeftPort);
    shape.Ports.AddChild(myRightPort);
}

[VB.NET]
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports Nevron.Diagram
Imports Nevron.GraphicsCore
...
Public Class CustomDynamicPort
    Inherits NDynamicPort
    Public Overrides Sub Paint(ByVal context As NPaintContext)
        ' Compute the bounds of the port in device coordinates
        Dim location__1 As NPointF = context.SceneToDevice.TransformPoint(Location)
        Dim width As Single = NDR.BitmapPortSize.Width
        Dim height As Single = NDR.BitmapPortSize.Height
        Dim bounds As New NRectangleF(location__1.X - width / 2, location__1.Y - height / 2, width, height)
 
        ' Clip test
        If context.DevicePaintArea.IntersectsWith(bounds) = False Then
            Return
        End If
 
        ' Paint the port
        Dim rectangle As New NRectanglePath(bounds.Location, bounds.RightBottom)
        NStyle.SetFillStyle(rectangle, New NColorFillStyle(Color.FromArgb(125, Color.Green)))
        NStyle.SetStrokeStyle(rectangle, New NStrokeStyle(KnownArgbColorValue.Red))
        rectangle.Paint(context)
    End Sub
End Class
 
Public Class CustomRotatedBoundsPort
    Inherits NRotatedBoundsPort
    Public Overrides Sub Paint(ByVal context As NPaintContext)
        ' Compute the bounds of the port in device coordinates
        Dim location__1 As NPointF = context.SceneToDevice.TransformPoint(Location)
        Dim width As Single = NDR.BitmapPortSize.Width
        Dim height As Single = NDR.BitmapPortSize.Height
        Dim bounds As New NRectangleF(location__1.X - width / 2, location__1.Y - height / 2, width, height)
 
        ' Clip test
        If context.DevicePaintArea.IntersectsWith(bounds) = False Then
            Return
        End If
 
        ' Paint the port
        Dim circle As New NEllipsePath(bounds.Location, bounds.RightBottom)
        NStyle.SetFillStyle(circle, New NColorFillStyle(Color.FromArgb(125, Color.Yellow)))
        NStyle.SetStrokeStyle(circle, New NStrokeStyle(KnownArgbColorValue.Red))
        circle.Paint(context)
    End Sub
End Class
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' Create a shape
    Dim shape As NShape = New NRectangleShape(300, 300, 100, 100)
    NDrawingDocument1.ActiveLayer.AddChild(shape)
 
    ' Create the ports collection
    shape.CreateShapeElements(ShapeElementsMask.Ports)
 
    ' Create an instance of the custom ports
    Dim myCenterPort As New CustomDynamicPort()
    Dim myTopPort As New CustomRotatedBoundsPort()
    Dim myBottomPort As New CustomRotatedBoundsPort()
    Dim myLeftPort As New CustomRotatedBoundsPort()
    Dim myRightPort As New CustomRotatedBoundsPort()
 
    ' Set the port's alignment and glue
    myCenterPort.Alignment = New NContentAlignment(0, 0)
    myCenterPort.GlueMode = DynamicPortGlueMode.GlueToContour
 
    myTopPort.Alignment = New NContentAlignment(0, -50)
    myBottomPort.Alignment = New NContentAlignment(0, 50)
    myLeftPort.Alignment = New NContentAlignment(-50, 0)
    myRightPort.Alignment = New NContentAlignment(50, 0)
 
    ' Add the port to the port's collection of the shape
    shape.Ports.AddChild(myCenterPort)
    shape.Ports.AddChild(myTopPort)
    shape.Ports.AddChild(myBottomPort)
    shape.Ports.AddChild(myLeftPort)
    shape.Ports.AddChild(myRightPort)
End Sub

Article ID: 191, Created On: 4/29/2011, Modified: 4/29/2011