无边框窗体设置

#region 方法:无边框拖动窗体
        Point mouseOff;//鼠标移动位置变量
        bool RightFlag;//标签是否为左键
        private void groupMenu_MouseUp(object sender, MouseEventArgs e)
        {
            if (RightFlag)
            {
                RightFlag = false;//释放鼠标后标注为false;
            }

        }

        private void groupMenu_MouseMove(object sender, MouseEventArgs e)
        {
            if (RightFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }

        private void groupMenu_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                RightFlag = true;                  //点击左键按下时标注为true;
            }
            //MouseDown_ResizeForm(this);
        }
        #endregion
#region 两边阴影
        private const int CS_DropSHADOW = 0x20000;
        private const int GCL_STYLE = (-26);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassLong(IntPtr hwnd, int nIndex);

        private void SetShadow()
        {
            SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW);
        }
        #endregion
#region 四边阴影
        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
                    (
                        int nLeftRect,
                        int nTopRect,
                        int nRightRect,
                        int nBottomRect,
                        int nWidthEllipse,
                        int nHeightEllipse
                     );

        [DllImport("dwmapi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);

        [DllImport("dwmapi.dll")]
        public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

        [DllImport("dwmapi.dll")]
        public static extern int DwmIsCompositionEnabled(ref int pfEnabled);

        private bool m_aeroEnabled;
        private const int CS_DROPSHADOW = 0x00020000;
        private const int WM_NCPAINT = 0x0085;
        private const int WM_ACTIVATEAPP = 0x001C;

        public struct MARGINS
        {
            public int leftWidth;
            public int rightWidth;
            public int topHeight;
            public int bottomHeight;
        }

        private const int WM_NCHITTEST = 0x84;
        private const int HTCLIENT = 0x1;
        private const int HTCAPTION = 0x2;

        protected override CreateParams CreateParams
        {
            get
            {
                m_aeroEnabled = CheckAeroEnabled();

                CreateParams cp = base.CreateParams;
                if (!m_aeroEnabled)
                    cp.ClassStyle |= CS_DROPSHADOW;

                return cp;
            }
        }

        private bool CheckAeroEnabled()
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                int enabled = 0;
                DwmIsCompositionEnabled(ref enabled);
                return (enabled == 1) ? true : false;
            }
            return false;
        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_NCPAINT:
                    if (m_aeroEnabled)
                    {
                        var v = 2;
                        DwmSetWindowAttribute(this.Handle, 2, ref v, 4);
                        MARGINS margins = new MARGINS()
                        {
                            bottomHeight = 1,
                            leftWidth = 1,
                            rightWidth = 1,
                            topHeight = 1
                        };
                        DwmExtendFrameIntoClientArea(this.Handle, ref margins);

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

            if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT)     // drag the form
                m.Result = (IntPtr)HTCAPTION;

        }
        #endregion

原文地址:https://www.cnblogs.com/mamaxiaoling/p/8251816.html

时间: 2024-07-30 14:38:16

无边框窗体设置的相关文章

windows无边框窗体移动设置

int cx;         int cy; private void btnClose_Click(object sender, EventArgs e) {             this.Close();         } private void btnClose_MouseLeave(object sender, EventArgs e) {             btnClose.BackColor = Color.Transparent;         } private

无边框窗体和用户控件以及权限

无边框窗体: 就是吧窗体的边框去掉,然后自己做按钮设置功能. 无边框窗体的移动: 将下面代码直接复制粘贴,将窗体的鼠标按下事件的方法改成下面方法的名字就可以直接使用 1 //窗体移动API 2 [DllImport("user32.dll")] 3 public static extern bool ReleaseCapture(); 4 [DllImport("user32.dll")] 5 public static extern bool SendMessag

01.WPF中制作无边框窗体

[引用:]http://blog.csdn.net/johnsuna/article/details/1893319 众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormBorderStyle属性设置为None来完成.如果要制作成异形窗体,则需要使用图片或者使用GDI+自定义绘制. 那么,在WPF中,我们怎样制作一个无边框窗体呢? 答案是将Window的WindowStyle属性设置为None,即WindowStyle="None" .如果是非矩形的异形窗体,则

Winform自定义无边框窗体

你还在为Winform原生窗体的丑陋而烦恼么?下面来看一下如何制作一个既漂亮又简单的窗体 先看一下效果图: 首先我们新建一个窗体FormM继承原生Form 看一下主要的代码 public partial class FormM : Form { public FormM() { InitializeComponent(); } /// <summary> /// 是否允许最大化 /// </summary> private bool maxVisible = true; [Desc

无边框窗体、用户控件、Timer控件

一.无边框窗体1 最大化.最小化以及关闭按钮制作 实际上就是更换点击前.指向时.点击时的图片 (1)将图片放在该文件夹的Debug中, 获取图片的路径 Application.StartupPath + "\\图片名.类型"(2)若是放在该文件夹的中,Application.StartupPath + "\\..\\..\\images\\图片名.类型" \..\文件夹名称... 向上翻一个文件夹,上面的第一个\是转义 pictureBox2.BackgroundI

无边框窗体、后台创建控件、简单通讯

一.无边框窗体 1.控制按钮如何制作: 就是放置可以点击的控件,不局限于使用按钮或是什么别的,只要可以点击能触发点击事件就可以了 (1)美化一下的话那就可以把鼠标移入,移出,按下三个事件让按钮改变样式 (2)如何获取图片的相对路径 //鼠标移入时显示的图片 private void pictureBox1_MouseEnter(object sender, EventArgs e) { pictureBox1.BackgroundImage = Image.FromFile(Applicatio

无边框窗体

一.无边框窗体 1.创建无边框窗体 将窗体FormBorderStyle属性设为None 2.设置一个关闭按钮功能 (1)添加一个pictureBox,将背景改为关闭图像 (2)设置鼠标移入,移出,点击等事件 /// <summary> /// 鼠标移入 /// </summary> /// <param name="sender"></param> /// <param name="e"></par

WPF 无边框窗体

第一步:去掉那些最大化最小化和关闭 代码如下: WindowStyle="None" 第二步:去掉那边框 代码如下: AllowsTransparency="True" 第三步:拖动窗体 方法:给窗体设置MouseLeftButtonDown事件 代码如下: (1)前台代码: MouseLeftButtonDown="Border_MouseLeftButtonDown_1" (2)后台代码: private void Border_MouseL

C# WinForm 拖动无边框窗体 改变无边框窗体尺寸

经常遇到这种情况.窗体的边框去掉了.然后种种问题就出来了:不能拖动.不能改变窗体大小.不能......当然.肯定有解决方案滴*^_^*今天的目标就是:可以直接拖动没有边框的窗体.可以直接拉拽窗体改变其大小.制作步骤如下:新建WinForm程序.添加一个启动的窗体.将其边框设置为None.进入代码编辑界面.定义如下常量值: const int Guying_HTLEFT = 10; const int Guying_HTRIGHT = 11; const int Guying_HTTOP = 12