Эх сурвалжийг харах

1.添加热键工具类;
2.完善设置窗口逻辑;
3.完善视频播放逻辑。

Yumin 6 жил өмнө
parent
commit
4ce4b58958

+ 3 - 0
TouchVideo/App.config

@@ -43,6 +43,9 @@
             <setting name="key" serializeAs="String">
                 <value />
             </setting>
+            <setting name="loopPlay" serializeAs="String">
+                <value>False</value>
+            </setting>
         </TouchVideo.Properties.Settings>
     </userSettings>
 </configuration>

+ 39 - 0
TouchVideo/HotKey.cs

@@ -0,0 +1,39 @@
+using System;
+using System.Runtime.InteropServices;
+using System.Windows.Forms;
+
+namespace TouchVideo
+{
+    class HotKey
+    {
+        // 如果函数执行成功,返回值不为0。
+        // 如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
+        // 用于注册热键。由于这个函数需要引用user32.dll动态链接库后才能使用,并且user32.dll是非托管代码,不能用命名空间的方式直接引用,所以需要用“DllImport”进行引入后才能使用。
+        [DllImport("user32.dll", SetLastError = true)]
+        public static extern bool RegisterHotKey(
+            IntPtr hWnd,                // 要定义热键的窗口的句柄
+            int id,                     // 定义热键ID(不能与其它ID重复)           
+            KeyModifiers fsModifiers,   // 标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
+            Keys vk                     // 定义热键的内容
+            );
+
+        // 用于注销热键,同理也需要用DllImport引用user32.dll后才能使用。
+        [DllImport("user32.dll", SetLastError = true)]
+        public static extern bool UnregisterHotKey(
+            IntPtr hWnd,                // 要取消热键的窗口的句柄
+            int id                      // 要取消热键的ID
+            );
+
+        // 定义了一组枚举,将辅助键的数字代码直接表示为文字,以方便使用。这样在调用时我们不必记住每一个辅助键的代码而只需直接选择其名称即可。
+        // 定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
+        [Flags()]
+        public enum KeyModifiers
+        {
+            None = 0,
+            Alt = 1,
+            Ctrl = 2,
+            Shift = 4,
+            WindowsKey = 8
+        }
+    }
+}

+ 5 - 5
TouchVideo/MainWindow.xaml

@@ -1,21 +1,21 @@
-<Window x:Class="TouchVideo.MainWindow"
+<Window x:Name="mainWindow" x:Class="TouchVideo.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:TouchVideo"
         mc:Ignorable="d"
-        Title="触摸视频" Height="350" Width="525" Loaded="Window_Loaded">
+        Title="触摸视频" Height="350" Width="525" Loaded="Window_Loaded" Initialized="Window_Initialized" ResizeMode="CanMinimize" Closing="Window_Closing" KeyDown="Window_KeyDown" Activated="Window_Activated" Closed="Window_Closed">
     <Grid>
         <ContentControl x:Name="contentControl" MouseDoubleClick="contentControl_MouseDoubleClick">
             <MediaElement x:Name="mediaElement" Volume="1" Margin="0,30,0,0"/>
         </ContentControl>
         <ToolBar x:Name="toolBar" VerticalAlignment="Top" Padding="0,0">
-            <Button x:Name="fullScreen" Content="&#xF31E; 全屏显示" ToolTip="全屏显示" Style="{StaticResource FontAwesome}" Click="fullScreen_Click" VerticalAlignment="Stretch"/>
+            <Button x:Name="fullScreen" Content="&#xF31E; 全屏显示" ToolTip="全屏显示" Style="{StaticResource FontAwesome}" Click="fullScreen_Click" VerticalAlignment="Stretch" FontSize="12" Padding="4"/>
             <Separator/>
-            <Button x:Name="settings" Content="&#xF013; 设置" ToolTip="设置" Style="{StaticResource FontAwesome}" Click="settings_Click" VerticalAlignment="Stretch"/>
+            <Button x:Name="settings" Content="&#xF013; 设置" ToolTip="设置" Style="{StaticResource FontAwesome}" Click="settings_Click" VerticalAlignment="Stretch" FontSize="12" Padding="4"/>
             <Separator/>
-            <Button x:Name="about" Content="&#xF05A; 关于" ToolTip="关于" Style="{StaticResource FontAwesome}" Click="about_Click" VerticalAlignment="Stretch"/>
+            <Button x:Name="about" Content="&#xF05A; 关于" ToolTip="关于" Style="{StaticResource FontAwesome}" Click="about_Click" VerticalAlignment="Stretch" FontSize="12" Padding="4"/>
             <Separator/>
         </ToolBar>
 

