Skip to content

Commit

Permalink
Added the possibility to set the position of a window (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed Aug 3, 2023
1 parent b770644 commit a1ad2d5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
19 changes: 19 additions & 0 deletions PermaTop/Classes/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ internal struct WINDOWPLACEMENT
public System.Drawing.Rectangle rcNormalPosition;
}

[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

internal static Rect GetWindowPosition(IntPtr windowHandle)
{
GetWindowRect(windowHandle, out RECT windowRect);
return new Rect(windowRect.Left, windowRect.Top, windowRect.Right - windowRect.Left, windowRect.Bottom - windowRect.Top);
}

[DllImport("user32.dll")]
internal static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

Expand Down
54 changes: 47 additions & 7 deletions PermaTop/UserControls/WindowPropertyItem.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PermaTop.UserControls"
xmlns:lang="clr-namespace:PermaTop.Properties"
mc:Ignorable="d" FontFamily="..\Fonts\#Hauora"
Height="50" d:DesignWidth="800" Foreground="{DynamicResource Foreground1}">
<Border CornerRadius="10" Padding="5" Margin="5" Background="{DynamicResource CardBackground}">
Expand All @@ -21,18 +22,57 @@
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Image x:Name="IconImg" Grid.Column="0" Margin="5 0" MaxWidth="15" MaxHeight="15"/>
<TextBlock x:Name="TitleTxt" FontWeight="ExtraBold" d:Text="WindowTItle" FontSize="14" VerticalAlignment="Center" Grid.Column="1">
<Image x:Name="IconImg" Grid.Column="0" Margin="5 0" MaxWidth="15" MaxHeight="15"/>
<TextBlock x:Name="TitleTxt" FontWeight="ExtraBold" d:Text="WindowTItle" FontSize="14" VerticalAlignment="Center" Grid.Column="1">
<TextBlock.ToolTip>
<ToolTip x:Name="TitleToolTip" Foreground="{DynamicResource Foreground1}" Background="{DynamicResource Background1}" />
</TextBlock.ToolTip>
</TextBlock>
<Button Foreground="{DynamicResource Foreground1}" x:Name="FavBtn" Click="FavBtn_Click" Width="30" Grid.Column="3" Style="{DynamicResource DefaultButton}" Background="{DynamicResource Background2}" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Content="&#xF710;" FontSize="14" Margin="0 0 5 0"/>
<Button Foreground="{DynamicResource Foreground1}" x:Name="PinBtn" Click="PinBtn_Click" Width="30" Grid.Column="4" Style="{DynamicResource DefaultButton}" Background="{DynamicResource Background2}" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Content="&#xF602;" FontSize="14" Margin="0 0 5 0"/>
<Button Foreground="{DynamicResource Foreground1}" x:Name="MinBtn" Click="MinBtn_Click" Width="30" Grid.Column="5" Style="{DynamicResource DefaultButton}" Background="{DynamicResource CardBackground}" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Content="&#xF4E0;" FontSize="14" Margin="0 0 5 0"/>
<Button Foreground="{DynamicResource Foreground1}" x:Name="MaxRestoreBtn" Click="MaxRestoreBtn_Click" Width="30" Grid.Column="6" Style="{DynamicResource DefaultButton}" Background="{DynamicResource CardBackground}" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Content="&#xFA41;" FontSize="14" Margin="0 0 5 0"/>
<Button Foreground="{DynamicResource Foreground1}" x:Name="CloseBtn" Click="CloseBtn_Click" Width="30" Grid.Column="7" Style="{DynamicResource DefaultButton}" Background="{DynamicResource CardBackground}" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Content="&#xF36A;" FontSize="14"/>
<Button Foreground="{DynamicResource Foreground1}" x:Name="MoreBtn" Click="MoreBtn_Click" Width="30" Grid.Column="3" Style="{DynamicResource DefaultButton}" Background="{DynamicResource Background2}" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Content="&#xFC2E;" FontSize="14" Margin="0 0 5 0"/>
<Button Foreground="{DynamicResource Foreground1}" x:Name="FavBtn" Click="FavBtn_Click" Width="30" Grid.Column="4" Style="{DynamicResource DefaultButton}" Background="{DynamicResource Background2}" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Content="&#xF710;" FontSize="14" Margin="0 0 5 0"/>
<Button Foreground="{DynamicResource Foreground1}" x:Name="PinBtn" Click="PinBtn_Click" Width="30" Grid.Column="5" Style="{DynamicResource DefaultButton}" Background="{DynamicResource Background2}" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Content="&#xF602;" FontSize="14" Margin="0 0 5 0"/>
<Button Foreground="{DynamicResource Foreground1}" x:Name="MinBtn" Click="MinBtn_Click" Width="30" Grid.Column="6" Style="{DynamicResource DefaultButton}" Background="{DynamicResource CardBackground}" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Content="&#xF4E0;" FontSize="14" Margin="0 0 5 0"/>
<Button Foreground="{DynamicResource Foreground1}" x:Name="MaxRestoreBtn" Click="MaxRestoreBtn_Click" Width="30" Grid.Column="7" Style="{DynamicResource DefaultButton}" Background="{DynamicResource CardBackground}" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Content="&#xFA41;" FontSize="14" Margin="0 0 5 0"/>
<Button Foreground="{DynamicResource Foreground1}" x:Name="CloseBtn" Click="CloseBtn_Click" Width="30" Grid.Column="8" Style="{DynamicResource DefaultButton}" Background="{DynamicResource CardBackground}" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Content="&#xF36A;" FontSize="14"/>

<Popup StaysOpen="False" x:Name="MorePopup" Placement="Bottom" PlacementTarget="{Binding ElementName=MoreBtn}" AllowsTransparency="True">
<Border Background="{DynamicResource Background1}" Padding="10" CornerRadius="10">
<Border.Effect>
<DropShadowEffect BlurRadius="10" Color="#1F1F1F" Direction="270" Opacity="0.1" ShadowDepth="0"/>
</Border.Effect>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<TextBlock Text="{x:Static lang:Resources.Position}" FontWeight="Bold"/>
<TextBlock Text="{x:Static lang:Resources.X}" Grid.Row="1" VerticalAlignment="Center" Margin="10" FontWeight="Bold"/>
<TextBlock Text="{x:Static lang:Resources.Y}" Grid.Row="2" VerticalAlignment="Center" Margin="10" FontWeight="Bold"/>
<Border Grid.Row="1" Background="{DynamicResource CardBackground}" Width="50" CornerRadius="5" HorizontalAlignment="Center" Grid.Column="1" VerticalAlignment="Center">
<Border.Effect>
<DropShadowEffect Color="{DynamicResource LightAccentColor}" BlurRadius="15" Opacity="0.2" ShadowDepth="0"/>
</Border.Effect>
<TextBox x:Name="XTxt" Padding="3" Margin="3" BorderThickness="0" FontWeight="Bold" Foreground="{DynamicResource DarkGray}" d:Text="123" Background="Transparent" TextAlignment="Center"/>
</Border>
<Border Grid.Row="2" Background="{DynamicResource CardBackground}" Width="50" CornerRadius="5" HorizontalAlignment="Center" Grid.Column="1" VerticalAlignment="Center">
<Border.Effect>
<DropShadowEffect Color="{DynamicResource LightAccentColor}" BlurRadius="15" Opacity="0.2" ShadowDepth="0"/>
</Border.Effect>
<TextBox x:Name="YTxt" Padding="3" Margin="3" BorderThickness="0" FontWeight="Bold" Foreground="{DynamicResource DarkGray}" d:Text="123" Background="Transparent" TextAlignment="Center"/>
</Border>
<Button Content="{x:Static lang:Resources.Apply}" x:Name="ApplyBtn" Style="{DynamicResource PrimaryButton}" Click="ApplyBtn_Click" Grid.ColumnSpan="2" Grid.Row="3" HorizontalAlignment="Center" Margin="5" Padding="5" Background="{DynamicResource Accent}" FontWeight="Bold" Foreground="{DynamicResource WindowButtonsHoverForeground1}"/>
</Grid>
</Border>
</Popup>
</Grid>
</Border>
</UserControl>
31 changes: 31 additions & 0 deletions PermaTop/UserControls/WindowPropertyItem.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ private void InitUI()
// Set the Image control's Source property to display the icon
IconImg.Source = iconSource;
}
Rect windowPosition = Global.GetWindowPosition(WindowInfo.Hwnd);
XTxt.Text = windowPosition.Left.ToString();
YTxt.Text = windowPosition.Top.ToString();
}

