Abstracted type RECT used by native Windows API.

This commit is contained in:
dbuechel 2017-08-04 08:25:49 +02:00
parent c9a53397a4
commit 32980f9f97
14 changed files with 101 additions and 28 deletions

View file

@ -0,0 +1,20 @@
/*
* Copyright (c) 2017 ETH Zürich, Educational Development and Technology (LET)
*
* 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 SafeExamBrowser.Contracts.WindowsApi;
namespace SafeExamBrowser.Configuration
{
internal class Bounds : IBounds
{
public int Left { get; set; }
public int Top { get; set; }
public int Right { get; set; }
public int Bottom { get; set; }
}
}

View file

@ -60,6 +60,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Bounds.cs" />
<Compile Include="Settings\BrowserSettings.cs" />
<Compile Include="Settings\SettingsImpl.cs" />
<Compile Include="WorkingArea.cs" />

View file

@ -11,7 +11,6 @@ using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Contracts.WindowsApi;
using SafeExamBrowser.Contracts.WindowsApi.Types;
namespace SafeExamBrowser.Configuration
{
@ -19,7 +18,7 @@ namespace SafeExamBrowser.Configuration
{
private ILogger logger;
private INativeMethods nativeMethods;
private RECT? originalWorkingArea;
private IBounds originalWorkingArea;
public WorkingArea(ILogger logger, INativeMethods nativeMethods)
{
@ -31,9 +30,9 @@ namespace SafeExamBrowser.Configuration
{
originalWorkingArea = nativeMethods.GetWorkingArea();
LogWorkingArea("Saved original working area", originalWorkingArea.Value);
LogWorkingArea("Saved original working area", originalWorkingArea);
var area = new RECT
var area = new Bounds
{
Left = 0,
Top = 0,
@ -48,14 +47,14 @@ namespace SafeExamBrowser.Configuration
public void Reset()
{
if (originalWorkingArea.HasValue)
if (originalWorkingArea != null)
{
nativeMethods.SetWorkingArea(originalWorkingArea.Value);
LogWorkingArea("Restored original working area", originalWorkingArea.Value);
nativeMethods.SetWorkingArea(originalWorkingArea);
LogWorkingArea("Restored original working area", originalWorkingArea);
}
}
private void LogWorkingArea(string message, RECT area)
private void LogWorkingArea(string message, IBounds area)
{
logger.Info($"{message}: Left = {area.Left}, Top = {area.Top}, Right = {area.Right}, Bottom = {area.Bottom}.");
}

View file

@ -98,8 +98,8 @@
<Compile Include="UserInterface\IWindow.cs" />
<Compile Include="UserInterface\MessageBoxAction.cs" />
<Compile Include="UserInterface\MessageBoxIcon.cs" />
<Compile Include="WindowsApi\IBounds.cs" />
<Compile Include="WindowsApi\INativeMethods.cs" />
<Compile Include="WindowsApi\Types\RECT.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View file

@ -0,0 +1,18 @@
/*
* Copyright (c) 2017 ETH Zürich, Educational Development and Technology (LET)
*
* 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/.
*/
namespace SafeExamBrowser.Contracts.WindowsApi
{
public interface IBounds
{
int Left { get; }
int Top { get; }
int Right { get; }
int Bottom { get; }
}
}

View file

