using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace K.Controls
{
public class AutoControl
{
private static double baseWidth = 1920;
private static double baseHeight = 1080;
private static double currWidth = SystemParameters.PrimaryScreenWidth;
private static double currHeight = SystemParameters.PrimaryScreenHeight;
private static Stopwatch sw = new Stopwatch();
private static List<string> list = new List<string>();
public bool Auto { get; set; } = true;
static AutoControl()
{
list.Add(typeof(Grid).FullName);
list.Add(typeof(Border).FullName);
list.Add(typeof(StackPanel).FullName);
list.Add(typeof(Button).FullName);
list.Add(typeof(Image).FullName);
list.Add(typeof(TextBlock).FullName);
list.Add(typeof(TextBox).FullName);
list.Add(typeof(VirtualKeyboard).FullName);
}
public static void Apply(DependencyObject obj)
{
sw.Restart();
AutoSize(obj);
sw.Stop();
//Debug.Print($"用时:{sw.ElapsedMilliseconds}");
}
public static double Calc()
{
double bsWidth = 1920;
double bsHeight = 1080;
var width = SystemParameters.PrimaryScreenWidth;
var height = SystemParameters.PrimaryScreenHeight;
var sacle = width / height;
if (sacle > 1.77 && sacle < 1.79)
{
bsWidth = 1920;
bsHeight = 1080;
}
else if (sacle > 1.33 && sacle < 1.35)
{
bsWidth = 1440;
bsHeight = 1080;
}
var saclenumber = bsWidth / width;
return saclenumber;
}
public static void AutoSize(DependencyObject obj)
{
if (obj is FrameworkElement fe)
{
var width = SystemParameters.PrimaryScreenWidth;
var height = SystemParameters.PrimaryScreenHeight;
var sacle = width / height;
if (sacle > 1.77 && sacle < 1.79)
{
fe.Width = 1920;
fe.Height = 1080;
}
else if (sacle > 1.33 && sacle < 1.35)
{
fe.Width = 1440;
fe.Height = 1080;
}
else if (sacle == 1.6)
{
fe.Width = 1728;
fe.Height = 1080;
}
else if (sacle == 1.5)
{
fe.Width = 1620;
fe.Height = 1080;
}
else
{
fe.Width = 1080 * sacle;
fe.Height = 1080;
}
}
//int childrenCount = VisualTreeHelper.GetChildrenCount(obj);
//for (int i = 0; i < childrenCount; i++)
//{
// DependencyObject child = VisualTreeHelper.GetChild(obj, i);
// if (child is FrameworkElement fe)
// {
// if (CheckType(fe))
// {
// autoScale(fe);
// //autoWidth(fe);
// //autoHeight(fe);
// //autoMargin(fe);
// //autoFontSize(fe);
// }
// }
// int count = VisualTreeHelper.GetChildrenCount(child);
// if (count > 0)
// {
// AutoSize(child);
// }
//}
}
public static bool Is1920x1080()
{
var width = SystemParameters.PrimaryScreenWidth;
var height = SystemParameters.PrimaryScreenHeight;
var sacle = width / height;
if (sacle > 1.77 && sacle < 1.79)
{
return true;
}
else
{
return false;
}
}
private static void autoScale(FrameworkElement fe)
{
//Debug.Print($"控件名:{fe.Name}***控件类型:{fe.GetType().Name}");
fe.RenderTransformOrigin = new Point(0.5, 0.5);
ScaleTransform st = new ScaleTransform();
st.ScaleX = currWidth / baseWidth;
st.ScaleY = currWidth / baseWidth;
fe.RenderTransform = st;
//autoWidth(fe);
//Debug.Print($"控件名:{fe.Name}***控件类型:{fe.GetType().Name}");
}
private static void autoWidth(FrameworkElement fe)
{
if (fe.Width.Equals(double.NaN))
{
return;
}
//Debug.Print($"控件名:{fe.Name}***控件类型:{fe.GetType().Name}***控件原始宽度:{fe.Width}");
var width = fe.Width * (currWidth / baseWidth);
fe.Width = width;
//Debug.Print($"控件名:{fe.Name}***控件类型:{fe.GetType().Name}***控件新宽度:{fe.Width}");
}
private static void autoHeight(FrameworkElement fe)
{
if (fe.Height.Equals(double.NaN))
{
return;
}
//Debug.Print($"控件名:{fe.Name}***控件类型:{fe.GetType().Name}***控件原始高度:{fe.Height}");
var height = fe.Height * (currHeight / baseHeight);
fe.Height = height;
//Debug.Print($"控件名:{fe.Name}***控件类型:{fe.GetType().Name}***控件新高度:{fe.Height}");
}
private static void autoMargin(FrameworkElement fe)
{
var thickness = fe.Margin;
if (thickness.Left > 5)
{
thickness.Left = thickness.Left * (currWidth / baseWidth);
}
if (thickness.Right > 5)
{
thickness.Right = thickness.Right * (currWidth / baseWidth);
}
if (thickness.Top > 5)
{
thickness.Top = thickness.Top * (currHeight / baseHeight);
}
if (thickness.Bottom > 5)
{
thickness.Bottom = thickness.Bottom * (currHeight / baseHeight);
}
fe.Margin = thickness;
}
private static void autoFontSize(FrameworkElement fe)
{
if (fe is TextBlock control)
{
//Debug.Print($"控件名:{control.Name}***控件类型:{control.GetType().Name}***控件原始大小:{control.FontSize}");
var size = control.FontSize * (currHeight / baseHeight);
control.FontSize = size;
//Debug.Print($"控件名:{control.Name}***控件类型:{control.GetType().Name}***控件新大小:{control.FontSize}");
}
}
private static bool CheckType(FrameworkElement fe)
{
if (list.Contains(fe.GetType().FullName) && fe.Tag != null)
{
return true;
}
else
{
return false;
}
}
public static List<T> FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
try
{
List<T> list = new List<T>();
int childrenCount = VisualTreeHelper.GetChildrenCount(obj);
for (int i = 0; i < childrenCount; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child is T)
{
list.Add((T)child);
List<T> childOfChildren = FindVisualChild<T>(child);
if (childOfChildren != null)
{
list.AddRange(childOfChildren);
}
}
else
{
List<T> childOfChildren = FindVisualChild<T>(child);
if (childOfChildren != null)
{
list.AddRange(childOfChildren);
}
}
}
return list;
}
catch (Exception)
{
//MessageBox.Show(ee.Message);
return null;
}
}
}
}
调用以上封装类里面的方法:
1、调用Apply()
在页面的Load事件里调用(.xaml.cs中的Load事件):
K.Controls.AutoControl.Apply(content);
.xaml中内容:
<Grid Background="#00232A34">
<Viewbox>
<ContentControl x:Name="content" Width="1440" Height="1080" region:RegionManager.RegionName="{x:Static calc:RegionType.ContentRegion}"/>
</Viewbox>
<Viewbox>
<Canvas Width="{Binding ElementName=content,Path=ActualWidth}" Height="{Binding ElementName=content,Path=ActualHeight}" x:Name="canvasBorder" IsHitTestVisible="False" Visibility="Hidden">
<Image Source="{DynamicResource PracticeBack}" Stretch="Fill" Width="{Binding ElementName=content,Path=ActualWidth}" Height="{Binding ElementName=content,Path=ActualHeight}"/>
<Image Width="84" Height="56" Source="{DynamicResource PracticeDesc}" Stretch="Fill" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Canvas>
</Viewbox>
</Grid>
或者:
.xaml.cs中Load事件内容:
K.Controls.AutoControl.Apply(grid);
.xaml中内容:
<Viewbox>
<Grid x:Name="grid" Width="1440" Height="1080">
</Grid>
</ViewBox>
2、Calc()方法
var sacle = K.Controls.AutoControl.Calc();
item.BorderHeight = 1 * sacle;
3、Is1920x1080()方法---分辨率是否1920*1080
var isLarge = K.Controls.AutoControl.Is1920x1080();
if (isLarge)
{
columnpay.Width = new GridLength(670); 重新设置宽度
}
else
{
columnpay.Width = new GridLength(500);
}
.xaml中内容:
<Grid Grid.Row="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1.46*"/>
<ColumnDefinition Width="2"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition x:Name="columnpay" Width="706"/>
</Grid.ColumnDefinitions>
</Grid>