【问题标题】:C# Transparent GUIC# 透明图形用户界面
【发布时间】:2009-05-18 18:29:02
【问题描述】:

大家好。我想知道如何在 C# 中制作一个“透明”的 GUI。现在我不是在谈论透明度键等。

我想制作一个使用 vistas 航空主题的窗口,但我只是希望它显示更多的 seethru 航空外观,而不是表单中的控件。我还想从窗口中删除所有按钮、图标和文本。我该怎么做?

编辑...我找到了我想要创建的完美示例。在 vista 上加载 windows 移动中心。我怎样才能创建这样的东西,但没有盒子。

【问题讨论】:

    标签: c# user-interface transparency


    【解决方案1】:

    如何进入您的客户区

    这是我几年前为我们的 Windows 应用程序执行此操作时使用的 API。

    查看方法 ExtendGlassIntoClientArea(Form form, int leftMargin, int topMargin, int rightMargin, int bottomMargin)

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace UserInterface
    {
        public enum TextStyle
        {
            Normal,
            Glowing
        }
    
        public static class Glass
        {
            [DllImport("dwmapi.dll")]
            private static extern void DwmIsCompositionEnabled(ref bool pfEnabled);
            [DllImport("dwmapi.dll")]
            private static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMargins);
            [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
            private static extern IntPtr CreateCompatibleDC(IntPtr hDC);
            [DllImport("gdi32.dll", ExactSpelling = true)]
            private static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
            [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
            private static extern bool DeleteObject(IntPtr hObject);
            [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
            private static extern bool DeleteDC(IntPtr hdc);
            [DllImport("gdi32.dll")]
            private static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
            [DllImport("UxTheme.dll", CharSet = CharSet.Unicode)]
            private static extern int DrawThemeTextEx(IntPtr hTheme, IntPtr hdc, int iPartId, int iStateId, string text, int iCharCount, int dwFlags, ref RECT pRect, ref DTTOPTS pOptions);
            [DllImport("gdi32.dll")]
            static extern IntPtr CreateDIBSection(IntPtr hdc, [In] ref BITMAPINFO pbmi, uint iUsage, out IntPtr ppvBits, IntPtr hSection, uint dwOffset);
    
            [StructLayout(LayoutKind.Sequential)]
            private struct DTTOPTS
            {
                public int dwSize;
                public int dwFlags;
                public int crText;
                public int crBorder;
                public int crShadow;
                public int iTextShadowType;
                public POINT ptShadowOffset;
                public int iBorderSize;
                public int iFontPropId;
                public int iColorPropId;
                public int iStateId;
                public bool fApplyOverlay;
                public int iGlowSize;
                public int pfnDrawTextCallback;
                public IntPtr lParam;
            }
    
            private const int DTT_COMPOSITED = 8192;
            private const int DTT_GLOWSIZE = 2048;
            private const int DTT_TEXTCOLOR = 1;
    
            [StructLayout(LayoutKind.Sequential)]
            private struct POINT
            {
                public POINT(int x, int y)
                {
                    this.x = x;
                    this.y = y;
                }
    
                public int x;
                public int y;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private class BITMAPINFO
            {
                public int biSize;
                public int biWidth;
                public int biHeight;
                public short biPlanes;
                public short biBitCount;
                public int biCompression;
                public int biSizeImage;
                public int biXPelsPerMeter;
                public int biYPelsPerMeter;
                public int biClrUsed;
                public int biClrImportant;
                public byte bmiColors_rgbBlue;
                public byte bmiColors_rgbGreen;
                public byte bmiColors_rgbRed;
                public byte bmiColors_rgbReserved;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct MARGINS
            {
                public int left, right, top, bottom;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct RECT
            {
                public int Left;
                public int Top;
                public int Right;
                public int Bottom;
    
                public RECT(int left, int top, int right, int bottom)
                {
                    Left = left;
                    Top = top;
                    Right = right;
                    Bottom = bottom;
                }
    
                public RECT(Rectangle rectangle)
                {
                    Left = rectangle.X;
                    Top = rectangle.Y;
                    Right = rectangle.Right;
                    Bottom = rectangle.Bottom;
                }
    
                public Rectangle ToRectangle()
                {
                    return new Rectangle(Left, Top, Right - Left, Bottom - Top);
                }
    
                public override string ToString()
                {
                    return "Left: " + Left + ", " + "Top: " + Top + ", Right: " + Right + ", Bottom: " + Bottom;
                }
            }
            public static bool IsCompositionEnabled
            {
                get
                {
                    if (Environment.OSVersion.Version.Major < 6)
                        return false;
    
                    bool compositionEnabled = false;
                    DwmIsCompositionEnabled(ref compositionEnabled);
                    return compositionEnabled;
                }
            }
    
            public static void ExtendGlassIntoClientArea(Form form, int leftMargin, int topMargin, int rightMargin, int bottomMargin)
            {
                MARGINS m = new MARGINS();
                m.left = leftMargin;
                m.right = rightMargin;
                m.top = topMargin;
                m.bottom = bottomMargin;
    
                DwmExtendFrameIntoClientArea(form.Handle, ref m);
            }
    
            public static void DrawText(Graphics graphics, string text, Font font, Rectangle bounds, Color color, TextFormatFlags flags)
            {
                DrawText(graphics, text, font, bounds, color, flags, TextStyle.Normal);
            }
    
            public static void DrawText(Graphics graphics, string text, Font font, Rectangle bounds, Color color, TextFormatFlags flags, TextStyle textStyle)
            {
                IntPtr primaryHdc = graphics.GetHdc();
    
                // Create a memory DC so we can work offscreen
                IntPtr memoryHdc = CreateCompatibleDC(primaryHdc);
    
                // Create a device-independent bitmap and select it into our DC
                BITMAPINFO info = new BITMAPINFO();
                info.biSize = Marshal.SizeOf(info);
                info.biWidth = bounds.Width;
                info.biHeight = -bounds.Height;
                info.biPlanes = 1;
                info.biBitCount = 32;
                info.biCompression = 0; // BI_RGB
                int ppvBits;
                IntPtr dib = CreateDIBSection(primaryHdc, ref info, 0, out ppvBits, IntPtr.Zero, 0);
                SelectObject(memoryHdc, dib);
    
                // Create and select font
                IntPtr fontHandle = font.ToHfont();
                SelectObject(memoryHdc, fontHandle);
    
                // Draw glowing text
                System.Windows.Forms.VisualStyles.VisualStyleRenderer renderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active);
                DTTOPTS dttOpts = new DTTOPTS();
                dttOpts.dwSize = Marshal.SizeOf(typeof(DTTOPTS));
    
                if (textStyle == TextStyle.Glowing)
                {
                    dttOpts.dwFlags = DTT_COMPOSITED | DTT_GLOWSIZE | DTT_TEXTCOLOR;
                }
                else
                {
                    dttOpts.dwFlags = DTT_COMPOSITED | DTT_TEXTCOLOR;
                }
                dttOpts.crText = ColorTranslator.ToWin32(color);
                dttOpts.iGlowSize = 8; // This is about the size Microsoft Word 2007 uses
                RECT textBounds = new RECT(0, 0, bounds.Right - bounds.Left, bounds.Bottom - bounds.Top);
                DrawThemeTextEx(renderer.Handle, memoryHdc, 0, 0, text, -1, (int)flags, ref textBounds, ref dttOpts);
    
                // Copy to foreground
                const int SRCCOPY = 0x00CC0020;
                BitBlt(primaryHdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, memoryHdc, 0, 0, SRCCOPY);
    
                // Clean up
                DeleteObject(fontHandle);
                DeleteObject(dib);
                DeleteDC(memoryHdc);
    
                graphics.ReleaseHdc(primaryHdc);
            }
    
        }
    }
    

    【讨论】:

    • 我在 IntPtr dib = CreateDIBSection(primaryHdc, info, 0, 0, IntPtr.Zero, 0); 上收到 AccessViolationException。有什么想法吗?
    • 请添加一个如何使用DrawText的例子(你自己的方法)。
    【解决方案2】:

    如果是 WPF,则需要打开透明度,并为背景设置正确的值(此外,WPF 本身就支持 AERO 用于边框等)。

    对于传统的 winforms... 事情开始变得艰难。

    【讨论】:

      【解决方案3】:

      另外,windows 窗体的属性总是Form.Opacity。

      【讨论】:

      • 这在我的项目中稍后会出现。目前我想创建一个看起来像 windows 移动中心但没有内容的窗口
      【解决方案4】:

      您是否尝试在另一个问题中检查 this 哪个是 referred ?

      【讨论】:

      • 我看了看,但对于我想要实现的目标来说似乎太复杂了。
      猜你喜欢
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      • 2013-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多