From 62ebe4176f867fb7616cbe5f9858d7bfce7e9d8b Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Mon, 1 Oct 2018 10:44:24 -0700 Subject: [PATCH] Replace BUILD_FOR_WINDOWS with a runtime check Mono runs the same executable, so #if isn't useful. --- CommonWinForms/CommonWinForms.csproj | 4 +-- CommonWinForms/NativeMethods.cs | 2 -- CommonWinForms/WinFormsExtensions.cs | 50 +++++++++++++++++++--------- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/CommonWinForms/CommonWinForms.csproj b/CommonWinForms/CommonWinForms.csproj index ea54c28..10e868e 100644 --- a/CommonWinForms/CommonWinForms.csproj +++ b/CommonWinForms/CommonWinForms.csproj @@ -18,7 +18,7 @@ full false bin\Debug\ - TRACE;DEBUG;BUILD_FOR_WINDOWS + TRACE;DEBUG prompt 4 @@ -26,7 +26,7 @@ pdbonly true bin\Release\ - TRACE;BUILD_FOR_WINDOWS + TRACE prompt 4 diff --git a/CommonWinForms/NativeMethods.cs b/CommonWinForms/NativeMethods.cs index dc5847e..90700a5 100644 --- a/CommonWinForms/NativeMethods.cs +++ b/CommonWinForms/NativeMethods.cs @@ -17,7 +17,6 @@ using System; using System.Runtime.InteropServices; using System.Windows.Forms; -#if BUILD_FOR_WINDOWS // I don't know if other platforms will emulate the LVITEM stuff namespace CommonWinForms { // From https://stackoverflow.com/questions/1019388/ @@ -82,4 +81,3 @@ namespace CommonWinForms { } } } -#endif //BUILD_FOR_WINDOWS diff --git a/CommonWinForms/WinFormsExtensions.cs b/CommonWinForms/WinFormsExtensions.cs index 9f54ea0..662e08b 100644 --- a/CommonWinForms/WinFormsExtensions.cs +++ b/CommonWinForms/WinFormsExtensions.cs @@ -37,6 +37,26 @@ namespace CommonWinForms { /// Add functions to select and deselect all items. /// public static class ListViewExtensions { + // I don't know if NativeMethods is going to work on other platforms, but I assume + // not. I'm not sure how expensive the runtime check is, so cache the result. + private enum DNY { Dunno = 0, No, Yes }; + private static DNY IsPlatformWindows; + private static bool IsWindows { + get { + if (IsPlatformWindows == DNY.Dunno) { + // This is the .NET framework 4.6 way. Later versions (4.7, netcommon) + // prefer the RuntimeInformation.IsOSPlatform() approach. + OperatingSystem os = Environment.OSVersion; + if (os.Platform == PlatformID.Win32NT) { + IsPlatformWindows = DNY.Yes; + } else { + IsPlatformWindows = DNY.No; + } + } + return (IsPlatformWindows == DNY.Yes); + } + } + /// /// Selects all items in the list view. /// @@ -50,23 +70,23 @@ namespace CommonWinForms { // https://stackoverflow.com/questions/9039989/ // https://stackoverflow.com/questions/1019388/ -#if BUILD_FOR_WINDOWS // defined in our project properties - NativeMethods.SelectAllItems(listView); -#else - try { - Application.UseWaitCursor = true; - Cursor.Current = Cursors.WaitCursor; - listView.BeginUpdate(); - int max = listView.VirtualListSize; - for (int i = 0; i < max; i++) { - //codeListView.Items[i].Selected = true; - listView.SelectedIndices.Add(i); + if (IsWindows) { + NativeMethods.SelectAllItems(listView); + } else { + try { + Application.UseWaitCursor = true; + Cursor.Current = Cursors.WaitCursor; + listView.BeginUpdate(); + int max = listView.VirtualListSize; + for (int i = 0; i < max; i++) { + //codeListView.Items[i].Selected = true; + listView.SelectedIndices.Add(i); + } + } finally { + listView.EndUpdate(); + Application.UseWaitCursor = false; } - } finally { - listView.EndUpdate(); - Application.UseWaitCursor = false; } -#endif } ///