XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEToolBox

【WPF】XFE工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Threading;

namespace XFEToolBox.QuickTools;

public sealed partial class QuickToolPage
{
    private void BuildSystem()
    {
        var outputs = new ComboBox { Name = "OutputDevices", MinHeight = 38, Margin = new Thickness(0, 6, 0, 12) }; var audioState = Label("选择输出设备", 18);
        void RefreshAudio() { var devices = SystemActions.OutputDevices(); outputs.ItemsSource = devices; outputs.SelectedItem = devices.FirstOrDefault(d => d.Default); audioState.Text = $"{devices.Length} 个可用输出"; status.Text = "已刷新音频输出设备。"; }
        var audio = Section("◖ 声音输出", audioState, outputs, Actions(ActionButton("刷新输出设备", RefreshAudio), Primary("切换到所选输出", () => { if (outputs.SelectedItem is not AudioEndpoint endpoint) throw new InvalidOperationException("请先刷新并选择设备。"); SystemActions.SetOutput(endpoint.Id); RefreshAudio(); status.Text = "默认输出已切换:" + endpoint.Name; })));
        var micState = Label("尚未读取", 26); var micHint = Hint("控制默认通信麦克风,不录制声音。");
        void ReadMic() { micState.Text = SystemActions.MicrophoneMuted() ? "已静音" : "麦克风开启"; status.Text = "已读取默认通信麦克风状态。"; }
        var mic = Section("♩ 麦克风", micState, micHint, Actions(ActionButton("查看麦克风状态", ReadMic), Primary("麦克风静音 / 恢复", () => { SystemActions.SetMicrophoneMute(!SystemActions.MicrophoneMuted()); ReadMic(); })));
        nint target = Context.PreviousWindow; var targetInfo = Label(target == 0 ? "尚未选取窗口" : "已捕获先前窗口", 18);
        var top = Section("◇ 窗口置顶", targetInfo, Hint("选择自己的目标窗口,不关闭它或结束进程。"), Actions(ActionButton("3 秒后选取前台窗口", async () => { status.Text = "请在 3 秒内切到目标窗口…"; await Task.Delay(3000, cancellation.Token); target = SystemActions.ForegroundWindow(); if (target == new WindowInteropHelper(Window.GetWindow(this)).Handle) { target = 0; throw new InvalidOperationException("仍是本工具窗口,请重试并切换到目标窗口。"); } targetInfo.Text = "HWND 0x" + target.ToString("X"); status.Text = "已选取目标窗口。"; }), Primary("置顶 / 取消置顶", () => { SystemActions.SetTopmost(target, !SystemActions.IsTopmost(target)); status.Text = SystemActions.IsTopmost(target) ? "目标窗口已置顶。" : "已取消置顶。"; })));
        var minutes = Choices(["15", "30", "60", "120", "240"], 1); var awakeState = Label("正常休眠", 24);
        void RefreshAwake() => awakeState.Text = AwakeLease.Shared.Until is { } until ? $"保持唤醒 · 剩余 {Math.Max(0, (until - DateTimeOffset.Now).TotalMinutes):0} 分钟" : "正常休眠";
        var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) }; timer.Tick += (_, _) => RefreshAwake(); Loaded += (_, _) => timer.Start(); cleanup.Add(timer.Stop);
        var awake = Section("☀ 临时防休眠", awakeState, Actions(minutes, Hint("分钟")), Actions(Primary("启动防休眠", () => { AwakeLease.Shared.Start(int.Parse((string)minutes.SelectedItem)); RefreshAwake(); status.Text = "已启动临时防休眠,到期自动恢复。"; }), ActionButton("停止防休眠", () => { AwakeLease.Shared.Stop(); RefreshAwake(); status.Text = "已恢复正常休眠策略。"; })), Hint("不修改电源计划 · 不阻止手动睡眠"));
        body.Children.Add(Split(audio, mic)); body.Children.Add(Split(top, awake));
        var info = Field("LocalAddresses", "点击查看本机 IP,列出当前联网接口。", true, true); info.MinHeight = 80; info.Height = 95;
        body.Children.Add(Section("↔ 本机网络", Actions(ActionButton("查看本机 IP", async () => Result(await Task.Run(SystemActions.LocalAddresses, cancellation.Token), info)), Copy(info)), info));
    }
}