Skip to content

Commit

Permalink
Added a popup to select the screen where to move the window (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed Sep 4, 2023
1 parent 0c118b3 commit 5f5e439
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
40 changes: 39 additions & 1 deletion PermaTop/Classes/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal struct WINDOWPLACEMENT
}

[StructLayout(LayoutKind.Sequential)]
private struct RECT
public struct RECT
{
public int Left;
public int Top;
Expand Down Expand Up @@ -208,6 +208,44 @@ public static List<WindowInfo> GetWindows()
return windowInfos;
}

[DllImport("user32.dll")]
static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip,
MonitorEnumProc lpfnEnum, IntPtr dwData);

[DllImport("user32.dll")]
static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfo lpmi);

delegate bool MonitorEnumProc(IntPtr hMonitor, IntPtr hdcMonitor,
RECT lprcMonitor, IntPtr dwData);

[StructLayout(LayoutKind.Sequential)]
struct MonitorInfo
{
public uint Size;
public RECT Monitor;
public RECT WorkArea;
public uint Flags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
}

public static List<ScreenInfo> GetScreenInfos()
{
var screenInfos = new List<ScreenInfo>();
EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
(hMonitor, hdcMonitor, lprcMonitor, dwData) =>
{
var mi = new MonitorInfo();
mi.Size = (uint)Marshal.SizeOf(mi);
if (GetMonitorInfo(hMonitor, ref mi))
{
screenInfos.Add(new ScreenInfo(mi.DeviceName, lprcMonitor));
}
return true; // Continue enumeration
}, IntPtr.Zero);
return screenInfos;
}

public static void SetWindowTopMost(IntPtr hWnd, bool topMost)
{
if (hWnd == IntPtr.Zero)
Expand Down
26 changes: 26 additions & 0 deletions PermaTop/Classes/ScreenInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
MIT License
Copyright (c) Léo Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

namespace PermaTop.Classes;
public record ScreenInfo(string Name, Global.RECT Bounds);
39 changes: 39 additions & 0 deletions PermaTop/UserControls/WindowPropertyItem.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,51 @@
Padding="2"
HorizontalContentAlignment="Left"
Background="Transparent"
Click="SetScreenBtn_Click"
Content="{x:Static lang:Resources.MoveToScreen}"
FontWeight="Bold"
Style="{DynamicResource PrimaryButton}" />
</StackPanel>
</Border>
</Popup>
<Popup
x:Name="ScreenPopup"
AllowsTransparency="True"
Placement="Bottom"
PlacementTarget="{Binding ElementName=MoreBtn}"
StaysOpen="False">
<Border
Margin="10"
Padding="10"
Background="{DynamicResource Background1}"
CornerRadius="10">
<Border.Effect>
<DropShadowEffect
BlurRadius="10"
Direction="270"
Opacity="0.1"
ShadowDepth="0"
Color="#1F1F1F" />
</Border.Effect>

<StackPanel>
<ComboBox
x:Name="ScreenSelector"
Margin="0 0 0 10"
Foreground="{DynamicResource Foreground1}"
Style="{DynamicResource ComboBoxStyle1}" />
<Button
x:Name="MoveBtn"
Padding="2"
HorizontalAlignment="Center"
Background="Transparent"
Click="MoveBtn_Click"
Content="{x:Static lang:Resources.SetPosition}"
FontWeight="Bold"
Style="{DynamicResource PrimaryButton}" />
</StackPanel>
</Border>
</Popup>
</Grid>
</Border>
</UserControl>
26 changes: 26 additions & 0 deletions PermaTop/UserControls/WindowPropertyItem.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using PermaTop.Classes;
using PeyrSharp.Env;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -196,4 +198,28 @@ private void SetPosBtn_Click(object sender, RoutedEventArgs e)
SizePopup.IsOpen = true;
ContextMenu.IsOpen = false;
}
List<ScreenInfo> _screens = Global.GetScreenInfos();
private void MoveBtn_Click(object sender, RoutedEventArgs e)
{
if (ScreenSelector.SelectedIndex >= 0)
{
MoveWindow(WindowInfo.Hwnd, _screens[ScreenSelector.SelectedIndex].Bounds.Left, _screens[ScreenSelector.SelectedIndex].Bounds.Top);
}
}

private void SetScreenBtn_Click(object sender, RoutedEventArgs e)
{
ScreenPopup.IsOpen = true;
ContextMenu.IsOpen = false;
LoadScreens();
}

private void LoadScreens()
{
ScreenSelector.Items.Clear();
for (int i = 0; i < _screens.Count; i++)
{
ScreenSelector.Items.Add($"{_screens[i].Name} ({_screens[i].Bounds.Right - _screens[i].Bounds.Left}x{_screens[i].Bounds.Bottom - _screens[i].Bounds.Top})");
}
}
}

0 comments on commit 5f5e439

Please sign in to comment.