【问题标题】:C# - How to reduce Form's Opacity onDeactivate eventC# - 如何减少表单的不透明度 onDeactivate 事件
【发布时间】:2020-09-30 05:55:48
【问题描述】:

我的目的是减少 Opacity onDeactivate 事件,因为表单具有“留在顶部”设置。

我试过这样做:

        protected override void OnDeactivate(EventArgs e)
        {
            base.OnDeactivate(e);
            Opacity = 0.7;
        }

一切正常(我单击另一个窗口,我的表单的不透明度降低),直到我关闭表单。如果我关闭表单,我会收到此错误:

System.ComponentModel.Win32Exception (0x80004005):参数不正确 在 System.Windows.Forms.Form.UpdateLayered() 在 System.Windows.Forms.Form.set_Opacity(双值) 在 C:\Users\lrena\source\repos\Hazar\Hazar\Form1.cs:riga 中的 Hazar.Form1.OnDeactivate(EventArgs e) 中 在 System.Windows.Forms.Form.set_Active(布尔值) 在 System.Windows.Forms.Form.WmActivate(Message&m) 在 System.Windows.Forms.Form.WndProc(Message&m) 在 C:\Users\lrena\source\repos\Hazar\Hazar\Form1.cs:riga 81 中的 Hazar.Form1.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

【问题讨论】:

    标签: c# event-handling


    【解决方案1】:

    您收到此异常是因为您尝试访问正在处理的控件的属性。 Deactivate 事件也在FormClosed 事件之后引发。这意味着,在设置或调用任何属性或方法之前,您需要检查 OnDeactivate 委托中的 Control.Disposing 属性:

    protected override void OnDeactivate(EventArgs e)
    {
        base.OnDeactivate(e);
    
        if (!this.Disposing)
            Opacity = 0.7;
    }
    

    如果控件(在您的情况下为 Form)已被处置,您也可能会从返回 trueControl.IsDisposed 属性中受益。如果有的话,用它来检查全局/类变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-01
      • 2017-06-20
      • 2013-03-15
      • 1970-01-01
      • 2013-05-13
      • 2017-07-01
      • 1970-01-01
      • 2011-12-04
      相关资源
      最近更新 更多