zoukankan      html  css  js  c++  java
  • C#透明窗体代码详解

    using System; 
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace TransForm  {
        class WinAPI 
        {
            [DllImport("user32.dll")]
            public extern static IntPtr GetDesktopWindow(); 
            [DllImport("user32.dll")]
            public extern static bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);  
            
            public static uint LWA_COLORKEY = 0×00000001;
            public static uint LWA_ALPHA = 0×00000002;
            
            [DllImport("user32.dll")]
            public extern static uint SetWindowLong(IntPtr hwnd,int nIndex, uint dwNewLong);
            
            [DllImport("user32.dll")]
            public extern static uint GetWindowLong(IntPtr hwnd, int nIndex);
            
            public enum WindowStyle : int
            { 
                GWL_EXSTYLE = -20
            }
     
            public enum ExWindowStyle : uint 
            {
                WS_EX_LAYERED = 0×00080000
            }
         }
    }
    
    
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    namespace TransForm{  
    
        public partial class Form1 : Form  
        {  
            public Form1(){InitializeComponent();}  
            private void Form1_Load(object sender, EventArgs e)
            {  
                this.SetWindowTransparent(100);
            } 
    
            private void SetWindowTransparent(byte bAlpha)
            {  
                try {
                    WinAPI.SetWindowLong(this.Handle,(int)WinAPI.WindowStyle.GWL_EXSTYLE,WinAPI.GetWindowLong(this.Handle,(int)WinAPI.WindowStyle.GWL_EXSTYLE)|(uint)WinAPI.ExWindowStyle.WS_EX_LAYERED);
                    WinAPI.SetLayeredWindowAttributes(  this.Handle, 0, bAlpha,   WinAPI.LWA_COLORKEY | WinAPI.LWA_ALPHA);  
                }
                catch 
                {
    
                }
            }
    
            protected override CreateParams CreateParams  
            {  
                get {  
                    CreateParams cp = base.CreateParams;
                    cp.Parent = WinAPI.GetDesktopWindow();
                    cp.ExStyle = 0×00000080 | 0×00000008;
                    //WS_EX_TOOLWINDOW | WS_EX_TOPMOST
                    return cp;  
                }  
            }  
        }  
    }  
  • 相关阅读:
    一口气说出9种分布式ID生成方式,面试官有点懵
    13个Mongodb GUI可视化管理工具,总有一款适合你
    基于mysql-8.0.16-winx64的主从搭建(Windows10系统)
    Windows10安装多个版本的PostgreSQL数据库,但是均没有自动注册Windows服务的解决方法
    Win10安装多个MySQL实例
    内核编译步骤及模块管理
    进程管理类命令
    进程监控类命令
    进程概念介绍
    文件打包压缩
  • 原文地址:https://www.cnblogs.com/rinack/p/4137494.html
Copyright © 2011-2022 走看看