From 4adb414a72b57d2a94aaada96daca6f6f583d934 Mon Sep 17 00:00:00 2001 From: Gericom Date: Thu, 6 Nov 2014 18:17:20 +0100 Subject: [PATCH] Moved all DllImports to the Win32Util class --- 3DS/UI/CLYTViewer.cs | 8 +- EveryFileExplorer/EveryFileExplorer.csproj | 1 - EveryFileExplorer/Form1.cs | 75 +----------- EveryFileExplorer/Program.cs | 3 +- EveryFileExplorer/Win32MessageHelper.cs | 39 ------ LibEveryFileExplorer/EveryFileExplorerUtil.cs | 5 +- .../LibEveryFileExplorer.csproj | 1 + LibEveryFileExplorer/UI/FileBrowser.cs | 7 +- LibEveryFileExplorer/Win32Util.cs | 112 ++++++++++++++++++ NDS/UI/NSBMDViewer.cs | 5 +- 10 files changed, 127 insertions(+), 129 deletions(-) delete mode 100644 EveryFileExplorer/Win32MessageHelper.cs create mode 100644 LibEveryFileExplorer/Win32Util.cs diff --git a/3DS/UI/CLYTViewer.cs b/3DS/UI/CLYTViewer.cs index 3b778f7..34f886e 100644 --- a/3DS/UI/CLYTViewer.cs +++ b/3DS/UI/CLYTViewer.cs @@ -12,6 +12,7 @@ using LibEveryFileExplorer.Files; using System.Drawing.Imaging; using NintendoWare.LYT; using System.Runtime.InteropServices; +using LibEveryFileExplorer; namespace _3DS.UI { @@ -23,15 +24,12 @@ namespace _3DS.UI BasicShader BShader = new BasicShader(); ImageList ImageL; - [DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] - private static extern int SetWindowTheme(IntPtr hWnd, string appName, string partList); - public CLYTViewer(CLYT Layout) { this.NWLayout = Layout; InitializeComponent(); - SetWindowTheme(treeView1.Handle, "explorer", null); - SetWindowTheme(treeView2.Handle, "explorer", null); + Win32Util.SetWindowTheme(treeView1.Handle, "explorer", null); + Win32Util.SetWindowTheme(treeView2.Handle, "explorer", null); } private void CLYTViewer_Load(object sender, EventArgs e) diff --git a/EveryFileExplorer/EveryFileExplorer.csproj b/EveryFileExplorer/EveryFileExplorer.csproj index fc4dd76..6afcb6a 100644 --- a/EveryFileExplorer/EveryFileExplorer.csproj +++ b/EveryFileExplorer/EveryFileExplorer.csproj @@ -60,7 +60,6 @@ - Form1.cs diff --git a/EveryFileExplorer/Form1.cs b/EveryFileExplorer/Form1.cs index 43afea5..5572f4f 100644 --- a/EveryFileExplorer/Form1.cs +++ b/EveryFileExplorer/Form1.cs @@ -18,33 +18,6 @@ namespace EveryFileExplorer { public partial class Form1 : Form { - private const int GWL_STYLE = -16; - private const int GWL_EXSTYLE = -20; - - private const int WS_BORDER = 0x00800000; - private const int WS_EX_CLIENTEDGE = 0x00000200; - - private const uint SWP_NOSIZE = 0x0001; - private const uint SWP_NOMOVE = 0x0002; - private const uint SWP_NOZORDER = 0x0004; - private const uint SWP_NOREDRAW = 0x0008; - private const uint SWP_NOACTIVATE = 0x0010; - private const uint SWP_FRAMECHANGED = 0x0020; - private const uint SWP_SHOWWINDOW = 0x0040; - private const uint SWP_HIDEWINDOW = 0x0080; - private const uint SWP_NOCOPYBITS = 0x0100; - private const uint SWP_NOOWNERZORDER = 0x0200; - private const uint SWP_NOSENDCHANGING = 0x0400; - - [DllImport("user32.dll", CharSet = CharSet.Auto)] - private static extern int SetWindowLong(IntPtr hWnd, int Index, int Value); - - [DllImport("user32.dll", CharSet = CharSet.Auto)] - private static extern int GetWindowLong(IntPtr hWnd, int Index); - - [DllImport("user32.dll", ExactSpelling = true)] - private static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); - public Form1() { InitializeComponent(); @@ -66,7 +39,7 @@ namespace EveryFileExplorer MdiClient mdiClient = this.Controls[i] as MdiClient; if (mdiClient != null) { - SetMDIBorderStyle(mdiClient, BorderStyle.None); + Win32Util.SetMDIBorderStyle(mdiClient, BorderStyle.None); } } menuNew.MenuItems.Clear(); @@ -127,40 +100,7 @@ namespace EveryFileExplorer PendingPath = null; } - private void SetMDIBorderStyle(MdiClient mdiClient, BorderStyle value) - { - // Get styles using Win32 calls - int style = GetWindowLong(mdiClient.Handle, GWL_STYLE); - int exStyle = GetWindowLong(mdiClient.Handle, GWL_EXSTYLE); - - // Add or remove style flags as necessary. - switch (value) - { - case BorderStyle.Fixed3D: - exStyle |= WS_EX_CLIENTEDGE; - style &= ~WS_BORDER; - break; - - case BorderStyle.FixedSingle: - exStyle &= ~WS_EX_CLIENTEDGE; - style |= WS_BORDER; - break; - - case BorderStyle.None: - style &= ~WS_BORDER; - exStyle &= ~WS_EX_CLIENTEDGE; - break; - } - - // Set the styles using Win32 calls - SetWindowLong(mdiClient.Handle, GWL_STYLE, style); - SetWindowLong(mdiClient.Handle, GWL_EXSTYLE, exStyle); - - // Update the non-client area. - SetWindowPos(mdiClient.Handle, IntPtr.Zero, 0, 0, 0, 0, - SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | - SWP_NOOWNERZORDER | SWP_FRAMECHANGED); - } + public void BringMDIWindowToFront(Form Dialog) { @@ -380,19 +320,14 @@ namespace EveryFileExplorer panel2.Controls.Clear(); } - [DllImport("user32.dll")] - private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); - - private const int SW_RESTORE = 0x09; - protected override void WndProc(ref Message m) { - if (m.Msg == Win32MessageHelper.WM_COPYDATA) + if (m.Msg == Win32Util.WM_COPYDATA) { - if (WindowState == FormWindowState.Minimized) ShowWindowAsync(Handle, SW_RESTORE); + if (WindowState == FormWindowState.Minimized) Win32Util.ShowWindowAsync(Handle, Win32Util.SW_RESTORE); TopMost = true; TopMost = false; - String path = Win32MessageHelper.GetStringFromMessage(m); + String path = Win32Util.GetStringFromMessage(m); if (path.Length < 1 || !System.IO.File.Exists(path)) return; Program.FileManager.OpenFile(new EFEDiskFile(path)); return; diff --git a/EveryFileExplorer/Program.cs b/EveryFileExplorer/Program.cs index bce061a..0f1aef2 100644 --- a/EveryFileExplorer/Program.cs +++ b/EveryFileExplorer/Program.cs @@ -8,6 +8,7 @@ using System.Reflection; using System.IO; using System.Threading; using System.Runtime.InteropServices; +using LibEveryFileExplorer; namespace EveryFileExplorer { @@ -37,7 +38,7 @@ namespace EveryFileExplorer { String arg0 = ""; if (Arguments.Length > 0) arg0 = Arguments[0]; - Win32MessageHelper.SendString((IntPtr)Win32MessageHelper.HWND_BROADCAST, arg0); + Win32Util.SendString((IntPtr)Win32Util.HWND_BROADCAST, arg0); } } diff --git a/EveryFileExplorer/Win32MessageHelper.cs b/EveryFileExplorer/Win32MessageHelper.cs deleted file mode 100644 index 68f99c2..0000000 --- a/EveryFileExplorer/Win32MessageHelper.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Runtime.InteropServices; -using System.Windows.Forms; - -namespace EveryFileExplorer -{ - class Win32MessageHelper - { - public const int HWND_BROADCAST = 0xffff; - public const int WM_COPYDATA = 0x004A; - - [DllImport("user32")] - static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, ref COPYDATASTRUCT lParam); - - [StructLayout(LayoutKind.Sequential)] - private struct COPYDATASTRUCT - { - public IntPtr dwData; - public int cbData; - [MarshalAs(UnmanagedType.LPWStr)] - public String lpData; - } - - public static bool SendString(IntPtr Handle, String Data) - { - COPYDATASTRUCT c = new COPYDATASTRUCT() { lpData = Data, cbData = Data.Length * 2 + 2 }; - return SendMessage(Handle, WM_COPYDATA, IntPtr.Zero, ref c) == IntPtr.Zero; - } - - public static String GetStringFromMessage(Message m) - { - COPYDATASTRUCT c = (COPYDATASTRUCT)m.GetLParam(typeof(COPYDATASTRUCT)); - return c.lpData; - } - } -} diff --git a/LibEveryFileExplorer/EveryFileExplorerUtil.cs b/LibEveryFileExplorer/EveryFileExplorerUtil.cs index b321e0e..c8452ec 100644 --- a/LibEveryFileExplorer/EveryFileExplorerUtil.cs +++ b/LibEveryFileExplorer/EveryFileExplorerUtil.cs @@ -50,15 +50,12 @@ namespace LibEveryFileExplorer return s; } - [DllImport("Shlwapi.dll", CharSet = CharSet.Auto)] - private static extern long StrFormatByteSize(long fileSize, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder buffer, int bufferSize); - public static ListViewItem GetFileItem(EFEFile File) { Type[] formats = Program.FileManager.GetPossibleFormats(File); ListViewItem i = new ListViewItem(File.Name); StringBuilder b = new StringBuilder(50); - StrFormatByteSize(File.Data.Length, b, 50); + Win32Util.StrFormatByteSize(File.Data.Length, b, 50); i.SubItems.Add(b.ToString()); if (formats.Length == 1) { diff --git a/LibEveryFileExplorer/LibEveryFileExplorer.csproj b/LibEveryFileExplorer/LibEveryFileExplorer.csproj index 772492e..92c1c15 100644 --- a/LibEveryFileExplorer/LibEveryFileExplorer.csproj +++ b/LibEveryFileExplorer/LibEveryFileExplorer.csproj @@ -108,6 +108,7 @@ GameDataSectionViewerBase.cs + diff --git a/LibEveryFileExplorer/UI/FileBrowser.cs b/LibEveryFileExplorer/UI/FileBrowser.cs index aef631d..58e5ef3 100644 --- a/LibEveryFileExplorer/UI/FileBrowser.cs +++ b/LibEveryFileExplorer/UI/FileBrowser.cs @@ -16,14 +16,11 @@ namespace LibEveryFileExplorer.UI ImageList images; ImageList images2; - [DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] - private static extern int SetWindowTheme(IntPtr hWnd, string appName, string partList); - public FileBrowser() { InitializeComponent(); - SetWindowTheme(treeView1.Handle, "explorer", null); - SetWindowTheme(listView1.Handle, "explorer", null); + Win32Util.SetWindowTheme(treeView1.Handle, "explorer", null); + Win32Util.SetWindowTheme(listView1.Handle, "explorer", null); } private void FileBrowser_Load(object sender, EventArgs e) diff --git a/LibEveryFileExplorer/Win32Util.cs b/LibEveryFileExplorer/Win32Util.cs new file mode 100644 index 0000000..04d5615 --- /dev/null +++ b/LibEveryFileExplorer/Win32Util.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.InteropServices; +using System.Windows.Forms; + +namespace LibEveryFileExplorer +{ + public class Win32Util + { + public const int HWND_BROADCAST = 0xffff; + public const int WM_COPYDATA = 0x004A; + + private const int GWL_STYLE = -16; + private const int GWL_EXSTYLE = -20; + + private const int WS_BORDER = 0x00800000; + private const int WS_EX_CLIENTEDGE = 0x00000200; + + public const int SW_RESTORE = 0x09; + + private const uint SWP_NOSIZE = 0x0001; + private const uint SWP_NOMOVE = 0x0002; + private const uint SWP_NOZORDER = 0x0004; + private const uint SWP_NOREDRAW = 0x0008; + private const uint SWP_NOACTIVATE = 0x0010; + private const uint SWP_FRAMECHANGED = 0x0020; + private const uint SWP_SHOWWINDOW = 0x0040; + private const uint SWP_HIDEWINDOW = 0x0080; + private const uint SWP_NOCOPYBITS = 0x0100; + private const uint SWP_NOOWNERZORDER = 0x0200; + private const uint SWP_NOSENDCHANGING = 0x0400; + + [DllImport("Shlwapi.dll", CharSet = CharSet.Auto)] + public static extern long StrFormatByteSize(long fileSize, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder buffer, int bufferSize); + + [DllImport("user32", CharSet = CharSet.Auto)] + private static extern int GetWindowLong(IntPtr hWnd, int Index); + + [DllImport("user32")] + private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, ref COPYDATASTRUCT lParam); + + [DllImport("user32", CharSet = CharSet.Auto)] + private static extern int SetWindowLong(IntPtr hWnd, int Index, int Value); + + [DllImport("user32", ExactSpelling = true)] + private static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); + + [DllImport("user32.dll")] + public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); + + [DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] + public static extern int SetWindowTheme(IntPtr hWnd, string appName, string partList); + + [StructLayout(LayoutKind.Sequential)] + private struct COPYDATASTRUCT + { + public IntPtr dwData; + public int cbData; + [MarshalAs(UnmanagedType.LPWStr)] + public String lpData; + } + + public static bool SendString(IntPtr Handle, String Data) + { + COPYDATASTRUCT c = new COPYDATASTRUCT() { lpData = Data, cbData = Data.Length * 2 + 2 }; + return SendMessage(Handle, WM_COPYDATA, IntPtr.Zero, ref c) == IntPtr.Zero; + } + + public static String GetStringFromMessage(Message m) + { + COPYDATASTRUCT c = (COPYDATASTRUCT)m.GetLParam(typeof(COPYDATASTRUCT)); + return c.lpData; + } + + public static void SetMDIBorderStyle(MdiClient mdiClient, BorderStyle value) + { + // Get styles using Win32 calls + int style = GetWindowLong(mdiClient.Handle, GWL_STYLE); + int exStyle = GetWindowLong(mdiClient.Handle, GWL_EXSTYLE); + + // Add or remove style flags as necessary. + switch (value) + { + case BorderStyle.Fixed3D: + exStyle |= WS_EX_CLIENTEDGE; + style &= ~WS_BORDER; + break; + + case BorderStyle.FixedSingle: + exStyle &= ~WS_EX_CLIENTEDGE; + style |= WS_BORDER; + break; + + case BorderStyle.None: + style &= ~WS_BORDER; + exStyle &= ~WS_EX_CLIENTEDGE; + break; + } + + // Set the styles using Win32 calls + SetWindowLong(mdiClient.Handle, GWL_STYLE, style); + SetWindowLong(mdiClient.Handle, GWL_EXSTYLE, exStyle); + + // Update the non-client area. + SetWindowPos(mdiClient.Handle, IntPtr.Zero, 0, 0, 0, 0, + SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | + SWP_NOOWNERZORDER | SWP_FRAMECHANGED); + } + } +} diff --git a/NDS/UI/NSBMDViewer.cs b/NDS/UI/NSBMDViewer.cs index a41caee..d188757 100644 --- a/NDS/UI/NSBMDViewer.cs +++ b/NDS/UI/NSBMDViewer.cs @@ -25,14 +25,11 @@ namespace NDS.UI NSBMD mod; NSBTX tex = null; - [DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] - private static extern int SetWindowTheme(IntPtr hWnd, string appName, string partList); - public NSBMDViewer(NSBMD mod) { this.mod = mod; InitializeComponent(); - SetWindowTheme(treeView1.Handle, "explorer", null); + Win32Util.SetWindowTheme(treeView1.Handle, "explorer", null); ImageL = new ImageList(); ImageL.ColorDepth = ColorDepth.Depth32Bit; ImageL.ImageSize = new Size(16, 16);