private void PinBtn_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -100,6 +103,8 @@ private void FavBtn_Click(object sender, RoutedEventArgs e)
private const int ICON_SMALL = 0;
private const int ICON_BIG = 1;
private const uint WM_GETICON = 0x007F;
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOZORDER = 0x0004;

[DllImport("user32.dll")]
private static extern IntPtr GetClassLong(IntPtr hWnd, int nIndex);
Expand All @@ -123,6 +128,14 @@ private BitmapSource GetIconFromHandle(IntPtr iconHandle)

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
private void MoveWindow(IntPtr windowHandle, int x, int y)
{
SetWindowPos(windowHandle, IntPtr.Zero, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}

private void CloseBtn_Click(object sender, RoutedEventArgs e)
{
try
Expand Down Expand Up @@ -159,4 +172,22 @@ private void MinBtn_Click(object sender, RoutedEventArgs e)
}
catch { }
}

private void MoreBtn_Click(object sender, RoutedEventArgs e)
{
MorePopup.IsOpen = !MorePopup.IsOpen;
}

private void ApplyBtn_Click(object sender, RoutedEventArgs e)
{
bool xValid = int.TryParse(XTxt.Text, out int x);
bool yValid = int.TryParse(YTxt.Text, out int y);

if (!xValid || !yValid)
{
return;
}

MoveWindow(WindowInfo.Hwnd, x, y);
}
}

0 comments on commit a1ad2d5

Please sign in to comment.