Пример #1
0
        public void CreateViews()
        {
            var windows = 0;

            _views.Clear();
            _viewModels.Clear();
            foreach (var window in _windows)
            {
                window.AllowClose = true;
                window.Hide();
            }

            EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, (IntPtr hMonitor, IntPtr hdcMonitor, ref Rect rect, IntPtr data) =>
            {
                if (Settings.Monitor == null || Settings.Monitor == windows)
                {
                    var info = new MonitorInfo();
                    info.Init();
                    GetMonitorInfo(hMonitor, ref info);

                    var viewModel = Settings.WallpaperType == WallpaperType.SystemInformation ? new SystemInformationViewModel(info, Settings) :
                                    Settings.WallpaperType == WallpaperType.Video ? new VideoViewModel(info, Settings) :
                                    Settings.WallpaperType == WallpaperType.Image ? new ImageViewModel(info, Settings) :
                                    (BaseViewModel) new GalleryViewModel(info, Settings);
                    var view = Settings.WallpaperType == WallpaperType.SystemInformation ? new SystemInformationView() :
                               Settings.WallpaperType == WallpaperType.Video ? new VideoView() :
                               Settings.WallpaperType == WallpaperType.Image ? new ImageView() :
                               (UserControl) new GalleryView();
                    var mainWindow = new MainWindow
                    {
                        Left        = info.rcWork.Left,
                        Top         = info.rcWork.Top,
                        Width       = info.rcWork.Width,
                        Height      = info.rcWork.Height,
                        DataContext = viewModel,
                    };
                    mainWindow.GridContainer.Children.Add(view);
                    _viewModels.Add(viewModel);
                    _views.Add(view);

                    mainWindow.Show();
                    _windows.Add(mainWindow);

                    if (Settings.WallpaperType == WallpaperType.Video && Settings.VideoAutoPlay)
                    {
                        VideoPlay();
                    }
                }

                windows++;
                return(true);
            }, IntPtr.Zero);

            foreach (var window in _windows)
            {
                if (window.AllowClose)
                {
                    window.Close();
                }
            }
            _windows.RemoveAll(x => x.AllowClose == true);
        }