+ 210 - 6
TouchVideo/MainWindow.xaml.cs

@@ -1,5 +1,9 @@
-using System.Windows;
+using System;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Forms;
 using System.Windows.Input;
+using System.Windows.Interop;
 
 namespace TouchVideo
 {
@@ -8,6 +12,15 @@ namespace TouchVideo
     /// </summary>
     public partial class MainWindow : Window
     {
+        private void Window_Initialized(object sender, System.EventArgs e)
+        {
+            if (!Properties.Settings.Default.key.Equals("123456"))
+            {
+                new LockWindow().Show();
+                Close();
+            }
+        }
+
         public MainWindow()
         {
             InitializeComponent();
@@ -15,16 +28,207 @@ namespace TouchVideo
 
         private void Window_Loaded(object sender, RoutedEventArgs e)
         {
-            if (!Properties.Settings.Default.key.Equals("123456"))
+            // 监听 Windows 消息,获取窗口句柄一定要写在窗口 loaded 事件里,才能获取到窗口句柄,否则为空
+            HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;  // 窗口过程
+            if (hwndSource != null)
             {
-                new LockWindow().Show();
-                Close();
+                hwndSource.AddHook(new HwndSourceHook(Key_Down)); // 挂钩
             }
+
+            // 绑定视频文件
+            // 交互式控制
+            mediaElement.LoadedBehavior = MediaState.Manual;
+            // 添加元素加载完成事件 -- 自动开始播放
+            mediaElement.Loaded += new RoutedEventHandler(media_Loaded);
+            // 添加媒体播放结束事件 -- 重新播放
+            mediaElement.MediaEnded += new RoutedEventHandler(media_MediaEnded);
+            // 添加元素卸载完成事件 -- 停止播放
+            mediaElement.Unloaded += new RoutedEventHandler(media_Unloaded);
         }
 
-        private void fullScreen_Click(object sender, RoutedEventArgs e)
+        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
+        {
+            MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("确定退出程序吗?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
+            if (messageBoxResult == MessageBoxResult.Cancel)
+            {
+                e.Cancel = true;
+            }
+            else
+            {
+                System.Windows.Application.Current.Shutdown();
+            }
+        }
+
+        private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
+        {
+            if (e.Key == System.Windows.Input.Key.Escape)
+            {
+                toolBar.Visibility = Visibility.Visible;
+                Thickness thickness = new Thickness();
+                thickness.Top = 30;
+                mediaElement.Margin = thickness;
+                this.WindowState = WindowState.Normal;
+                this.WindowStyle = WindowStyle.SingleBorderWindow;
+            }
+        }
+
+        private void Window_Activated(object sender, EventArgs e)
         {
+            HotKey.RegisterHotKey(((HwndSource)PresentationSource.FromVisual(mainWindow)).Handle, 100, HotKey.KeyModifiers.None, Keys.W);
+            HotKey.RegisterHotKey(new WindowInteropHelper(this).Handle, 101, HotKey.KeyModifiers.None, Keys.A);
+            HotKey.RegisterHotKey(new WindowInteropHelper(this).Handle, 102, HotKey.KeyModifiers.None, Keys.S);
+            HotKey.RegisterHotKey(new WindowInteropHelper(this).Handle, 103, HotKey.KeyModifiers.None, Keys.D);
+            HotKey.RegisterHotKey(new WindowInteropHelper(this).Handle, 104, HotKey.KeyModifiers.None, Keys.F);
+            HotKey.RegisterHotKey(new WindowInteropHelper(this).Handle, 105, HotKey.KeyModifiers.None, Keys.G);
+            HotKey.RegisterHotKey(new WindowInteropHelper(this).Handle, 106, HotKey.KeyModifiers.None, Keys.Up);
+            HotKey.RegisterHotKey(new WindowInteropHelper(this).Handle, 107, HotKey.KeyModifiers.None, Keys.Down);
+            HotKey.RegisterHotKey(new WindowInteropHelper(this).Handle, 108, HotKey.KeyModifiers.None, Keys.Left);
+            HotKey.RegisterHotKey(new WindowInteropHelper(this).Handle, 109, HotKey.KeyModifiers.None, Keys.Right);
+        }
+
+        private void Window_Closed(object sender, EventArgs e)
+        {
+            HotKey.UnregisterHotKey(new WindowInteropHelper(this).Handle, 100);
+            HotKey.UnregisterHotKey(new WindowInteropHelper(this).Handle, 101);
+            HotKey.UnregisterHotKey(new WindowInteropHelper(this).Handle, 102);
+            HotKey.UnregisterHotKey(new WindowInteropHelper(this).Handle, 103);
+            HotKey.UnregisterHotKey(new WindowInteropHelper(this).Handle, 104);
+            HotKey.UnregisterHotKey(new WindowInteropHelper(this).Handle, 105);
+            HotKey.UnregisterHotKey(new WindowInteropHelper(this).Handle, 106);
+            HotKey.UnregisterHotKey(new WindowInteropHelper(this).Handle, 107);
+            HotKey.UnregisterHotKey(new WindowInteropHelper(this).Handle, 108);
+            HotKey.UnregisterHotKey(new WindowInteropHelper(this).Handle, 109);
+        }
 
+        protected IntPtr Key_Down(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
+        {
+            const int WM_HOTKEY = 0x0312;
+            // 按快捷键 
+            switch (msg)
+            {
+                case WM_HOTKEY:
+                    string videoPath;
+                    switch (wParam.ToInt32())
+                    {
+                        case 100:
+                            videoPath = Properties.Settings.Default.video_1;
+                            if (videoPath != "")
+                            {
+                                mediaElement.Source = new Uri(videoPath);
+                                mediaElement.Play();
+                            }
+                            break;
+                        case 101:
+                            videoPath = Properties.Settings.Default.video_2;
+                            if (videoPath != "")
+                            {
+                                mediaElement.Source = new Uri(videoPath);
+                                mediaElement.Play();
+                            }
+                            break;
+                        case 102:
+                            videoPath = Properties.Settings.Default.video_3;
+                            if (videoPath != "")
+                            {
+                                mediaElement.Source = new Uri(videoPath);
+                                mediaElement.Play();
+                            }
+                            break;
+                        case 103:
+                            videoPath = Properties.Settings.Default.video_4;
+                            if (videoPath != "")
+                            {
+                                mediaElement.Source = new Uri(videoPath);
+                                mediaElement.Play();
+                            }
+                            break;
+                        case 104:
+                            videoPath = Properties.Settings.Default.video_5;
+                            if (videoPath != "")
+                            {
+                                mediaElement.Source = new Uri(videoPath);
+                                mediaElement.Play();
+                            }
+                            break;
+                        case 105:
+                            videoPath = Properties.Settings.Default.video_6;
+                            if (videoPath != "")
+                            {
+                                mediaElement.Source = new Uri(videoPath);
+                                mediaElement.Play();
+                            }
+                            break;
+                        case 106:
+                            videoPath = Properties.Settings.Default.video_7;
+                            if (videoPath != "")
+                            {
+                                mediaElement.Source = new Uri(videoPath);
+                                mediaElement.Play();
+                            }
+                            break;
+                        case 107:
+                            videoPath = Properties.Settings.Default.video_8;
+                            if (videoPath != "")
+                            {
+                                mediaElement.Source = new Uri(videoPath);
+                                mediaElement.Play();
+                            }
+                            break;
+                        case 108:
+                            videoPath = Properties.Settings.Default.video_9;
+                            if (videoPath != "")
+                            {
+                                mediaElement.Source = new Uri(videoPath);
+                                mediaElement.Play();
+                            }
+                            break;
+                        case 109:
+                            videoPath = Properties.Settings.Default.video_10;
+                            if (videoPath != "")
+                            {
+                                mediaElement.Source = new Uri(videoPath);
+                                mediaElement.Play();
+                            }
+                            break;
+                    }
+                    break;
+            }
+            return IntPtr.Zero;
+        }
+
+        private void media_Loaded(object sender, RoutedEventArgs e)
+        {
+            (sender as MediaElement).Play();
+        }
+
+        private void media_MediaEnded(object sender, RoutedEventArgs e)
+        {
+            if (Properties.Settings.Default.loopPlay)
+            {
+                // MediaElement 需要先停止播放才能再开始播放,否则会停在最后一帧不动
+                (sender as MediaElement).Stop();
+                (sender as MediaElement).Play();
+            }
+            else
+            {
+                (sender as MediaElement).Stop();
+            }
+        }
+
+        private void media_Unloaded(object sender, RoutedEventArgs e)
+        {
+            (sender as MediaElement).Stop();
+        }
+
+        private void fullScreen_Click(object sender, RoutedEventArgs e)
+        {
+            toolBar.Visibility = Visibility.Hidden;
+            Thickness thickness = new Thickness();
+            thickness.Top = 0;
+            mediaElement.Margin = thickness;
+            this.Topmost = false;
+            this.WindowStyle = WindowStyle.None;
+            this.WindowState = WindowState.Maximized;
         }
 
         private void settings_Click(object sender, RoutedEventArgs e)
@@ -39,7 +243,7 @@ namespace TouchVideo
 
         private void contentControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
         {
-
+            fullScreen_Click(null, null);
         }
     }
 }

+ 2 - 4
TouchVideo/Properties/AssemblyInfo.cs

@@ -1,6 +1,4 @@
 using System.Reflection;
-using System.Resources;
-using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Windows;
 
@@ -8,9 +6,9 @@ using System.Windows;
 // 控制。更改这些特性值可修改
 // 与程序集关联的信息。
 [assembly: AssemblyTitle("TouchVideo")]
-[assembly: AssemblyDescription("")]
+[assembly: AssemblyDescription("为易达工作室 - 互动艺术装置专家\r\n\r\n如需技术帮助请联系:Richard Wu\r\nWeChat:ffkyd188\r\nEmail:1029071285@qq.com")]
 [assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("")]
+[assembly: AssemblyCompany("郑州为易达电子科技工作室")]
 [assembly: AssemblyProduct("TouchVideo")]
 [assembly: AssemblyCopyright("Copyright ©  2018")]
 [assembly: AssemblyTrademark("")]

+ 25 - 16
TouchVideo/Properties/Settings.Designer.cs

@@ -22,22 +22,7 @@ namespace TouchVideo.Properties {
                 return defaultInstance;
             }
         }
-
-        [global::System.Configuration.UserScopedSettingAttribute()]
-        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
-        [global::System.Configuration.DefaultSettingValueAttribute("")]
-        public string key
-        {
-            get
-            {
-                return ((string)(this["key"]));
-            }
-            set
-            {
-                this["key"] = value;
-            }
-        }
-
+        
         [global::System.Configuration.UserScopedSettingAttribute()]
         [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
         [global::System.Configuration.DefaultSettingValueAttribute("")]
@@ -157,5 +142,29 @@ namespace TouchVideo.Properties {
                 this["video_10"] = value;
             }
         }
+        
+        [global::System.Configuration.UserScopedSettingAttribute()]
+        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [global::System.Configuration.DefaultSettingValueAttribute("")]
+        public string key {
+            get {
+                return ((string)(this["key"]));
+            }
+            set {
+                this["key"] = value;
+            }
+        }
+        
+        [global::System.Configuration.UserScopedSettingAttribute()]
+        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [global::System.Configuration.DefaultSettingValueAttribute("False")]
+        public bool loopPlay {
+            get {
+                return ((bool)(this["loopPlay"]));
+            }
+            set {
+                this["loopPlay"] = value;
+            }
+        }
     }
 }

