使用C#画非客户区

win6_1

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using HANDLE = System.IntPtr;
using HWND = System.IntPtr;

namespace WuyinMuManager
{
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class WuyinMsnForm : System.Windows.Forms.Form
    {
        private System.ComponentModel.IContainer components = null;
        Point location = new Point(0, 0);
        Point p = new Point(0, 0);
        Size size = new Size(600, 400);
        bool isMax = false;
        private System.Windows.Forms.ContextMenu ControlMenu;
        private System.Windows.Forms.MenuItem menuItem6;
        private System.Windows.Forms.MenuItem menuRestore;
        private System.Windows.Forms.MenuItem menuMove;
        private System.Windows.Forms.MenuItem menuSize;
        private System.Windows.Forms.MenuItem menuMin;
        private System.Windows.Forms.MenuItem menuMax;
        private System.Windows.Forms.MenuItem menuClose;
        bool cursor = false;
        Graphics g = null;
        bool IsDiaLog = false;
        public const int WM_NCPAINT = 0x85;
        Color DeskTop = Color.FromArgb(122, 150, 223);
        Color Caption = Color.FromArgb(216, 228, 248);

        protected override void WndProc(ref Message m)
        {
            RectangleF rectf;

            switch (m.Msg)
            {
                case WM_NCPAINT:
                    IntPtr hrgn = m.WParam;
                    if (hrgn != (IntPtr)1)
                    {
                        Region rgn = Region.FromHrgn(hrgn);
                        g = CreateGraphics();
                        rectf = rgn.GetBounds(g);
                        PaintNonClientArea(g, rectf);
                    }
                    else
                    {
                        g = CreateGraphics();
                        rectf = new RectangleF(0, -23, Width, 23);
                        PaintNonClientArea(g, rectf);
                        g.Dispose();
                    }

                    //base.WndProc(ref m);
                    break;

                default:
                    base.WndProc(ref m);
                    break;
            }
        }

        public bool isDialog
        {
            get
            {
                return IsDiaLog;
            }
            set
            {
                IsDiaLog = value;
            }
        }

        void PaintNonClientArea(Graphics g, RectangleF r)
        {
            //this.PainWindow(g);
        }

        public WuyinMsnForm()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();
            this.MinimumSize = new Size(160, 20);
            //int MyMenu = GetSystemMenu(this.Handle,0);
            this.FormBorderStyle = FormBorderStyle.None;
            //LoadMenu((IntPtr)MyMenu,"");
            //MessageBox.Show(MyMenu.ToString());
            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //int handle = Win32.User.GetWindowDC(this.Handle);
            g = this.CreateGraphics();
            //g = Graphics.FromHdc(((IntPtr)handle));
            this.PainWindow(g);
            //Win32.RECT Rect = new Win32.RECT();
            //Win32.User.GetWindowRect((IntPtr)handle,ref Rect);
            //Win32.User.ReleaseDC(this.Handle,(IntPtr)handle);
        }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                DrawText(this.CreateGraphics());
                base.Text = value;
            }
        }


        protected override void* onMouseMove*(MouseEventArgs e)
        {
            //this.Form1_MouseMove(this,e);
            base.*onMouseMove* (e);
        }

    void PainWindow(Graphics gx)
    {
        Bitmap bmp = new Bitmap(this.Width, this.Height);
        Graphics g = Graphics.FromImage(bmp);
        Pen pen;
        //左边框1
        pen = new Pen(Color.FromArgb(133, 150, 202));
        g.DrawLine(pen, new Point(0, 0), new Point(0, this.Height));
        //上边框1
        g.DrawLine(pen, new Point(0, 0), new Point(this.Width, 0));
        pen = new Pen(Color.White);
        g.DrawLine(pen, new Point(1, 1), new Point(1, this.Height - 1));
        g.DrawLine(pen, new Point(1, 1), new Point(this.Width - 1, 1));

        //右边框1
        pen = new Pen(Color.FromArgb(90, 108, 162));
        g.DrawLine(pen, new Point(this.Width - 1, 1), new Point(this.Width - 1, this.Height));
        //下边框1
        g.DrawLine(pen, new Point(1, this.Height - 1), new Point(this.Width, this.Height - 1));
        pen = new Pen(Color.FromArgb(191, 202, 235));
        g.DrawLine(pen, new Point(this.Width - 2, 2), new Point(this.Width - 2, this.Height - 2));
        g.DrawLine(pen, new Point(2, this.Height - 2), new Point(this.Width - 2, this.Height - 2));

        Brush brush = new SolidBrush(Color.FromArgb(206, 216, 239));
        g.FillRectangle(brush, 2, 2, this.Width - 4, this.Height - 4);
        if (this.BackColor.ToKnownColor() != KnownColor.Control)
        {
            brush = new SolidBrush(this.BackColor);
            g.FillRectangle(brush, 4, 4, this.Width - 8, this.Height - 8);
        }
        else
        {
            this.BackColor = Caption;
            brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(4, 4), new Size(this.Width - 8, 20)), Color.FromArgb(248, 248, 248), Color.FromArgb(220, 226, 240), 3600, true);
            g.FillRectangle(brush, 4, 4, this.Width - 8, this.Height - 8);
        }

        //画标题栏
        brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(4, 4), new Size(this.Width - 8, 20)), Color.FromArgb(192, 208, 240), Color.FromArgb(248, 248, 248), 120, true);
        g.FillRectangle(brush, 4, 4, this.Width - 8, 20);

        //画图标和标题
        DrawIcon(g);
        DrawText(g);

        //画按钮
        if (!this.isDialog)
        {
            this.DrawMinButton1(g);
            if (this.MaximizeBox)
                this.DrawMaxButton1(g);
        }
        this.DrawCloseButton1(g);
        gx.DrawImage(bmp, 0, 0);
    }

    void DrawIcon(Graphics g)
    {
        g.DrawIcon(this.Icon, new Rectangle(new Point(6, 6), new Size(16, 16)));
    }

    void DrawText(Graphics g)
    {
        Brush brush = new SolidBrush(Color.FromArgb(61, 77, 129));
        g.DrawString(this.Text, new Font("tohoma", 9), brush, new Point(26, 6), StringFormat.GenericDefault);
    }

    void DrawMinButton1(Graphics g)
    {
        //最小化
        Brush brush = new SolidBrush(Color.FromArgb(128, 144, 168));
        g.FillRectangle(brush, this.Width - 57, 6, 16, 16);
        brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6, this.Width - 63), new Size(17, 17)), Color.FromArgb(199, 207, 231), Color.FromArgb(248, 248, 248), System.Drawing.Drawing2D.LinearGradientMode.Vertical);
        g.FillRectangle(brush, this.Width - 56, 7, 14, 14);
        Pen pen;
        if (this.MinimizeBox)
            pen = new Pen(Color.FromArgb(48, 64, 120), 3);
        else
            pen = new Pen(Color.Silver, 3);
        g.DrawLine(pen, new Point(this.Width - 53, 17), new Point(this.Width - 46, 17));
    }

    void DrawMinButton2(Graphics g)
    {
        //最小化
        Brush brush = new SolidBrush(Color.FromArgb(128, 144, 168));
        g.FillRectangle(brush, this.Width - 57, 6, 16, 16);
        brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6, this.Width - 63), new Size(17, 17)), Color.White, Color.Snow, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
        g.FillRectangle(brush, this.Width - 56, 7, 14, 14);
        Pen pen;
        if (this.MinimizeBox)
            pen = new Pen(Color.FromArgb(48, 64, 120), 3);
        else
            pen = new Pen(Color.Silver, 3);
        g.DrawLine(pen, new Point(this.Width - 53, 17), new Point(this.Width - 46, 17));
    }

    void DrawMaxButton1(Graphics g)
    {
        //最大化或者恢复
        Brush brush = new SolidBrush(Color.FromArgb(128, 144, 168));
        g.FillRectangle(brush, this.Width - 40, 6, 16, 16);
        brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6, this.Width - 63), new Size(17, 17)), Color.FromArgb(199, 207, 231), Color.FromArgb(248, 248, 248), System.Drawing.Drawing2D.LinearGradientMode.Vertical);
        g.FillRectangle(brush, this.Width - 39, 7, 14, 14);
        if (!isMax)
        {
            brush = new SolidBrush(Color.FromArgb(48, 64, 120));
            g.FillRectangle(brush, this.Width - 37, 9, 10, 10);
            Pen pen = new Pen(Color.White, 6);
            g.DrawLine(pen, new Point(this.Width - 36, 15), new Point(this.Width - 28, 15));
        }
        else
        {
            brush = new SolidBrush(Color.FromArgb(48, 64, 120));
            g.FillRectangle(brush, this.Width - 34, 9, 7, 7);
            Pen pen = new Pen(Color.White, 4);
            g.DrawLine(pen, new Point(this.Width - 33, 13), new Point(this.Width - 28, 13));
            g.FillRectangle(brush, this.Width - 37, 12, 7, 7);
            g.DrawLine(pen, new Point(this.Width - 36, 16), new Point(this.Width - 31, 16));
        }
    }

    void DrawMaxButton2(Graphics g)
    {
        //最大化或者恢复
        Brush brush = new SolidBrush(Color.FromArgb(128, 144, 168));
        g.FillRectangle(brush, this.Width - 40, 6, 16, 16);
        brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6, this.Width - 63), new Size(17, 17)), Color.White, Color.Snow, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
        g.FillRectangle(brush, this.Width - 39, 7, 14, 14);
        if (!isMax)
        {
            brush = new SolidBrush(Color.FromArgb(48, 64, 120));
            g.FillRectangle(brush, this.Width - 37, 9, 10, 10);
            Pen pen = new Pen(Color.White, 6);
            g.DrawLine(pen, new Point(this.Width - 36, 15), new Point(this.Width - 28, 15));
        }
        else
        {
            brush = new SolidBrush(Color.FromArgb(48, 64, 120));
            g.FillRectangle(brush, this.Width - 34, 9, 7, 7);
            Pen pen = new Pen(Color.White, 4);
            g.DrawLine(pen, new Point(this.Width - 33, 13), new Point(this.Width - 28, 13));
            g.FillRectangle(brush, this.Width - 37, 12, 7, 7);
            g.DrawLine(pen, new Point(this.Width - 36, 16), new Point(this.Width - 31, 16));
        }
    }

    void DrawCloseButton1(Graphics g)
    {
        //关闭
        Brush brush = new SolidBrush(Color.FromArgb(128, 144, 168));
        g.FillRectangle(brush, this.Width - 23, 6, 16, 16);
        brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6, this.Width - 63), new Size(17, 17)), Color.FromArgb(199, 207, 231), Color.FromArgb(248, 248, 248), System.Drawing.Drawing2D.LinearGradientMode.Vertical);
        g.FillRectangle(brush, this.Width - 22, 7, 14, 14);
        Pen pen = new Pen(Color.FromArgb(48, 64, 120), 2);
        g.DrawLine(pen, new Point(this.Width - 20, 9), new Point(this.Width - 11, 18));
        g.DrawLine(pen, new Point(this.Width - 11, 9), new Point(this.Width - 20, 18));
    }

    void DrawCloseButton2(Graphics g)
    {
        //关闭
        Brush brush = new SolidBrush(Color.FromArgb(128, 144, 168));
        g.FillRectangle(brush, this.Width - 23, 6, 16, 16);
        brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6, this.Width - 63), new Size(17, 17)), Color.Red, Color.Snow, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
        g.FillRectangle(brush, this.Width - 22, 7, 14, 14);
        Pen pen = new Pen(Color.FromArgb(48, 64, 120), 2);
        g.DrawLine(pen, new Point(this.Width - 20, 9), new Point(this.Width - 11, 18));
        g.DrawLine(pen, new Point(this.Width - 11, 9), new Point(this.Width - 20, 18));
    }

    #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.ControlMenu = new System.Windows.Forms.ContextMenu();
        this.menuRestore = new System.Windows.Forms.MenuItem();
        this.menuMove = new System.Windows.Forms.MenuItem();
        this.menuSize = new System.Windows.Forms.MenuItem();
        this.menuMin = new System.Windows.Forms.MenuItem();
        this.menuMax = new System.Windows.Forms.MenuItem();
        this.menuItem6 = new System.Windows.Forms.MenuItem();
        this.menuClose = new System.Windows.Forms.MenuItem();
        // 
        // ControlMenu
        // 
        this.ControlMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                        this.menuRestore,
                                        this.menuMove,
                                        this.menuSize,
                                        this.menuMin,
                                        this.menuMax,
                                        this.menuItem6,
                                        this.menuClose});
        // 
        // menuRestore
        // 
        this.menuRestore.Enabled = false;
        this.menuRestore.Index = 0;
        this.menuRestore.Text = "还原(&R)";
        // 
        // menuMove
        // 
        this.menuMove.Index = 1;
        this.menuMove.Text = "移动(&M)";
        this.menuMove.Click += new System.EventHandler(this.menuMove_Click);
        // 
        // menuSize
        // 
        this.menuSize.Index = 2;
        this.menuSize.Text = "大小(&S)";
        this.menuSize.Click += new System.EventHandler(this.menuSize_Click);
        // 
        // menuMin
        // 
        this.menuMin.Index = 3;
        this.menuMin.Text = "最小化(&N)";
        this.menuMin.Click += new System.EventHandler(this.menuMin_Click);
        // 
        // menuMax
        // 
        this.menuMax.Index = 4;
        this.menuMax.Text = "最大化(&X)";
        this.menuMax.Click += new System.EventHandler(this.menuMax_Click);
        // 
        // menuItem6
        // 
        this.menuItem6.Index = 5;
        this.menuItem6.Text = "-";
        // 
        // menuClose
        // 
        this.menuClose.DefaultItem = true;
        this.menuClose.Index = 6;
        this.menuClose.Shortcut = System.Windows.Forms.Shortcut.AltF4;
        this.menuClose.Text = "关闭(&C)";
        this.menuClose.Click += new System.EventHandler(this.menuClose_Click);
        // 
        // WuyinMsnForm
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
        this.ClientSize = new System.Drawing.Size(160, 120);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "WuyinMsnForm";
        this.Text = "Form1";
        this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
        this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
        this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
        this.Load += new System.EventHandler(this.WuyinMsnForm_Load);
        this.DoubleClick += new System.EventHandler(this.Form1_DoubleClick);
        this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
        this.Locati * onChange * d += new System.EventHandler(this.Form1_Locati * onChange * d);
        this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);

    }
    #endregion

    protected override void OnCreateControl()
    {
        base.OnCreateControl();
    }

    private void Form1_DoubleClick(object sender, EventArgs e)
    {
        if (!this.isDialog)
        {
            Point p = new Point(0, 0);
            GetCursorPos(out p);
            if (p.X - this.Location.X >= this.Width - 70 || p.Y - this.Location.Y >= 20)
                return;
            if (this.Size.Width >= Screen.AllScreens[0].WorkingArea.Size.Width && this.Size.Height >= Screen.AllScreens[0].WorkingArea.Size.Height)
                Restore();
            else
            {
                Max();
            }
        }
    }

    [DllImport("user32")] public static extern int GetCursorPos(out Point lpPoint);
    [DllImport("user32")] public static extern int GetSystemMenu(HWND hwnd, int bRevert);
    [DllImport("user32")] public static extern int LoadMenu(HANDLE hInstance, string lpString);
    //[DllImport("user32")] public static extern int MoveWindow(HWND hwnd, int x, int y, int nWidth, int nHeight, int bRepaint);

    void Restore()
    {
        //MessageBox.Show(String.Format("W:{0},H:{1}",size.Width,size.Height));
        this.Size = this.size;
        this.Location = this.location;
        this.WindowState = FormWindowState.Normal;
        this.isMax = false;
        this.Invalidate();
        this.menuMax.Enabled = true;
        this.menuRestore.Enabled = false;
        this.menuSize.Enabled = true;
        this.menuMove.Enabled = true;
        //this.PainWindow(g);
    }

    void Max()
    {
        //MessageBox.Show(String.Format("W:{0},H:{1}",size.Width,size.Height));
        //this.location = this.Location;
        //this.size = this.Size;
        this.isMax = true;
        this.Size = Screen.AllScreens[0].WorkingArea.Size;
        this.Location = new Point(0, 0);
        this.Invalidate();
        this.menuMax.Enabled = false;
        this.menuRestore.Enabled = true;
        this.menuSize.Enabled = false;
        this.menuMove.Enabled = false;
        //this.PainWindow(g);
    }

    void CgBt(MouseEventArgs e)
    {
        if (e.X > this.Width - 23 && e.X < this.Width - 7 && e.Y > 7 && e.Y < 20)
        {
            this.DrawCloseButton2(g);
            return;
        }
        else
        {
            this.DrawCloseButton1(g);
        }
        if (this.isDialog)
            return;
        if (e.X > this.Width - 57 && e.X < this.Width - 41 && e.Y > 7 && e.Y < 22)
        {
            this.DrawMinButton2(g);
            return;
        }
        else
        {
            this.DrawMinButton1(g);
        }
        if (e.X > this.Width - 40 && e.X < this.Width - 24 && e.Y > 7 && e.Y < 22)
        {
            this.DrawMaxButton2(g);
            return;
        }
        else
        {
            this.DrawMaxButton1(g);
        }
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.X > this.Width - 70 && e.Y < 25)
            CgBt(e);

        if (isMax)
            return;

        CursorResizeForm(e, this);

        if (e.Button == MouseButtons.Left && e.Y > 0 && e.Y < 30 && e.X < this.Width - 70 && e.Y <= 20)
            this.Location = new Point(Control.MousePosition.X - p.X, Control.MousePosition.Y - p.Y);
    }

    private void CursorResizeForm(MouseEventArgs e, Control control)
    {   //鼠标控制窗体大小
        if (e.Y > control.Height - 10 && e.Y < control.Height && e.X > control.Width - 10 && e.X < control.Width)
            control.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
        else if (e.X > control.Width - 6 && e.X < control.Width && control.Cursor != System.Windows.Forms.Cursors.SizeNWSE)
            control.Cursor = System.Windows.Forms.Cursors.SizeWE;
        else if (e.Y > control.Height - 6 && e.Y < control.Height && control.Cursor != System.Windows.Forms.Cursors.SizeNWSE)
            control.Cursor = System.Windows.Forms.Cursors.SizeNS;

        else if (e.X > 0 && e.X < 6 && control.Cursor != System.Windows.Forms.Cursors.SizeNESW)
            control.Cursor = System.Windows.Forms.Cursors.SizeWE;
        else if (!cursor)
            control.Cursor = Cursors.Default;
        if (e.Button == MouseButtons.Left)
        {
            if (control.Cursor == System.Windows.Forms.Cursors.SizeWE && e.X > 16)
                control.Width = Control.MousePosition.X - control.Left + 3;
            else if (control.Cursor == Cursors.SizeNWSE)
            {
                control.Width = Control.MousePosition.X - control.Left + 3;
                control.Height = Control.MousePosition.Y - control.Top + 3;
            }
            else if (control.Cursor == System.Windows.Forms.Cursors.SizeNS)
                control.Height = Control.MousePosition.Y - control.Top + 3;
        }
    }

    private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (this.isDialog)
            return;
        cursor = true;
        p = new Point(e.X, e.Y);
        //location = this.Location;
        //size = this.Size;
        if (e.Button == MouseButtons.Right && e.X > 0 && e.X < this.Width - 70 && e.Y > 0 && e.Y < 22)
        {
            this.ControlMenu.Show(this, new Point(e.X, e.Y));
            return;
        }
        if (e.Button == MouseButtons.Left && e.X > 0 && e.X < 26 && e.Y > 0 && e.Y < 22)
            this.ControlMenu.Show(this, new Point(0, 20));
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left)
            return;
        cursor = false;
        //三个控制按钮的
        if (e.X > this.Width - 24 && e.X < this.Width - 6 && e.Y > 7 && e.Y < 20)
            Close();
        if (this.isDialog)
            return;
        if (e.X > this.Width - 57 && e.X < this.Width - 48 && e.Y > 7 && e.Y < 20)
            this.WindowState = FormWindowState.Minimized;
        if (e.X > this.Width - 48 && e.X < this.Width - 23 && e.Y > 7 && e.Y < 20)
        {
            if (isMax)
                Restore();
            else
                Max();
        }
    }

    private void Form1_SizeChanged(object sender, EventArgs e)
    {
        if (this.Size.Width <= 20 || this.Size.Height <= 20)
            return;
        if (!isMax)
        {
            this.location = this.Location;
            this.size = this.Size;
        }
        //this.PainWindow(g);
    }

    protected override void DefWndProc(ref Message m)
    {
        //MessageBox.Show(m.ToString());
        base.DefWndProc(ref m);
    }


    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Alt && e.KeyCode == Keys.Space)
            this.ControlMenu.Show(this, new Point(4, 22));
    }

    private void menuClose_Click(object sender, System.EventArgs e)
    {
        Close();
    }

    private void menuMax_Click(object sender, System.EventArgs e)
    {
        Max();
    }

    private void menuMin_Click(object sender, System.EventArgs e)
    {
        this.WindowState = FormWindowState.Minimized;
    }

    private void menuSize_Click(object sender, System.EventArgs e)
    {
        Pen pen = new Pen(Color.Black, 4);
        g.DrawLine(pen, new Point(0, 0), new Point(0, this.Height));
        g.DrawLine(pen, new Point(0, 0), new Point(this.Width, 0));
        g.DrawLine(pen, new Point(this.Width, 0), new Point(this.Width, this.Height));
        g.DrawLine(pen, new Point(0, this.Height), new Point(this.Width, this.Height));
        this.Cursor = Cursors.SizeAll;
    }

    private void menuMove_Click(object sender, System.EventArgs e)
    {
        Pen pen = new Pen(Color.Black, 4);
        g.DrawLine(pen, new Point(0, 0), new Point(0, this.Height));
        g.DrawLine(pen, new Point(0, 0), new Point(this.Width, 0));
        g.DrawLine(pen, new Point(this.Width, 0), new Point(this.Width, this.Height));
        g.DrawLine(pen, new Point(0, this.Height), new Point(this.Width, this.Height));
        mouse_event(MouseEvents.MOUSEEVENTF_MOVE, this.Location.X + this.Width / 2, this.Location.Y + this.Height / 2, 0, 0);
        //MoveWindow(this.Handle,0,0,this.Width,this.Height,0);
        this.Cursor = Cursors.SizeAll;
    }

    /// <summary>
    /// 模拟鼠标事件
    /// </summary>
    /// <param name="dwFlags">鼠标事件的Enum</param>
    /// <param name="dx">X座标</param>
    /// <param name="dy">Y座标</param>
    /// <param name="cButtons"></param>
    /// <param name="dwExtraInfo"></param>
    [DllImport("user32.dll")]
    public static extern void mouse_event(MouseEvents dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

    /// <summary>
    /// 定义模拟鼠标的常数
    /// </summary>
    public enum MouseEvents : uint
    {
        MOUSEEVENTF_LEFTDOWN = 0x2,
        MOUSEEVENTF_LEFTUP = 0x4,
        MOUSEEVENTF_MIDDLEDOWN = 0x20,
        MOUSEEVENTF_MIDDLEUP = 0x40,
        MOUSEEVENTF_MOVE = 0x1,
        MOUSEEVENTF_ABSOLUTE = 0x8000,
        MOUSEEVENTF_RIGHTDOWN = 0x8,
        MOUSEEVENTF_RIGHTUP = 0x10,
    }

    private void Form1_Locati* onChange* d(object sender, EventArgs e)
    {
        if (!isMax)
            location = Location;
    }

    protected override void OnControlAdded(ControlEventArgs e)
    {
        if (e.Control.Top < 24) e.Control.Top += 24;
        if (e.Control.Left < 2) e.Control.Left += 2;

        /*
        if(e.Control.BackColor.ToKnownColor()==KnownColor.Control)
        {        
                //((MyControl)e.Control).DoPaint();
                //Color c = Color.FromArgb(255,220,226,240);
                //e.Control.BackColor=c;        
                int C = (Win32.GDI.GetPixel((IntPtr)Win32.User.GetWindowDC((IntPtr)0),e.Control.Location.X,e.Control.Location.Y));
                Color c = Color.FromName(C.ToString());
                e.Control.BackColor = c;
        }
        base.OnControlAdded (e);
        */
    }

    private void WuyinMsnForm_Load(object sender, System.EventArgs e)
    {
        this.Height += 22;
        this.Width += 2;
    }
}

public class MyControl : Control
{
    public void DoPaint()
    {

    }

    public void DoPaint(PaintEventArgs e)
    {
        if (this.GetType().ToString() == "System.Windows.Forms.Label")
        {
            e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), this.Location, StringFormat.GenericDefault);
            return;
        }

        this.BackColor = Color.Empty;
        Brush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(4, 4), new Size(this.Width - 8, 20)), Color.FromArgb(248, 248, 248), Color.FromArgb(220, 226, 240), 3600, true);
        e.Graphics.FillRectangle(brush, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        //base.OnPaint (e);
        DoPaint(e);
    }
}
}
Contributors: FHL