設置窗體透明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

源碼下載:https://github.com/shangyuxian/HoverTree


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM