65.9K
CodeProject is changing. Read more.
Home

Cool Angle Select Control (Photoshop style)

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.69/5 (8 votes)

Jul 29, 2006

CPOL
viewsIcon

40586

downloadIcon

380

A cool control to select angles.

Angle Select Control

Introduction

This is the first time I have decided to release a code of mine on CodeProject. It's quite straightforward, I don't have much to say about it, and I think the picture above describes it well. well.

Basically, I needed a control to select an angle, and I made the one above. I saw the one used on Photoshop and decided to create one of my own.

The implementation is really simple, as is shown in the code below:

/// <summary>
/// Event - User is moving the mouse.
/// </summary>

private void AngleSelect_MouseMove(object sender, MouseEventArgs e)
{
    if (Clicked == true)
    {
        //if mouse down, change the Angle.
        Point centerPoint = new Point(ClientRectangle.Width / 2, 
                                      ClientRectangle.Height / 2);
        Point mousePos = ((MouseEventArgs)e).Location;
        //Using the Atan2 function in order to get the Angle 
        //of the Slope between the center Point
        // of the control and the Mouse Point.
        double radians = Math.Atan2(mousePos.Y - centerPoint.Y, 
                                    mousePos.X - centerPoint.X);
        //Then converting from Radians to regular Units.
        angle = (int)(radians * (180 / Math.PI));
        Refresh();
        //call delegated function
        try
        {
            angleChanged(angle);
        }
        catch { }
    }
}

Here are the main properties and the events to use:

  • FillColor - Sets the circle's fill color.
  • LineColor - Sets the circle's line color.
  • Angle - Sets or gets the angle of the control.
  • AngleChanged - Called whenever the user is changing the angle.

Hope, you'll find this useful.