设置窗体透明C#代码
上个示例是C#调用windows api
在原来代码上加入窗体透明,控件不透明代码:
1 using System; 2 using System.Runtime.InteropServices; 3 using System.Windows.Forms; 4 5 namespace HoverTreeWinForm 6 { 7 public partial class FormHewenqi : Form 8 { 9 10 /// <summary> 11 /// 使窗体透明 by 何问起 12 /// </summary> 13 protected override CreateParams CreateParams 14 { 15 get 16 { 17 18 CreateParams parameter = base.CreateParams; 19 parameter.ExStyle |= 0x00080000; 20 return parameter; 21 22 } 23 } 24 25 /// <summary> 26 /// http://hovertree.com/h/bjaf/v4y0b2l6.htm 27 /// </summary> 28 /// <returns></returns> 29 [DllImport("User32.dll")] 30 public static extern int MessageBox(int h, string m, string c, int type); 31 public FormHewenqi() 32 { 33 InitializeComponent(); 34 //使控件不透明 35 this.TransparencyKey = BackColor; 36 } 37 38 private void button_hewenqi_Click(object sender, EventArgs e) 39 { 40 MessageBox(0, "Hello Win32 API HoverTree", "何问起网", 4); 41 } 42 43 private void linkLabel_help_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 44 { 45 System.Diagnostics.Process.Start("http://hovertree.com/h/bjaf/v4y0b2l6.htm"); 46 } 47 48 private void button_hoverc_Click(object sender, EventArgs e) 49 { 50 //获取主窗体句柄 51 IntPtr h_handle = HewenqiUser32.GetCurrentWindowHandle(); 52 int h_jubing = 0; ; 53 if (h_handle != IntPtr.Zero) 54 h_jubing = (int)(h_handle); 55 MessageBox(h_jubing, "Hello hovertree.com", "欢迎光临何问起", 4); 56 } 57 } 58 }
1、先把窗体的颜色变成一种很不常见的颜色。
2、把该窗体的TransparecyKey的颜色设置成窗体的颜色。
3、加入控件,但是控件的颜色不能和窗体颜色一致,否则控件也透明。
运行程序后,就只能看见标题栏和控件了。
转自:http://hovertree.com/h/bjaf/i3lp7ay0.htm
演示下载:http://pan.baidu.com/s/1mhfgJ6C g56m