C# 製作透明表單

來源:互聯網
上載者:User

標籤:des   winform   style   class   blog   code   

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 }    }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.