C# 创建无边框,任意样式窗体,无边框窗体的移动


界面布局如下:

窗体中添加一个PictureBox控件
技术分享


有边框窗体
技术分享


无边框窗体
技术分享


代码实现:

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        #region 创建无边框,任意样式窗体
        private void Form2_Load(object sender, EventArgs e)
        {
            this.TransparencyKey = Color.White;             //设置默认透明色
            this.BackColor = this.TransparencyKey;          //设置当前窗体的背景色为透明
            this.FormBorderStyle = FormBorderStyle.None;    //隐藏窗体边框
        }
        #endregion

        #region 控制无边框窗体的移动
        //using System.Runtime.InteropServices;
        [DllImport("user32.dll")]       
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            //常量
            int WM_SYSCOMMAND = 0x0112;

            //窗体移动
            int SC_MOVE = 0xF010;
            int HTCAPTION = 0x0002;

            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        ////常量
        //int WM_SYSCOMMAND = 0x0112;
        ////改变窗体大小
        //int WMSZ_LEFT = 0xF001;
        //int WMSZ_RIGHT = 0xF002;
        //int WMSZ_TOP = 0xF003;
        //int WMSZ_TOPLEFT = 0xF004;
        //int WMSZ_TOPRIGHT = 0xF005;
        //int WMSZ_BOTTOM = 0xF006;
        //int WMSZ_BOTTOMLEFT = 0xF007;
        //int WMSZ_BOTTOMRIGHT = 0xF008;
        //ReleaseCapture();
        //SendMessage(this.Handle, WM_SYSCOMMAND, WMSZ_BOTTOM, 0);

        //SendMessage(this.Handle, WM_SYSCOMMAND, WMSZ_TOP, 0);
        #endregion
    }


png图像资源
技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。