Laden...

Glasfenster erstellen

Erstellt von admirandis vor 14 Jahren Letzter Beitrag vor 14 Jahren 4.690 Views
admirandis Themenstarter:in
72 Beiträge seit 2009
vor 14 Jahren
Glasfenster erstellen

Beschreibung:

Dies ist eine statische Klasse, mit der ihre sehr leicht eure Form zu einem Glasfenster machen könnt. Dazu müsst ihr nur die Methode "glassify" ausführen und ihre eure Form als Parameter übergeben. Schon fertig.
Alternativ kann man der Methode auch noch ein Control übergeben, welches dann verglast wird. Sinnvoll erscheinen mir hier vorallem Container.

Falls ihr überprüfen wollt, ob ihr Erfolg hattet, hilft euch die Methode isGlassForm oder ihr überprüft den Rückgabewert von "glassify".

Noch zu erwähnen wäre, dass die erst ab Windows Vista funktioniert. Bei allen vorhergehenden Windowsversionen gibt die Methode ein false zurück.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing;
using System.ComponentModel;

namespace GlassTest
{
    static class GlassEffect
    {
        [System.Runtime.InteropServices.DllImport("dwmapi.dll")]
        internal static extern void DwmExtendFrameIntoClientArea(System.IntPtr hWnd, ref Margins pMargins);

        [System.Runtime.InteropServices.DllImport("dwmapi.dll")]
        internal static extern void DwmIsCompositionEnabled(ref bool isEnabled);

        internal struct Margins { public int Left, Right, Top, Bottom; }

        public static bool glassify(Form gForm)
        {
            try
            {
                // Glasseffects are supported since VISTA
                if (Environment.OSVersion.Version.Major < 6) return false;

                // Set the margins to infinty to glass the winodow in every size
                Margins margins;
                margins.Top = -1;
                margins.Bottom = -1;
                margins.Left = -1;
                margins.Right = -1;

                // The API-Call
                DwmExtendFrameIntoClientArea(gForm.Handle, ref margins);

                // Finally we set the Preferences for the form
                Color c = Color.FromArgb(255, 221, 220, 220);
                gForm.TransparencyKey = c;
                gForm.BackColor = c;
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool glassify(Form gForm, Control gPanel)
        {
            try
            {
                // Glasseffects are supported since VISTA
                if (Environment.OSVersion.Version.Major < 6) return false;

                // Set the margins to infinty to glass the winodow in every size
                Margins margins;
                margins.Top = -1;
                margins.Bottom = -1;
                margins.Left = -1;
                margins.Right = -1;

                // The API-Call
                DwmExtendFrameIntoClientArea(gForm.Handle, ref margins);

                // Finally we set the Preferences for the form and the Control
                Color c = Color.FromArgb(255, 221, 220, 220);
                gForm.TransparencyKey = c;
                gPanel.BackColor = c;
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool isGlassForm()
        {
            bool retVal = false;
            DwmIsCompositionEnabled(ref retVal);
            return retVal;
        }
    }
}


Schlagwörter: glasfenster, glass, DwmExtendFrameIntoClientArea, DwmIsCompositionEnabled

<--- Sir, we are surrounded!! <-> Perfect! We can attack in any direction °_° --->