winform C# 绘制阴影圆角

82 篇文章 3 订阅

 

 private int shadowSize = 5;

        //将预备的小图标转化

        static Image shadowDownRight = new Bitmap(Resources.tshadowdownright);//下右
        static Image shadowDown = new Bitmap(Resources.tshadowdown);// 下
        static Image shadowRight = new Bitmap(Resources.tshadowright);//右
        static Image shadowTop = new Bitmap(Resources.tshadowtop);//上
        static Image shadowLeft = new Bitmap(Resources.tshadowleft);//左
        static Image shadowLeftDown = new Bitmap(Resources.tshadowleftdown);//左下
        static Image shadowLeftTop = new Bitmap(Resources.tshadowlefttop);//左上
        static Image shadowTopRight = new Bitmap(Resources.tshadowtopleft);//上右

Graphics g = this.CreateGraphics();
            bool ShowDown = true;
            bool ShowRight = true;
            bool ShowTop = true;
            bool ShowLeft = true;
            int x = 50;
            int y = 100;
            int Height = 100;
            int Width = 100;
            //在底部和右边画出阴影
            if (ShowDown) //底部
            {
                //下边画笔
                TextureBrush shadowDownBrush = new TextureBrush(shadowDown, WrapMode.Tile);
                //给画笔定位
                shadowDownBrush.TranslateTransform(0, Height - shadowSize);
                //每个阴影区域非配一个矩形区域
                Rectangle shadowDownRectangle = new Rectangle(
                    x + shadowSize,                                                     //X
                    y + Height - shadowSize,                                         //Y
                    Width - shadowSize * 2,                                     //width(stretches)           
                    shadowSize                                                    //height
                    );
                g.FillRectangle(shadowDownBrush, shadowDownRectangle);

                //释放画笔资源
                shadowDownBrush.Dispose();
                shadowDownBrush = null;

            }
            if (ShowRight)//右部
            {
                TextureBrush shadowRightBrush = new TextureBrush(shadowRight, WrapMode.Tile);
                shadowRightBrush.TranslateTransform(Width - shadowSize, 0);
                Rectangle shadowRightRectangle = new Rectangle(
                x + Width - shadowSize,                                           //X
                y+shadowSize,                                                     //Y
                shadowSize,                                                    //width         
                Height - shadowSize * 2                                       //height(stretches)  
                );
                g.FillRectangle(shadowRightBrush, shadowRightRectangle);
                //释放画笔资源
                shadowRightBrush.Dispose();
                shadowRightBrush = null;
            }

            //在上部和左边画出阴影
            if (ShowTop)//上部
            {
                TextureBrush shadowTopBrush = new TextureBrush(shadowTop, WrapMode.Tile);
                shadowTopBrush.TranslateTransform(0, 0);
                Rectangle shadowTopRectangle = new Rectangle(
                    x + shadowSize,                                                    //X
                    y+0,                                                                     //Y
                    Width - shadowSize * 2,                                       //width         
                    shadowSize                                                     //height(stretches)  
                );
                g.FillRectangle(shadowTopBrush, shadowTopRectangle);
                //释放画笔资源
                shadowTopBrush.Dispose();
                shadowTopBrush = null;
            }
            if (ShowLeft)//左部
            {
                TextureBrush shadowLeftBrush = new TextureBrush(shadowLeft, WrapMode.Tile);
                shadowLeftBrush.TranslateTransform(0, 0);
                Rectangle shadowLeftRectangle = new Rectangle(
                    x+0,                                                                    //X
                    y+shadowSize,                                                    //Y
                    shadowSize,                                                    //width         
                    Height - shadowSize * 2                                       //height(stretches)  
                );
                g.FillRectangle(shadowLeftBrush, shadowLeftRectangle);
                //释放画笔资源
                shadowLeftBrush.Dispose();
                shadowLeftBrush = null;
            }
            //四个角落处的阴影
            if (ShowTop && ShowDown && ShowLeft && ShowRight)
            {
                g.DrawImage(shadowDownRight, new Rectangle(x+Width - shadowSize, y+Height - shadowSize, shadowSize, shadowSize));//右下
                g.DrawImage(shadowLeftDown, new Rectangle(x+0, y + Height - shadowSize, shadowSize, shadowSize));//左下
                g.DrawImage(shadowLeftTop, new Rectangle(x+0, y+0, shadowSize, shadowSize));//左上
                g.DrawImage(shadowTopRight, new Rectangle(x+Width - shadowSize, y+0, shadowSize, shadowSize));//右上
            }
            else if (ShowLeft && ShowDown && ShowRight)
            {
                g.DrawImage(shadowDownRight, new Rectangle(x + Width - shadowSize, y + Height - shadowSize, shadowSize, shadowSize));//右下
                g.DrawImage(shadowLeftDown, new Rectangle(x + 0, y + Height - shadowSize, shadowSize, shadowSize));//左下

            }
            else if (ShowDown && ShowRight && ShowTop)
            {
                g.DrawImage(shadowDownRight, new Rectangle(x + Width - shadowSize, y + Height - shadowSize, shadowSize, shadowSize));//右下       
                g.DrawImage(shadowTopRight, new Rectangle(x + Width - shadowSize, y + 0, shadowSize, shadowSize));//右上
            }
            else if (ShowRight && ShowTop && ShowLeft)
            {
                g.DrawImage(shadowLeftTop, new Rectangle(x + 0, y + 0, shadowSize, shadowSize));//左上
                g.DrawImage(shadowTopRight, new Rectangle(x + Width - shadowSize, y + 0, shadowSize, shadowSize));//右上

            }
            else if (ShowTop && ShowLeft && ShowDown)
            {
                g.DrawImage(shadowLeftDown, new Rectangle(x + 0, y + Height - shadowSize, shadowSize, shadowSize));//左下
                g.DrawImage(shadowLeftTop, new Rectangle(x + 0, y + 0, shadowSize, shadowSize));//左上
            }
            else if (ShowDown && ShowLeft)
            {
                g.DrawImage(shadowLeftDown, new Rectangle(x + 0, y + Height - shadowSize, shadowSize, shadowSize));//左下
            }
            else if (ShowDown && ShowRight)
            {
                g.DrawImage(shadowDownRight, new Rectangle(x + Width - shadowSize, y + Height - shadowSize, shadowSize, shadowSize));//右下   
            }
            else if (ShowTop && ShowLeft)
            {
                g.DrawImage(shadowLeftTop, new Rectangle(x + 0, y + 0, shadowSize, shadowSize));//左上
            }
            else if (ShowTop && ShowRight)
            {
                g.DrawImage(shadowTopRight, new Rectangle(x + Width - shadowSize, y + 0, shadowSize, shadowSize));//右上
            }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
public void SetBits() { //绘制绘图层背景 Bitmap bitmap = new Bitmap(Main.Width + 10, Main.Height + 10); Rectangle _BacklightLTRB = new Rectangle(20, 20, 20, 20);//窗体光泽重绘边界 Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.HighQuality; //高质量 g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量 ImageDrawRect.DrawRect(g, Properties.Resources.main_light_bkg_top123, ClientRectangle, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), 1, 1); if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat)) throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); IntPtr oldBits = IntPtr.Zero; IntPtr screenDC = Win32.GetDC(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; IntPtr memDc = Win32.CreateCompatibleDC(screenDC); try { Win32.Point topLoc = new Win32.Point(Left, Top); Win32.Size bitMapSize = new Win32.Size(Width, Height); Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION(); Win32.Point srcLoc = new Win32.Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER; blendFunc.SourceConstantAlpha = Byte.Parse("255"); blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA); } finally { if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBits); Win32.DeleteObject(hBitmap); } Win32.ReleaseDC(IntPtr.Zero, screenDC); Win32.DeleteDC(memDc); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lijingguang

有钱捧个钱场,没钱捧个人场

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值