I don't want to learn WPF for a long time. Every time I open my computer, I will stay on the webpage. Don't talk nonsense .......
First:
This is the basic step in the Panel layout. If you want to customize the Panel, you generally choose to inherit the Panel directly, and then you only need to reload Messure and Arrange.
Here is an example of mine:
:
It is a custom panel that distributes controls around the center. The buttons in the chart rotate around the center and can be used in some situations that require simple verification conditions.
The following is the main code:
Using System;
Using System. Windows;
Using System. Windows. Media;
Using System. Windows. Input;
Using System. Windows. Controls;
Using System. Windows. Media. Imaging;
Using System. Windows. Controls. Primitives;
Using System. Windows. Documents;
Using System. Windows. Threading;
Namespace xiaohai. CirclePanel
{
Class CirclePanel: Panel // Panel features: timed rotation. Two methods are published: TimerControl1: Start, TimerControl2: Stop, used to control the timer from the outside.
{
Double angleEach;
Size sizeTotal, sizeLargest;
Double radius, x1;
Point ptCenter;
Double angle = 5;
Public static DependencyProperty OrientationProperty;
DispatcherTimer tim;
Static CirclePanel ()
{
OrientationProperty = DependencyProperty. Register ("Orientation", typeof (Oritension), typeof (CirclePanel), new FrameworkPropertyMetadata (Oritension. ByWidth, FrameworkPropertyMetadataOptions. AffectsRender ));
}
Public Oritension Orientation
{
Set {SetValue (OrientationProperty, value );}
Get {return (Oritension) GetValue (OrientationProperty );}
}
Protected override Size MeasureOverride (Size availableSize)
{
SizeTotal = new Size (0, 0 );
AngleEach = 360.0/InternalChildren. Count;
SizeLargest = new Size (0, 0 );
Foreach (UIElement ui in InternalChildren)
{
Ui. Measure (new Size (double. PositiveInfinity, double. PositiveInfinity ));
SizeLargest. Width = Math. Max (sizeLargest. Width, ui. DesiredSize. Width );
SizeLargest. Height = Math. Max (sizeLargest. Height, ui. DesiredSize. Height );
}
X1 = sizeLargest. Width/(2 * Math. Tan (angleEach * Math. PI/360 ));
Radius = Math. Sqrt (Math. Pow (x1 + sizeLargest. Height), 2) + Math. Pow (sizeLargest. Width/2 ));
SizeTotal. Width + = 2 * radius;
SizeTotal. Height + = 2 * radius;
Return sizeTotal;
}
Protected override Size ArrangeOverride (Size finalSize)
{
Double multi = Math. Min (finalSize. Width/(2 * radius), finalSize. Height/(2 * radius ));
Double angle1 = 0;
PtCenter = new Point (finalSize. Width/2, finalSize. Height/2 );
Foreach (UIElement ui in InternalChildren)
{
Ui. RenderTransform = Transform. Identity;
Ui. arrange (new Rect (ptCenter. x-sizeLargest. width * multi/2, ptCenter. y-(x1 + sizeLargest. height) * multi, sizeLargest. width * multi, sizeLargest. height * multi ));
Point pt = TranslatePoint (ptCenter, ui );
Ui. RenderTransform = new RotateTransform (angle1, pt. X, pt. Y );
Angle1 + = angleEach;
}
Return finalSize;
}
Void transform (object sender, EventArgs e)
{
Foreach (UIElement ui in InternalChildren)
{
Point pt = TranslatePoint (ptCenter, ui );
Ui. RenderTransform = new RotateTransform (angle, pt. X, pt. Y );
Angle + = angleEach;
}
Angle + = 5 ;//Why is there a problem when it is set to less than 5. Solving...
If (angle = 360) angle = 0;
}
Public CirclePanel ()
{
Tim = new DispatcherTimer ();
Tim. Interval = TimeSpan. FromMilliseconds (50 );
Tim. Tick + = transform;
Tim. Start ();
}
Public void TimerControl1 ()
{
Tim. Start ();
}
Public void TimerControl2 ()
{
Tim. Stop ();
}
}
Public enum Oritension
{
ByWidth,
ByHeight
}
}
It can also be arranged by the control height. The above Code ignores this option. Other extensions are what you think.