Ali
Ali

Reputation: 1152

How to dynamically resize a label inside a WPF application?

I need to know how I could dynamically resize a label in a WPF application.


I've already found a sample in this article which has already achieved both dragging and resizing a label at the same time. I dug into the code, and to make it short, I found out that inside OnMouseMove event of the label, it checks the mouse cursor shape and if it was Hand it will do the dragging and if it was either of the resizing arrows it will do the resizing correspondingly. Check it out. you'll see. In this certain example I couldn't manage to find out how the cursor shape changes to resizing arrows when the mouse is hovered on the label's border.

So

I either need to find out 'how I could change the mouse cursor shape to resizing arrows when hovered on a label's border', OR to find a new approach to resize a label, dynamically.

Upvotes: 0

Views: 559

Answers (1)

Blachshma
Blachshma

Reputation: 17405

Changing the cursor is done via the this.Cursor property.

I opened the code in the article and saw how they do it...

In the OnMouseMove the cursor is changed if the left mouse button is NOT clicked:

Point currentLocation = e.MouseDevice.GetPosition(wnd);
       ......
       ......
const int dragHandleWidth = 3;

var bottomHandle = new Rect(0, height - dragHandleWidth, width, dragHandleWidth);
var rightHandle = new Rect(width - dragHandleWidth, 0, dragHandleWidth, height);

Point relativeLocation = wnd.TranslatePoint(currentLocation, this);

if (rightHandle.Contains(relativeLocation))
{
    this.Cursor = Cursors.SizeWE;
}
else if (bottomHandle.Contains(relativeLocation))
{
    this.Cursor = Cursors.SizeNS;
}
else
{
    this.Cursor = Cursors.Hand;
}

In other words, they check if the current mouse location is within 3 px of the bottom or right border, if it is, they change the cursor accordingly...

You can easily change this logic to suite your needs....

Upvotes: 1

Related Questions