473,385 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to Create a Custom Shape using Inheritance?

Hello,

I am trying to create a WPF application that will have a custom rectangle shape. Since Rectangle is a sealed class I have to inherit from Shape class. Basically my rectangle will consist of four corners (triangles) and 4 edges (trapezoids) that the user can interact with by selecting and dragging the edge or corner to resize the rectangle. Each side/corner will highlight when the mouse is over it (opacity on or off). The body can also be clicked on to move the rectangle. I envision having 8 different RenderedGeometry functions for the 4 corners and 4 sides.

However, my question is mainly about how I begin my definition of a custom Shape object? This is what I can think of for a start based on the Rectangle class:
Expand|Select|Wrap|Line Numbers
  1.     public sealed class dsgnShape : Shape
  2.     {
  3.         public dsgnShape();
  4.  
  5.         protected override Geometry DefiningGeometry { get; }
  6.         public override Transform GeometryTransform { get; }
  7.         public override Geometry RenderedGeometry { get; }
  8.  
  9.         protected override Size ArrangeOverride(Size finalSize);
  10.         protected override Size MeasureOverride(Size constraint);
  11.         protected override void OnRender(DrawingContext drawingContext);
  12.     }
  13.  
However I have had a hard time finding examples on what code goes inside these functions.

For quick reference here is the Shape class:
Expand|Select|Wrap|Line Numbers
  1.         public abstract class Shape : FrameworkElement
  2.         {
  3.             public static readonly DependencyProperty FillProperty;
  4.             public static readonly DependencyProperty StretchProperty;
  5.             public static readonly DependencyProperty StrokeDashArrayProperty;
  6.             public static readonly DependencyProperty StrokeDashCapProperty;
  7.             public static readonly DependencyProperty StrokeDashOffsetProperty;
  8.             public static readonly DependencyProperty StrokeEndLineCapProperty;
  9.             public static readonly DependencyProperty StrokeLineJoinProperty;
  10.             public static readonly DependencyProperty StrokeMiterLimitProperty;
  11.             public static readonly DependencyProperty StrokeProperty;
  12.             public static readonly DependencyProperty StrokeStartLineCapProperty;
  13.             public static readonly DependencyProperty StrokeThicknessProperty;
  14.  
  15.             protected Shape();
  16.  
  17.             protected abstract Geometry DefiningGeometry { get; }
  18.             public Brush Fill { get; set; }
  19.             public virtual Transform GeometryTransform { get; }
  20.             public virtual Geometry RenderedGeometry { get; }
  21.             public Stretch Stretch { get; set; }
  22.             public Brush Stroke { get; set; }
  23.             public DoubleCollection StrokeDashArray { get; set; }
  24.             public PenLineCap StrokeDashCap { get; set; }
  25.             public double StrokeDashOffset { get; set; }
  26.             public PenLineCap StrokeEndLineCap { get; set; }
  27.             public PenLineJoin StrokeLineJoin { get; set; }
  28.             public double StrokeMiterLimit { get; set; }
  29.             public PenLineCap StrokeStartLineCap { get; set; }
  30.  
  31.             [TypeConverter(typeof(LengthConverter))]
  32.             public double StrokeThickness { get; set; }
  33.  
  34.             protected override Size ArrangeOverride(Size finalSize);
  35.             protected override Size MeasureOverride(Size constraint);
  36.             protected override void OnRender(DrawingContext drawingContext);
  37.         }
  38.  
Thanks in advance for the help!
Sep 18 '11 #1

✓ answered by Frinavale

Honestly, it sounds like what you want to do is create a control template....

I just implemented something similar to what you're talking about. I created various classes that inherited from the Thumb Class (a class that lets you manipulate an object). I created a MoveThumb, a ResizeThumb, and a RotateThumb class. Each class manages their assigned task for manipulating the object on the screen.

Once I implemented these classes I was able to define a ControlTemplate that has 4 corner ResizeThumbs, 4 edge Resize thumbs, a MoveThumb, and a rotate thumb.

You don't have to use a ControlTemplate to do this, you could us Adoners, but I wanted to keep things simple and I haven't researched the topic of Adoners enough to know how to use them properly.