Пример #2
0
 public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfo info);
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (Settings.WindowPreviouseHandle != null)
            {
                var handle = new IntPtr(Settings.WindowPreviouseHandle.Value);
                SetParent(handle, IntPtr.Zero);
                if (!Settings.WindowPreviouseExTool)
                {
                    EnableExToolWindow(handle, false);
                }

                SetWindowPos(handle, new IntPtr(HWND_TOP), 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
                Settings.WindowPreviouseHandle = null;
                Settings.WindowPreviouseExTool = false;
                RefreshDesktop();
                Thread.Sleep(1000);
                SetParent(handle, IntPtr.Zero);
            }

            if (Settings.WindowHandle != null)
            {
                var handle = new IntPtr(Settings.WindowHandle.Value);
                SetParent(handle, IntPtr.Zero);
                if (!Settings.WindowExTool)
                {
                    EnableExToolWindow(handle, false);
                }
                SetWindowPos(handle, new IntPtr(HWND_TOP), 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
                RefreshDesktop();
                Thread.Sleep(1000);
                SetParent(handle, IntPtr.Zero);
            }

            var rect = new Native.Rect();

            if (Settings.WallpaperType == WallpaperType.Window)
            {
                Hide();
                var handle = Settings.WindowHandle == null ? IntPtr.Zero : new IntPtr(Settings.WindowHandle.Value);
                if (handle == IntPtr.Zero)
                {
                    RefreshDesktop();
                    return;
                }
                SendMessageToProgman();
                EnableExToolWindow(handle, true);
                var workerWHandle = GetWorkerW(handle);
                if (Settings.WindowFullScreen)
                {
                    SetWindowPos(handle, (IntPtr)1, MonitorRect.Left, MonitorRect.Top, MonitorRect.Width, MonitorRect.Height, 0 | 0x0010);
                }
                else
                {
                    // Move a window to new monitor if nessesary
                    var currentMonitorHandle = MonitorFromWindow(handle, MONITOR_DEFAULTTONEAREST);
                    var currentMonitorInfo   = new MonitorInfo();
                    currentMonitorInfo.Init();

                    GetMonitorInfo(currentMonitorHandle, ref currentMonitorInfo);
                    var currentMonitorRect = currentMonitorInfo.rcMonitor;

                    GetWindowRect(handle, out Native.Rect windowRect);
                    var left = MonitorRect.Left + windowRect.Left - currentMonitorRect.Left;
                    var top  = MonitorRect.Top + windowRect.Top - currentMonitorRect.Top;
                    MoveWindow(handle, left, top, windowRect.Width, windowRect.Height, true);

                    // Alignment a window
                    switch (Settings.WindowAlignment)
                    {
                    case WindowAlignment.TopLeft:
                    {
                        left = MonitorRect.Left;
                        top  = MonitorRect.Top;
                    }
                    break;

                    case WindowAlignment.TopCenter:
                    {
                        left = MonitorRect.Left + ((MonitorRect.Width - windowRect.Width) / 2);
                        top  = MonitorRect.Top;
                    }
                    break;

                    case WindowAlignment.TopRight:
                    {
                        left = MonitorRect.Left + (MonitorRect.Width - windowRect.Width);
                        top  = MonitorRect.Top;
                    }
                    break;

                    case WindowAlignment.MiddleLeft:
                    {
                        left = MonitorRect.Left;
                        top  = MonitorRect.Top + ((MonitorRect.Height - windowRect.Height) / 2);
                    }
                    break;

                    case WindowAlignment.MiddleCenter:
                    {
                        left = MonitorRect.Left + ((MonitorRect.Width - windowRect.Width) / 2);
                        top  = MonitorRect.Top + ((MonitorRect.Height - windowRect.Height) / 2);
                    }
                    break;

                    case WindowAlignment.MiddleRight:
                    {
                        left = MonitorRect.Left + (MonitorRect.Width - windowRect.Width);
                        top  = MonitorRect.Top + ((MonitorRect.Height - windowRect.Height) / 2);
                    }
                    break;

                    case WindowAlignment.BottomLeft:
                    {
                        left = MonitorRect.Left;
                        top  = MonitorRect.Top + (MonitorRect.Height - windowRect.Height);
                    }
                    break;

                    case WindowAlignment.BottomCenter:
                    {
                        left = MonitorRect.Left + ((MonitorRect.Width - windowRect.Width) / 2);
                        top  = MonitorRect.Top + (MonitorRect.Height - windowRect.Height);
                    }
                    break;

                    case WindowAlignment.BottomRight:
                    {
                        left = MonitorRect.Left + (MonitorRect.Width - windowRect.Width);
                        top  = MonitorRect.Top + (MonitorRect.Height - windowRect.Height);
                    }
                    break;
                    }
                    MoveWindow(handle, left, top, windowRect.Width, windowRect.Height, true);
                }
                SetParent(handle, workerWHandle);
                RefreshDesktop();
            }
            else
            {
                Settings.ClearWindow();
                var handle = new WindowInteropHelper(this).Handle;
                SendMessageToProgman();
                EnableExToolWindow(handle, true);
                var workerWHandle = GetWorkerW(handle);
                SetWindowPos(handle, (IntPtr)1, MonitorRect.Left, MonitorRect.Top, MonitorRect.Width, MonitorRect.Height, 0 | 0x0010);
                MapWindowPoints(handle, workerWHandle, ref rect, 2);
                SetParent(handle, workerWHandle);
                SetWindowPos(handle, (IntPtr)1, rect.Left, rect.Top, MonitorRect.Width, MonitorRect.Height, 0 | 0x0010);
                var imageBrush = new ImageBrush();
                using (var originalBitmap = CaptureWindow(workerWHandle, rect.Left, rect.Top, MonitorRect.Width, MonitorRect.Height))
                    using (var bitmap = ConvertBitmap(originalBitmap, Settings.BackgroundMode))
                    {
                        imageBrush.ImageSource = bitmap.ConvertToBitmapSource();
                        Background             = imageBrush;
                    }
                RefreshDesktop();
                var hwndSource = HwndSource.FromHwnd(handle);
                hwndSource.AddHook(WindowProc);
            }
        }