To create a custom component simply derive from any of the built-in component classes, ultimately from the ComponentBase class. Usually you will want to override the Draw method in order to render the component and eventually the GetDesiredSize method. A simple custom component is illustrated below:
C# Copy Code |
---|
public class RectangleComponent : ComponentBase |
The above component only renders a red rectangle.
To provide custom component behavior based on the user input, override the OnMouseDown, OnMouseUp, OnMouseMove, OnKeyDown, OnKeyUp and OnKeyPress methods.
To create a custom container component, you need to derive from the ContainerComponent class and override the ArrangeComponents method and eventually the GetDesiredSize method. From within the ArrangeComponents method you position the individual child components by assigning values to their Bounds property according to their desired size and the arrangement logic of the panel. Below you can find the implementation of a very simple custom panel class, which arranges its children with a constant offset relative to each other.
C# Copy Code |
---|
public class OffsetPanel : ContainerComponent |
If you want to use custom components in XML you may need to supply a custom ITypeResolver object to the loader. This is especially true if you want to use shortcut names for your components. Consider the above RectangleComponent in the following example:
XML Copy Code |
---|
<Rectangle /> |
Loading this XML will fail unless you supply an ITypeResolver, which can resolve "Rectangle" to RectangleComponent.