动态设置/删除WinForms表单的CS_DROPSHADOW样式(C#) [英] Setting/removing CS_DROPSHADOW style for a WinForms form dynamically (C#)

查看:376
本文介绍了动态设置/删除WinForms表单的CS_DROPSHADOW样式(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to do that? I could not find any useful sample for C#. I know I should use SetClassLong/SetClassLongPtr, but here is the definition I only found: http://www.pinvoke.net/default.aspx/user32/SetClassLongPtr.html.

显然,我应该使用GCL_STYLE调用GetClassLongPtr以读取当前样式标志,添加或排除CS_DROPSHADOW,然后使用更改后的标志值调用SetClassLongPtr.但是,从PInvoke的定义来看,它并不是微不足道的,特别是考虑到32/64位系统.

Obviously, I should call GetClassLongPtr with GCL_STYLE to read the current style flags, add or exclude CS_DROPSHADOW, and then call SetClassLongPtr with the changed flag value. But looking at that PInvoke definition, it is not trivial, especially taking into account 32/64-bit systems.

任何人都可以提供链接或一个很好的例子吗?而且,请不要提供覆盖CreateParams的示例,因为这不适用于我们的动态方案.也许还有另一种[托管]方法可以做到这一点?

Can anybody give a link or a good example of this? And please, do not provide samples with overwriting CreateParams as this does not work for our dynamic scenario. Maybe, there is another [managed] way to do that?

推荐答案

这是我设法写的东西:

    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; 
    }

如果我错了,请纠正我.

Correct me if I'm wrong.

这篇关于动态设置/删除WinForms表单的CS_DROPSHADOW样式(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