LoginSignup
0
2

More than 3 years have passed since last update.

ノートパソコンのユーザー情報取得やアプリ起動・終了、ディスプレイ画面の切替などを、ボタンを作って動かす【C#】

Last updated at Posted at 2021-03-12

動機

これまでボタン一つでWindows ノートパソコンのディスプレイ輝度や音量を変更したり、バッテリー状況や通信料などを把握することが出来ました。

C#(WpfAppですが)を使えばWindowsの操作をいろいろできることが分かったので、自分が実現したかったことでまだ記事にしていないことについて、一つのUI上でいろいろなボタンを作ってみました。

今回は以下の機能をボタンから操作することができます。

  • 情報取得系
    • コンピューター名の表示
    • Windowsにログインしているユーザー名の表示
    • Windows OS の情報の表示
    • Windows パソコンの端末情報の表示
  • アプリ操作系
    • ペイント起動操作
    • メモ帳起動操作
    • メモ帳終了操作
    • ボタンフォント変更操作
  • 外部ディスプレイ設定系
    • PC画面のみ表示
    • 複製表示
    • 拡張表示
    • モニターのみ表示

参考 1:ノートパソコンの画面の明るさをボタン一つで変更する【C#】
参考 2:ノートパソコンの画面の音量をボタン一つで変更する【C#】
参考 3:ノートパソコンの画面のバッテリー状況をボタン一つで取得する【C#】
参考 4:ノートパソコンの画面の通信料状況をボタン一つで取得する【C#】
参考 5:ノートパソコンで現在使用中のアプリをボタン一つで把握する【C#】
参考 6: ノートパソコンで現在使用中のオーディオ機器情報をボタン一つで取得する【C#】

イメージ画像

UIとしては各機能名を表示したボタンを貼り付けただけです。

情報取得系ではそれぞれのボタンを選択することで情報を取得することができ、アプリ操作系ではボタンを選択肢てペイントやメモ帳などのアプリを起動・終了することができます。

外部ディスプレイ設定系では、ボタンを選択することで外部ディスプレイへの表示設定を切り替えることができます。

アプリ起動終了.PNG

ソース

MainWindow.xaml

MainWindow.xaml
<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="845.6">
    <Grid>
        <Button x:Name="button" Content="コンピューター名表示" HorizontalAlignment="Left" Margin="50,22,0,0" VerticalAlignment="Top" Width="151" Click="button_Click" Height="94"/>
        <Button x:Name="button1" Content="ログインユーザー名" HorizontalAlignment="Left" Margin="50,139,0,0" VerticalAlignment="Top" Width="151" Height="99" Click="button1_Click"/>
        <Button x:Name="button2" Content="Windows情報" HorizontalAlignment="Left" Margin="50,267,0,0" VerticalAlignment="Top" Width="151" Height="95" Click="button2_Click"/>
        <Button x:Name="button3" Content="端末情報" HorizontalAlignment="Left" Margin="242,22,0,0" VerticalAlignment="Top" Width="151" Click="button3_Click" Height="94"/>
        <Button x:Name="button4" Content="ペイント起動" HorizontalAlignment="Left" Margin="242,139,0,0" VerticalAlignment="Top" Width="151" Click="button4_Click" Height="99"/>
        <Button x:Name="button5" Content="メモ帳起動" HorizontalAlignment="Left" Margin="242,267,0,0" VerticalAlignment="Top" Width="151" Click="button5_Click" Height="95"/>
        <Button x:Name="button6" Content="メモ帳終了" HorizontalAlignment="Left" Margin="423,22,0,0" VerticalAlignment="Top" Width="145" Click="button6_Click" Height="94"/>
        <Button x:Name="button7" Content="FontSize" HorizontalAlignment="Left" Margin="423,139,0,0" VerticalAlignment="Top" Width="145" FontSize="10.0" Click="button7_Click" Height="99"/>
        <Button x:Name="button8" Content="ディスプレイ(PC画面のみ)" HorizontalAlignment="Left" Margin="598,22,0,0" VerticalAlignment="Top" Width="151" Height="60" Click="button8_Click"/>
        <Button x:Name="button9" Content="ディスプレイ(複製)" HorizontalAlignment="Left" Margin="598,118,0,0" VerticalAlignment="Top" Width="151" Height="66" Click="button9_Click"/>
        <Button x:Name="button10" Content="ディスプレイ(拡張)" HorizontalAlignment="Left" Margin="598,204,0,0" VerticalAlignment="Top" Width="151" Height="58" Click="button10_Click"/>
        <Button x:Name="button11" Content="ディスプレイ(モニターのみ)" HorizontalAlignment="Left" Margin="598,287,0,0" VerticalAlignment="Top" Width="151" Height="56" Click="button11_Click"/>

    </Grid>
