WinForm自定义窗体

2020-12-13 03:57

阅读:203

标签:winform   自定义窗体   

  public partial class Form3 : Form
    {
        const int WM_NCHITTEST = 0x0084;
        const int HT_LEFT = 10;
        const int HT_RIGHT = 11;
        const int HT_TOP = 12;
        const int HT_TOPLEFT = 13;
        const int HT_TOPRIGHT = 14;
        const int HT_BOTTOM = 15;
        const int HT_BOTTOMLEFT = 16;
        const int HT_BOTTOMRIGHT = 17;
        const int HT_CAPTION = 2;
        public Form3()
        {
            InitializeComponent();
            rectRegion = new System.Drawing.Rectangle(0, 0, this.Width, this.Height);
            //SetWindowRegion();
        }
        private System.Drawing.Rectangle rectRegion;
        private static int x, y = 0;
        public void SetWindowRegion()
        {
            System.Drawing.Drawing2D.GraphicsPath FormPath;
            FormPath = new System.Drawing.Drawing2D.GraphicsPath();
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, this.Width, this.Height);
            FormPath = GetRoundedRectPath(rect,10);
            this.Region = new Region(FormPath);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            System.Drawing.Drawing2D.GraphicsPath FormPath;
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, this.Width, this.Height);
            FormPath = GetRoundedRectPath(rect, 10);
            Pen mypen = new Pen(Color.Gray, 2);
            g.DrawPath(mypen, FormPath);
            base.OnPaint(e);
        }
        private GraphicsPath GetRoundedRectPath(System.Drawing.Rectangle rect, int radius)
        {
            int diameter = radius;
            System.Drawing.Rectangle arcRect = new System.Drawing.Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            // 左上角
            path.AddArc(arcRect, 180, 90);

            // 右上角
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);

            // 右下角
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // 左下角
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();//闭合曲线
            return path;
        }
        protected override void WndProc(ref Message Msg)
        {
            if (Msg.Msg == WM_NCHITTEST)
            {
                //获取鼠标位置
                int nPosX = (Msg.LParam.ToInt32() & 65535);
                int nPosY = (Msg.LParam.ToInt32() >> 16);
                //右下角
                if (nPosX >= this.Right - 6 && nPosY >= this.Bottom - 6)
                {
                    Msg.Result = new IntPtr(HT_BOTTOMRIGHT);
                    return;
                }
                //左上角
                else if (nPosX = this.Bottom - 6)
                {
                    Msg.Result = new IntPtr(HT_BOTTOMLEFT);
                    return;
                }
                //右上角
                else if (nPosX >= this.Right - 6 && nPosY = this.Right - 2)
                {
                    Msg.Result = new IntPtr(HT_RIGHT);
                    return;
                }
                else if (nPosY >= this.Bottom - 2)
                {
                    Msg.Result = new IntPtr(HT_BOTTOM);
                    return;
                }
                else if (nPosX 
Demo 下载地址http://download.csdn.net/detail/u011470119/7589233

WinForm自定义窗体,搜素材,soscw.com

WinForm自定义窗体

标签:winform   自定义窗体   

原文地址:http://blog.csdn.net/xiaoweiserver/article/details/36695957


评论


亲,登录后才可以留言!