C# 毛玻璃 透明效果


  1. using System.Runtime.InteropServices;
  2. public struct Margins //邊距結構
  3. {
  4. public int Left;
  5. public int Right;
  6. public int Top;
  7. public int Bottom;
  8. }
  9. [DllImport("dwmapi.dll", PreserveSig = false)]//引入Dll
  10. static extern void DwmExtendFrameIntoClientArea(IntPtr dwm, ref Margins margins);//Dll中的用法
  11. [DllImport("dwmapi.dll", PreserveSig = false)]//引入Dll
  12. static extern bool DwmIsCompositionEnabled();//Dll中的用法
  13.  
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. this.Opacity = 0.95;//改變視窗透明度
  18. }
  19.  
  20. protected override void OnLoad(EventArgs e)//複寫OnLoad
  21. {
  22. Margins margins = new Margins() { };
  23. margins.Right = margins.Left = margins.Top = margins.Bottom = -1;
  24. if (DwmIsCompositionEnabled())//檢查特效是否開啟
  25. DwmExtendFrameIntoClientArea(this.Handle, ref margins);//使用Aero特效
  26. base.OnLoad(e);
  27. }
  28.  
  29. protected override void OnPaintBackground(PaintEventArgs e)
  30. {
  31. base.OnPaintBackground(e);
  32. if (DwmIsCompositionEnabled())
  33. e.Graphics.Clear(Color.Black);//將視窗顏色填充為黑色(Aero中黑色為要用特效的區域)
  34. }

留言

這個網誌中的熱門文章

C# 井字遊戲