独上高楼网站
  •    你所在位置:首页 VS.netC#C#编程〉C#简单实现窗口全透明
  • C#简单实现窗口全透明
  • 作者:薛雪  文章来源:blog.csdn.net  发布日期:2008-10-23  浏览次数:78
  • 打印这篇文章
  • 实现窗口全透明,不管用什么编程语言,其实原理都是一样的,为了进一步展现各语言实现整个窗口全透明,现将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();

                }
                }
        }
    }
    源码下载:

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

  • 打印这篇文章
  • 与本文主题相关的文章
  • 返回首页