@ -9,7 +9,6 @@
using System;
using System.Collections.Generic;
using SafeExamBrowser.Contracts.Monitoring;
using SafeExamBrowser.Contracts.WindowsApi.Types;
namespace SafeExamBrowser.Contracts.WindowsApi
{
@ -52,7 +51,7 @@ namespace SafeExamBrowser.Contracts.WindowsApi
/// <exception cref="System.ComponentModel.Win32Exception">
/// If the working area could not be retrieved.
/// </exception>
RECT GetWorkingArea();
IBounds GetWorkingArea();
/// <summary>
/// Hides the given window. Returns <c>true</c> if successful, otherwise <c>false</c>.
@ -105,7 +104,7 @@ namespace SafeExamBrowser.Contracts.WindowsApi
/// <exception cref="System.ComponentModel.Win32Exception">
/// If the working area could not be set.
/// </exception>
void SetWorkingArea(RECT bounds);
void SetWorkingArea(IBounds bounds);
/// <summary>
/// Unregisters the system hook for the given keyboard interceptor.

View file

@ -10,6 +10,7 @@ using System;
using System.Runtime.InteropServices;
using SafeExamBrowser.Contracts.Monitoring;
using SafeExamBrowser.WindowsApi.Constants;
using SafeExamBrowser.WindowsApi.Types;
namespace SafeExamBrowser.WindowsApi.Monitoring
{

View file

@ -15,9 +15,9 @@ using System.Runtime.InteropServices;
using System.Text;
using SafeExamBrowser.Contracts.Monitoring;
using SafeExamBrowser.Contracts.WindowsApi;
using SafeExamBrowser.Contracts.WindowsApi.Types;
using SafeExamBrowser.WindowsApi.Constants;
using SafeExamBrowser.WindowsApi.Monitoring;
using SafeExamBrowser.WindowsApi.Types;
namespace SafeExamBrowser.WindowsApi
{
@ -99,7 +99,7 @@ namespace SafeExamBrowser.WindowsApi
return string.Empty;
}
public RECT GetWorkingArea()
public IBounds GetWorkingArea()
{
var workingArea = new RECT();
var success = User32.SystemParametersInfo(SPI.GETWORKAREA, 0, ref workingArea, SPIF.NONE);
@ -109,7 +109,7 @@ namespace SafeExamBrowser.WindowsApi
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return workingArea;
return workingArea.ToBounds();
}
public bool HideWindow(IntPtr window)
@ -194,9 +194,10 @@ namespace SafeExamBrowser.WindowsApi
User32.SendMessage(window, Constant.WM_SYSCOMMAND, (IntPtr) SystemCommand.CLOSE, IntPtr.Zero);
}
public void SetWorkingArea(RECT bounds)
public void SetWorkingArea(IBounds bounds)
{
var success = User32.SystemParametersInfo(SPI.SETWORKAREA, 0, ref bounds, SPIF.UPDATEANDCHANGE);
var workingArea = new RECT { Left = bounds.Left, Top = bounds.Top, Right = bounds.Right, Bottom = bounds.Bottom };
var success = User32.SystemParametersInfo(SPI.SETWORKAREA, 0, ref workingArea, SPIF.UPDATEANDCHANGE);
if (!success)
{

View file

@ -60,8 +60,9 @@
<ItemGroup>
<Compile Include="Constants\Constant.cs" />
<Compile Include="Constants\HookType.cs" />
<Compile Include="Constants\KBDLLHOOKSTRUCT.cs" />
<Compile Include="Constants\KBDLLHOOKSTRUCTFlags.cs" />
<Compile Include="Types\Bounds.cs" />
<Compile Include="Types\KBDLLHOOKSTRUCT.cs" />
<Compile Include="Types\KBDLLHOOKSTRUCTFlags.cs" />
<Compile Include="Constants\ShowWindowCommand.cs" />
<Compile Include="Constants\SystemCommand.cs" />
<Compile Include="Kernel32.cs" />
@ -70,6 +71,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Constants\SPI.cs" />
<Compile Include="Constants\SPIF.cs" />
<Compile Include="Types\RECT.cs" />
<Compile Include="User32.cs" />
</ItemGroup>
<ItemGroup>

View file

@ -0,0 +1,20 @@
/*
* Copyright (c) 2017 ETH Zürich, Educational Development and Technology (LET)
*
* 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 SafeExamBrowser.Contracts.WindowsApi;
namespace SafeExamBrowser.WindowsApi.Types
{
internal class Bounds : IBounds
{
public int Left { get; set; }
public int Top { get; set; }
public int Right { get; set; }
public int Bottom { get; set; }
}
}

View file

@ -9,7 +9,7 @@
using System;
using System.Runtime.InteropServices;
namespace SafeExamBrowser.WindowsApi.Constants
namespace SafeExamBrowser.WindowsApi.Types
{
/// <remarks>
/// See http://www.pinvoke.net/default.aspx/Structures/KBDLLHOOKSTRUCT.html.

View file

@ -6,7 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
namespace SafeExamBrowser.WindowsApi.Constants
namespace SafeExamBrowser.WindowsApi.Types
{
/// <remarks>
/// See http://www.pinvoke.net/default.aspx/Structures/KBDLLHOOKSTRUCT.html.

View file

@ -7,18 +7,30 @@
*/
using System.Runtime.InteropServices;
using SafeExamBrowser.Contracts.WindowsApi;
namespace SafeExamBrowser.Contracts.WindowsApi.Types
namespace SafeExamBrowser.WindowsApi.Types
{
/// <summary>
/// See https://msdn.microsoft.com/en-us/library/windows/desktop/dd162897(v=vs.85).aspx.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct RECT
internal struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
internal int Left;
internal int Top;
internal int Right;
internal int Bottom;
internal IBounds ToBounds()
{
return new Bounds
{
Left = Left,
Top = Top,
Right = Right,
Bottom = Bottom
};
}
}
}

View file

@ -9,8 +9,8 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
using SafeExamBrowser.Contracts.WindowsApi.Types;
using SafeExamBrowser.WindowsApi.Constants;
using SafeExamBrowser.WindowsApi.Types;
namespace SafeExamBrowser.WindowsApi
{