C#简单实现窗口全透明

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
备注:实现方法虽然很简单,转载时敬请注明出处,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值