ImageBrowser.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.IO;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using System.Windows.Media.Imaging;
  7. using HandyControl.Data;
  8. using HandyControl.Interactivity;
  9. namespace HandyControl.Controls;
  10. /// <summary>
  11. /// 图片浏览器
  12. /// </summary>
  13. [TemplatePart(Name = ElementPanelTop, Type = typeof(Panel))]
  14. [TemplatePart(Name = ElementImageViewer, Type = typeof(ImageViewer))]
  15. public class ImageBrowser : Window
  16. {
  17. #region Constants
  18. private const string ElementPanelTop = "PART_PanelTop";
  19. private const string ElementImageViewer = "PART_ImageViewer";
  20. #endregion Constants
  21. #region Data
  22. private Panel _panelTop;
  23. private ImageViewer _imageViewer;
  24. #endregion Data
  25. static ImageBrowser()
  26. {
  27. IsFullScreenProperty.AddOwner(typeof(ImageBrowser), new PropertyMetadata(ValueBoxes.FalseBox));
  28. }
  29. public ImageBrowser()
  30. {
  31. CommandBindings.Add(new CommandBinding(ControlCommands.Close, ButtonClose_OnClick));
  32. WindowStartupLocation = WindowStartupLocation.CenterScreen;
  33. WindowStyle = WindowStyle.None;
  34. Topmost = true;
  35. AllowsTransparency = true;
  36. }
  37. /// <summary>
  38. /// 带一个图片Uri的构造函数
  39. /// </summary>
  40. /// <param name="uri"></param>
  41. public ImageBrowser(Uri uri) : this()
  42. {
  43. Loaded += (s, e) =>
  44. {
  45. try
  46. {
  47. _imageViewer.ImageSource = BitmapFrame.Create(uri);
  48. _imageViewer.ImgPath = uri.AbsolutePath;
  49. if (File.Exists(_imageViewer.ImgPath))
  50. {
  51. var info = new FileInfo(_imageViewer.ImgPath);
  52. _imageViewer.ImgSize = info.Length;
  53. }
  54. }
  55. catch
  56. {
  57. MessageBox.Show(Properties.Langs.Lang.ErrorImgPath);
  58. }
  59. };
  60. }
  61. /// <summary>
  62. /// 带一个图片路径的构造函数
  63. /// </summary>
  64. /// <param name="path"></param>
  65. public ImageBrowser(string path) : this(new Uri(path))
  66. {
  67. }
  68. public override void OnApplyTemplate()
  69. {
  70. if (_panelTop != null)
  71. {
  72. _panelTop.MouseLeftButtonDown -= PanelTopOnMouseLeftButtonDown;
  73. }
  74. if (_imageViewer != null)
  75. {
  76. _imageViewer.MouseLeftButtonDown -= ImageViewer_MouseLeftButtonDown;
  77. }
  78. base.OnApplyTemplate();
  79. _panelTop = GetTemplateChild(ElementPanelTop) as Panel;
  80. _imageViewer = GetTemplateChild(ElementImageViewer) as ImageViewer;
  81. if (_panelTop != null)
  82. {
  83. _panelTop.MouseLeftButtonDown += PanelTopOnMouseLeftButtonDown;
  84. }
  85. if (_imageViewer != null)
  86. {
  87. _imageViewer.MouseLeftButtonDown += ImageViewer_MouseLeftButtonDown;
  88. }
  89. }
  90. private void ButtonClose_OnClick(object sender, RoutedEventArgs e) => Close();
  91. private void PanelTopOnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  92. {
  93. if (e.LeftButton == MouseButtonState.Pressed)
  94. {
  95. DragMove();
  96. }
  97. }
  98. private void ImageViewer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  99. {
  100. if (e.LeftButton == MouseButtonState.Pressed && !(_imageViewer.ImageWidth > ActualWidth || _imageViewer.ImageHeight > ActualHeight))
  101. {
  102. DragMove();
  103. }
  104. }
  105. }