Restore SEBPatch
This commit is contained in:
86
SafeExamBrowser.UserInterface.Shared/Utilities/IconLoader.cs
Normal file
86
SafeExamBrowser.UserInterface.Shared/Utilities/IconLoader.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 2024 ETH Zürich, IT Services
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace SafeExamBrowser.UserInterface.Shared.Utilities
|
||||
{
|
||||
public static class IconLoader
|
||||
{
|
||||
private const int MAX_PATH_CHARS = 260;
|
||||
private const uint SHGFI_ICON = 0x100;
|
||||
private const uint SHGFI_LARGEICON = 0x0;
|
||||
private const uint SHGFI_SMALLICON = 0x1;
|
||||
|
||||
public static ImageSource LoadIconFor(DirectoryInfo directory)
|
||||
{
|
||||
var fileInfo = new SHFILEINFO();
|
||||
var result = SHGetFileInfo(directory.FullName, 0, ref fileInfo, (uint) Marshal.SizeOf(fileInfo), SHGFI_ICON | SHGFI_SMALLICON);
|
||||
var imageSource = Imaging.CreateBitmapSourceFromHIcon(fileInfo.hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
||||
|
||||
DestroyIcon(fileInfo.hIcon);
|
||||
|
||||
return imageSource;
|
||||
}
|
||||
|
||||
public static ImageSource LoadIconFor(FileInfo file)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var icon = Icon.ExtractAssociatedIcon(file.FullName))
|
||||
{
|
||||
return Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Icon.ExtractAssociatedIcon(...) has proven to be error-prone, especially for network paths. Thus we try to use the native API
|
||||
// in those cases, see also https://stackoverflow.com/questions/1842226/how-to-get-the-associated-icon-from-a-network-share-file.
|
||||
var builder = new StringBuilder(MAX_PATH_CHARS);
|
||||
|
||||
builder.Append(file.FullName);
|
||||
|
||||
var handle = ExtractAssociatedIcon(IntPtr.Zero, builder, out _);
|
||||
|
||||
using (var icon = Icon.FromHandle(handle))
|
||||
{
|
||||
return Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int DestroyIcon(IntPtr hIcon);
|
||||
|
||||
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
|
||||
static extern IntPtr ExtractAssociatedIcon(IntPtr hInst, StringBuilder lpIconPath, out ushort lpiIcon);
|
||||
|
||||
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
|
||||
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct SHFILEINFO
|
||||
{
|
||||
public IntPtr hIcon;
|
||||
public IntPtr iIcon;
|
||||
public uint dwAttributes;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
|
||||
public string szDisplayName;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
|
||||
public string szTypeName;
|
||||
};
|
||||
}
|
||||
}
|
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (c) 2024 ETH Zürich, IT Services
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Markup;
|
||||
using System.Windows.Media.Imaging;
|
||||
using SafeExamBrowser.Core.Contracts.Resources.Icons;
|
||||
using Brushes = System.Windows.Media.Brushes;
|
||||
using Image = System.Windows.Controls.Image;
|
||||
|
||||
namespace SafeExamBrowser.UserInterface.Shared.Utilities
|
||||
{
|
||||
public static class IconResourceLoader
|
||||
{
|
||||
public static UIElement Load(IconResource resource)
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (resource)
|
||||
{
|
||||
case BitmapIconResource bitmap:
|
||||
return LoadBitmapResource(bitmap);
|
||||
case EmbeddedIconResource embedded:
|
||||
return LoadEmbeddedResource(embedded);
|
||||
case NativeIconResource native:
|
||||
return LoadNativeResource(native);
|
||||
case XamlIconResource xaml:
|
||||
return LoadXamlResource(xaml);
|
||||
default:
|
||||
throw new NotSupportedException($"Application icon resource of type '{resource.GetType()}' is not supported!");
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return NotFoundSymbol();
|
||||
}
|
||||
}
|
||||
|
||||
private static UIElement LoadBitmapResource(BitmapIconResource resource)
|
||||
{
|
||||
return new Image
|
||||
{
|
||||
Source = new BitmapImage(resource.Uri)
|
||||
};
|
||||
}
|
||||
|
||||
private static UIElement LoadEmbeddedResource(EmbeddedIconResource resource)
|
||||
{
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
var bitmap = new BitmapImage();
|
||||
|
||||
Icon.ExtractAssociatedIcon(resource.FilePath).ToBitmap().Save(stream, ImageFormat.Png);
|
||||
|
||||
bitmap.BeginInit();
|
||||
bitmap.StreamSource = stream;
|
||||
bitmap.CacheOption = BitmapCacheOption.OnLoad;
|
||||
bitmap.EndInit();
|
||||
bitmap.Freeze();
|
||||
|
||||
return new Image
|
||||
{
|
||||
Source = bitmap
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private static UIElement LoadNativeResource(NativeIconResource resource)
|
||||
{
|
||||
return new Image
|
||||
{
|
||||
Source = Imaging.CreateBitmapSourceFromHIcon(resource.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())
|
||||
};
|
||||
}
|
||||
|
||||
private static UIElement LoadXamlResource(XamlIconResource resource)
|
||||
{
|
||||
using (var stream = Application.GetResourceStream(resource.Uri)?.Stream)
|
||||
{
|
||||
return XamlReader.Load(stream) as UIElement;
|
||||
}
|
||||
}
|
||||
|
||||
private static UIElement NotFoundSymbol()
|
||||
{
|
||||
return new TextBlock(new Run("X") { Foreground = Brushes.Red, FontWeight = FontWeights.Bold })
|
||||
{
|
||||
HorizontalAlignment = HorizontalAlignment.Center,
|
||||
VerticalAlignment = VerticalAlignment.Center
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
30
SafeExamBrowser.UserInterface.Shared/Utilities/Parser.cs
Normal file
30
SafeExamBrowser.UserInterface.Shared/Utilities/Parser.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (c) 2024 ETH Zürich, IT Services
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace SafeExamBrowser.UserInterface.Shared.Utilities
|
||||
{
|
||||
public static class Parser
|
||||
{
|
||||
public static bool TryParseBrush(string hexColorCode, out Brush brush)
|
||||
{
|
||||
brush = default;
|
||||
|
||||
try
|
||||
{
|
||||
brush = new BrushConverter().ConvertFromString(hexColorCode) as Brush;
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
return brush != default;
|
||||
}
|
||||
}
|
||||
}
|
63
SafeExamBrowser.UserInterface.Shared/Utilities/Thumbnail.cs
Normal file
63
SafeExamBrowser.UserInterface.Shared/Utilities/Thumbnail.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2024 ETH Zürich, IT Services
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace SafeExamBrowser.UserInterface.Shared.Utilities
|
||||
{
|
||||
/// <remarks>
|
||||
/// See https://docs.microsoft.com/en-us/windows/win32/dwm/thumbnail-ovw.
|
||||
/// </remarks>
|
||||
public static class Thumbnail
|
||||
{
|
||||
public const int DWM_TNP_VISIBLE = 0x8;
|
||||
public const int DWM_TNP_OPACITY = 0x4;
|
||||
public const int DWM_TNP_RECTDESTINATION = 0x1;
|
||||
public const int S_OK = 0;
|
||||
|
||||
[DllImport("dwmapi.dll")]
|
||||
public static extern int DwmQueryThumbnailSourceSize(IntPtr thumbnail, out Size size);
|
||||
|
||||
[DllImport("dwmapi.dll")]
|
||||
public static extern int DwmRegisterThumbnail(IntPtr destinationWindow, IntPtr sourceWindow, out IntPtr thumbnail);
|
||||
|
||||
[DllImport("dwmapi.dll")]
|
||||
public static extern int DwmUnregisterThumbnail(IntPtr thumbnail);
|
||||
|
||||
[DllImport("dwmapi.dll")]
|
||||
public static extern int DwmUpdateThumbnailProperties(IntPtr thumbnail, ref Properties properties);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Properties
|
||||
{
|
||||
public int Flags;
|
||||
public Rectangle Destination;
|
||||
public Rectangle Source;
|
||||
public byte Opacity;
|
||||
public bool Visible;
|
||||
public bool SourceClientAreaOnly;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Size
|
||||
{
|
||||
public int X;
|
||||
public int Y;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Rectangle
|
||||
{
|
||||
public int Left;
|
||||
public int Top;
|
||||
public int Right;
|
||||
public int Bottom;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2024 ETH Zürich, IT Services
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace SafeExamBrowser.UserInterface.Shared.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// WPF works with device-independent pixels. These methods are required to transform
|
||||
/// such values to their absolute, device-specific pixel values and vice versa.
|
||||
///
|
||||
/// Source: https://stackoverflow.com/questions/3286175/how-do-i-convert-a-wpf-size-to-physical-pixels
|
||||
/// </summary>
|
||||
public static class VisualExtensions
|
||||
{
|
||||
public static Vector TransformToPhysical(this Visual visual, double x, double y)
|
||||
{
|
||||
var matrix = default(Matrix);
|
||||
var source = PresentationSource.FromVisual(visual);
|
||||
|
||||
if (source != null)
|
||||
{
|
||||
matrix = source.CompositionTarget.TransformToDevice;
|
||||
}
|
||||
else
|
||||
{
|
||||
using (var newSource = new HwndSource(new HwndSourceParameters()))
|
||||
{
|
||||
matrix = newSource.CompositionTarget.TransformToDevice;
|
||||
}
|
||||
}
|
||||
|
||||
return matrix.Transform(new Vector(x, y));
|
||||
}
|
||||
|
||||
public static Vector TransformFromPhysical(this Visual visual, double x, double y)
|
||||
{
|
||||
var matrix = default(Matrix);
|
||||
var source = PresentationSource.FromVisual(visual);
|
||||
|
||||
if (source != null)
|
||||
{
|
||||
matrix = source.CompositionTarget.TransformFromDevice;
|
||||
}
|
||||
else
|
||||
{
|
||||
using (var newSource = new HwndSource(new HwndSourceParameters()))
|
||||
{
|
||||
matrix = newSource.CompositionTarget.TransformFromDevice;
|
||||
}
|
||||
}
|
||||
|
||||
return matrix.Transform(new Vector(x, y));
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (c) 2024 ETH Zürich, IT Services
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
|
||||
namespace SafeExamBrowser.UserInterface.Shared.Utilities
|
||||
{
|
||||
public static class WindowExtensions
|
||||
{
|
||||
private const int GWL_STYLE = -16;
|
||||
private const uint MF_BYCOMMAND = 0x00000000;
|
||||
private const uint MF_ENABLED = 0x00000000;
|
||||
private const uint MF_GRAYED = 0x00000001;
|
||||
private const uint SC_CLOSE = 0xF060;
|
||||
private const uint SWP_SHOWWINDOW = 0x0040;
|
||||
private const int WS_SYSMENU = 0x80000;
|
||||
|
||||
private static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
|
||||
|
||||
public static void DisableCloseButton(this Window window)
|
||||
{
|
||||
var helper = new WindowInteropHelper(window);
|
||||
var systemMenu = GetSystemMenu(helper.Handle, false);
|
||||
|
||||
if (systemMenu != IntPtr.Zero)
|
||||
{
|
||||
EnableMenuItem(systemMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
|
||||
}
|
||||
}
|
||||
|
||||
public static void EnableCloseButton(this Window window)
|
||||
{
|
||||
var helper = new WindowInteropHelper(window);
|
||||
var systemMenu = GetSystemMenu(helper.Handle, false);
|
||||
|
||||
if (systemMenu != IntPtr.Zero)
|
||||
{
|
||||
EnableMenuItem(systemMenu, SC_CLOSE, MF_BYCOMMAND | MF_ENABLED);
|
||||
}
|
||||
}
|
||||
|
||||
public static void HideCloseButton(this Window window)
|
||||
{
|
||||
var helper = new WindowInteropHelper(window);
|
||||
var style = GetWindowLong(helper.Handle, GWL_STYLE) & ~WS_SYSMENU;
|
||||
|
||||
SetWindowLong(helper.Handle, GWL_STYLE, style);
|
||||
}
|
||||
|
||||
public static void MoveToBackground(this Window window)
|
||||
{
|
||||
var helper = new WindowInteropHelper(window);
|
||||
var x = (int) window.TransformFromPhysical(window.Left, 0).X;
|
||||
var y = (int) window.TransformFromPhysical(0, window.Top).Y;
|
||||
var width = (int) window.TransformFromPhysical(window.Width, 0).X;
|
||||
var height = (int) window.TransformFromPhysical(0, window.Height).Y;
|
||||
|
||||
SetWindowPos(helper.Handle, HWND_BOTTOM, x, y, width, height, SWP_SHOWWINDOW);
|
||||
}
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user