示例:计算屏幕分辨率比例

作者:追风剑情 发布于:2019-12-12 14:37 分类:C#

一、创建WPF项目

MainWindow.xaml

<Window x:Class="Resolution.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Resolution"
        mc:Ignorable="d"
        Title="" Height="150" Width="320" WindowStyle="ToolWindow">
    <Grid>
        <Button x:Name="calBtn" Content="计算分辨率比例" HorizontalAlignment="Left" Margin="77,61,0,0" VerticalAlignment="Top" Width="135" FontSize="16" Click="calBtn_Click"/>
        <Label x:Name="rateText" Content="16 : 9" HorizontalAlignment="Left" Margin="217,56,0,0" VerticalAlignment="Top" FontSize="20" Background="#00000000" Foreground="Red"/>
        <Label Content="分辨率:" HorizontalAlignment="Left" Margin="10,16,0,0" VerticalAlignment="Top" FontSize="16"/>
        <TextBox x:Name="widthText" InputMethod.IsInputMethodEnabled="False" HorizontalAlignment="Left" Height="23" Margin="78,20,0,0" TextWrapping="Wrap" Text="1920" VerticalAlignment="Top" Width="90" FontSize="18"/>
        <TextBox x:Name="heightText" InputMethod.IsInputMethodEnabled="False" HorizontalAlignment="Left" Height="23" Margin="195,20,0,0" TextWrapping="Wrap" Text="1080" VerticalAlignment="Top" Width="90" FontSize="18" PreviewTextInput="tb_PreviewTextInput"/>
        <Label Content="×" HorizontalAlignment="Left" Margin="169,14,0,0" VerticalAlignment="Top" FontSize="20"/>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Input;

namespace Resolution
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            rateText.Content = string.Empty;
        }

        private void calBtn_Click(object sender, RoutedEventArgs e)
        {
            rateText.Content = string.Empty;

            if (string.IsNullOrEmpty(widthText.Text) || string.IsNullOrEmpty(heightText.Text))
                return;

            string widthStr = widthText.Text.Trim();
            string heightStr = heightText.Text.Trim();

            if (string.IsNullOrEmpty(widthStr) || string.IsNullOrEmpty(heightStr))
                return;

            int width = int.Parse(widthStr);
            int height = int.Parse(heightStr);

            int gcd = EuclidGcd(width, height);
            int w = width / gcd;
            int h = height / gcd;

            rateText.Content = string.Format("{0} : {1}", w, h);
        }

        /// <summary>
        /// 辗转相除法求a和b的最大公约数
        /// </summary>
        private int EuclidGcd(int a, int b)
        {
            if (b > a)
            {
                int tmp = a;
                a = b;
                b = tmp;
            }

            while (b != 0)
            {
                int tmp_b = b;
                b = a % b;
                a = tmp_b;
            }

            return a;
        }

        private void tb_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex re = new Regex("[^0-9.-]+");
            e.Handled = re.IsMatch(e.Text);
        }
    }
}

运行测试

11111.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号