C#简单实现窗口全透明

来源:互联网 发布:怎么样开通淘宝 编辑:程序博客网 时间:2024/05/17 03:33

C#简单实现窗口全透明

 

实现窗口全透明,不管用什么编程语言,其实原理都是一样的,为了进一步展现各语言实现整个窗口全透明,现将C#的源码送上,供大家参考。

我用的编写环境为:Windows Vista 、 Visual Studio 2008

效果图如下:

程序实现源码:

//Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;


namespace WindowsVistaFormsApplication
{
    public partial class Form1 : Form
    {
        int en;
        GraphicsPath fontpath;
        GraphicsPath glowpath;
        PathGradientBrush pathbrush;
        Graphics gph;

        //API 结构声明

        public struct MARGINS
        {
            public int m_Left;
            public int m_Right;
            public int m_Top;
            public int m_Buttom;
        };

        [DllImport("dwmapi.dll")]
        private static extern void DwmIsCompositionEnabled(ref int enabledptr);
        [DllImport("dwmapi.dll")]
        private static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margin);
        public Form1()
        {
            InitializeComponent();
           en=0;
            MARGINS mg=new MARGINS();
            mg.m_Buttom = -1;
            mg.m_Left = -1;
            mg.m_Right = -1;
            mg.m_Top = -1 ;
            //判断Vista系统
            if (System.Environment.OSVersion.Version.Major >= 6)            
            {
                DwmIsCompositionEnabled(ref en);    //检测Aero是否为打开
                if(en>0)
                {
                      DwmExtendFrameIntoClientArea(this.Handle, ref mg);

                }else{
                      MessageBox.Show("Desktop Composition is Disabled!");
                }
            }else{
                 MessageBox.Show("Please run this on Windows Vista.");
            }
            this.Paint += new PaintEventHandler(Form1_Paint);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (en > 0)
            {
                Graphics g = e.Graphics;
                SolidBrush bsh = new SolidBrush(Color.Black);
                g.FillRectangle(bsh, this.ClientRectangle);
                bsh.Dispose();

            }
            }
    }
}
源码下载:  http://download.csdn.net/source/714815

发布:薛雪     E_mail:SnowEmail3074@163.com
备注:实现方法虽然很简单,转载时敬请注明出处,谢谢!

原创粉丝点击