2019-05-08 11:02:59 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2019-05-08 11:02:59 +02:00
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
{
|
2019-12-06 10:04:22 +01:00
|
|
|
|
public static class WindowExtensions
|
2019-05-08 11:02:59 +02:00
|
|
|
|
{
|
2021-03-23 21:12:47 +01:00
|
|
|
|
private const int GWL_STYLE = -16;
|
2019-05-08 11:02:59 +02:00
|
|
|
|
private const uint MF_BYCOMMAND = 0x00000000;
|
|
|
|
|
private const uint MF_ENABLED = 0x00000000;
|
2024-02-29 21:05:43 +01:00
|
|
|
|
private const uint MF_GRAYED = 0x00000001;
|
2019-05-08 11:02:59 +02:00
|
|
|
|
private const uint SC_CLOSE = 0xF060;
|
2021-06-25 11:51:59 +02:00
|
|
|
|
private const uint SWP_SHOWWINDOW = 0x0040;
|
2021-03-23 21:12:47 +01:00
|
|
|
|
private const int WS_SYSMENU = 0x80000;
|
2019-05-08 11:02:59 +02:00
|
|
|
|
|
2021-06-25 11:51:59 +02:00
|
|
|
|
private static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
|
|
|
|
|
|
2019-12-06 10:04:22 +01:00
|
|
|
|
public static void DisableCloseButton(this Window window)
|
2019-05-08 11:02:59 +02:00
|
|
|
|
{
|
2019-12-06 10:04:22 +01:00
|
|
|
|
var helper = new WindowInteropHelper(window);
|
|
|
|
|
var systemMenu = GetSystemMenu(helper.Handle, false);
|
2019-05-08 11:02:59 +02:00
|
|
|
|
|
|
|
|
|
if (systemMenu != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
EnableMenuItem(systemMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-29 21:05:43 +01:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 21:12:47 +01:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 11:51:59 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 21:12:47 +01:00
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
private static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
|
|
|
|
|
|
2019-05-08 11:02:59 +02:00
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
|
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
2021-03-23 21:12:47 +01:00
|
|
|
|
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
|
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
|
2021-06-25 11:51:59 +02:00
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
|
2019-05-08 11:02:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|