Skip to content

Commit

Permalink
音量調整のスクロールコントロールをメニューへ追加
Browse files Browse the repository at this point in the history
  • Loading branch information
esperecyan committed May 4, 2023
1 parent a9aef2a commit 1011f0b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 3 deletions.
3 changes: 3 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<setting name="OuputDeviceName" serializeAs="String">
<value />
</setting>
<setting name="Volume" serializeAs="String">
<value>1</value>
</setting>
</Esperecyan.NCVVoicevox.Properties.Settings>
</userSettings>
<applicationSettings>
Expand Down
24 changes: 21 additions & 3 deletions App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Esperecyan.NCVVoicevox;
public partial class App : Application
{
internal static readonly string Title;
private static readonly int PossibleVolumeValueCount = 100;

static App()
{
Expand Down Expand Up @@ -147,10 +148,27 @@ private App()
return responseEntity;
};

var contextMenuStrip = new ContextMenuStrip();
contextMenuStrip.Items.Add(new ToolStripMenuItem("終了", image: null, (sender, e) => this.Shutdown()));
var contextMenuStrip = new ContextMenuStrip()
{
Renderer = new VolumeIconRenderer(),
};
var menuItems = contextMenuStrip.Items;
var volumeBar = new TrackBar()
{
Maximum = App.PossibleVolumeValueCount,
Width = 200,
TickFrequency = 10,
Value = (int)((waveOut.Volume = Settings.Default.Volume) * App.PossibleVolumeValueCount),
};
volumeBar.ValueChanged += (_, _) =>
{
Settings.Default.Volume = waveOut.Volume = (float)volumeBar.Value / App.PossibleVolumeValueCount;
Settings.Default.Save();
};
menuItems.Add(new ToolStripControlHost(volumeBar, "volume"));
menuItems.Add(new ToolStripMenuItem("終了", image: null, (sender, e) => this.Shutdown()));

contextMenuStrip.Opening += (_, _) => App.UpdateDeviceMenuItems(contextMenuStrip.Items);
contextMenuStrip.Opening += (_, _) => App.UpdateDeviceMenuItems(menuItems);

var notifyIcon = new NotifyIcon()
{
Expand Down
12 changes: 12 additions & 0 deletions Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
<Setting Name="OuputDeviceName" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="Volume" Type="System.Single" Scope="User">
<Value Profile="(Default)">1</Value>
</Setting>
</Settings>
</SettingsFile>
36 changes: 36 additions & 0 deletions VolumeIconRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace Esperecyan.NCVVoicevox;

/// <summary>
/// 「volume」という名前の項目に、スピーカーアイコンを表示します。
/// </summary>
internal class VolumeIconRenderer : ToolStripProfessionalRenderer
{
private static readonly int Size = 24;
private static readonly Icon Icon;

static VolumeIconRenderer()
{
VolumeIconRenderer.Icon = IconExtractor.ExtractFromFile(
Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "DDORes.dll"),
index: 1
);
}

protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
base.OnRenderImageMargin(e);
var volumeItem = e.ToolStrip.Items.Cast<ToolStripItem>().First(item => item.Name == "volume");
e.Graphics.DrawIcon(VolumeIconRenderer.Icon, new Rectangle(
x: 1,
y: volumeItem.Bounds.Y + volumeItem.Height / 2 - VolumeIconRenderer.Size / 2,
width: VolumeIconRenderer.Size,
height: VolumeIconRenderer.Size
));
}
}

0 comments on commit 1011f0b

Please sign in to comment.