+ 3 - 0
TouchVideo/Properties/Settings.settings

@@ -35,5 +35,8 @@
     <Setting Name="key" Type="System.String" Scope="User">
       <Value Profile="(Default)" />
     </Setting>
+    <Setting Name="loopPlay" Type="System.Boolean" Scope="User">
+      <Value Profile="(Default)">False</Value>
+    </Setting>
   </Settings>
 </SettingsFile>

+ 2 - 1
TouchVideo/SettingsWindow.xaml

@@ -5,7 +5,7 @@
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:TouchVideo"
         mc:Ignorable="d"
-        Title="设置" Height="350" Width="350" Loaded="Window_Loaded">
+        Title="设置" Height="380" Width="350" Loaded="Window_Loaded" ResizeMode="NoResize" Closed="Window_Closed">
     <Grid>
         <GroupBox x:Name="groupBox" Header="视频设置" Margin="10">
             <Grid Margin="10">
@@ -29,6 +29,7 @@
                 <Button x:Name="video9Choose" Content="视频九" Margin="220,200,0,0" VerticalAlignment="Top" Height="20" Click="video9Choose_Click"/>
                 <TextBox x:Name="video10" Height="20" Width="215" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,225,0,0" IsEnabled="False"/>
                 <Button x:Name="video10Choose" Content="视频十" Margin="220,225,0,0" VerticalAlignment="Top" Height="20" Click="video10Choose_Click"/>
