(vb.net/C#)实现绘制Winform窗体的四边阴影效果

文章来源:365jz.com     点击数:151    更新时间:2017-11-13 01:52   参与评论
首先我们得有这样一张阴影图片。



2.然后分别有两个窗体去实现这个阴影效果。
SkinForm - 用于实现阴影的绘制,特性:鼠标可穿透,无法点击,跟随窗体。
SkinMain - 主窗体,也是承载控件的容器窗体, 特性:与普通窗体无一区别,移动和拉伸,阴影窗体都会跟随。
3.在SkinMain主窗体的OnVisibleChanged事件中new出阴影窗体


//绘制层
private SkinForm skin;
skin = new SkinForm(this);
skin.Show(this);

protected override void OnVisibleChanged(EventArgs e) {
            if (Visible) {
                if (!DesignMode) {
                //判断不是在设计器中
                if (!DesignMode && skin == null) {
                    skin = new SkinForm(this);
                    skin.Show(this);
                }
                base.OnVisibleChanged(e);
            } else {
                base.OnVisibleChanged(e);
            }
        }


4.阴影窗体中绘制不规则透明图片的代码:

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);
    }
}


 ImageDrawRect.DrawRect 函数部分:

public static void DrawRect(Graphics g, Bitmap img, Rectangle r, Rectangle lr, int index, int Totalindex)
        {
            if (img == null) return;
           
            Rectangle r1, r2;
            int x = (index - 1) * img.Width / Totalindex;
            int y = 0;
            int x1 = r.Left;
            int y1 = r.Top;

                r1 = new Rectangle(x, y, img.Width / Totalindex, lr.Top);
                r2 = new Rectangle(x1, y1, r.Width, lr.Top);
                g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);

                r1 = new Rectangle(x, y + lr.Top, img.Width / Totalindex, img.Height - lr.Top - lr.Bottom);
                r2 = new Rectangle(x1, y1 + lr.Top, r.Width, r.Height - lr.Top - lr.Bottom);
                if ((lr.Top + lr.Bottom) == 0) r1.Height = r1.Height - 1;
                g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);

                r1 = new Rectangle(x, y + img.Height - lr.Bottom, img.Width / Totalindex, lr.Bottom);
                r2 = new Rectangle(x1, y1 + r.Height - lr.Bottom, r.Width, lr.Bottom);

                g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
                 。。。。。。
           
          }


如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛


发表评论 (151人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码: 点击我更换图片
最新评论
------分隔线----------------------------