有效使用CreateParams的按位运算符,不期望的行为?

时间:2010-09-11 06:01:37

标签: c# bit-manipulation createparams

我正在为ProgressBar控件编写一个包装器(不是真正的包装器,而是正确实现Vista功能)。这是我的代码:

    /// <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上设置值之前我应该​​做些什么,或者我的按位操作(或者我的代码实际上)是否正确?

更新感谢Jon Skeet 能够完全修复和工作,并使用应用的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();
        }
    }

1 个答案:

答案 0 :(得分:4)

|操作是正确的(虽然我使用|=),^不是。

此:

cp.Style = cp.Style ^ PBS_SMOOTHREVERSE;

只会反转现有的内容。你想要:

cp.Style &= ~PBS_SMOOTHREVERSE;

这就是说,“用掩码掩盖值,所有掩码都设置为除了 PBS_SMOOTHREVERSE”。