1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Uni_Trend.MSO7000X.Common.Helper
- {
- public static class DesignTimeHelper
- {
- private static Boolean? _isAssemblyVisualStudio;
- private static Boolean? _isLicenseDesignTime;
- private static Boolean? _isProcessDevEnv;
- private static Boolean? _mIsDesignerHosted;
- /// <summary>
- /// Property <see cref="Form.DesignMode"/> does not correctly report if a nested <see cref="UserControl"/>
- /// is in design mode. InDesignMode is a corrected that property which .
- /// (see https://connect.microsoft.com/VisualStudio/feedback/details/553305
- /// and https://stackoverflow.com/a/2693338/238419 )
- /// </summary>
- public static Boolean InDesignMode(this Control userControl, String source = null)
- => IsLicenseDesignTime
- || IsProcessDevEnv
- || IsExecutingAssemblyVisualStudio
- || IsDesignerHosted(userControl);
- private static Boolean IsExecutingAssemblyVisualStudio
- => _isAssemblyVisualStudio
- ?? (_isAssemblyVisualStudio = Assembly
- .GetExecutingAssembly()
- .Location.Contains(value: "VisualStudio"))
- .Value;
- private static Boolean IsLicenseDesignTime
- => _isLicenseDesignTime
- ?? (_isLicenseDesignTime = LicenseManager.UsageMode == LicenseUsageMode.Designtime)
- .Value;
- private static Boolean IsDesignerHosted(
- Control control)
- {
- if (_mIsDesignerHosted.HasValue)
- return _mIsDesignerHosted.Value;
- while (control != null)
- {
- if (control.Site?.DesignMode == true)
- {
- _mIsDesignerHosted = true;
- return true;
- }
- control = control.Parent;
- }
- _mIsDesignerHosted = false;
- return false;
- }
- private static Boolean IsProcessDevEnv
- => _isProcessDevEnv
- ?? (_isProcessDevEnv = Process.GetCurrentProcess()
- .ProcessName == "devenv")
- .Value;
- }
- }
|