UcFrontPage.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. using BellwetherBackend.Utility;
  2. using Microsoft.Win32;
  3. using Microsoft.WindowsAPICodePack.Shell;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. namespace BellwetherBackend.SubPage
  14. {
  15. /// <summary>
  16. /// Interaction logic for UcBasicSetting.xaml
  17. /// </summary>
  18. public partial class UcFrontPage : UserControl
  19. {
  20. private const int avalibleTotalElement = 100;
  21. private const string frontpageDataPath = @"Data\FrontPage\";
  22. private Utility.FrontPageJson frontPageJson;
  23. private int selectMediaIndex = -1;
  24. public UcFrontPage()
  25. {
  26. InitializeComponent();
  27. //icon load
  28. AddCategoryToolBar();
  29. LoadSettingData();
  30. this.Loaded += UcAbout_Loaded;
  31. }
  32. private void UcAbout_Loaded(object sender, RoutedEventArgs e)
  33. {
  34. //load media
  35. List<string> fileLists = frontPageJson.MediaList; //Utility.Setting.AboutComapnyImgList;
  36. Image imageBuf;
  37. Grid gridBuf;
  38. if (fileLists != null)
  39. for (int fileIndex = 0; fileIndex < fileLists.Count; fileIndex++)
  40. {
  41. Border bd = new Border() { BorderThickness = new Thickness(5), BorderBrush = new SolidColorBrush(Colors.Gray), CornerRadius = new CornerRadius(2), Margin = new Thickness(3, 20, 3, 20), Background = Brushes.White };
  42. bd.Focusable = true;
  43. bd.PreviewMouseLeftButtonUp += Sc_PreviewMouseLeftButtonUp;
  44. string aboutImgDirPath = frontpageDataPath;
  45. string path = System.IO.Path.Combine(aboutImgDirPath, fileLists[fileIndex]); //GlobalFunction.rootPath + GlobalFunction.dataFolder + GlobalFunction.multiMediaFolderName + multiMediaJsonData.folderColletion[currentFolderIndex].folderName + @"\" + fileLists[fileIndex];
  46. //string path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), fileLists[fileIndex]);
  47. if (GlobalFunction.CheckFileIsImage(path))
  48. {
  49. imageBuf = new Image();
  50. //imageBuf.Source = new BitmapImage(new Uri(path));
  51. imageBuf.Source = GlobalFunction.GetBitMap(path);
  52. imageBuf.Stretch = Stretch.Uniform;
  53. gridBuf = new Grid();
  54. //imageBuf.TouchDown += StackElemnt_TouchDown;
  55. gridBuf.Children.Add(imageBuf);
  56. gridBuf.Width = 213;
  57. gridBuf.Height = 120;
  58. bd.Child = gridBuf;
  59. uxFileViewer.Children.Add(bd);
  60. }
  61. else if (GlobalFunction.CheckFileIsVideo(path))
  62. {
  63. ShellFile shellFile = ShellFile.FromFilePath(path);
  64. imageBuf = new Image();
  65. if (shellFile != null && shellFile.Thumbnail != null && shellFile.Thumbnail.ExtraLargeBitmapSource != null)
  66. imageBuf.Source = shellFile.Thumbnail.ExtraLargeBitmapSource;
  67. //imageBuf.TouchDown += BotImage_TouchDown;
  68. imageBuf.Stretch = Stretch.Uniform;
  69. gridBuf = new Grid();
  70. gridBuf.Width = 213;
  71. gridBuf.Height = 120;
  72. gridBuf.Children.Add(imageBuf);
  73. gridBuf.Children.Add(new Image() { Source = new BitmapImage(new Uri("pack://application:,,,/BellwetherBackend;component/Image/play.png")), Width = 80 });
  74. bd.Child = gridBuf;
  75. uxFileViewer.Children.Add(bd);
  76. }
  77. }
  78. }
  79. private void AddCategoryToolBar()
  80. {
  81. new GlobalFunction();
  82. uxAddBtn.IconData = GlobalFunction.AddBtnData;
  83. uxDeleteBtn.IconData = GlobalFunction.DeleteBtnData;
  84. uxUpBtn.IconData = GlobalFunction.MoveUpBtnData;
  85. uxDownBtn.IconData = GlobalFunction.MoveDownBtnData;
  86. }
  87. private void LoadSettingData()
  88. {
  89. frontPageJson = Setting.frontPageJsonFile;
  90. uxSolarEnergyUrlText.Text = frontPageJson.SolarEnergyUrl;
  91. }
  92. private void SaveData()
  93. {
  94. Setting.frontPageJsonFile = frontPageJson;
  95. }
  96. private void uxSolarUrlText_TextChanged(object sender, TextChangedEventArgs e)
  97. {
  98. if (this.IsLoaded)
  99. {
  100. uxSolarEnergyUrlText.TextChanged -= uxSolarUrlText_TextChanged;
  101. frontPageJson.SolarEnergyUrl = uxSolarEnergyUrlText.Text;
  102. SaveData();
  103. uxSolarEnergyUrlText.TextChanged += uxSolarUrlText_TextChanged;
  104. }
  105. }
  106. private void uxAutoPlaIntervalText_TextChanged(object sender, TextChangedEventArgs e)
  107. {
  108. if (this.IsLoaded)
  109. {
  110. uxAutoPlaIntervalText.TextChanged -= uxSolarUrlText_TextChanged;
  111. if (int.TryParse(uxAutoPlaIntervalText.Text, out var interval))
  112. {
  113. frontPageJson.AutoPlayIntervalSec = interval;
  114. SaveData();
  115. }
  116. uxAutoPlaIntervalText.TextChanged += uxSolarUrlText_TextChanged;
  117. }
  118. }
  119. private void uxAddBtn_Click(object sender, RoutedEventArgs e)
  120. {
  121. if (uxFileViewer.Children.Count >= avalibleTotalElement)
  122. {
  123. MessageBox.Show("容許數量已滿");
  124. return;
  125. }
  126. OpenFileDialog openfileDlg = new OpenFileDialog();
  127. openfileDlg.Multiselect = true;
  128. openfileDlg.Filter = "Supported files|";
  129. for (int i = 0; i < GlobalFunction.imgDimList.Count(); i++)
  130. {
  131. openfileDlg.Filter += "*" + GlobalFunction.imgDimList[i] + ";";
  132. }
  133. for (int i = 0; i < GlobalFunction.mediaDimList.Count(); i++)
  134. {
  135. openfileDlg.Filter += "*" + GlobalFunction.mediaDimList[i] + ";";
  136. }
  137. openfileDlg.Filter += "*" + ".PPT" + ";";
  138. openfileDlg.Filter += "*" + ".PPTX" + ";";
  139. if ((bool)openfileDlg.ShowDialog())
  140. {
  141. Image imageBuf;
  142. Grid gridBuf;
  143. foreach (string name in openfileDlg.FileNames)
  144. {
  145. //string[] imageList = mediaLists[mediaIndex];
  146. Border bd = new Border() { BorderThickness = new Thickness(5), BorderBrush = new SolidColorBrush(Colors.Gray), CornerRadius = new CornerRadius(2), Margin = new Thickness(3, 20, 3, 20), Background = Brushes.White };
  147. bd.Focusable = true;
  148. bd.PreviewMouseLeftButtonUp += Sc_PreviewMouseLeftButtonUp;
  149. if (GlobalFunction.CheckFileIsImage(name))
  150. {
  151. //image
  152. string legalFileName = GlobalFunction.GetFolderFileName(frontpageDataPath, name);
  153. string fileTo = System.IO.Path.Combine(frontpageDataPath, legalFileName);
  154. //複製
  155. //File.Copy(name, fileTo);
  156. ReduceImageResolution.CheckImageResolution(name, fileTo, 1920, 1080);
  157. //json
  158. frontPageJson.MediaList.Add(legalFileName);
  159. //ux - bd
  160. imageBuf = new Image();
  161. //imageBuf.Source = new BitmapImage(new Uri(fileTo));
  162. imageBuf.Source = GlobalFunction.GetBitMap(fileTo);
  163. imageBuf.Stretch = Stretch.Uniform;
  164. gridBuf = new Grid();
  165. gridBuf.Children.Add(imageBuf);
  166. gridBuf.Width = 213;
  167. gridBuf.Height = 120;
  168. bd.Child = gridBuf;
  169. uxFileViewer.Children.Add(bd);
  170. }
  171. else if (GlobalFunction.CheckFileIsVideo(name))
  172. {
  173. string legalFileName = GlobalFunction.GetFolderFileName(frontpageDataPath, name);
  174. string fileTo = System.IO.Path.Combine(frontpageDataPath, legalFileName);
  175. //複製
  176. File.Copy(name, fileTo);
  177. //json - mediafile
  178. frontPageJson.MediaList.Add(legalFileName);
  179. //ux - bd
  180. ShellFile shellFile = ShellFile.FromFilePath(fileTo);
  181. imageBuf = new Image();
  182. if (shellFile != null && shellFile.Thumbnail != null && shellFile.Thumbnail.ExtraLargeBitmapSource != null)
  183. imageBuf.Source = shellFile.Thumbnail.ExtraLargeBitmapSource;
  184. imageBuf.Stretch = Stretch.Uniform;
  185. gridBuf = new Grid();
  186. gridBuf.Children.Add(imageBuf);
  187. gridBuf.Children.Add(new Image() { Source = new BitmapImage(new Uri("pack://application:,,,/BellwetherBackend;component/Image/play.png")), Width = 80 });
  188. gridBuf.Width = 213;
  189. gridBuf.Height = 120;
  190. bd.Child = gridBuf;
  191. uxFileViewer.Children.Add(bd);
  192. }
  193. else if (GlobalFunction.pptDimList.Contains(System.IO.Path.GetExtension(name).ToLower()))
  194. {
  195. string legalFileName;
  196. string fileTo;
  197. string tempPath = System.Windows.Forms.Application.StartupPath + @"\Temp"; //暫存資料夾路徑
  198. string testResImgFile = tempPath + "\\testResolution.BMP";
  199. Microsoft.Office.Interop.PowerPoint.Application pptApplication =
  200. new Microsoft.Office.Interop.PowerPoint.Application();
  201. Microsoft.Office.Interop.PowerPoint.Presentation pptPresentation =
  202. pptApplication.Presentations.Open(name, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse);
  203. //lb_noimage.Refresh();
  204. // Export the first page to retrieve its resolution
  205. pptPresentation.Slides[1].Export(testResImgFile, "BMP");
  206. BitmapImage directImage = new BitmapImage();
  207. directImage.BeginInit();
  208. directImage.UriSource = new Uri(testResImgFile);
  209. directImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
  210. directImage.CacheOption = BitmapCacheOption.OnLoad;
  211. directImage.EndInit();
  212. int width = directImage.PixelWidth;
  213. int height = directImage.PixelHeight;
  214. // Resolution adaptation
  215. if (directImage.PixelWidth > directImage.PixelHeight)
  216. {
  217. // landscape (1600*900)
  218. width = 1600;
  219. height = 900;
  220. }
  221. else
  222. {
  223. // portrait (900*1600)
  224. width = 900;
  225. height = 1600;
  226. }
  227. double bestResFactor = Math.Min((double)width / directImage.PixelWidth, (double)height / directImage.PixelHeight);
  228. width = (int)(directImage.PixelWidth * bestResFactor);
  229. height = (int)(directImage.PixelHeight * bestResFactor);
  230. int slidesCount = pptPresentation.Slides.Count;
  231. for (int idx = 1; idx <= slidesCount; idx++)
  232. {
  233. bd = new Border() { BorderThickness = new Thickness(5), BorderBrush = new SolidColorBrush(Colors.Gray), CornerRadius = new CornerRadius(2), Margin = new Thickness(3, 15, 3, 15) , Background = Brushes.White };
  234. bd.PreviewMouseLeftButtonUp += Sc_PreviewMouseLeftButtonUp;
  235. legalFileName = GlobalFunction.GetFolderFileName(frontpageDataPath, "1.BMP");
  236. fileTo = System.IO.Path.Combine(frontpageDataPath, legalFileName);// GlobalFunction.rootPath + GlobalFunction.dataFolder + GlobalFunction.multiMediaFolderName + multiMediaJsonData.folderColletion[currentFolderIndex].folderName + @"\" + legalFileName;
  237. //json - mediafile
  238. frontPageJson.MediaList.Add(legalFileName);
  239. fileTo = System.IO.Path.Combine(Directory.GetCurrentDirectory(), fileTo);
  240. pptPresentation.Slides[idx].Export(fileTo, "BMP", width, height);
  241. //ux - bd
  242. imageBuf = new Image();
  243. //imageBuf.Source = new BitmapImage(new Uri(fileTo));
  244. imageBuf.Source = GlobalFunction.GetBitMap(fileTo);
  245. imageBuf.Stretch = Stretch.Uniform;
  246. gridBuf = new Grid();
  247. gridBuf.Children.Add(imageBuf);
  248. gridBuf.Width = 213;
  249. gridBuf.Height = 120;
  250. bd.Child = gridBuf;
  251. uxFileViewer.Children.Add(bd);
  252. }
  253. pptPresentation.Close();
  254. System.IO.File.Delete(testResImgFile);
  255. }
  256. }
  257. if (selectMediaIndex != -1)
  258. {
  259. ((Border)uxFileViewer.Children[selectMediaIndex]).BorderBrush = new SolidColorBrush(Colors.Gray);
  260. }
  261. selectMediaIndex = uxFileViewer.Children.Count - 1;
  262. ((Border)uxFileViewer.Children[selectMediaIndex]).BorderBrush = new SolidColorBrush(Colors.Blue);
  263. ((Border)uxFileViewer.Children[selectMediaIndex]).Focus();
  264. }
  265. Setting.frontPageJsonFile = frontPageJson;
  266. }
  267. private void uxDeleteBtn_Click(object sender, RoutedEventArgs e)
  268. {
  269. if (selectMediaIndex < 0)
  270. {
  271. return;
  272. }
  273. uxFileViewer.Children.RemoveAt(selectMediaIndex);
  274. string path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), frontPageJson.MediaList[selectMediaIndex]);
  275. File.Delete(System.IO.Path.Combine(frontpageDataPath, frontPageJson.MediaList[selectMediaIndex]));// GlobalFunction.rootPath + GlobalFunction.dataFolder + GlobalFunction.multiMediaFolderName + multiMediaJsonData.folderColletion[currentFolderIndex].folderName + @"\" + multiMediaJsonData.folderColletion[currentFolderIndex].fileNameList[selectMediaIndex]);
  276. frontPageJson.MediaList.RemoveAt(selectMediaIndex);
  277. //remove items
  278. if (frontPageJson.MediaList.Count == 0)
  279. {
  280. selectMediaIndex = -1;
  281. }
  282. else
  283. {
  284. if (selectMediaIndex > (frontPageJson.MediaList.Count - 1))
  285. {
  286. selectMediaIndex = frontPageJson.MediaList.Count - 1;
  287. }
  288. else
  289. {
  290. //selectMediaIndex = selectMediaIndex;
  291. }
  292. Border gd = uxFileViewer.Children[selectMediaIndex] as Border;
  293. gd.BorderBrush = new SolidColorBrush(Colors.Blue);
  294. }
  295. Setting.frontPageJsonFile = frontPageJson;
  296. }
  297. private void uxLeftBtn_Click(object sender, RoutedEventArgs e)
  298. {
  299. if (selectMediaIndex > 0)
  300. {
  301. Border bdTemp = uxFileViewer.Children[selectMediaIndex] as Border;
  302. string stTemp = frontPageJson.MediaList[selectMediaIndex];
  303. uxFileViewer.Children.RemoveAt(selectMediaIndex);
  304. frontPageJson.MediaList.RemoveAt(selectMediaIndex);
  305. selectMediaIndex = selectMediaIndex - 1;
  306. uxFileViewer.Children.Insert(selectMediaIndex, bdTemp);
  307. frontPageJson.MediaList.Insert(selectMediaIndex, stTemp);
  308. bdTemp.Focus();
  309. Setting.frontPageJsonFile = frontPageJson;
  310. }
  311. }
  312. private void uxRightBtn_Click(object sender, RoutedEventArgs e)
  313. {
  314. if (selectMediaIndex != (uxFileViewer.Children.Count - 1) && selectMediaIndex >= 0)
  315. {
  316. Border bdTemp = uxFileViewer.Children[selectMediaIndex] as Border;
  317. string stTemp = frontPageJson.MediaList[selectMediaIndex];
  318. uxFileViewer.Children.RemoveAt(selectMediaIndex);
  319. frontPageJson.MediaList.RemoveAt(selectMediaIndex);
  320. selectMediaIndex = selectMediaIndex + 1;
  321. uxFileViewer.Children.Insert(selectMediaIndex, bdTemp);
  322. frontPageJson.MediaList.Insert(selectMediaIndex, stTemp);
  323. bdTemp.Focus();
  324. Setting.frontPageJsonFile = frontPageJson;
  325. }
  326. }
  327. private void Sc_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  328. {
  329. int newIndex = uxFileViewer.Children.IndexOf((Border)sender);
  330. if (selectMediaIndex == newIndex)
  331. {
  332. return;
  333. }
  334. else
  335. {
  336. Border bd;
  337. if (selectMediaIndex != -1)
  338. {
  339. bd = uxFileViewer.Children[selectMediaIndex] as Border;
  340. bd.BorderBrush = new SolidColorBrush(Colors.Gray);
  341. }
  342. selectMediaIndex = newIndex;
  343. bd = uxFileViewer.Children[selectMediaIndex] as Border;
  344. bd.BorderBrush = new SolidColorBrush(Colors.Blue);
  345. bd.Focus();
  346. }
  347. }
  348. }
  349. }