Skip to content

Commit

Permalink
Moved methods to Global class (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed Oct 30, 2023
1 parent 5f8be74 commit e37731d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
17 changes: 16 additions & 1 deletion PermaTop/Classes/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace PermaTop.Classes;
public static class Global
Expand Down Expand Up @@ -121,7 +123,7 @@ public struct RECT

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

internal static Rect GetWindowPosition(IntPtr windowHandle)
{
Expand Down Expand Up @@ -340,4 +342,17 @@ public static void ChangeLanguage()
break;
}
}

[DllImport("user32.dll")]
internal static extern IntPtr GetClassLong(IntPtr hWnd, int nIndex);

[DllImport("user32.dll", SetLastError = true)]
internal static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);


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

[DllImport("user32.dll")]
internal static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
}
64 changes: 25 additions & 39 deletions PermaTop/UserControls/WindowPropertyItem.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ public WindowPropertyItem(WindowInfo windowInfo, StackPanel parent)
InitUI();
}

internal void MoveWindow(IntPtr windowHandle, int x, int y)
{
Global.SetWindowPos(windowHandle, IntPtr.Zero, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}

private void InitUI()
{
TitleTxt.Text = WindowInfo.Title.Length > 50 ? WindowInfo.Title[0..50] + "..." : WindowInfo.Title;

GetWindowRect(WindowInfo.Hwnd, out Global.RECT bounds);
Global.GetWindowRect(WindowInfo.Hwnd, out Global.RECT bounds);

TitleToolTip.Content = $"{WindowInfo.Title}\n{bounds.Right - bounds.Left}x{bounds.Bottom - bounds.Top}";

Expand All @@ -76,6 +81,20 @@ private void InitUI()
YTxt.Text = windowPosition.Top.ToString();
}

internal IntPtr GetWindowIconHandle(IntPtr windowHandle, bool largeIcon)
{
int index = largeIcon ? ICON_BIG : ICON_SMALL;
return (IntPtr)Global.SendMessage(windowHandle, WM_GETICON, index, 0);
}

internal BitmapSource GetIconFromHandle(IntPtr iconHandle)
{
if (iconHandle == IntPtr.Zero)
return null;

return Imaging.CreateBitmapSourceFromHIcon(iconHandle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}

private void PinBtn_Click(object sender, RoutedEventArgs e)
{
WindowInfo.IsPinned = !WindowInfo.IsPinned;
Expand Down Expand Up @@ -111,44 +130,11 @@ private void FavBtn_Click(object sender, RoutedEventArgs e)
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOZORDER = 0x0004;

[DllImport("user32.dll")]
private static extern IntPtr GetClassLong(IntPtr hWnd, int nIndex);

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

private IntPtr GetWindowIconHandle(IntPtr windowHandle, bool largeIcon)
{
int index = largeIcon ? ICON_BIG : ICON_SMALL;
return (IntPtr)SendMessage(windowHandle, WM_GETICON, index, 0);
}

private BitmapSource GetIconFromHandle(IntPtr iconHandle)
{
if (iconHandle == IntPtr.Zero)
return null;

return Imaging.CreateBitmapSourceFromHIcon(iconHandle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}

[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);
}

[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hWnd, out Global.RECT lpRect);

private void CloseBtn_Click(object sender, RoutedEventArgs e)
{
try
{
SendMessage(WindowInfo.Hwnd, WM_SYSCOMMAND, (IntPtr)SC_CLOSE, IntPtr.Zero);
Global.SendMessage(WindowInfo.Hwnd, WM_SYSCOMMAND, (IntPtr)SC_CLOSE, IntPtr.Zero);
ParentElement.Children.Remove(this);
}
catch { }
Expand All @@ -160,12 +146,12 @@ private void MaxRestoreBtn_Click(object sender, RoutedEventArgs e)
{
if (!Global.IsWindowMaximized(WindowInfo.Hwnd))
{
SendMessage(WindowInfo.Hwnd, WM_SYSCOMMAND, (IntPtr)SC_MAXIMIZE, IntPtr.Zero);
Global.SendMessage(WindowInfo.Hwnd, WM_SYSCOMMAND, (IntPtr)SC_MAXIMIZE, IntPtr.Zero);
MaxRestoreBtn.Content = "\uF670";
MaxRestoreBtn.FontSize = 18;
return;
}
SendMessage(WindowInfo.Hwnd, WM_SYSCOMMAND, (IntPtr)SC_RESTORE, IntPtr.Zero);
Global.SendMessage(WindowInfo.Hwnd, WM_SYSCOMMAND, (IntPtr)SC_RESTORE, IntPtr.Zero);
MaxRestoreBtn.Content = "\uFA40";
MaxRestoreBtn.FontSize = 14;
}
Expand All @@ -176,7 +162,7 @@ private void MinBtn_Click(object sender, RoutedEventArgs e)
{
try
{
SendMessage(WindowInfo.Hwnd, WM_SYSCOMMAND, (IntPtr)SC_MINIMIZE, IntPtr.Zero);
Global.SendMessage(WindowInfo.Hwnd, WM_SYSCOMMAND, (IntPtr)SC_MINIMIZE, IntPtr.Zero);
}
catch { }
}
Expand Down Expand Up @@ -209,7 +195,7 @@ private void MoveBtn_Click(object sender, RoutedEventArgs e)
{
if (ScreenSelector.SelectedIndex >= 0)
{
GetWindowRect(WindowInfo.Hwnd, out Global.RECT rect);
Global.GetWindowRect(WindowInfo.Hwnd, out Global.RECT rect);
var screen = _screens[ScreenSelector.SelectedIndex];

int centerX = (screen.Bounds.Left + screen.Bounds.Right) / 2;
Expand Down

0 comments on commit e37731d

Please sign in to comment.