</Window>

MainWindow.xaml

MainWindow.xaml.cs
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(GetLocalMachineName());
        }

        public string GetLocalMachineName()
        {
            return Environment.MachineName;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(GetLocalUserName());
        }

        public string GetLocalUserName()
        {
            return Environment.UserName;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(GetOSName());
        }

        public string GetOSName()
        {
            return System.Environment.OSVersion.ToString();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            String message = "";
            foreach (var prop in typeof(EnvInfo).GetProperties())
            {
                //Console.WriteLine(prop.Name + ": " + prop.GetValue(null));
                message += prop.Name + ": " + prop.GetValue(null) + "\n";
            }
            MessageBox.Show(message);
        }

        public class EnvInfo
        {
            private static string getRegistryValue(string keyname, string valuename)
                => Registry.GetValue(keyname, valuename, "").ToString();

            public static string os_version { get; }
                = Environment.OSVersion.VersionString;

            public static string os_product_name { get; }
                = getRegistryValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName");

            public static string os_release { get; }
                = getRegistryValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ReleaseId");

            public static string os_build { get; }
                = getRegistryValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentBuild");

            public static string os_bit { get; }
                = Environment.Is64BitOperatingSystem ? "64 bit" : "32 bit";

            public static string process_bit { get; }
                = Environment.Is64BitProcess ? "64 bit" : "32 bit";

            public static string framework_version { get; }
                = Environment.Version.ToString();

            public static string registry_framework_version { get; }
                = getRegistryValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full", "Version");

            public static string registry_framework_release { get; }
                = getRegistryValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full", "Release");

            public static string host_name { get; }
                = Dns.GetHostName();

            public static string machine_name { get; }
                = Environment.MachineName;
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(@"c:\Windows\System32\mspaint.exe");
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(@"c:\Windows\System32\notepad.exe");
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            //notepadのプロセスを取得
            System.Diagnostics.Process[] ps =
                System.Diagnostics.Process.GetProcessesByName("notepad");

            foreach (System.Diagnostics.Process p in ps)
            {
                //プロセスを強制的に終了させる
                p.Kill();
            }
        }

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            double fsize = button7.FontSize;
            if (fsize == 16.0)
            {
                button7.FontSize = 10.0;
                button7.Content = "FontSize";
            }
            else
            {
                button7.FontSize = 16.0;
                button7.Content = "Control font size changes from 10 to 16.";
            }
        }

        private void button8_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(@"c:\Windows\System32\DisplaySwitch.exe", "/internal");
        }

        private void button9_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(@"c:\Windows\System32\DisplaySwitch.exe", "/clone");
        }

        private void button10_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(@"c:\Windows\System32\DisplaySwitch.exe", "/extend");
        }

        private void button11_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(@"c:\Windows\System32\DisplaySwitch.exe", "/external");
        }
    }
}

おわりに

今回は、ボタン一つで自分が個人的に行いたい Windowsの設定回り、情報表示回り、操作回りなどをプロトで作成してみました。

まだまだWindows端末上で、ボタン一つや操作簡略化を目指したいと思っているので、これからもC# を駆使して便利なアプリなどを作っていけたらと思っています。

将来的には、これらを一つにまとめて、Windows操作簡単アプリなど作成できたらいいな、とも考えています。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2