C#WinForm仿qq窗体拖到windows窗体边上时,自动隐藏C#WinForm

C#WinForm仿qq窗体拖到windows窗体边上时,自动隐藏C#WinForm。

代码:

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

        private void Form1_Load(object sender, EventArgs e)
        {
           
            System.Windows.Forms.Timer StopRectTimer = new System.Windows.Forms.Timer();
            StopRectTimer.Tick += new EventHandler(timer1_Tick);
            StopRectTimer.Interval = 50;
            StopRectTimer.Enabled = true;
            this.TopMost = true;
            StopRectTimer.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Bounds.Contains(Cursor.Position))
            {
                switch (this.StopAanhor)
                {
                    case AnchorStyles.Top:
                        this.Location = new Point(this.Location.X, 0);
                        break;
                    case AnchorStyles.Left:
                        this.Location = new Point(0, this.Location.Y);
                        break;
                    case AnchorStyles.Right:
                        this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
                        break;
                    case AnchorStyles.Bottom:
                        this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
                        break;
                }
            }
            else
            {
                switch (this.StopAanhor)
                {
                    case AnchorStyles.Top:
                        this.Location = new Point(this.Location.X, (this.Height - 8) * (-1));
                        break;
                    case AnchorStyles.Left:
                        this.Location = new Point((-1) * (this.Width - 8), this.Location.Y);
                        break;
                    case AnchorStyles.Right:
                        this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 8, this.Location.Y);
                        break;
                    case AnchorStyles.Bottom:
                        this.Location = new Point(this.Location.X, (Screen.PrimaryScreen.Bounds.Height - 8));
                        break;
                }
            }

        }

        internal AnchorStyles StopAanhor = AnchorStyles.None;
        private void mStopAnhor()
        {
            if (this.Top <= 0 && this.Left <= 0)
            {
                StopAanhor = AnchorStyles.None;
            }
            else if (this.Top <= 0)
            {
                StopAanhor = AnchorStyles.Top;
            }
            else if (this.Left <= 0)
            {
                StopAanhor = AnchorStyles.Left;
            }
            else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
            {
                StopAanhor = AnchorStyles.Right;
            }
            else if (this.Top >= Screen.PrimaryScreen.Bounds.Height - this.Height)
            {
                StopAanhor = AnchorStyles.Bottom;
            }
            else
            {
                StopAanhor = AnchorStyles.None;
            }
        }

        private void hide_LocationChanged(object sender, EventArgs e)
        {
            this.mStopAnhor();
        }

}

在WinForm中添加Load事件给Form1_Load,添加LocationChanged事件给hide_LocationChanged。

原理:主要是用Timer控件,在实时判断当前窗口的位置,如果在windows窗口的边上时,修改窗口的位置。当窗口隐藏后,实时判断鼠标的位置,如果在窗口附件,窗口显示

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
回答: 在C# WinForm窗体中,如果你想要在表格控件上填充颜色,但是鼠标经过其他控件整个表格里的数据都变了,可能是因为你在窗体上直接绘制了表格,而没有使用自定义控件来解决这个问题。\[1\] 你可以尝试使用自适应类来解决这个问题。首先,将自适应类的代码复制到你的工程命名空间中,然后声明自适应类的实例。接下来,在窗体上添加SizeChanged事件,并在事件方法中记录窗体和控件的初始位置和大小。最后,调用自适应类的自适应方法,完成自适应。这样,你就可以在表格控件上填充颜色而不会受到其他控件的影响了。\[2\] 你还可以使用Panel、DataGridView等控件来实现自适应。\[2\] 在窗体的代码中,你需要引入System、System.Collections.Generic、System.ComponentModel、System.Drawing和System.Windows.Forms这些命名空间,并在窗体的Load事件中调用自适应类的初始化方法,记录窗体和控件的初始位置和大小。然后,在窗体的SizeChanged事件中调用自适应类的自适应方法,完成自适应。\[3\] 这样,你就可以在C# WinForm窗体中使用表格控件,并且实现自适应的效果了。 #### 引用[.reference_title] - *1* [C#自定义控件实现窗体绘制表格](https://blog.csdn.net/qq_22955427/article/details/76252582)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [C# WinForm 窗体及其控件的自适应](https://blog.csdn.net/chbxgbin/article/details/109690260)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值