Cleaned up WindowPlacement class by moving native Win32 functions to the NativeMethods class

This commit is contained in:
David Sparer
2016-06-03 15:53:11 -06:00
parent 4c34a88a10
commit e5f9fce01e
2 changed files with 65 additions and 81 deletions

View File

@@ -43,7 +43,6 @@ namespace mRemoteNG.App
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, System.Text.StringBuilder lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
@@ -71,6 +70,11 @@ namespace mRemoteNG.App
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetDlgCtrlID(int hwndCtl);
[DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
[DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
#endregion
#region Structures
@@ -85,8 +89,32 @@ namespace mRemoteNG.App
public int cy;
public int flags;
}
public struct WINDOWPLACEMENT
{
public uint length;
public uint flags;
public uint showCmd;
public POINT ptMinPosition;
public POINT ptMaxPosition;
public RECT rcNormalPosition;
}
public struct POINT
{
public long x;
public long y;
}
private struct RECT
{
public long left;
public long top;
public long right;
public long bottom;
}
#endregion
#region Helpers
public static int MAKELONG(int wLow, int wHigh)
{
@@ -139,10 +167,17 @@ namespace mRemoteNG.App
#endregion
#region ShowWindow
public const int SW_HIDE = 0;
public const int SW_SHOWMAXIMIZED = 3;
public const int SW_SHOW = 5;
public const int SW_RESTORE = 9;
public const UInt32 SW_HIDE = 0;
public const UInt32 SW_SHOWNORMAL = 1;
public const UInt32 SW_SHOWMINIMIZED = 2;
public const UInt32 SW_SHOWMAXIMIZED = 3;
public const UInt32 SW_MAXIMIZE = 3;
public const UInt32 SW_SHOWNOACTIVATE = 4;
public const UInt32 SW_SHOW = 5;
public const UInt32 SW_MINIMIZE = 6;
public const UInt32 SW_SHOWMINNOACTIVE = 7;
public const UInt32 SW_SHOWNA = 8;
public const UInt32 SW_RESTORE = 9;
#endregion
#region SetWindowPos / WM_WINDOWPOSCHANGING / WM_WINDOWPOSCHANGED
@@ -232,6 +267,12 @@ namespace mRemoteNG.App
public const int SWP_STATECHANGED = 0x8000;
#endregion
#region Window Placement Flags (WPF)
public const UInt32 WPF_SETMINPOSITION = 0x1;
public const UInt32 WPF_RESTORETOMAXIMIZED = 0x2;
public const UInt32 WPF_ASYNCWINDOWPLACEMENT = 0x4;
#endregion
#region WM_ACTIVATE
/// <summary>
///
@@ -412,7 +453,7 @@ namespace mRemoteNG.App
public const int LB_ERR = -1;
public const int LB_SELECTSTRING = 0x18C;
#endregion
#endregion
}
}

View File

@@ -1,78 +1,21 @@
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using mRemoteNG.App;
namespace mRemoteNG.Tools
{
public class WindowPlacement
{
#region Windows API
#region Functions
[DllImport("user32", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
private static extern bool GetWindowPlacement(System.IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
[DllImport("user32", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
private static extern bool SetWindowPlacement(System.IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
#endregion
#region Structures
private struct WINDOWPLACEMENT
{
public UInt32 length;
public UInt32 flags;
public UInt32 showCmd;
public POINT ptMinPosition;
public POINT ptMaxPosition;
public RECT rcNormalPosition;
}
private struct POINT
{
public long x;
public long y;
}
private struct RECT
{
public long left;
public long top;
public long right;
public long bottom;
}
#endregion
#region Constants
// WINDOWPLACEMENT.flags values
private const UInt32 WPF_SETMINPOSITION = 0x1;
private const UInt32 WPF_RESTORETOMAXIMIZED = 0x2;
private const UInt32 WPF_ASYNCWINDOWPLACEMENT = 0x4;
// WINDOWPLACEMENT.showCmd values
private const UInt32 SW_HIDE = 0;
private const UInt32 SW_SHOWNORMAL = 1;
private const UInt32 SW_SHOWMINIMIZED = 2;
private const UInt32 SW_SHOWMAXIMIZED = 3;
private const UInt32 SW_MAXIMIZE = 3;
private const UInt32 SW_SHOWNOACTIVATE = 4;
private const UInt32 SW_SHOW = 5;
private const UInt32 SW_MINIMIZE = 6;
private const UInt32 SW_SHOWMINNOACTIVE = 7;
private const UInt32 SW_SHOWNA = 8;
private const UInt32 SW_RESTORE = 9;
#endregion
#endregion
#region Private Variables
private Form _form;
#endregion
#region Constructors/Destructors
public WindowPlacement(Form form)
{
_form = form;
}
#endregion
#region Public Properties
public Form Form
{
@@ -90,19 +33,19 @@ namespace mRemoteNG.Tools
{
get
{
WINDOWPLACEMENT windowPlacement = GetWindowPlacement();
return Convert.ToBoolean(windowPlacement.flags & WPF_RESTORETOMAXIMIZED);
NativeMethods.WINDOWPLACEMENT windowPlacement = GetWindowPlacement();
return Convert.ToBoolean(windowPlacement.flags & NativeMethods.WPF_RESTORETOMAXIMIZED);
}
set
{
WINDOWPLACEMENT windowPlacement = GetWindowPlacement();
NativeMethods.WINDOWPLACEMENT windowPlacement = GetWindowPlacement();
if (value)
{
windowPlacement.flags = windowPlacement.flags | WPF_RESTORETOMAXIMIZED;
windowPlacement.flags = windowPlacement.flags | NativeMethods.WPF_RESTORETOMAXIMIZED;
}
else
{
windowPlacement.flags = windowPlacement.flags & ~WPF_RESTORETOMAXIMIZED;
windowPlacement.flags = windowPlacement.flags & ~NativeMethods.WPF_RESTORETOMAXIMIZED;
}
SetWindowPlacement(windowPlacement);
}
@@ -110,17 +53,17 @@ namespace mRemoteNG.Tools
#endregion
#region Private Functions
private WINDOWPLACEMENT GetWindowPlacement()
private NativeMethods.WINDOWPLACEMENT GetWindowPlacement()
{
if (_form == null)
{
throw (new System.NullReferenceException("WindowPlacement.Form is not set."));
throw (new NullReferenceException("WindowPlacement.Form is not set."));
}
WINDOWPLACEMENT windowPlacement = new WINDOWPLACEMENT();
NativeMethods.WINDOWPLACEMENT windowPlacement = new NativeMethods.WINDOWPLACEMENT();
windowPlacement.length = (uint)Marshal.SizeOf(windowPlacement);
try
{
GetWindowPlacement(_form.Handle, ref windowPlacement);
NativeMethods.GetWindowPlacement(_form.Handle, ref windowPlacement);
return windowPlacement;
}
catch (Exception)
@@ -129,16 +72,16 @@ namespace mRemoteNG.Tools
}
}
private bool SetWindowPlacement(WINDOWPLACEMENT windowPlacement)
private bool SetWindowPlacement(NativeMethods.WINDOWPLACEMENT windowPlacement)
{
if (_form == null)
{
throw (new System.NullReferenceException("WindowPlacement.Form is not set."));
throw (new NullReferenceException("WindowPlacement.Form is not set."));
}
windowPlacement.length = (uint)Marshal.SizeOf(windowPlacement);
try
{
return SetWindowPlacement(_form.Handle, ref windowPlacement);
return NativeMethods.SetWindowPlacement(_form.Handle, ref windowPlacement);
}
catch (Exception)
{