C#透明窗体代码详解

开发 后端
C#透明窗体的实现这里向你介绍通过调用Windows API来完成,具体用到的方法和步骤是什么呢?那么本文就向你介绍具体的步骤。

实现C#透明窗体是如何实现的呢?这里向你介绍通过调用Windows API来实现C#透明窗体。那么具体的过程和步骤是什么呢?让我们来看看具体的实现。

C#透明窗体实现实例:

C#透明窗体之WinAPI.cs类文件,Invoke & Wrap了窗体透明所需要的API函数:

  1. [coolcode lang="cpp" download="WinAPI.cs"]  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5. using System.Runtime.InteropServices;  
  6.  
  7. namespace TransForm  
  8. {  
  9. class WinAPI  
  10. {  
  11. [DllImport("user32.dll")]  
  12. public extern static IntPtr GetDesktopWindow();  
  13.  
  14. [DllImport("user32.dll")]  
  15. public extern static bool   
  16. SetLayeredWindowAttributes(  
  17. IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);  
  18. public static uint LWA_COLORKEY = 0×00000001;  
  19. public static uint LWA_ALPHA = 0×00000002;  
  20.  
  21. [DllImport("user32.dll")]  
  22. public extern static uint   
  23. SetWindowLong(IntPtr hwnd,   
  24. int nIndex, uint dwNewLong);  
  25. [DllImport("user32.dll")]  
  26. public extern static uint   
  27. GetWindowLong(IntPtr hwnd, int nIndex);  
  28.  
  29. public enum WindowStyle : int 
  30. {  
  31. GWL_EXSTYLE = -20  
  32. }  
  33.  
  34. public enum ExWindowStyle : uint 
  35. {  
  36. WS_EX_LAYERED = 0×00080000  
  37. }  
  38.  
  39. }  
  40. }  
  41. [/coolcode]  

C#透明窗体之DeviceForm.cs单元是API函数的调用方式:

  1. [coolcode lang="cpp" download="form1.cs"]  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.ComponentModel;  
  5. using System.Data;  
  6. using System.Drawing;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.  
  10. namespace TransForm  
  11. {  
  12. public partial class Form1 : Form  
  13. {  
  14. public Form1()  
  15. {  
  16. InitializeComponent();  
  17. }  
  18.  
  19. private void Form1_Load(object sender, EventArgs e)  
  20. {  
  21. this.SetWindowTransparent(100);  
  22. }  
  23. private void SetWindowTransparent(byte bAlpha)  
  24. {  
  25. try 
  26. {  
  27. WinAPI.SetWindowLong(  
  28. this.Handle,   
  29. (int)WinAPI.WindowStyle.GWL_EXSTYLE,  
  30. WinAPI.GetWindowLong(  
  31. this.Handle,   
  32. (int)WinAPI.WindowStyle.GWL_EXSTYLE) |   
  33. (uint)WinAPI.ExWindowStyle.WS_EX_LAYERED);  
  34.  
  35. WinAPI.SetLayeredWindowAttributes(  
  36. this.Handle, 0, bAlpha,  
  37.  WinAPI.LWA_COLORKEY | WinAPI.LWA_ALPHA);  
  38. }  
  39. catch 
  40. {  
  41. }  
  42. }  
  43. protected override CreateParams CreateParams  
  44. {  
  45. get 
  46. {  
  47. CreateParams cp = base.CreateParams;  
  48.  
  49. cp.Parent = WinAPI.GetDesktopWindow();  
  50. cp.ExStyle = 0×00000080 | 0×00000008;  
  51. //WS_EX_TOOLWINDOW | WS_EX_TOPMOST  
  52.  
  53. return cp;  
  54. }  
  55. }  
  56. }  
  57. }  
  58. [/coolcode]  

C#透明窗体的实现基本内容就向你介绍到这里,希望对你了解和学习C#透明窗体有所帮助。

【编辑推荐】

  1. C#窗体位置与大小设置详解
  2. C# Timer用法及实例详解
  3. C#窗体设计操作浅析
  4. C#窗体设计器开发实例详解
  5. C#窗体移动实例解析
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-08-20 10:10:55

C#透明窗体

2009-09-07 06:56:46

C#透明窗体

2009-09-07 05:10:52

C#模式窗体

2009-08-26 11:07:36

C#打印窗体

2009-09-07 03:44:50

C#窗体间传值

2009-09-07 06:18:57

C#窗体设计器

2009-09-07 05:40:16

C#窗体位置C#窗体大小

2010-08-31 09:46:23

C#

2009-08-10 14:23:39

C# Setting

2009-09-02 17:12:06

C#关机代码

2009-09-01 10:37:51

C#项目代码C#代码规范

2009-09-07 04:19:56

C#窗体事件

2009-09-07 06:31:32

C#窗体移动

2009-08-28 15:58:54

C#窗体里调用

2009-09-07 06:07:46

C#窗体设计

2009-08-13 10:42:31

C#窗体拖动事件

2009-09-02 14:06:14

C#文件传送

2009-04-03 10:25:32

C#XML扩展代码

2009-08-25 09:19:01

C#实现窗体显示

2009-09-07 04:56:52

C#模式窗体
点赞
收藏

51CTO技术栈公众号