LoginSignup
1
2

More than 3 years have passed since last update.

ノートパソコンで現在使用中のアプリをボタン一つで把握する【C#】

Last updated at Posted at 2021-03-12

動機

これまで同様、今回も C# で Windows10 用の便利ボタンを作成していきます。

今度はノートパソコンで現在起動中のアプリをボタン一つで把握する機能を作成していきたいと思います。

ちなみに、今回も作成に関しては WpfApp で作成をしました。

参考 1:ノートパソコンの画面の明るさをボタン一つで変更する【C#】
参考 2:ノートパソコンの画面の音量をボタン一つで変更する【C#】
参考 3:ノートパソコンの画面のバッテリー状況をボタン一つで取得する【C#】
参考 4:ノートパソコンの画面の通信料状況をボタン一つで取得する【C#】

イメージ画像

もはやそっけない UI とは呼べません。。。単にボタンを配置しただけです。

アプリ情報.PNG

ただ、こちらを選択すると現在起動中のアプリが表示されるようになります。

アプリ表示結果.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="800">
    <Grid>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="270,149,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>

    </Grid>
</Window>

MainWindow.xaml

MainWindow.xaml.cs
using System.Diagnostics;
using System.Windows;

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

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

        public string showAppTitleList()
        {
            string lists = "";
            foreach (Process p in Process.GetProcesses())
            {
                // プロセス名(string)
                //p.ProcessName
                if (p.MainWindowTitle != "")
                {
                    // タイトル名(string)
                    lists += p.MainWindowTitle + "\n";
                }
            }
            return lists;
        }
    }
}

おわりに

起動中のアプリを、ボタン一つで表示できるようになりました。

これでまた一つ Windows10 便利化計画に一歩近づいた(?)気がします。

1
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
1
2