Restore SEBPatch

This commit is contained in:
2025-06-01 11:44:20 +02:00
commit 8c656e3137
1297 changed files with 142172 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
/*
* 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.Input;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.UserInterface.Contracts.Shell;
using SafeExamBrowser.UserInterface.Contracts.Shell.Events;
using SafeExamBrowser.WindowsApi.Contracts;
using SafeExamBrowser.WindowsApi.Contracts.Events;
namespace SafeExamBrowser.UserInterface.Shared.Activators
{
public class ActionCenterKeyboardActivator : KeyboardActivator, IActionCenterActivator
{
private readonly ILogger logger;
private bool A, LeftWindows;
public event ActivatorEventHandler Activated { add { } remove { } }
public event ActivatorEventHandler Deactivated { add { } remove { } }
public event ActivatorEventHandler Toggled;
public ActionCenterKeyboardActivator(ILogger logger, INativeMethods nativeMethods) : base(nativeMethods)
{
this.logger = logger;
}
protected override void OnBeforeResume()
{
A = false;
LeftWindows = false;
}
protected override bool Process(Key key, KeyModifier modifier, KeyState state)
{
var changed = false;
var pressed = state == KeyState.Pressed;
switch (key)
{
case Key.A:
changed = A != pressed;
A = pressed;
break;
case Key.LWin:
changed = LeftWindows != pressed;
LeftWindows = pressed;
break;
}
if (A && LeftWindows && changed)
{
logger.Debug("Detected toggle sequence for action center.");
Toggled?.Invoke();
}
return false;
}
}
}

View File

@@ -0,0 +1,90 @@
/*
* 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.Threading.Tasks;
using System.Windows;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.UserInterface.Contracts.Shell;
using SafeExamBrowser.UserInterface.Contracts.Shell.Events;
using SafeExamBrowser.WindowsApi.Contracts;
using SafeExamBrowser.WindowsApi.Contracts.Events;
namespace SafeExamBrowser.UserInterface.Shared.Activators
{
public class ActionCenterTouchActivator : TouchActivator, IActionCenterActivator
{
private const int ACTIVATION_INTERVAL = 100;
private bool isDown;
private ILogger logger;
private INativeMethods nativeMethods;
public event ActivatorEventHandler Activated;
public event ActivatorEventHandler Deactivated { add { } remove { } }
public event ActivatorEventHandler Toggled { add { } remove { } }
public ActionCenterTouchActivator(ILogger logger, INativeMethods nativeMethods) : base(nativeMethods)
{
this.logger = logger;
this.nativeMethods = nativeMethods;
}
protected override void OnBeforeResume()
{
isDown = false;
}
protected override bool Process(MouseButton button, MouseButtonState state, MouseInformation info)
{
var inActivationArea = 0 < info.X && info.X < SystemParameters.PrimaryScreenWidth * 0.1;
if (button == MouseButton.Left)
{
if (state == MouseButtonState.Released)
{
isDown = false;
}
if (state == MouseButtonState.Pressed && inActivationArea)
{
isDown = true;
Task.Delay(ACTIVATION_INTERVAL).ContinueWith(async _ =>
{
for (var t = 0; t < 4; t++)
{
if (WasActivated())
{
break;
}
await Task.Delay(ACTIVATION_INTERVAL);
}
});
}
}
return false;
}
private bool WasActivated()
{
var (x, y) = nativeMethods.GetCursorPosition();
var hasMoved = x > SystemParameters.PrimaryScreenWidth * 0.1;
var activate = isDown && hasMoved;
if (activate)
{
logger.Debug("Detected activation gesture for action center.");
Activated?.Invoke();
}
return activate;
}
}
}

View File

@@ -0,0 +1,72 @@
/*
* 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.Windows.Input;
using SafeExamBrowser.WindowsApi.Contracts;
using SafeExamBrowser.WindowsApi.Contracts.Events;
namespace SafeExamBrowser.UserInterface.Shared.Activators
{
public abstract class KeyboardActivator
{
private Guid? hookId;
private INativeMethods nativeMethods;
private bool paused;
protected KeyboardActivator(INativeMethods nativeMethods)
{
this.nativeMethods = nativeMethods;
}
protected abstract bool Process(Key key, KeyModifier modifier, KeyState state);
public void Pause()
{
OnBeforePause();
paused = true;
}
public void Resume()
{
OnBeforeResume();
paused = false;
}
public void Start()
{
hookId = nativeMethods.RegisterKeyboardHook(KeyboardHookCallback);
}
public void Stop()
{
if (hookId.HasValue)
{
nativeMethods.DeregisterKeyboardHook(hookId.Value);
}
}
protected virtual void OnBeforePause()
{
}
protected virtual void OnBeforeResume()
{
}
private bool KeyboardHookCallback(int keyCode, KeyModifier modifier, KeyState state)
{
if (!paused)
{
return Process(KeyInterop.KeyFromVirtualKey(keyCode), modifier, state);
}
return false;
}
}
}

View File

@@ -0,0 +1,122 @@
/*
* 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.Input;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.UserInterface.Contracts.Shell;
using SafeExamBrowser.UserInterface.Contracts.Shell.Events;
using SafeExamBrowser.WindowsApi.Contracts;
using SafeExamBrowser.WindowsApi.Contracts.Events;
namespace SafeExamBrowser.UserInterface.Shared.Activators
{
public class TaskviewKeyboardActivator : KeyboardActivator, ITaskviewActivator
{
private bool Activated, LeftShift, Tab;
private ILogger logger;
public event ActivatorEventHandler Deactivated;
public event ActivatorEventHandler NextActivated;
public event ActivatorEventHandler PreviousActivated;
public TaskviewKeyboardActivator(ILogger logger, INativeMethods nativeMethods) : base(nativeMethods)
{
this.logger = logger;
}
protected override void OnBeforePause()
{
if (Activated)
{
logger.Debug("Auto-deactivation.");
Deactivated?.Invoke();
}
Activated = false;
}
protected override void OnBeforeResume()
{
Activated = false;
LeftShift = false;
Tab = false;
}
protected override bool Process(Key key, KeyModifier modifier, KeyState state)
{
/*
if (IsDeactivation(modifier))
{
return false;
}
if (IsActivation(key, modifier, state))
{
return true;
}
*/
return false;
}
private bool IsActivation(Key key, KeyModifier modifier, KeyState state)
{
var changed = false;
var pressed = state == KeyState.Pressed && modifier.HasFlag(KeyModifier.Alt);
switch (key)
{
case Key.Tab:
changed = Tab != pressed;
Tab = pressed;
break;
case Key.LeftShift:
changed = LeftShift != pressed;
LeftShift = pressed;
break;
}
var isActivation = Tab && changed;
if (isActivation)
{
Activated = true;
if (LeftShift)
{
logger.Debug("Detected sequence for previous instance.");
PreviousActivated?.Invoke();
}
else
{
logger.Debug("Detected sequence for next instance.");
NextActivated?.Invoke();
}
}
return isActivation;
}
private bool IsDeactivation(KeyModifier modifier)
{
var isDeactivation = Activated && !modifier.HasFlag(KeyModifier.Alt);
if (isDeactivation)
{
Activated = false;
LeftShift = false;
Tab = false;
logger.Debug("Detected deactivation sequence.");
Deactivated?.Invoke();
}
return isDeactivation;
}
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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.Input;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.UserInterface.Contracts.Shell;
using SafeExamBrowser.UserInterface.Contracts.Shell.Events;
using SafeExamBrowser.WindowsApi.Contracts;
using SafeExamBrowser.WindowsApi.Contracts.Events;
namespace SafeExamBrowser.UserInterface.Shared.Activators
{
public class TaskbarKeyboardActivator : KeyboardActivator, ITaskbarActivator
{
private readonly ILogger logger;
private bool LeftWindows;
public event ActivatorEventHandler Activated;
public TaskbarKeyboardActivator(ILogger logger, INativeMethods nativeMethods) : base(nativeMethods)
{
this.logger = logger;
}
protected override bool Process(Key key, KeyModifier modifier, KeyState state)
{
/*
var changed = false;
var pressed = state == KeyState.Pressed;
if (key == Key.LWin)
{
changed = LeftWindows != pressed;
LeftWindows = pressed;
}
if (LeftWindows && changed)
{
logger.Debug("Detected activation sequence for taskbar.");
Activated?.Invoke();
}
*/
return false;
}
}
}

