2010-09-11 27 views
0

我正在爲ProgressBar控件編寫一個包裝器(不是真正的包裝器,但正確實現了Vista功能)。這裏是我的代碼:對於CreateParams有效使用按位運算符,意外行爲?

/// <summary> 
    /// Encapsulates the information needed when creating a control 
    /// </summary> 
    protected override CreateParams CreateParams { 
     [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] 
     get { 
      CreateParams cp = base.CreateParams; 
      if (SmoothReverse) { 
       // Add using bitwise OR 
       cp.Style = cp.Style | PBS_SMOOTHREVERSE; 
      } 
      else { 
       // Remove using bitwise XOR 
       cp.Style = cp.Style^PBS_SMOOTHREVERSE; 
      } 
      if (Vertical) { 
       // Add using bitwise OR 
       cp.Style = cp.Style | PBS_VERTICAL; 
      } 
      else { 
       // Remove using bitwise XOR 
       cp.Style = cp.Style^PBS_VERTICAL; 
      } 
      return cp; 
     } 
    } 

    private bool m_SmoothReverse = false; 
    /// <summary> 
    /// Gets or sets a System.Boolean value indicating whether the SmoothReverse style is used 
    /// </summary> 
    [Category("Behavior")] 
    [DefaultValue(false)] 
    [Description("Gets or sets a System.Boolean value indicating whether the SmoothReverse style is used")] 
    public bool SmoothReverse { 
     get { 
      return m_SmoothReverse; 
     } 
     set { 
      m_SmoothReverse = value; 
     } 
    } 

    private bool m_Vertical = false; 
    /// <summary> 
    /// Gets or sets a System.Boolean value indicating whether the progress bar will be rendered vertically 
    /// </summary> 
    [Category("Behavior")] 
    [DefaultValue(false)] 
    [Description("Gets or sets a System.Boolean value indicating whether the progress bar will be rendered vertically")] 
    public bool Vertical { 
     get { 
      return m_Vertical; 
     } 
     set { 
      m_Vertical = value; 
     } 
    } 

雖然,當你放下控件的窗體上它開始爲垂直,與PBS_SMOOTH。所以我的問題是,在使用按位操作設置CreateParams.Style上的值之前應該做些什麼額外的檢查,或者是我的按位操作(或我的代碼作爲事實)甚至是正確的?

更新感謝喬恩斯基特 能得到這個完全固定的和工作,並與應用UpdateStyles(),以在性能需求力的新樣式適用(),控制現在可以按預期和拋光:)

/// <summary> 
    /// Encapsulates the information needed when creating a control 
    /// </summary> 
    protected override CreateParams CreateParams { 
     [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] 
     get { 
      CreateParams cp = base.CreateParams; 
      if (SmoothReverse) { 
       cp.Style |= PBS_SMOOTHREVERSE; 
      } 
      else { 
       cp.Style &= ~PBS_SMOOTHREVERSE; 
      } 
      if (Vertical) { 
       cp.Style |= PBS_VERTICAL; 
      } 
      else { 
       cp.Style &= ~PBS_VERTICAL; 
      } 
      return cp; 
     } 
    } 

    private bool m_SmoothReverse = false; 
    /// <summary> 
    /// Gets or sets a System.Boolean value indicating whether the SmoothReverse style is used 
    /// </summary> 
    [Category("Behavior")] 
    [DefaultValue(false)] 
    [Description("Gets or sets a System.Boolean value indicating whether the SmoothReverse style is used")] 
    public bool SmoothReverse { 
     get { 
      return m_SmoothReverse; 
     } 
     set { 
      m_SmoothReverse = value; 
      UpdateStyles(); 
     } 
    } 

    private bool m_Vertical = false; 
    /// <summary> 
    /// Gets or sets a System.Boolean value indicating whether the progress bar will be rendered vertically 
    /// </summary> 
    [Category("Behavior")] 
    [DefaultValue(false)] 
    [Description("Gets or sets a System.Boolean value indicating whether the progress bar will be rendered vertically")] 
    public bool Vertical { 
     get { 
      return m_Vertical; 
     } 
     set { 
      m_Vertical = value; 
      UpdateStyles(); 
     } 
    } 

回答

4

|操作是正確的(雖然我會用|=),該^不是。

此:

cp.Style = cp.Style^PBS_SMOOTHREVERSE; 

只會顛倒一切已經在那裏了。你想:

cp.Style &= ~PBS_SMOOTHREVERSE; 

這話說,「與具有所有位設置除了 PBS_SMOOTHREVERSE面具掩蓋值」。

+0

@David:Urgh - 我還沒有看到它是一個現有的屬性。這太可怕了 - 但不是你的錯:)將編輯出那一點。 – 2010-09-11 07:11:57

+0

壞的雙向飛碟!開玩笑,每個人都會打水仗;沒有汗水! :) – 2010-09-11 07:20:54

+0

CreateParams導致我的CPU使用率降低到30%以上,用於避免flikering,而我用動態控件推廣TableLayout面板,無論如何減少這..... ....., – Dhana 2011-08-26 06:39:11