求助!怎么实现窗体四周阴影??

dangyj 2015-01-29 04:41:30
怎么实现winform窗体的四周阴影?不用WPF,就是C# GDI+
如果有,请具体写下源码,谢谢
...全文
602 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tzjtzj181 2016-11-29
  • 打赏
  • 举报
回复
public partial class Form1 : Form { [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")] private static extern IntPtr CreateRoundRectRgn ( int nLeftRect, // x-coordinate of upper-left corner int nTopRect, // y-coordinate of upper-left corner int nRightRect, // x-coordinate of lower-right corner int nBottomRect, // y-coordinate of lower-right corner int nWidthEllipse, // height of ellipse int nHeightEllipse // width of ellipse ); [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; // variables for box shadow private const int CS_DROPSHADOW = 0x00020000; private const int WM_NCPAINT = 0x0085; private const int WM_ACTIVATEAPP = 0x001C; public struct MARGINS // struct for box shadow { public int leftWidth; public int rightWidth; public int topHeight; public int bottomHeight; } private const int WM_NCHITTEST = 0x84; // variables for dragging the form private const int HTCLIENT = 0x1; private const int HTCAPTION = 0x2; 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; } protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_NCPAINT: // box shadow 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; default: break; } base.WndProc(ref m); if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT) // drag the form m.Result = (IntPtr)HTCAPTION; } public Form1() { m_aeroEnabled = false; this.FormBorderStyle = FormBorderStyle.None; InitializeComponent(); } } 已测,无问题
crystal_lz 2015-02-08
  • 打赏
  • 举报
回复
简单一点的话 设置一下窗体的样式就可以了 SetWindowLong函数 把窗体设置成弹出菜单样式就会有一个阴影了 不过那个阴影不怎么好看 好看一点的还是分层窗体吧
fervour 2015-02-07
  • 打赏
  • 举报
回复
win7下面窗体自带就有这样的效果。得在xp系统才能看到效果吧。
dylike 2015-02-07
  • 打赏
  • 举报
回复
做一个透明的阴影层,可用PS等画,保存为PNG,然后用代码设置为窗体透明显示. DSAPI多功能组件编程应用-PNG成为窗体
mcff 2015-02-01
  • 打赏
  • 举报
回复
在CNBLOG有相关的帖子..我帮楼主转过来..http://www.cnblogs.com/Keep-Silence-/archive/2013/01/09/2852937.html
忘丿殇 2015-01-30
  • 打赏
  • 举报
回复
dangyj 2015-01-30
  • 打赏
  • 举报
回复
bdmh 你给的链接效果是系统自带的那种,也就是只是下方和右边有。我要的是四周都有阴影的。
devmiao 2015-01-29
  • 打赏
  • 举报
回复
用updatelayeredwindow api
bdmh 2015-01-29
  • 打赏
  • 举报
回复
不要总想着是在窗体外面,你就把窗体的边缘当做外面,在窗体边缘绘制一定像素的阴影色,就能模拟出阴影效果
如果是无边框窗体,可以考虑
http://www.cnblogs.com/huangcong/archive/2010/03/26/1697088.html

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