UcFrontPage.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. uxAutoPlaIntervalText.Text = frontPageJson.AutoPlayIntervalSec.ToString();
  92. }
  93. private void SaveData()
  94. {
  95. Setting.frontPageJsonFile = frontPageJson;
  96. }
  97. private void uxSolarUrlText_TextChanged(object sender, TextChangedEventArgs e)
  98. {
  99. if (this.IsLoaded)
  100. {
  101. uxSolarEnergyUrlText.TextChanged -= uxSolarUrlText_TextChanged;
  102. frontPageJson.SolarEnergyUrl = uxSolarEnergyUrlText.Text;
  103. SaveData();
  104. uxSolarEnergyUrlText.TextChanged += uxSolarUrlText_TextChanged;
  105. }
  106. }
  107. private void uxAutoPlaIntervalText_TextChanged(object sender, TextChangedEventArgs e)
  108. {
  109. if (this.IsLoaded)
  110. {
  111. uxAutoPlaIntervalText.TextChanged -= uxSolarUrlText_TextChanged;
  112. if (int.TryParse(uxAutoPlaIntervalText.Text, out var interval))
  113. {
  114. frontPageJson.AutoPlayIntervalSec = interval;
  115. SaveData();
  116. }
  117. uxAutoPlaIntervalText.TextChanged += uxSolarUrlText_TextChanged;
  118. }
  119. }
  120. private void uxAddBtn_Click(object sender, RoutedEventArgs e)
  121. {
  122. if (uxFileViewer.Children.Count >= avalibleTotalElement)
  123. {
  124. MessageBox.Show("容許數量已滿");
  125. return;
  126. }
  127. OpenFileDialog openfileDlg = new OpenFileDialog();
  128. openfileDlg.Multiselect = true;
  129. openfileDlg.Filter = "Supported files|";
  130. for (int i = 0; i < GlobalFunction.imgDimList.Count(); i++)
  131. {
  132. openfileDlg.Filter += "*" + GlobalFunction.imgDimList[i] + ";";
  133. }
  134. for (int i = 0; i < GlobalFunction.mediaDimList.Count(); i++)
  135. {
  136. openfileDlg.Filter += "*" + GlobalFunction.mediaDimList[i] + ";";
  137. }
  138. //openfileDlg.Filter += "*" + ".PPT" + ";";
  139. //openfileDlg.Filter += "*" + ".PPTX" + ";";
  140. if ((bool)openfileDlg.ShowDialog())
  141. {
  142. Image imageBuf;
  143. Grid gridBuf;
  144. foreach (string name in openfileDlg.FileNames)
  145. {
  146. //string[] imageList = mediaLists[mediaIndex];
  147. 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 };
  148. bd.Focusable = true;
  149. bd.PreviewMouseLeftButtonUp += Sc_PreviewMouseLeftButtonUp;
  150. if (GlobalFunction.CheckFileIsImage(name))
  151. {
  152. //image
  153. string legalFileName = GlobalFunction.GetFolderFileName(frontpageDataPath, name);
  154. string fileTo = System.IO.Path.Combine(frontpageDataPath, legalFileName);
  155. //複製
  156. //File.Copy(name, fileTo);
  157. ReduceImageResolution.CheckImageResolution(name, fileTo, 1920, 1080);
  158. //json
  159. frontPageJson.MediaList.Add(legalFileName);
  160. //ux - bd
  161. imageBuf = new Image();
  162. //imageBuf.Source = new BitmapImage(new Uri(fileTo));
  163. imageBuf.Source = GlobalFunction.GetBitMap(fileTo);
  164. imageBuf.Stretch = Stretch.Uniform;
  165. gridBuf = new Grid();
  166. gridBuf.Children.Add(imageBuf);
  167. gridBuf.Width = 213;
  168. gridBuf.Height = 120;
  169. bd.Child = gridBuf;
  170. uxFileViewer.Children.Add(bd);
  171. }
  172. else if (GlobalFunction.CheckFileIsVideo(name))
  173. {
  174. string legalFileName = GlobalFunction.GetFolderFileName(frontpageDataPath, name);
  175. string fileTo = System.IO.Path.Combine(frontpageDataPath, legalFileName);
  176. //複製
  177. File.Copy(name, fileTo);
  178. //json - mediafile
  179. frontPageJson.MediaList.Add(legalFileName);
  180. //ux - bd
  181. ShellFile shellFile = ShellFile.FromFilePath(fileTo);
  182. imageBuf = new Image();
  183. if (shellFile != null && shellFile.Thumbnail != null && shellFile.Thumbnail.ExtraLargeBitmapSource != null)
  184. imageBuf.Source = shellFile.Thumbnail.ExtraLargeBitmapSource;
  185. imageBuf.Stretch = Stretch.Uniform;
  186. gridBuf = new Grid();
  187. gridBuf.Children.Add(imageBuf);
  188. gridBuf.Children.Add(new Image() { Source = new BitmapImage(new Uri("pack://application:,,,/BellwetherBackend;component/Image/play.png")), Width = 80 });
  189. gridBuf.Width = 213;
  190. gridBuf.Height = 120;
  191. bd.Child = gridBuf;
  192. uxFileViewer.Children.Add(bd);
  193. }
  194. else if (GlobalFunction.pptDimList.Contains(System.IO.Path.GetExtension(name).ToLower()))
  195. {
  196. string legalFileName;
  197. string fileTo;
  198. string tempPath = System.Windows.Forms.Application.StartupPath + @"\Temp"; //暫存資料夾路徑
  199. string testResImgFile = tempPath + "\\testResolution.BMP";
  200. Microsoft.Office.Interop.PowerPoint.Application pptApplication =
  201. new Microsoft.Office.Interop.PowerPoint.Application();
  202. Microsoft.Office.Interop.PowerPoint.Presentation pptPresentation =
  203. pptApplication.Presentations.Open(name, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse);
  204. //lb_noimage.Refresh();
  205. // Export the first page to retrieve its resolution
  206. pptPresentation.Slides[1].Export(testResImgFile, "BMP");
  207. BitmapImage directImage = new BitmapImage();
  208. directImage.BeginInit();
  209. directImage.UriSource = new Uri(testResImgFile);
  210. directImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
  211. directImage.CacheOption = BitmapCacheOption.OnLoad;
  212. directImage.EndInit();
  213. int width = directImage.PixelWidth;
  214. int height = directImage.PixelHeight;
  215. // Resolution adaptation
  216. if (directImage.PixelWidth > directImage.PixelHeight)
  217. {
  218. // landscape (1600*900)
  219. width = 1600;
  220. height = 900;
  221. }
  222. else
  223. {
  224. // portrait (900*1600)
  225. width = 900;
  226. height = 1600;
  227. }
  228. double bestResFactor = Math.Min((double)width / directImage.PixelWidth, (double)height / directImage.PixelHeight);
  229. width = (int)(directImage.PixelWidth * bestResFactor);
  230. height = (int)(directImage.PixelHeight * bestResFactor);
  231. int slidesCount = pptPresentation.Slides.Count;
  232. for (int idx = 1; idx <= slidesCount; idx++)
  233. {
  234. 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 };
  235. bd.PreviewMouseLeftButtonUp += Sc_PreviewMouseLeftButtonUp;
  236. legalFileName = GlobalFunction.GetFolderFileName(frontpageDataPath, "1.BMP");
  237. fileTo = System.IO.Path.Combine(frontpageDataPath, legalFileName);// GlobalFunction.rootPath + GlobalFunction.dataFolder + GlobalFunction.multiMediaFolderName + multiMediaJsonData.folderColletion[currentFolderIndex].folderName + @"\" + legalFileName;
  238. //json - mediafile
  239. frontPageJson.MediaList.Add(legalFileName);
  240. fileTo = System.IO.Path.Combine(Directory.GetCurrentDirectory(), fileTo);
  241. pptPresentation.Slides[idx].Export(fileTo, "BMP", width, height);
  242. //ux - bd
  243. imageBuf = new Image();
  244. //imageBuf.Source = new BitmapImage(new Uri(fileTo));
  245. imageBuf.Source = GlobalFunction.GetBitMap(fileTo);
  246. imageBuf.Stretch = Stretch.Uniform;
  247. gridBuf = new Grid();
  248. gridBuf.Children.Add(imageBuf);
  249. gridBuf.Width = 213;
  250. gridBuf.Height = 120;
  251. bd.Child = gridBuf;
  252. uxFileViewer.Children.Add(bd);
  253. }
  254. pptPresentation.Close();
  255. System.IO.File.Delete(testResImgFile);
  256. }
  257. }
  258. if (selectMediaIndex != -1)
  259. {
  260. ((Border)uxFileViewer.Children[selectMediaIndex]).BorderBrush = new SolidColorBrush(Colors.Gray);
  261. }
  262. selectMediaIndex = uxFileViewer.Children.Count - 1;
  263. ((Border)uxFileViewer.Children[selectMediaIndex]).BorderBrush = new SolidColorBrush(Colors.Blue);
  264. ((Border)uxFileViewer.Children[selectMediaIndex]).Focus();
  265. }
  266. Setting.frontPageJsonFile = frontPageJson;
  267. }
  268. private void uxDeleteBtn_Click(object sender, RoutedEventArgs e)
  269. {
  270. if (selectMediaIndex < 0)
  271. {
  272. return;
  273. }
  274. uxFileViewer.Children.RemoveAt(selectMediaIndex);
  275. string path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), frontPageJson.MediaList[selectMediaIndex]);
  276. 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]);
  277. frontPageJson.MediaList.RemoveAt(selectMediaIndex);
  278. //remove items
  279. if (frontPageJson.MediaList.Count == 0)
  280. {
  281. selectMediaIndex = -1;
  282. }
  283. else
  284. {
  285. if (selectMediaIndex > (frontPageJson.MediaList.Count - 1))
  286. {
  287. selectMediaIndex = frontPageJson.MediaList.Count - 1;
  288. }
  289. else
  290. {
  291. //selectMediaIndex = selectMediaIndex;
  292. }
  293. Border gd = uxFileViewer.Children[selectMediaIndex] as Border;
  294. gd.BorderBrush = new SolidColorBrush(Colors.Blue);
  295. }
  296. Setting.frontPageJsonFile = frontPageJson;
  297. }
  298. private void uxLeftBtn_Click(object sender, RoutedEventArgs e)
  299. {
  300. if (selectMediaIndex > 0)
  301. {
  302. Border bdTemp = uxFileViewer.Children[selectMediaIndex] as Border;
  303. string stTemp = frontPageJson.MediaList[selectMediaIndex];
  304. uxFileViewer.Children.RemoveAt(selectMediaIndex);
  305. frontPageJson.MediaList.RemoveAt(selectMediaIndex);
  306. selectMediaIndex = selectMediaIndex - 1;
  307. uxFileViewer.Children.Insert(selectMediaIndex, bdTemp);
  308. frontPageJson.MediaList.Insert(selectMediaIndex, stTemp);
  309. bdTemp.Focus();
  310. Setting.frontPageJsonFile = frontPageJson;
  311. }
  312. }
  313. private void uxRightBtn_Click(object sender, RoutedEventArgs e)
  314. {
  315. if (selectMediaIndex != (uxFileViewer.Children.Count - 1) && selectMediaIndex >= 0)
  316. {
  317. Border bdTemp = uxFileViewer.Children[selectMediaIndex] as Border;
  318. string stTemp = frontPageJson.MediaList[selectMediaIndex];
  319. uxFileViewer.Children.RemoveAt(selectMediaIndex);
  320. frontPageJson.MediaList.RemoveAt(selectMediaIndex);
  321. selectMediaIndex = selectMediaIndex + 1;
  322. uxFileViewer.Children.Insert(selectMediaIndex, bdTemp);
  323. frontPageJson.MediaList.Insert(selectMediaIndex, stTemp);
  324. bdTemp.Focus();
  325. Setting.frontPageJsonFile = frontPageJson;
  326. }
  327. }
  328. private void Sc_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  329. {
  330. int newIndex = uxFileViewer.Children.IndexOf((Border)sender);
  331. if (selectMediaIndex == newIndex)
  332. {
  333. return;
  334. }
  335. else
  336. {
  337. Border bd;
  338. if (selectMediaIndex != -1)
  339. {
  340. bd = uxFileViewer.Children[selectMediaIndex] as Border;
  341. bd.BorderBrush = new SolidColorBrush(Colors.Gray);
  342. }
  343. selectMediaIndex = newIndex;
  344. bd = uxFileViewer.Children[selectMediaIndex] as Border;
  345. bd.BorderBrush = new SolidColorBrush(Colors.Blue);
  346. bd.Focus();
  347. }
  348. }
  349. }
  350. }