C# Создаем красивую форму

Bug

Through thorns to stars
Администратор
Bug

Bug

Through thorns to stars
Администратор
Сообщения
1,452
Реакции
2,816

Класс:
C#:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.VisualBasic.CompilerServices;

namespace WindowsFormsApplication1
{
    public class LayeredForm
    {
        public LayeredForm()
        {
            this.mMovable = true;
        }
     
        public Form Parent
        {
            get
            {
                return this.mParent;
            }
        }
     
        public string Movable
        {
            get
            {
                return Conversions.ToString(this.mMovable);
            }
            set
            {
                this.mMovable = Conversions.ToBoolean(value);
            }
        }

        public void SetImage(Form frm, Bitmap btm)
        {
            try
            {
                this.mParent = frm;
                frm.MouseDown += new MouseEventHandler(this.OnMouseDown);
                frm.MouseMove += new MouseEventHandler(this.OnMouseMove);
                int exStyle = APIs.GetWindowLong(frm.Handle.ToInt32(), -20);
                APIs.SetWindowLong(frm.Handle.ToInt32(), -20, exStyle | 524288);
                bool flag = !Image.IsCanonicalPixelFormat(btm.PixelFormat) | !Image.IsAlphaPixelFormat(btm.PixelFormat);
                if (flag)
                {
                    throw new ApplicationException("The picture must be 32bit picture with alpha channel.");
                }
                int hDC = APIs.GetDC(0);
                int memDC = APIs.CreateCompatibleDC(hDC);
                APIs.POINTAPI dstPos = new APIs.POINTAPI(frm.Left, frm.Top);
                APIs.POINTAPI srcPos = new APIs.POINTAPI(0, 0);
                APIs.SizeAPI Siz = new APIs.SizeAPI(btm.Width, btm.Height);
                APIs.BLEND_OPTIONS Opt = default(APIs.BLEND_OPTIONS);
                Opt.SourceConstantAlpha = 255;
                Opt.AlphaFormat = 1;
                Opt.BlendOp = 0;
                Opt.BlendFlags = 0;
                int hBtm = 0;
                int Bits = 0;
                try
                {
                    hBtm = btm.GetHbitmap(Color.Black).ToInt32();
                    Bits = APIs.SelectObject(memDC, hBtm);
                    APIs.UpdateLayeredWindow((int)frm.Handle, hDC, ref dstPos, ref Siz, memDC, ref srcPos, 0, ref Opt, 2);
                }
                catch (Exception expr_13A)
                {
                    ProjectData.SetProjectError(expr_13A);
                    ProjectData.ClearProjectError();
                }
                bool flag2 = hBtm != 0;
                if (flag2)
                {
                    APIs.SelectObject(memDC, Bits);
                    APIs.DeleteObject(hBtm);
                }
                APIs.ReleaseDC(0, hDC);
                APIs.DeleteDC(memDC);
            }
            catch (Exception expr_178)
            {
                ProjectData.SetProjectError(expr_178);
                ProjectData.ClearProjectError();
            }
        }

        private void OnMouseDown(object sender, MouseEventArgs e)
        {
            this.mLastLocation = e.Location;
        }

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            bool flag = this.mMovable && e.Button == MouseButtons.Left;
            checked
            {
                if (flag)
                {
                    Point pos = this.Parent.PointToScreen(e.Location);
                    pos.Offset(0 - this.mLastLocation.X, 0 - this.mLastLocation.Y);
                    this.Parent.Location = pos;
                }
            }
        }
     
        private Form mParent;
        private bool mMovable;
        private Point mLastLocation;
     
        private class APIs
        {
            [DllImport("gdi32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
            public static extern int CreateCompatibleDC(int hdc);
         
            [DllImport("user32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
            public static extern int GetDC(int hwnd);
         
            [DllImport("gdi32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
            public static extern int SelectObject(int hdc, int hObject);
         
            [DllImport("user32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
            public static extern int ReleaseDC(int hwnd, int hdc);
         
            [DllImport("gdi32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
            public static extern int DeleteDC(int hdc);
         
            [DllImport("gdi32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
            public static extern int DeleteObject(int hObject);
         
            [DllImport("user32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
            public static extern int UpdateLayeredWindow(int hWnd, int hdcDst, ref LayeredForm.APIs.POINTAPI pptDst, ref LayeredForm.APIs.SizeAPI psize, int hdcSrc, ref LayeredForm.APIs.POINTAPI pptSrc, int crKey, ref LayeredForm.APIs.BLEND_OPTIONS pblend, int dwFlags);
         
            [DllImport("user32.dll", CharSet = CharSet.Ansi, EntryPoint = "GetWindowLongW", ExactSpelling = true, SetLastError = true)]
            public static extern int GetWindowLong(int hwnd, int nIndex);
         
            [DllImport("user32.dll", CharSet = CharSet.Ansi, EntryPoint = "SetWindowLongW", ExactSpelling = true, SetLastError = true)]
            public static extern int SetWindowLong(int hwnd, int nIndex, int dwNewLong);
        
            [StructLayout(LayoutKind.Sequential, Pack = 1)]
            public struct BLEND_OPTIONS
            {
                public byte BlendOp;
                public byte BlendFlags;
                public byte SourceConstantAlpha;
                public byte AlphaFormat;
            }
         
            public struct SizeAPI
            {
                public SizeAPI(int cx, int cy)
                {
                    this = default(LayeredForm.APIs.SizeAPI);
                    this.cx = cx;
                    this.cy = cy;
                }
                public int cx;
                public int cy;
            }
         
            public struct POINTAPI
            {
                public POINTAPI(int x, int y)
                {
                    this = default(LayeredForm.APIs.POINTAPI);
                    this.x = x;
                    this.y = y;
                }
             
                public int x;
                public int y;
            }
        
            public struct RECT
            {
                public int Left;
                public int Top;
                public int Right;
                public int Bottom;
            }
        }
    }
}
Использование:
C#:
LayeredForm Layer = new LayeredForm();
            Layer.SetImage(this, (Bitmap)this.BackgroundImage);
На форме выставляем цвет и ставим .png (с прозрачным фоном ) как BackgroundImage
 

Сверху Снизу