Skip to content
This repository has been archived by the owner on Aug 17, 2024. It is now read-only.

Commit

Permalink
优化翻译窗口置顶效果
Browse files Browse the repository at this point in the history
  • Loading branch information
Isayama-Kagura committed Feb 12, 2022
1 parent 6fa8ae2 commit 826569a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion TsubakiTranslator/TranslateWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
MaxWidth="{StaticResource {x:Static SystemParameters.MaximizedPrimaryScreenWidthKey}}"
MaxHeight="{StaticResource {x:Static SystemParameters.MaximizedPrimaryScreenHeightKey}}"
Closing="On_TranslateWindow_Closing" MouseEnter="TranslateWindow_MouseEnter"
MinHeight="200" MinWidth="400" MouseLeave="TranslateWindow_MouseLeave"
MinHeight="100" MinWidth="400" MouseLeave="TranslateWindow_MouseLeave"
MouseLeftButtonDown="Window_MouseLeftButtonDown">

<!--窗口可改变尺寸-->
Expand Down
27 changes: 26 additions & 1 deletion TsubakiTranslator/TranslateWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using TsubakiTranslator.BasicLibrary;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using System.Windows.Threading;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace TsubakiTranslator
{
Expand All @@ -23,16 +26,26 @@ public partial class TranslateWindow : Window

private ClipboardHookHandler clipboardHookHandler;

private DispatcherTimer timer;

private SpeechSynthesizer synthesizer;
public bool IsHookMode { get; }

private void Init()
{
this.DataContext = MainWindow.WindowConfig;

//确保翻译窗口永远在前
WindowInteropHelper helper = new WindowInteropHelper(this);
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += (sender, e) => BringWindowToTop(HwndSource.FromHwnd(helper.Handle).Handle);

if (MainWindow.WindowConfig.TranslateWindowTopmost)
{
PinButton.Visibility = Visibility.Visible;
PinOffButton.Visibility = Visibility.Collapsed;
timer.Start();
}
this.Background = new SolidColorBrush(Color.FromArgb((byte)MainWindow.WindowConfig.TranslateWindowTransparency, 0, 0, 0));

Expand All @@ -41,6 +54,7 @@ private void Init()
{
TTSButton.IsEnabled = true;
var config = SpeechConfig.FromSubscription(UserConfigPage.TranslateAPIConfig.TTSResourceKey, UserConfigPage.TranslateAPIConfig.TTSRegion);
config.SetSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Riff24Khz16BitMonoPcm);
// Note: if only language is set, the default voice of that language is chosen.
if (UserConfigPage.TranslateAPIConfig.SourceLanguage.Equals("Japanese"))
{
Expand Down Expand Up @@ -144,7 +158,7 @@ private void On_TranslateWindow_Closing(object sender, System.ComponentModel.Can
clipboardHookHandler.ClipboardUpdated -= TranslatedResultDisplay.TranslteClipboardText;
clipboardHookHandler.Dispose();
}

timer.Stop();
mainWindow.Show();

}
Expand Down Expand Up @@ -207,13 +221,15 @@ private void Pin_Button_Click(object sender, RoutedEventArgs e)
PinButton.Visibility = Visibility.Collapsed;
PinOffButton.Visibility = Visibility.Visible;
this.Topmost = false;
timer.Stop();
}

private void PinOff_Button_Click(object sender, RoutedEventArgs e)
{
PinButton.Visibility = Visibility.Visible;
PinOffButton.Visibility = Visibility.Collapsed;
this.Topmost = true;
timer.Start();
}

private void TranslateWindow_MinimizeButton_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -254,5 +270,14 @@ private void VolumeSource_Button_Click(object sender, RoutedEventArgs e)
string sourceText = TranslatedResultDisplay.SourceText.Text;
Parallel.Invoke(async () => { await synthesizer.SpeakTextAsync(sourceText); });
}


/// <summary>
/// 该函数将指定的窗口设置到Z序的顶部。
/// </summary>
///
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern int BringWindowToTop(IntPtr hWnd);

}
}

0 comments on commit 826569a

Please sign in to comment.