Location>code7788 >text

C# Custom Controls - Rotate Button

Popularity:755 ℃/2024-09-07 23:59:15

C# User Controls - Rotate Button

Button Function:Hand auto-rotation, labeled text display, click on secondary popup box confirmation (source code at the end);


[Production Method]
  • Use the method to find the center coordinates of the control, draw the background outer ring and inner circle; then draw the rectangular switch and rotate it by angle to get it;

[Key Node]
  • No.1 Get the center coordinates and think about the relative coordinates, width and height of the graph to be drawn;
  • No.2 Change the origin of the coordinate system, draw a rectangular switch with this origin as the coordinate, and then rotate it by the specified angle.
//method to get the origin
Point centerPoint = GetCenterPoint();

#region Get center origin
private Point GetCenterPoint()
{
    if ( > )
    {
        return new Point( / 2, / 2);
    }
    else
    {
        return new Point( / 2, / 2);
    }
}
#endregion
//change the origin of the coordinate system
(, );

//Rotate the specified angle
if (switchStatus)
{
    (36.0f); //Rotate the specified angle if (switchStatus) {
}
else
} else
    (-36.0f); } else {
}

【1】The background of the button (outer ring <>, inner circle <>) is drawn in the same way as for the indicator;

Note: This coordinate system is based on the upper left corner of the control.

// Draw the outer ring -(Pen)-DrawEllipse
p = new Pen(, );
RectangleF rec = new RectangleF(, , ( - ) * 2, ( - ) * 2);
(p, rec);

// Fill the inner circle -(SolidBrush)-FillEllipse
sb = new SolidBrush(); rec = new RectangleF()

(sb, rec);

【2】Draws the center rectangle and dots, and the brush fills the specified area (,).

Note: This coordinate system is based on the center point.

//Changing the coordinate system origin
(, );

//Fill Rectangle Switch
rec = new RectangleF(- * 0.5f, - , togWidth, ( - togGap) * 2);
(new SolidBrush(), rec);

//Fill Rectangle Switch圆点
rec = new RectangleF(- * 0.5f + togForeGap, - + togForeGap, togWidth - 2 * togForeGap, togForeHeight);
(new SolidBrush(), rec);

【3】Draw text, draw the specified string in the specified rectangle ()

//specified string
rec = new RectangleF( * 0.05f, 1, , 20);
(, , new SolidBrush(), rec, sf);
rec = new RectangleF( * 0.63f, 1, , 20);
(, , new SolidBrush(), rec, sf);

【4】Create mouse click event, add mouse click event handling <change property value>, trigger event in property (Event)

#region Add event
[Browsable(true)]
[Category("Operation_G")]
[Description("Double click to enter the event")]
public event EventHandler MouseDown_G; //event declaration
//Initialize the function to add mouse click event handling.
 += Switch_MouseDown; ;)
// Mouse click event logic
private void Switch_MouseDown(object sender, MouseEventArgs e)
{
    DialogResult dr = ("Second confirmation of operation?" , "Prompting you", , ); if (dr == )
    if (dr == )
    {
        SwitchStatus = !SwitchStatus; // property value here, not field
    }
    else return; }
}
#endregion
//Switch State Attributes
 private bool switchStatus = false;
 [Browsable(true)]
 [Category("opening (chess jargon)_G")]
 [Description("switching state")]
 public bool SwitchStatus
 {
     get { return switchStatus; }
     set
     {
         switchStatus = value; ();

         //activate a trigger event
         this.MouseDown_G?.Invoke(this, null);
     }
 }

Memo: Specify the default event (clicking the mouse when applying will go to the custom event, otherwise it will go to the 'load' event)

[DefaultEvent("MouseDown_G")]

final generation


Next: a header panel for easy UI layout


[1] New user component

[2] Change component inheritance to Panel

[3] Defining Attributes(Header color, font, height; header background color; border color)

private Font titleFont = new Font("Microsoft Black and White (computer science)", 12);
[Browsable(true)]
[Category("opening (chess jargon)_G")]
[Description("headline font")]
public Font TitleFont
{
    get { return titleFont; }
    set
    {
        titleFont = value;
        ();
    }
}

[4] Repainted Canvas

//frame
(new Pen(), new Rectangle(0, 0, - 1, - 1));

//Fill Heads Up Rectangle
RectangleF rec = new RectangleF(0.5f, 0.5f, - 2, );
(new SolidBrush(), rec);

//Text Drawing
(, , new SolidBrush(), rec, sf);

[5] Remarks

  • Initialize font formatting - two more methods are needed to define the text alignment format.
// font alignment format
 = new StringFormat();
 = ;
 = ;

//Specify the size of the control
 = new (300, 150); //Specify the size of the control.

Finally generate and apply


Link to source code

(Do not want to toss the direct use of Dll, if there is a better remember to leave a message to share Oh! (If the code is not enough, please instruct the god)
/s/1QM_iZ-UMksPqwWo2ssS5Ow?pwd=ju01


End