一组窗体代码
别搜baidu了,很多都有问题---比如:图像窗体,添加不了组件,事件响应没有;无边框阴影窗体得作图;毛玻璃只适用win7,win10没效果等等。
这组代码是我常用的,解决了使用中的各类问题,直接拿过去用,无DLL的坑,全部cs源码示例。在win10上测试没问题,没装win7效果不知道,自己测。
在附件下载。
包括:
winForm毛玻璃
winForm图像化窗体
Wpf毛玻璃
Wpf无边框阴影
winForm无边框阴影窗体
窗体代码.rar
(657.91 KB)
比如winForm无边框阴影窗体代码
程序代码:
/* * 无边框阴影窗体 * 适用win10、win7,XP不适用 * qiaoke_song@*/ using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; namespace contextUI { public partial class ConextForm : Form { public bool Sizeable = true; [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")] private static extern IntPtr CreateRoundRectRgn ( int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse ); [DllImport("dwmapi.dll")] public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset); [DllImport("dwmapi.dll")] public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize); [DllImport("dwmapi.dll")] public static extern int DwmIsCompositionEnabled(ref int pfEnabled); private bool m_aeroEnabled = false; private const int CS_DROPSHADOW = 0x00020000; private const int WM_NCPAINT = 0x0085; public struct MARGINS { public int leftWidth; public int rightWidth; public int topHeight; public int bottomHeight; } protected override CreateParams CreateParams { get { m_aeroEnabled = CheckAeroEnabled(); CreateParams cp = base.CreateParams; if (!m_aeroEnabled) cp.ClassStyle |= CS_DROPSHADOW; return cp; } } private bool CheckAeroEnabled() { if (Environment.OSVersion.Version.Major >= 6) { int enabled = 0; DwmIsCompositionEnabled(ref enabled); return (enabled == 1) ? true : false; } return false; } const int HTLEFT = 10; const int HTRIGHT = 11; const int HTTOP = 12; const int HTTOPLEFT = 13; const int HTTOPRIGHT = 14; const int HTBOTTOM = 15; const int HTBOTTOMLEFT = 0x10; const int HTBOTTOMRIGHT = 17; protected override void WndProc(ref Message m) { base.WndProc(ref m); switch (m.Msg) { case WM_NCPAINT: if (m_aeroEnabled) { var v = 2; DwmSetWindowAttribute(this.Handle, 2, ref v, 4); MARGINS margins = new MARGINS() { bottomHeight = 1, leftWidth = 1, rightWidth = 1, topHeight = 1 }; DwmExtendFrameIntoClientArea(this.Handle, ref margins); } break; case 0x0084: if (Sizeable) { base.WndProc(ref m); Point vPoint = new Point((int)m.LParam & 0xFFFF, (int)m.LParam >> 16 & 0xFFFF); vPoint = PointToClient(vPoint); if (vPoint.X <= 5) { if (vPoint.Y <= 5) { m.Result = (IntPtr)HTTOPLEFT; } else if (vPoint.Y >= ClientSize.Height - 5) { m.Result = (IntPtr)HTBOTTOMLEFT; } else { m.Result = (IntPtr)HTLEFT; } } else if (vPoint.X >= ClientSize.Width - 5) { if (vPoint.Y <= 5) { m.Result = (IntPtr)HTTOPRIGHT; } else if (vPoint.Y >= ClientSize.Height - 5) { m.Result = (IntPtr)HTBOTTOMRIGHT; } else { m.Result = (IntPtr)HTRIGHT; } } else if (vPoint.Y <= 5) { m.Result = (IntPtr)HTTOP; } else { if (vPoint.Y >= ClientSize.Height - 5) { m.Result = (IntPtr)HTBOTTOM; } } } break; default: break; } } } }
[此贴子已经被作者于2022-10-23 18:19编辑过]