The best resource that I could find on the topic was this codeplex article: WPF Diagram Designer.

Please note that the code posted in this article isn't 100% working.

When you rotate a Thumb, the mouse coordinates that you retrieve from in DragDelta event are messed up (they aren't rotated). To fix this, you need retrieve the appropriate mouse coordinates according to the rotation angle of the thumb.

-Frinny

4 10096
arie
64
Here

http://msdn.microsoft.com/en-us/library/ms747393.aspx
http://www.c-sharpcorner.com/UploadFile/mahesh/278/

are some useful materials.

Can't you just use paths and bind to their properties or modify them from code?
Sep 19 '11 #2
Frinavale
9,735 Expert Mod 8TB
Honestly, it sounds like what you want to do is create a control template....

I just implemented something similar to what you're talking about. I created various classes that inherited from the Thumb Class (a class that lets you manipulate an object). I created a MoveThumb, a ResizeThumb, and a RotateThumb class. Each class manages their assigned task for manipulating the object on the screen.

Once I implemented these classes I was able to define a ControlTemplate that has 4 corner ResizeThumbs, 4 edge Resize thumbs, a MoveThumb, and a rotate thumb.

You don't have to use a ControlTemplate to do this, you could us Adoners, but I wanted to keep things simple and I haven't researched the topic of Adoners enough to know how to use them properly.

The best resource that I could find on the topic was this codeplex article: WPF Diagram Designer.

Please note that the code posted in this article isn't 100% working.

When you rotate a Thumb, the mouse coordinates that you retrieve from in DragDelta event are messed up (they aren't rotated). To fix this, you need retrieve the appropriate mouse coordinates according to the rotation angle of the thumb.

-Frinny
Sep 19 '11 #3
@Frinavale
Perfect that is exactly what I was looking for! It is nice to know there is already a Thumb class that I can inherit from and then fill in the blanks, and the example looks great. Really helpful, thanks a lot :)
Sep 20 '11 #4
Frinavale
9,735 Expert Mod 8TB
It's a pretty sweet solution actually :)
If you have to implement rotation and need some help I'll give you a hand.
Sep 20 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Shailaja Kulkarni | last post by:
Hi All, I am new to component development. I want to create custom control to arrange contained controls in form of polygonal shape. The objects are placed in separate panel on the some form....
4
by: Giox | last post by:
Hello everybody, I have a little problm using custom shape windows form. I 'm using the idea illustrated in MSDN (use of TransparenceKey) and all works well but as spoecified on MSDN this works...
0
by: Chris Barnard | last post by:
Hi, I've got a form with a custom shape defined by a region. Does anyone know if it's possible to add a drop-shadow underneath the form similar to the standard windows one - with alpha blending?...
2
by: rushikesh.joshi | last post by:
Hi All, I want to create a webcontrol which will generate a bar (bar chart). I have done some graphics code in my ASPX page and it's working fine, but how do i create a Custom Control or User...
5
by: moondaddy | last post by:
I want to create a custom UI element which will be a custom rectangle object in a c# XAML application. I'm new to XAML and c# as most of my experience has been using vb with sql. I'm building a...
0
by: =?Utf-8?B?UGhpbGlw?= | last post by:
I have a web.config custom configuration section using asp.net 2.0 configuration APIs. My custom configuration section inherits from System.Configuration.ConfigurationSection. I have a IIS root...
8
by: robak | last post by:
Hello and sorry for my bad english :). I need to create simple editor (geometry basic shapes like lines, rectangles, ellipse). I have a little problem. I know how to create simple shape but dont know...
2
by: sennopati | last post by:
Dear all How to create custom event in form object?? I want to use it in inheritance form for add, edit, delete data FYI. I Using visual studio 2005 .net framework 2 thx
4
by: Linda Liu[MSFT] | last post by:
Hi Moondaddy, I downloaded your sample project and run it and did see the problem on my side. There're three problems in the source code of your project. 1. You should move the following...
2
by: =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post by:
Hello, I have an existing (WPF) Class Library project that I'd like to add a WPF Custom Control to. However, when I try to Add New Item, that isn't one of the available options. Perhaps this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.