view PreviewHandlerWPF/PreviewHandlerWPF/ListManager.cs @ 88:e84dc4926a5a

OSL work from 2011
author stevenhollidge <stevenhollidge@hotmail.com>
date Fri, 27 Apr 2012 08:44:37 +0100
parents
children
line wrap: on
line source

using System;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interop;

namespace PreviewHandlerWPF
{
    internal class ListManager : ThreadSafeObservableCollection<string>
    {
        private readonly string dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

        public ListManager()
        {
            var fsw = new FileSystemWatcher(dir);
            fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite;
            fsw.Created += fsw_Created;
            fsw.Deleted += fsw_Deleted;

            fsw.EnableRaisingEvents = true;

            string[] files = Directory.GetFiles(dir);
            for (int i = 0; i < files.Length; i++)
            {
                base.Add(files[i]);
            }
        }

        private void fsw_Deleted(object sender, FileSystemEventArgs e)
        {
            base.Remove(e.FullPath);
        }

        private void fsw_Created(object sender, FileSystemEventArgs e)
        {
            base.Add(e.FullPath);
        }
    }

    internal class FileNameConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string[] v = value.ToString().Split('\\');
            return v[v.Length - 1];
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    internal class WPFPreviewHandler : ContentPresenter
    {
        private Rect actualRect;
        private IntPtr mainWindowHandle = IntPtr.Zero;

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.Property == ActualHeightProperty | e.Property == ActualWidthProperty)
            {
                if (mainWindowHandle == IntPtr.Zero)
                {
                    var hwndSource = PresentationSource.FromVisual(Application.Current.MainWindow) as HwndSource;
                    mainWindowHandle = hwndSource.Handle;
                }
                else
                {
                    Point p0 = TranslatePoint(new Point(), Application.Current.MainWindow);
                    Point p1 = TranslatePoint(new Point(ActualWidth, ActualHeight), Application.Current.MainWindow);
                    actualRect = new Rect(p0, p1);
                    mainWindowHandle.InvalidateAttachedPreview(actualRect);
                }
            }
            if (e.Property == ContentControl.ContentProperty)
            {
                mainWindowHandle.AttachPreview(e.NewValue.ToString(), actualRect);
            }
            base.OnPropertyChanged(e);
        }
    }
}