+                <CheckBox x:Name="loopPlay" Content="循环播放" HorizontalAlignment="Left" Margin="0,250,0,0" VerticalAlignment="Top"/>
             </Grid>
         </GroupBox>
 

+ 68 - 12
TouchVideo/SettingsWindow.xaml.cs

@@ -8,6 +8,8 @@ namespace TouchVideo
     /// </summary>
     public partial class SettingsWindow : Window
     {
+        private OpenFileDialog ofd;
+
         public SettingsWindow()
         {
             InitializeComponent();
@@ -25,15 +27,17 @@ namespace TouchVideo
             video8.Text = Properties.Settings.Default.video_8;
             video9.Text = Properties.Settings.Default.video_9;
             video10.Text = Properties.Settings.Default.video_10;
-        }
+            loopPlay.IsChecked = Properties.Settings.Default.loopPlay;
 
-        private void video1Choose_Click(object sender, RoutedEventArgs e)
-        {
-            OpenFileDialog ofd = new OpenFileDialog();
+            ofd = new OpenFileDialog();
             ofd.Filter = "媒体文件(所有类型)|*.mp3;*.mpeg;*.wma;*.wmv;*.wav;*.avi;*.mp4|所有文件|*.*";
             ofd.ValidateNames = true;
             ofd.CheckPathExists = true;
             ofd.CheckFileExists = true;
+        }
+
+        private void video1Choose_Click(object sender, RoutedEventArgs e)
+        {
             if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
                 string strFileName = ofd.FileName;
@@ -44,47 +48,99 @@ namespace TouchVideo
 
         private void video2Choose_Click(object sender, RoutedEventArgs e)
         {
-
+            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+            {
+                string strFileName = ofd.FileName;
+                video2.Text = strFileName;
+                Properties.Settings.Default.video_2 = strFileName;
+            }
         }
 
         private void video3Choose_Click(object sender, RoutedEventArgs e)
         {
-
+            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+            {
+                string strFileName = ofd.FileName;
+                video3.Text = strFileName;
+                Properties.Settings.Default.video_3 = strFileName;
+            }
         }
 
         private void video4Choose_Click(object sender, RoutedEventArgs e)
         {
-
+            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+            {
+                string strFileName = ofd.FileName;
+                video4.Text = strFileName;
+                Properties.Settings.Default.video_4 = strFileName;
+            }
         }
 
         private void video5Choose_Click(object sender, RoutedEventArgs e)
         {
-
+            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+            {
+                string strFileName = ofd.FileName;
+                video5.Text = strFileName;
+                Properties.Settings.Default.video_5 = strFileName;
+            }
         }
 
         private void video6Choose_Click(object sender, RoutedEventArgs e)
         {
-
+            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+            {
+                string strFileName = ofd.FileName;
+                video6.Text = strFileName;
+                Properties.Settings.Default.video_6 = strFileName;
+            }
         }
 
         private void video7Choose_Click(object sender, RoutedEventArgs e)
         {
-
+            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+            {
+                string strFileName = ofd.FileName;
+                video7.Text = strFileName;
+                Properties.Settings.Default.video_7 = strFileName;
+            }
         }
 
         private void video8Choose_Click(object sender, RoutedEventArgs e)
         {
-
+            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+            {
+                string strFileName = ofd.FileName;
+                video8.Text = strFileName;
+                Properties.Settings.Default.video_8 = strFileName;
+            }
         }
 
         private void video9Choose_Click(object sender, RoutedEventArgs e)
         {
-
+            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+            {
+                string strFileName = ofd.FileName;
+                video9.Text = strFileName;
+                Properties.Settings.Default.video_9 = strFileName;
+            }
         }
 
         private void video10Choose_Click(object sender, RoutedEventArgs e)
         {
+            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+            {
+                string strFileName = ofd.FileName;
+                video10.Text = strFileName;
+                Properties.Settings.Default.video_10 = strFileName;
+            }
+        }
 
+        private void Window_Closed(object sender, System.EventArgs e)
+        {
+            Properties.Settings.Default.loopPlay = (bool)loopPlay.IsChecked;
+            Properties.Settings.Default.Save();
+            Properties.Settings.Default.Reload();
         }
     }
 }

+ 1 - 0
TouchVideo/TouchVideo.csproj

@@ -63,6 +63,7 @@
     <Compile Include="AboutBox.Designer.cs">
       <DependentUpon>AboutBox.cs</DependentUpon>
     </Compile>
+    <Compile Include="HotKey.cs" />
     <Compile Include="Settings.cs" />
     <Compile Include="SettingsWindow.xaml.cs">
       <DependentUpon>SettingsWindow.xaml</DependentUpon>