zoukankan      html  css  js  c++  java
  • C# 制作透明窗体

    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 ControlExs;
    
    namespace TransForm
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                //// 定义在窗体上,光标显示为手形
                this.Cursor = System.Windows.Forms.Cursors.Hand;
                //// 定义窗体的标题名称 
                this.Text = "透明的WinForm窗体!";
                //// 定义窗体的开始显示位置是屏幕的中间 
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                //// 窗体的边界是Fixed3D类型
                //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                ////以桌面的前景色作为窗体的前景色 
                this.ForeColor = System.Drawing.SystemColors.Desktop;
                //// 定义字体类型,大小 
                this.Font = new System.Drawing.Font("宋体", 9);
                //// 定义背景色为蓝色 
                this.BackColor = System.Drawing.Color.White;
                // 设置窗体的大小 
                //this.ClientSize = new System.Drawing.Size(440, 170);
                // Opacity属性设立窗体的透明程度,只对于视窗2000有效 
                this.Opacity = 0.60;
            }
    
            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 = 0x00000080 | 0x00000008;  //WS_EX_TOOLWINDOW | WS_EX_TOPMOST   
                    return cp;
                }
            }
        }
    }
     public 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 = 0x00000001;
            public static uint LWA_ALPHA = 0x00000002;
    
            [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 = 0x00080000 }
        }
  • 相关阅读:
    列表左边左右固定,右边可以左右滚动,且左右两边列表滚动时上下联动
    SQL分组多列统计(GROUP BY后按条件分列统计)
    C# 调用webservice 几种办法(转载)
    解析XML文档
    GDI+中发生一般性错误的解决办法
    所闻所获2:使用块回调来实现代理的功能
    OC基础11:基本的C语言特性2
    OC基础10:基本的C语言特性1
    OC基础9:预处理程序
    OC基础8:分类和协议
  • 原文地址:https://www.cnblogs.com/rinack/p/3772000.html
Copyright © 2011-2022 走看看