TestShadow2Form.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. namespace Y.Test.Views
  11. {
  12. public partial class TestShadow2Form : Form
  13. {
  14. [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
  15. private static extern IntPtr CreateRoundRectRgn
  16. (
  17. int nLeftRect, // x-coordinate of upper-left corner
  18. int nTopRect, // y-coordinate of upper-left corner
  19. int nRightRect, // x-coordinate of lower-right corner
  20. int nBottomRect, // y-coordinate of lower-right corner
  21. int nWidthEllipse, // height of ellipse
  22. int nHeightEllipse // width of ellipse
  23. );
  24. [DllImport("dwmapi.dll")]
  25. public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
  26. [DllImport("dwmapi.dll")]
  27. public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
  28. [DllImport("dwmapi.dll")]
  29. public static extern int DwmIsCompositionEnabled(ref int pfEnabled);
  30. private bool m_aeroEnabled; // variables for box shadow
  31. private const int CS_DROPSHADOW = 0x00020000;
  32. private const int WM_NCPAINT = 0x0085;
  33. private const int WM_ACTIVATEAPP = 0x001C;
  34. public struct MARGINS // struct for box shadow
  35. {
  36. public int leftWidth;
  37. public int rightWidth;
  38. public int topHeight;
  39. public int bottomHeight;
  40. }
  41. private const int WM_NCHITTEST = 0x84; // variables for dragging the form
  42. private const int HTCLIENT = 0x1;
  43. private const int HTCAPTION = 0x2;
  44. protected override CreateParams CreateParams
  45. {
  46. get
  47. {
  48. m_aeroEnabled = CheckAeroEnabled();
  49. CreateParams cp = base.CreateParams;
  50. if (!m_aeroEnabled)
  51. cp.ClassStyle |= CS_DROPSHADOW;
  52. return cp;
  53. }
  54. }
  55. private bool CheckAeroEnabled()
  56. {
  57. if (Environment.OSVersion.Version.Major >= 6)
  58. {
  59. int enabled = 0;
  60. DwmIsCompositionEnabled(ref enabled);
  61. return (enabled == 1) ? true : false;
  62. }
  63. return false;
  64. }
  65. protected override void WndProc(ref Message m)
  66. {
  67. switch (m.Msg)
  68. {
  69. case WM_NCPAINT: // box shadow
  70. if (m_aeroEnabled)
  71. {
  72. var v = 2;
  73. DwmSetWindowAttribute(this.Handle, 2, ref v, 4);
  74. MARGINS margins = new MARGINS()
  75. {
  76. bottomHeight = 1,
  77. leftWidth = 1,
  78. rightWidth = 1,
  79. topHeight = 1
  80. };
  81. DwmExtendFrameIntoClientArea(this.Handle, ref margins);
  82. }
  83. break;
  84. default:
  85. break;
  86. }
  87. base.WndProc(ref m);
  88. if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT) // drag the form
  89. m.Result = (IntPtr)HTCAPTION;
  90. }
  91. public TestShadow2Form()
  92. {
  93. m_aeroEnabled = false;
  94. this.FormBorderStyle = FormBorderStyle.None;
  95. InitializeComponent();
  96. }
  97. private void TestShadow2Form_Load(object sender, EventArgs e)
  98. {
  99. }
  100. }
  101. }