AeroGlassHelper.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Virtual Router v1.0 - http://virtualrouter.codeplex.com
  3. * Wifi Hot Spot for Windows 8, 7 and 2008 R2
  4. * Copyright (c) 2013 Chris Pietschmann (http://pietschsoft.com)
  5. * Licensed under the Microsoft Public License (Ms-PL)
  6. * http://virtualrouter.codeplex.com/license
  7. */
  8. using System;
  9. using System.Runtime.InteropServices;
  10. using System.Windows;
  11. using System.Windows.Interop;
  12. using System.Windows.Media;
  13. namespace GeekDesk.Util
  14. {
  15. public static class AeroGlassHelper
  16. {
  17. #region "Generic Static Methods"
  18. public static void ExtendGlass(IntPtr windowHandle)
  19. {
  20. ExtendGlass(windowHandle, -1, -1, -1, -1);
  21. }
  22. public static void ExtendGlass(IntPtr windowHandle, int left, int right, int top, int bottom)
  23. {
  24. internalExtendGlass(windowHandle, left, right, top, bottom);
  25. }
  26. private static int internalExtendGlass(IntPtr windowHandle, int left, int right, int top, int bottom)
  27. {
  28. var retVal = -1; // Returning less than zero will indicate that Aero Glass could not be extended
  29. // Calculate the Aero Glass Margins
  30. Win32.Margins margins = Win32.GetDpiAdjustedMargins(windowHandle, left, right, top, bottom);
  31. try
  32. {
  33. // Actually Enable Aero Glass
  34. retVal = Win32.DwmExtendFrameIntoClientArea(windowHandle, ref margins);
  35. }
  36. catch (Exception)
  37. {
  38. retVal = -1;
  39. }
  40. return retVal;
  41. }
  42. #endregion
  43. #region "WPF Static Methods"
  44. public static void ExtendGlass(Window win)
  45. {
  46. ExtendGlass(win, -1, -1, -1, -1);
  47. }
  48. public static void ExtendGlass(Window win, int left, int right, int top, int bottom)
  49. {
  50. Brush originalBackgroundBrush = win.Background;
  51. try
  52. {
  53. int retVal = -1;
  54. if (Win32.DwmIsCompositionEnabled())
  55. {
  56. win.Background = Brushes.Transparent;
  57. // Obtain the window handle for WPF application
  58. WindowInteropHelper windowInterop = new WindowInteropHelper(win);
  59. IntPtr windowHandle = windowInterop.Handle;
  60. // Set the Window background to be Transparent so the Aero Glass will show through
  61. HwndSource mainWindowSrc = HwndSource.FromHwnd(windowHandle);
  62. mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent;
  63. retVal = internalExtendGlass(windowHandle, left, right, top, bottom);
  64. }
  65. if (retVal < 0)
  66. {
  67. throw new NotSupportedException("Operation Failed: Aero Glass Not Supported");
  68. }
  69. }
  70. catch
  71. {
  72. // If an error occurred then change the Window.Background back to what it was
  73. win.Background = originalBackgroundBrush;
  74. }
  75. }
  76. #endregion
  77. //#region "Windows.Forms Static Methods"
  78. //public static void ExtendGlass(Form form)
  79. //{
  80. // ExtendGlass(form, -1, -1, -1, -1);
  81. //}
  82. //public static void ExtendGlass(Form form, int left, int right, int top, int bottom)
  83. //{
  84. // System.Drawing.Color oldBackColor = form.BackColor;
  85. // System.Drawing.Color oldTransparencyKey = form.TransparencyKey;
  86. // int retVal = -1;
  87. // try
  88. // {
  89. // form.TransparencyKey = System.Drawing.Color.Beige;
  90. // form.BackColor = form.TransparencyKey;
  91. // retVal = internalExtendGlass(form.Handle, left, right, top, bottom);
  92. // }
  93. // catch (Exception)
  94. // {
  95. // retVal = -1;
  96. // }
  97. // if (retVal < 0)
  98. // {
  99. // form.BackColor = oldBackColor;
  100. // form.TransparencyKey = oldTransparencyKey;
  101. // }
  102. //}
  103. //#endregion
  104. #region "Win32 / pinvoke"
  105. private static class Win32
  106. {
  107. [DllImport("DwmApi.dll")]
  108. public static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref Margins margins);
  109. [DllImport("dwmapi.dll", PreserveSig = false)]
  110. public static extern bool DwmIsCompositionEnabled();
  111. [StructLayout(LayoutKind.Sequential)]
  112. public struct Margins
  113. {
  114. public int Left; // width of left border that retains its size
  115. public int Right; // width of right border that retains its size
  116. public int Top; // height of top border that retains its size
  117. public int Bottom; // height of bottom border that retains its size
  118. }
  119. public static Win32.Margins GetDpiAdjustedMargins(IntPtr windowHandle, int left, int right, int top, int bottom)
  120. {
  121. float DesktopDpiX;
  122. float DesktopDpiY;
  123. // Get System Dpi
  124. using (System.Drawing.Graphics desktop = System.Drawing.Graphics.FromHwnd(windowHandle))
  125. {
  126. DesktopDpiX = desktop.DpiX;
  127. DesktopDpiY = desktop.DpiY;
  128. }
  129. // Set Margins
  130. Win32.Margins margins = new Win32.Margins();
  131. // Note that the default desktop Dpi is 96dpi. The margins are
  132. // adjusted for the system Dpi.
  133. margins.Left = Convert.ToInt32(left * (DesktopDpiX / 96));
  134. margins.Right = Convert.ToInt32(right * (DesktopDpiX / 96));
  135. margins.Top = Convert.ToInt32(top * (DesktopDpiX / 96));
  136. margins.Bottom = Convert.ToInt32(bottom * (DesktopDpiX / 96));
  137. return margins;
  138. }
  139. }
  140. #endregion
  141. }
  142. }