View File

@@ -0,0 +1,69 @@
/*
* 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.Input;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.UserInterface.Contracts.Shell;
using SafeExamBrowser.UserInterface.Contracts.Shell.Events;
using SafeExamBrowser.WindowsApi.Contracts;
using SafeExamBrowser.WindowsApi.Contracts.Events;
namespace SafeExamBrowser.UserInterface.Shared.Activators
{
public class TerminationActivator : KeyboardActivator, ITerminationActivator
{
private bool Q, LeftCtrl, RightCtrl;
private ILogger logger;
public event ActivatorEventHandler Activated;
public TerminationActivator(ILogger logger, INativeMethods nativeMethods) : base(nativeMethods)
{
this.logger = logger;
}
protected override void OnBeforeResume()
{
Q = false;
LeftCtrl = false;
RightCtrl = false;
}
protected override bool Process(Key key, KeyModifier modifier, KeyState state)
{
var changed = false;
var pressed = state == KeyState.Pressed;
switch (key)
{
case Key.Q:
changed = Q != pressed;
Q = pressed;
break;
case Key.LeftCtrl:
changed = LeftCtrl != pressed;
LeftCtrl = pressed;
break;
case Key.RightCtrl:
changed = RightCtrl != pressed;
RightCtrl = pressed;
break;
}
if (Q && (LeftCtrl || RightCtrl) && changed && !modifier.HasFlag(KeyModifier.Alt))
{
logger.Debug("Detected termination sequence.");
Activated?.Invoke();
return true;
}
return false;
}
}
}

View 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 SafeExamBrowser.WindowsApi.Contracts;
using SafeExamBrowser.WindowsApi.Contracts.Events;
namespace SafeExamBrowser.UserInterface.Shared.Activators
{
public abstract class TouchActivator
{
private Guid? hookId;
private INativeMethods nativeMethods;
protected bool paused;
protected TouchActivator(INativeMethods nativeMethods)
{
this.nativeMethods = nativeMethods;
}
protected abstract void OnBeforeResume();
protected abstract bool Process(MouseButton button, MouseButtonState state, MouseInformation info);
public void Pause()
{
paused = true;
}
public void Resume()
{
OnBeforeResume();
paused = false;
}
public void Start()
{
hookId = nativeMethods.RegisterMouseHook(MouseHookCallback);
}
public void Stop()
{
if (hookId.HasValue)
{
nativeMethods.DeregisterMouseHook(hookId.Value);
}
}
private bool MouseHookCallback(MouseButton button, MouseButtonState state, MouseInformation info)
{
if (!paused && info.IsTouch)
{
return Process(button, state, info);
}
return false;
}
}
}

View File

@@ -0,0 +1,51 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SafeExamBrowser.UserInterface.Shared")]
[assembly: AssemblyDescription("Safe Exam Browser")]
[assembly: AssemblyCompany("ETH Zürich")]
[assembly: AssemblyProduct("SafeExamBrowser.UserInterface.Shared")]
[assembly: AssemblyCopyright("Copyright © 2024 ETH Zürich, IT Services")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
//In order to begin building localizable applications, set
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>. For example, if you are using US english
//in your source files, set the <UICulture> to en-US. Then uncomment
//the NeutralResourceLanguage attribute below. Update the "en-US" in
//the line below to match the UICulture setting in the project file.
//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.8.0.742")]
[assembly: AssemblyFileVersion("3.8.0.742")]
[assembly: AssemblyInformationalVersion("1.0.0.0")]

View File

@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{38525928-87BA-4F8C-8010-4EB97BFAAE13}</ProjectGuid>
<OutputType>library</OutputType>
<RootNamespace>SafeExamBrowser.UserInterface.Shared</RootNamespace>
<AssemblyName>SafeExamBrowser.UserInterface.Shared</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<Compile Include="Activators\ActionCenterKeyboardActivator.cs" />
<Compile Include="Activators\ActionCenterTouchActivator.cs" />
<Compile Include="Activators\KeyboardActivator.cs" />
<Compile Include="Activators\TaskbarKeyboardActivator.cs" />
<Compile Include="Activators\TaskviewKeyboardActivator.cs" />
<Compile Include="Activators\TerminationActivator.cs" />
<Compile Include="Activators\TouchActivator.cs" />
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Utilities\IconLoader.cs" />
<Compile Include="Utilities\IconResourceLoader.cs" />
<Compile Include="Utilities\Parser.cs" />
<Compile Include="Utilities\Thumbnail.cs" />
<Compile Include="Utilities\VisualExtensions.cs" />
<Compile Include="Utilities\WindowExtensions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SafeExamBrowser.Core.Contracts\SafeExamBrowser.Core.Contracts.csproj">
<Project>{fe0e1224-b447-4b14-81e7-ed7d84822aa0}</Project>
<Name>SafeExamBrowser.Core.Contracts</Name>
</ProjectReference>
<ProjectReference Include="..\SafeExamBrowser.Logging.Contracts\SafeExamBrowser.Logging.Contracts.csproj">
<Project>{64ea30fb-11d4-436a-9c2b-88566285363e}</Project>
<Name>SafeExamBrowser.Logging.Contracts</Name>
</ProjectReference>
<ProjectReference Include="..\SafeExamBrowser.UserInterface.Contracts\SafeExamBrowser.UserInterface.Contracts.csproj">
<Project>{c7889e97-6ff6-4a58-b7cb-521ed276b316}</Project>
<Name>SafeExamBrowser.UserInterface.Contracts</Name>
</ProjectReference>
<ProjectReference Include="..\SafeExamBrowser.WindowsApi.Contracts\SafeExamBrowser.WindowsApi.Contracts.csproj">
<Project>{7016f080-9aa5-41b2-a225-385ad877c171}</Project>
<Name>SafeExamBrowser.WindowsApi.Contracts</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View 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;
};
}
}

View File

@@ -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
};
}
}
}

View 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;
}
}
}

View 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;
}
}
}

View 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.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));
}
}
}

View File

@@ -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);
}
}