Skip to content

Commit

Permalink
moved to evilbaschdi.core nuget package, ui adjustments, switched fro…
Browse files Browse the repository at this point in the history
…m german to english lables, button rotation, window resizing
  • Loading branch information
evilbaschdi committed Oct 8, 2016
1 parent 7f5edb1 commit a683a08
Show file tree
Hide file tree
Showing 14 changed files with 299 additions and 135 deletions.
6 changes: 0 additions & 6 deletions DisplayRotation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DisplayRotation", "DisplayRotation\DisplayRotation.csproj", "{7A789136-B210-452D-A16B-20B5D8CCFE76}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EvilBaschdi.Core", "..\EvilBaschdi.Core\EvilBaschdi.Core\EvilBaschdi.Core.csproj", "{8A06A423-244F-404F-AAD8-53DE2EE2AED7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -17,10 +15,6 @@ Global
{7A789136-B210-452D-A16B-20B5D8CCFE76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A789136-B210-452D-A16B-20B5D8CCFE76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A789136-B210-452D-A16B-20B5D8CCFE76}.Release|Any CPU.Build.0 = Release|Any CPU
{8A06A423-244F-404F-AAD8-53DE2EE2AED7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8A06A423-244F-404F-AAD8-53DE2EE2AED7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A06A423-244F-404F-AAD8-53DE2EE2AED7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A06A423-244F-404F-AAD8-53DE2EE2AED7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
90 changes: 68 additions & 22 deletions DisplayRotation/Core/ApplicationSettings.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using DisplayRotation.Internal;
using System;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using DisplayRotation.Internal;
using Button = System.Windows.Controls.Button;
using ContextMenu = System.Windows.Forms.ContextMenu;
using MenuItem = System.Windows.Forms.MenuItem;

namespace DisplayRotation.Core
{
Expand All @@ -14,15 +18,23 @@ public class ApplicationSettings : IApplicationSettings
private NotifyIcon _ni;
private readonly IActiveDevices _activeDevices;
private readonly IRotateDisplay _rotateDisplay;
private readonly IRotateButtonAndCanvas _rotateButtonAndCanvas;

/// <summary>
/// </summary>
/// <param name="mainWindow"></param>
/// <param name="ni"></param>
/// <param name="activeDevices"></param>
/// <param name="rotateDisplay"></param>
public ApplicationSettings(MainWindow mainWindow, NotifyIcon ni, IActiveDevices activeDevices,
IRotateDisplay rotateDisplay)
/// <param name="rotateButtonAndCanvas"></param>
/// <exception cref="ArgumentNullException">
/// <paramref name="mainWindow" /> is <see langword="null" />.
/// <paramref name="ni" /> is <see langword="null" />.
/// <paramref name="activeDevices" /> is <see langword="null" />.
/// <paramref name="rotateDisplay" /> is <see langword="null" />.
/// <paramref name="rotateButtonAndCanvas" /> is <see langword="null" />.
/// </exception>
public ApplicationSettings(MainWindow mainWindow, NotifyIcon ni, IActiveDevices activeDevices, IRotateDisplay rotateDisplay, IRotateButtonAndCanvas rotateButtonAndCanvas)
{
if (mainWindow == null)
{
Expand All @@ -40,10 +52,15 @@ public ApplicationSettings(MainWindow mainWindow, NotifyIcon ni, IActiveDevices
{
throw new ArgumentNullException(nameof(rotateDisplay));
}
if (rotateButtonAndCanvas == null)
{
throw new ArgumentNullException(nameof(rotateButtonAndCanvas));
}
_mainWindow = mainWindow;
_ni = ni;
_activeDevices = activeDevices;
_rotateDisplay = rotateDisplay;
_rotateButtonAndCanvas = rotateButtonAndCanvas;
}

/// <summary>
Expand All @@ -56,19 +73,17 @@ public void Run()
StartMinimized();
_ni.ContextMenu = NotifyIconContextMenu();
_ni.DoubleClick += NotifyIcon_DoubleClick;
//_ni.Click += (sender, args) => _ni.ShowBalloonTip(10);
}

/// <summary>
/// </summary>
public void StartMinimized()
{
_ni = new NotifyIcon
{
Icon = Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location),
//BalloonTipTitle = Resources.ApplicationSettings_StartMinimized_BalloonTipTitle,
Visible = true
};
{
Icon = Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location),
Visible = true
};

_mainWindow.Hide();
}
Expand All @@ -79,34 +94,68 @@ private ContextMenu NotifyIconContextMenu()

foreach (var device in _activeDevices.Get().OrderBy(d => d.PositionX))
{
var currentButton = new Button();

foreach (Canvas canvas in _mainWindow.DisplayStackPanel.Children)
{
foreach (Button button in canvas.Children)
{
if (button.Name == $"ButtonDisplay{device.Id}")
{
currentButton = button;
break;
}
}
}

var parent = new MenuItem(device.Name);

//anticlockwise
var anticlockwiseItem = new MenuItem("Hochformat 'anticlockwise'");
anticlockwiseItem.Click += (sender, args) => _rotateDisplay.For(NativeMethods.Dmdo90, device.Id);
var anticlockwiseItem = new MenuItem("Upright 'anticlockwise'");
anticlockwiseItem.Click += (sender, args) =>
{
_rotateDisplay.For(NativeMethods.Dmdo90, device.Id);
_rotateButtonAndCanvas.For(NativeMethods.Dmdo90, currentButton);
_mainWindow.SetWindowMargins();
};

//180
var clockwiseItem = new MenuItem("Querformat (gedreht)");
clockwiseItem.Click += (sender, args) => _rotateDisplay.For(NativeMethods.Dmdo180, device.Id);
var clockwiseItem = new MenuItem("Landscape (rotated)");
clockwiseItem.Click += (sender, args) =>
{
_rotateDisplay.For(NativeMethods.Dmdo180, device.Id);
_rotateButtonAndCanvas.For(NativeMethods.Dmdo180, currentButton);
_mainWindow.SetWindowMargins();
};

//clockwise
var mirrorItem = new MenuItem("Hochformat (gedreht) 'clockwise'");
mirrorItem.Click += (sender, args) => _rotateDisplay.For(NativeMethods.Dmdo270, device.Id);
var mirrorItem = new MenuItem("Upright 'clockwise'");
mirrorItem.Click += (sender, args) =>
{
_rotateDisplay.For(NativeMethods.Dmdo270, device.Id);
_rotateButtonAndCanvas.For(NativeMethods.Dmdo270, currentButton);
_mainWindow.SetWindowMargins();
};

//restore
var restoreItem = new MenuItem("Querformat");
var restoreItem = new MenuItem("Reset");
restoreItem.DefaultItem = true;
restoreItem.Click += (sender, args) => _rotateDisplay.For(NativeMethods.DmdoDefault, device.Id);
restoreItem.Click += (sender, args) =>
{
_rotateDisplay.For(NativeMethods.DmdoDefault, device.Id);
_rotateButtonAndCanvas.For(NativeMethods.DmdoDefault, currentButton);
_mainWindow.SetWindowMargins();
};

parent.MenuItems.AddRange(new[] { anticlockwiseItem, clockwiseItem, mirrorItem, restoreItem });

contextMenu.MenuItems.Add(parent);
}
contextMenu.MenuItems.Add("-");
var restore = new MenuItem("Fenster öffnen");
var restore = new MenuItem("Restore application");
restore.Click += ContextMenuItemRestore_Click;

var close = new MenuItem("Beenden");
var close = new MenuItem("Close application");
close.Click += ContextMenuItemClose_Click;

contextMenu.MenuItems.AddRange(new[] { restore, close });
Expand All @@ -116,21 +165,18 @@ private ContextMenu NotifyIconContextMenu()

private void ContextMenuItemClose_Click(object sender, EventArgs e)
{
// Will Close Your Application
_ni.Dispose();
_mainWindow.Close();
}

private void ContextMenuItemRestore_Click(object sender, EventArgs e)
{
//Will Restore Your Application
_mainWindow.Show();
_mainWindow.WindowState = WindowState.Normal;
}

private void NotifyIcon_DoubleClick(object sender, EventArgs e)
{
//Will Restore Your Application
_mainWindow.Show();
_mainWindow.WindowState = WindowState.Normal;
}
Expand Down
18 changes: 11 additions & 7 deletions DisplayRotation/DisplayRotation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="EvilBaschdi.Core, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\EvilBaschdi.Core.1.0.0.3\lib\net462\EvilBaschdi.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MahApps.Metro, Version=1.3.0.0, Culture=neutral, PublicKeyToken=f4fb5a3c4d1e5b4f, processorArchitecture=MSIL">
<HintPath>..\packages\MahApps.Metro.1.3.0\lib\net45\MahApps.Metro.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -86,9 +90,9 @@
<Compile Include="Internal\DisplayDevice.cs" />
<Compile Include="Internal\DisplayDeviceStateFlags.cs" />
<Compile Include="Internal\DisplaySettingsFlags.cs" />
<Compile Include="Internal\IRotateButtonAndCanvas.cs" />
<Compile Include="Internal\IRotateDisplay.cs" />
<Compile Include="Internal\Pointl.cs" />
<Compile Include="Internal\RotateDisplay.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand All @@ -99,6 +103,12 @@
</Compile>
<Compile Include="Internal\NativeMethods.cs" />
<Compile Include="Internal\Devmode.cs" />
<Compile Include="Internal\RotateButtonAndCanvas.cs">
<DependentUpon>IRotateButtonAndCanvas.cs</DependentUpon>
</Compile>
<Compile Include="Internal\RotateDisplay.cs">
<DependentUpon>IRotateDisplay.cs</DependentUpon>
</Compile>
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
Expand Down Expand Up @@ -147,12 +157,6 @@
<ItemGroup>
<Resource Include="Resources\Entypo-license.txt" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\EvilBaschdi.Core\EvilBaschdi.Core\EvilBaschdi.Core.csproj">
<Project>{8a06a423-244f-404f-aad8-53de2ee2aed7}</Project>
<Name>EvilBaschdi.Core</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
23 changes: 12 additions & 11 deletions DisplayRotation/Internal/ActiveDevices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public IEnumerable<DisplayHelper> Get()
device.cb = Marshal.SizeOf(device);

var list = new List<DisplayHelper>();
for(uint id = 0; NativeMethods.EnumDisplayDevices(null, id, ref device, 0); id++)
for (uint id = 0; NativeMethods.EnumDisplayDevices(null, id, ref device, 0); id++)
{
device.cb = Marshal.SizeOf(device);

Expand All @@ -22,28 +22,29 @@ public IEnumerable<DisplayHelper> Get()

device.cb = Marshal.SizeOf(device);

if(device.DeviceName.Trim().Length > 0)
if (device.DeviceName.Trim().Length > 0)
{
//Devices.Text +=
// $"{id}, {device.DeviceName}, {device.DeviceString}, {device.StateFlags}, {device.DeviceID}, {device.DeviceKey}, {device.cb} {Environment.NewLine}";

var helper = new DisplayHelper
{
Id = id,
Name = device.DeviceString
};
{
Id = id,
Name = device.DeviceString
};

foreach(var screen in Screen.AllScreens)
foreach (var screen in Screen.AllScreens)
{
if(device.DeviceName.Contains(screen.DeviceName))
if (device.DeviceName.Contains(screen.DeviceName))
{
var rectangle = screen.Bounds;
helper.PositionX = rectangle.X;
helper.PositionY = rectangle.Y;
helper.Height = rectangle.Height;
helper.Width = rectangle.Width;
}
}
list.Add(helper);
}
}

return list;
}
}
Expand Down
2 changes: 1 addition & 1 deletion DisplayRotation/Internal/DisplayDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace DisplayRotation.Internal
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal struct DisplayDevice
public struct DisplayDevice
{
[MarshalAs(UnmanagedType.U4)] public int cb;

Expand Down
7 changes: 1 addition & 6 deletions DisplayRotation/Internal/DisplayDeviceStateFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
namespace DisplayRotation.Internal
{
[Flags]
internal enum DisplayDeviceStateFlags
public enum DisplayDeviceStateFlags
{
AttachedToDesktop = 0x1,
MultiDriver = 0x2,

PrimaryDevice = 0x4,

MirroringDriver = 0x8,

VgaCompatible = 0x16,

Removable = 0x20,

ModesPruned = 0x8000000,
Remote = 0x4000000,
Disconnect = 0x2000000
Expand Down
3 changes: 3 additions & 0 deletions DisplayRotation/Internal/DisplayHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ namespace DisplayRotation.Internal
public class DisplayHelper
{
public int PositionX { get; set; }
public int PositionY { get; set; }
public uint Id { get; set; }
public string Name { get; set; }
public int Height { get; set; }
public int Width { get; set; }
}
}
9 changes: 9 additions & 0 deletions DisplayRotation/Internal/IRotateButtonAndCanvas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Windows.Controls;

namespace DisplayRotation.Internal
{
public interface IRotateButtonAndCanvas
{
void For(int rotation, Button button);
}
}
33 changes: 33 additions & 0 deletions DisplayRotation/Internal/RotateButtonAndCanvas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Windows;
using System.Windows.Controls;

namespace DisplayRotation.Internal
{
public class RotateButtonAndCanvas : IRotateButtonAndCanvas
{
public void For(int rotation, Button button)
{
var x = 10d;
var w = 192d;
var h = 108d;

switch (rotation)
{
case NativeMethods.Dmdo90:
case NativeMethods.Dmdo270:
button.Height = w;
button.Width = h;
((Canvas) button.Parent).Margin = new Thickness(x, 0, h, 0);
break;

case NativeMethods.Dmdo180:
case NativeMethods.DmdoDefault:

button.Height = h;
button.Width = w;
((Canvas) button.Parent).Margin = new Thickness(x, 0, w, 0);
break;
}
}
}
}
Loading

0 comments on commit a683a08

Please sign in to comment.