/// <summary> /// Enables you to produce special effects when showing this <see cref="ContextMenuStrip"/>. /// </summary> /// <param name="contextMenuStrip"> /// The <see cref="ContextMenuStrip"/>. /// </param> /// <param name="animation"> /// The type of animation. /// </param> /// <param name="time"> /// The time it takes to play the animation, in milliseconds. /// <para> /// Please note that this parameter is ignored if the animation is set to /// <see cref="ContextMenuStripExAnimation.Default"/>. /// </para> /// </param> public static void EnableAnimation(this ContextMenuStrip contextMenuStrip, ContextMenuStripExAnimation animation = ContextMenuStripExAnimation.Default, int time = 200) { if (!(contextMenuStrip is ContextMenuStrip cms)) { return; } var settings = new KeyValuePair <int, WinApi.AnimateWindowFlags>(time, (WinApi.AnimateWindowFlags)animation); if (EnabledAnimation.ContainsKey(cms)) { EnabledAnimation[cms] = settings; return; } EnabledAnimation.Add(cms, settings); var loaded = false; cms.Opening += (sender, args) => { if (animation != ContextMenuStripExAnimation.Default) { WinApi.NativeMethods.AnimateWindow(cms.Handle, EnabledAnimation[cms].Key, EnabledAnimation[cms].Value); if (loaded) { return; } loaded = true; cms.Refresh(); return; } cms.Opacity = 0d; var timer = new Timer { Interval = 1, Enabled = true }; timer.Tick += (s, a) => { if (cms.Opacity < 1d) { cms.Opacity += .1d; return; } timer.Dispose(); }; }; }
/// <summary> /// Enables you to produce special effects when showing this /// <see cref="ContextMenuStrip"/>. /// </summary> /// <param name="contextMenuStrip"> /// The <see cref="ContextMenuStrip"/>. /// </param> /// <param name="animation"> /// The type of animation. /// </param> /// <param name="time"> /// The time it takes to play the animation, in milliseconds. /// <para> /// Please note that this parameter is ignored if the animation is set to /// <see cref="ContextMenuStripExAnimation.Default"/>. /// </para> /// </param> public static void EnableAnimation(this ContextMenuStrip contextMenuStrip, ContextMenuStripExAnimation animation = ContextMenuStripExAnimation.Default, int time = 200) { if (!(contextMenuStrip is { } cms)) { return; } var hash = cms.GetHashCode() + nameof(EnableAnimation).GetHashCode(); if (HashList.Contains(hash)) { return; } HashList.Add(hash); var loaded = false; var timer = new Timer { Interval = 1, Enabled = false }; timer.Tick += OnTick; cms.Opening += OnOpening; cms.Disposed += OnDisposed; void OnTick(object sender, EventArgs e) { if (!(sender is Timer owner)) { return; } if (cms.Opacity < 1d) { cms.Opacity += .1d; return; } owner.Enabled = false; cms.Opacity = 1d; } void OnOpening(object sender, CancelEventArgs e) { if (!(sender is ContextMenuStrip owner) || e.Cancel) { return; } if (animation != ContextMenuStripExAnimation.Default) { WinApi.NativeMethods.AnimateWindow(owner.Handle, time, (WinApi.AnimateWindowFlags)animation); if (loaded) { return; } loaded = true; owner.Refresh(); return; } owner.Opacity = 0d; timer.Enabled = true; } void OnDisposed(object sender, EventArgs e) => timer.Dispose(); }