Ali
Ali

Reputation: 1152

How to define a custom Rectangle class driven from the Rectangle class?

I have already coded a custom made RichTextBox class in WPF. but I need to have a tiny Rectangle on the top-left corner of this RichTextBox, in order that I can use it as a dragging handle whenever I want to drag the RichTextBox.
So I started like this:

public class DragHandleRegtangle : Shape
    {
        public double len = 5;
        public double wid = 5;

        public DragHandleRegtangle()
        {
           //what should be here exactly, anyway? 
        }
    }
//Here goes my custom RichTextBox
public class CustomRichTextBox : RichTextBox
...

But I have no idea how I can specify the width/length/fill color of it, and the most important it's position related to the RichTextBox (which is exactly zero-zero related to RichTextBox's anchor point - i.e: top left corner of it)

And the first error I've got so far is :

'ResizableRichTextBox.DragHandleRegtangle' does not implement inherited abstract member 'System.Windows.Shapes.Shape.DefiningGeometry.get'

I'd appreciate if someone could help me define my rectangle and resolve this error.

Upvotes: 0

Views: 1888

Answers (2)

Walt Ritscher
Walt Ritscher

Reputation: 7037

The WPF framework has a class that does what you are looking for. The Thumb class represents a control that lets the user drag and resize controls. It is commonly used when making custom controls. MSDN Docs for Thumb class

Here's how to instantiate a thumb and wire up some drag handlers.

private void SetupThumb () {
  // the Thumb ...represents a control that lets the user drag and resize controls."
  var t = new Thumb();
  t.Width = t.Height = 20;
  t.DragStarted += new DragStartedEventHandler(ThumbDragStarted);
  t.DragCompleted += new DragCompletedEventHandler(ThumbDragCompleted);
  t.DragDelta += new DragDeltaEventHandler(t_DragDelta);
  Canvas.SetLeft(t, 0);
  Canvas.SetTop(t, 0);
  mainCanvas.Children.Add(t);
}

private void ThumbDragStarted(object sender, DragStartedEventArgs e)
{
  Thumb t = (Thumb)sender;
  t.Cursor = Cursors.Hand;
}

private void ThumbDragCompleted(object sender,      DragCompletedEventArgs e)
{
  Thumb t = (Thumb)sender;
  t.Cursor = null;
}
void t_DragDelta(object sender, DragDeltaEventArgs e)
{
  var item = sender as Thumb;

  if (item != null)
  {
    double left = Canvas.GetLeft(item);
    double top = Canvas.GetTop(item);

    Canvas.SetLeft(item, left + e.HorizontalChange);
    Canvas.SetTop(item, top + e.VerticalChange);
  }

}

Upvotes: 1

Emmanouil Chountasis
Emmanouil Chountasis

Reputation: 590

write this to your code

   protected override System.Windows.Media.Geometry DefiningGeometry
   {
      //your code
   }

Upvotes: 2

Related Questions