zoukankan      html  css  js  c++  java
  • 自定义窗体和控件

    有关的此内容好的文章参考:http://www.cnblogs.com/yuanfan/archive/2010/12/03/1895689.html

    http://blog.csdn.net/xiaohan2826/article/details/8520979

    1.自定义窗体

      项目中都保持一个窗体风格,比如窗体标题栏,最小化最大化按钮。建立一个form窗体,修改后,编译生成dll,添加引用此dll,继承此dll窗体。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using MyFormDLL;
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : FrmBaseWindow
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void panelTitle_Paint(object sender, PaintEventArgs e)
            {
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                CanResize = true;
                MyWindowTitle = "999988888";
            }
        }
    }

    自定义的窗体,FormBorderStyle 设置成None,放置Panel,在Panel放置最大化最小化关闭按钮

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace MyFormDLL
    {
        public partial class FrmBaseWindow : Form
        {
          
            private string _myWindowTitle;
            [DefaultValue(typeof(string), "TTC 称重管理平台")]
            public string MyWindowTitle
            {
                get
                {
                    return _myWindowTitle;
                }
                set
                {
                    _myWindowTitle = value;
                    lblTitle.Text = _myWindowTitle;
                }
            }
            
    
            //是否允许窗体改变大小     
            public bool CanResize{get;set;}
           
    
    
    
    
            public FrmBaseWindow()
            {
                InitializeComponent();
            }
    
            private void FrmBaseWindow_Load(object sender, EventArgs e)
            {
           
               
            }
    
            private void btnMaxSize_Click(object sender, EventArgs e)
            {
                if (this.WindowState == FormWindowState.Maximized)
                {
                    this.WindowState = FormWindowState.Normal;
                }
                else
                {
                    this.WindowState = FormWindowState.Maximized;
                }           
            }
           
    
            private void btnMinSize_Click(object sender, EventArgs e)
            {
                this.WindowState = FormWindowState.Minimized;
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            protected override void WndProc(ref Message m)
            {
                switch (m.Msg)
                {
                    //case Win32.WM_ERASEBKGND:
                    //    m.Result = IntPtr.Zero;
                    //    break;
                    case Win32.WM_NCHITTEST:
                        WmNcHitTest(ref m);
                        break;
                    default:
                        base.WndProc(ref m);
                        break;
                }
    
            }
    
    
            //调整窗体大小
            private void WmNcHitTest(ref Message m)
            {
                int wparam = m.LParam.ToInt32();
                Point mouseLocation = new Point(LOWORD(wparam), HIWORD(wparam));
                mouseLocation = PointToClient(mouseLocation);
    
                if (WindowState != FormWindowState.Maximized)
                {
                    if (CanResize == true)
                    {
                        if (mouseLocation.X < 5 && mouseLocation.Y < 5)
                        {
                            m.Result = new IntPtr(Win32.HTTOPLEFT);
                            return;
                        }
    
                        if (mouseLocation.X > Width - 5 && mouseLocation.Y < 5)
                        {
                            m.Result = new IntPtr(Win32.HTTOPRIGHT);
                            return;
                        }
    
                        if (mouseLocation.X < 5 && mouseLocation.Y > Height - 5)
                        {
                            m.Result = new IntPtr(Win32.HTBOTTOMLEFT);
                            return;
                        }
    
                        if (mouseLocation.X > Width - 5 && mouseLocation.Y > Height - 5)
                        {
                            m.Result = new IntPtr(Win32.HTBOTTOMRIGHT);
                            return;
                        }
    
                        if (mouseLocation.Y < 3)
                        {
                            m.Result = new IntPtr(Win32.HTTOP);
                            return;
                        }
    
                        if (mouseLocation.Y > Height - 3)
                        {
                            m.Result = new IntPtr(Win32.HTBOTTOM);
                            return;
                        }
    
                        if (mouseLocation.X < 3)
                        {
                            m.Result = new IntPtr(Win32.HTLEFT);
                            return;
                        }
    
                        if (mouseLocation.X > Width - 3)
                        {
                            m.Result = new IntPtr(Win32.HTRIGHT);
                            return;
                        }
                    }
                }
                m.Result = new IntPtr(Win32.HTCAPTION);
    
                //标题栏随最大化移动
                //panelTitle.Width = mouseLocation.X - 2;
            }
    
            /// <summary>
            /// 取低位 X 坐标
            /// </summary>
            public static int LOWORD(int value)
            {
                return value & 0xFFFF;
            }
    
            /// <summary>
            /// 取高位 Y 坐标
            /// </summary>
            public static int HIWORD(int value)
            {
                return value >> 16;
            }
    
            protected override void OnMouseMove(MouseEventArgs e)
            {
                base.OnMouseMove(e);
            }
    
            protected override void OnMouseDown(MouseEventArgs e)
            {
                base.OnMouseDown(e);
                if (e.Button == MouseButtons.Left)
                {
                    Win32.ReleaseCapture();
                    Win32.SendMessage(this.Handle, Win32.WM_NCLBUTTONDOWN, Win32.HTCAPTION, 0);
    
                }
            }
    
            private void panelTitle_MouseDown(object sender, MouseEventArgs e)
            {
                base.OnMouseDown(e);
                if (e.Button == MouseButtons.Left)
                {
                    Win32.ReleaseCapture();
                    Win32.SendMessage(this.Handle, Win32.WM_NCLBUTTONDOWN, Win32.HTCAPTION, 0);
    
                }
    
            }
          
            private void FrmWindow_SizeChanged(object sender, EventArgs e)
            {
               // this.lblTitle.Location = new System.Drawing.Point(50, 2);
    
                //标题栏随最大化移动
                panelTitle.Width = this.Width;
                //移动控制按钮
                this.btnMinSize.Location = new System.Drawing.Point(this.Width - 140, 2);
                this.btnMaxSize.Location = new System.Drawing.Point(this.Width - 90, 2);
                this.btnClose.Location = new System.Drawing.Point(this.Width - 50, 2);
            }
    
       
    
    
    
    
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    namespace MyFormDLL
    {
        public class Win32
        {
            #region Window Const
    
            public const int WM_ERASEBKGND = 0x0014;
            public const int WM_LBUTTONDOWN = 0x0201;
            public const int WM_LBUTTONUP = 0x0202;
            public const int WM_LBUTTONDBLCLK = 0x0203;
            public const int WM_WINDOWPOSCHANGING = 0x46;
            public const int WM_PAINT = 0xF;
            public const int WM_CREATE = 0x0001;
            public const int WM_ACTIVATE = 0x0006;
            public const int WM_NCCREATE = 0x0081;
            public const int WM_NCCALCSIZE = 0x0083;
            public const int WM_NCPAINT = 0x0085;
            public const int WM_NCACTIVATE = 0x0086;
            public const int WM_NCLBUTTONDOWN = 0x00A1;
            public const int WM_NCLBUTTONUP = 0x00A2;
            public const int WM_NCLBUTTONDBLCLK = 0x00A3;
            public const int WM_NCMOUSEMOVE = 0x00A0;
    
            public const int WM_NCHITTEST = 0x0084;
    
            public const int HTLEFT = 10;
            public const int HTRIGHT = 11;
            public const int HTTOP = 12;
            public const int HTTOPLEFT = 13;
            public const int HTTOPRIGHT = 14;
            public const int HTBOTTOM = 15;
            public const int HTBOTTOMLEFT = 0x10;
            public const int HTBOTTOMRIGHT = 17;
            public const int HTCAPTION = 2;
            public const int HTCLIENT = 1;
    
            public const int WM_FALSE = 0;
            public const int WM_TRUE = 1;
    
    
    
            #endregion
    
            [DllImport("user32.dll")]
            public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    
            [DllImport("user32.dll")]
            public static extern bool ReleaseCapture();
    
    
        }
    }
  • 相关阅读:
    特征选择(四)- 分散度
    机器学习实践中的7种常见错误
    逻辑回归 vs 决策树 vs 支持向量机(I)
    逻辑回归 vs 决策树 vs 支持向量机(II)
    线性回归和逻辑回归
    Mac 下 python 环境问题
    Linux目录/usr结构说明
    Python 处理 json
    感知机、logistic回归 损失函数对比探讨
    SVM探讨
  • 原文地址:https://www.cnblogs.com/ike_li/p/4441629.html
Copyright © 2011-2022 走看看