当窗体处于屏幕边缘时隐藏窗口的方法

当把软件窗口拖动到屏幕边缘时,让软件窗体隐藏,鼠标再次悬浮时再把它显示出来,

根据下面的代码给窗体添加一系列事件

具体代码如下:

using System.Runtime.InteropServices;

namespace 仿QQ侧边隐藏
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        //窗体的宽高
        int formwidth;
        int formheight;
        
        private void Form1_Move(object sender, EventArgs e)
        {
            formwidth = this.Width;
            formheight = this.Height;
            int x = this.Location.X;
            int y = this.Location.Y;
            if (x <= 0)
            {
                this.Location = new System.Drawing.Point(0, y);
                this.Width = 0;
            }
            if (y <= 0)
            {
                this.Location = new System.Drawing.Point(x, 0);
                this.Height = 0;
            }
        }

        private void Form1_MouseEnter(object sender, EventArgs e)
        {
            this.Width = formwidth;
            this.Height = formheight;
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        private void Form1_MouseLeave(object sender, EventArgs e)
        {
            int nowx = Cursor.Position.X;
            int nowy = Cursor.Position.Y;
            if (nowx >= (this.Location.X + this.Width) | nowx <= this.Location.X | nowy <= this.Location.Y | nowy >= (this.Location.Y + this.Height))
            {
                if (this.Location.X == 0)
                {
                    this.Width = 0;
                }
                if (this.Location.Y == 0)
                {
                    this.Height = 0;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

运行起来会发现效果是这样的:

把标题栏去掉

再次运行,调试程序,这个图不太好截,有需要的自己试一下吧。

展开阅读全文

页面更新:2024-04-22

标签:窗体   边缘   屏幕   窗口   侧边   拖动   一下吧   效果   代码   方法   软件

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top