动态设置/删除WinForms表单的CS_DROPSHADOW样式(C#)

时间:2012-11-19 13:53:23

标签: winforms forms shadow setclasslong

怎么做?我找不到任何有用的C#样本。我知道我应该使用SetClassLong / SetClassLongPtr,但这里是我找到的定义:http://www.pinvoke.net/default.aspx/user32/SetClassLongPtr.html

显然,我应该使用GCL_STYLE调用GetClassLongPtr来读取当前样式标志,添加或排除CS_DROPSHADOW,然后使用更改的标志值调用SetClassLongPtr。但是看看PInvoke的定义,这并不是一件容易的事情,特别是考虑到32/64位系统。

任何人都可以给出一个链接或一个很好的例子吗?请不要提供覆盖CreateParams的示例,因为这对我们的动态场景不起作用。也许,还有另一种[管理]方式吗?

1 个答案:

答案 0 :(得分:0)

这是我设法编写的内容:

    private void SetSizeableCore(bool value)
    {
        fSizeable = value;
        if (value)
        {
            FormBorderStyle = FormBorderStyle.SizableToolWindow;
            DockPadding.All = 0;
            System.Version ver = Environment.OSVersion.Version;
            // Always for WinXP family, but for higher systems only if the aero theme is not in effect
            bool needShadow = ((ver.Major == 5) && (ver.Minor > 0)) || ((ver.Major > 5) && !IsAeroThemeEnabled());
            SetShadowFlag(needShadow);
        }
        else
        {
            FormBorderStyle = FormBorderStyle.None;
            DockPadding.All = 1;
            SetShadowFlag(true);
        }
    }

    private void SetShadowFlag(bool hasShadow)
    {
        if (!IsDropShadowSupported())
            return;
        System.Runtime.InteropServices.HandleRef myHandleRef = new System.Runtime.InteropServices.HandleRef(this, this.Handle);
        int myStyle = iGNativeMethods.GetClassLongPtr(myHandleRef, iGNativeMethods.CS_DROPSHADOW).ToInt32();
        if (hasShadow)
            myStyle |= iGNativeMethods.CS_DROPSHADOW;
        else
            myStyle &= ~iGNativeMethods.CS_DROPSHADOW;
        iGNativeMethods.SetClassLong(myHandleRef, iGNativeMethods.GCL_STYLE, new IntPtr(myStyle));
    }

    private bool IsDropShadowSupported()
    {
        // Win2000 does not have this feature
        if (Environment.OSVersion.Version <= new Version(5, 0))
            return false;
        bool myResult = false;
        iGNativeMethods.SystemParametersInfo(iGNativeMethods.SPI_GETDROPSHADOW, 0, ref myResult, 0);
        return myResult;
    }

    private bool IsAeroThemeEnabled()
    {
        if (Environment.OSVersion.Version.Major > 5)
        {
            bool aeroEnabled;
            iGNativeMethods.DwmIsCompositionEnabled(out aeroEnabled);
            return aeroEnabled;
        }
        return false; 
    }

如果我错了,请纠正我。

相关问题