自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#透明窗體代碼詳解

開發(fā) 后端
C#透明窗體的實現(xiàn)這里向你介紹通過調用Windows API來完成,具體用到的方法和步驟是什么呢?那么本文就向你介紹具體的步驟。

實現(xiàn)C#透明窗體是如何實現(xiàn)的呢?這里向你介紹通過調用Windows API來實現(xiàn)C#透明窗體。那么具體的過程和步驟是什么呢?讓我們來看看具體的實現(xiàn)。

C#透明窗體實現(xiàn)實例:

C#透明窗體之WinAPI.cs類文件,Invoke & Wrap了窗體透明所需要的API函數(shù):

  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函數(shù)的調用方式:

  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#透明窗體的實現(xiàn)基本內容就向你介紹到這里,希望對你了解和學習C#透明窗體有所幫助。

【編輯推薦】

  1. C#窗體位置與大小設置詳解
  2. C# Timer用法及實例詳解
  3. C#窗體設計操作淺析
  4. C#窗體設計器開發(fā)實例詳解
  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 06:18:57

C#窗體設計器

2009-09-07 03:44:50

C#窗體間傳值

2009-09-07 05:40:16

C#窗體位置C#窗體大小

2009-08-10 14:23:39

C# Setting

2010-08-31 09:46:23

C#

2009-09-01 10:37:51

C#項目代碼C#代碼規(guī)范

2009-09-02 17:12:06

C#關機代碼

2009-09-07 06:07:46

C#窗體設計

2009-08-13 10:42:31

C#窗體拖動事件

2009-09-07 06:31:32

C#窗體移動

2009-08-28 15:58:54

C#窗體里調用

2009-09-07 04:19:56

C#窗體事件

2009-09-02 14:06:14

C#文件傳送

2009-04-03 10:25:32

C#XML擴展代碼

2024-07-03 08:21:56

MDI窗體界面

2009-09-01 18:06:06

c#保存窗體狀態(tài)
點贊
收藏

51CTO技術棧公眾號