Skip to content

Commit

Permalink
Added the possibility close a window (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed Aug 2, 2023
1 parent 93ef6e2 commit f9d95e9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion PermaTop/Pages/PinWindowsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal void InitUI()
var openedWindows = Global.GetWindows();
for (int i = 0; i < openedWindows.Count; i++)
{
WindowDisplayer.Children.Add(new WindowPropertyItem(openedWindows[i]));
WindowDisplayer.Children.Add(new WindowPropertyItem(openedWindows[i], WindowDisplayer));
}
}

Expand Down
4 changes: 3 additions & 1 deletion PermaTop/UserControls/WindowPropertyItem.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="TitleTxt" FontWeight="ExtraBold" d:Text="WindowTItle" FontSize="14" VerticalAlignment="Center">
<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="2" 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="3" Style="{DynamicResource DefaultButton}" Background="{DynamicResource Background2}" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Content="&#xF602;" FontSize="14"/>
<Button Foreground="{DynamicResource Foreground1}" x:Name="PinBtn" Click="PinBtn_Click" Width="30" Grid.Column="3" 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="CloseBtn" Click="CloseBtn_Click" Width="30" Grid.Column="4" Style="{DynamicResource DefaultButton}" Background="{DynamicResource Background2}" FontFamily="..\Fonts\#FluentSystemIcons-Regular" Content="&#xF36A;" FontSize="14"/>
</Grid>
</Border>
</UserControl>
21 changes: 20 additions & 1 deletion PermaTop/UserControls/WindowPropertyItem.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;

Expand All @@ -33,10 +35,12 @@ namespace PermaTop.UserControls;
public partial class WindowPropertyItem : UserControl
{
WindowInfo WindowInfo { get; set; }
public WindowPropertyItem(WindowInfo windowInfo)
StackPanel ParentElement { get; init; }
public WindowPropertyItem(WindowInfo windowInfo, StackPanel parent)
{
InitializeComponent();
WindowInfo = windowInfo;
ParentElement = parent;

InitUI();
}
Expand Down Expand Up @@ -75,4 +79,19 @@ private void FavBtn_Click(object sender, RoutedEventArgs e)
}
XmlSerializerManager.SaveToXml(Global.Favorites, $@"{FileSys.AppDataPath}\Léo Corporation\PermaTop\Favs.xml");
}

private const int WM_SYSCOMMAND = 0x0112;
private const int SC_CLOSE = 0xF060;

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
private void CloseBtn_Click(object sender, RoutedEventArgs e)
{
try
{
SendMessage(WindowInfo.Hwnd, WM_SYSCOMMAND, (IntPtr)SC_CLOSE, IntPtr.Zero);
ParentElement.Children.Remove(this);
}
catch { }
}
}

0 comments on commit f9d95e9

Please sign in to comment.