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

摘要:
当一个模仿C#WinForm的qq表单被拖到窗口表单的边缘时,C#WinForm会自动隐藏。代码:publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidForm1_加载(objectsender,EventArgse){System.Windows.Forms.TimerStopRectTimer=n

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窗口的边上时,修改窗口的位置。当窗口隐藏后,实时判断鼠标的位置,如果在窗口附件,窗口显示。

免责声明:文章转载自《C#WinForm仿qq窗体拖到windows窗体边上时,自动隐藏C#WinForm》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Log4j配置详解(转)编程行业里面的新行话下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

认识WPF

新开一节WPF桌面开发的讲解,这节先初步认识一下什么是WPF。 1.简介 WPF是 Windows Presentation Foundation 的英文缩写,意为“窗体呈现基础”,是微软基于.NET Framework 3.0 推出的新一代构建窗体程序的框架。不同于WinForm,WPF实现了界面和开发分离,它的界面是由Xaml语言构建的,这种形式对前端...

Winform开发常用控件之TreeView菜单导航和权限用法

TreeView一个很棒的控件,我们在做WEB开发时常常犯困的一个东东。当然这里介绍winform里面的用法唠。 先介绍几个属性吧,CheckBoxes设置为true的话树形节点前面会出现checkbox勾选框、ImageList绑定树形里面的图标(界面上放一个Imgelist控件,绑定上即可)、ShowLines(树形展开时显示线条)等等 哦,这个方法好...

c# Winform PropertyGrid 实现下拉框 多选

1 usingPropertyGridHelpers.Controls; 2 usingSystem; 3 usingSystem.Collections.Generic; 4 usingSystem.ComponentModel; 5 usingSystem.Drawing.Design; 6 usingSystem.Windows.Forms;...

Swing高级控件

  这篇文章给大家演示一下Swing中的高级控件的用法,为了方面大家入门,我们的示例将会尽可能的简化。Swing中的高级控件包括JTabbedPane(页签),JPanel(面板),JDesktopPane(桌面窗格),JInternalFrame(内部窗口),JProgressBar(进度条),JTree(树),JMenuBar(菜单栏),JPopupM...

MFC避免窗口闪烁的方法(OnEraseBkgnd) .

在图形图象处理编程过程中,双缓冲是一种基本的技术。我们知道,如果窗体在响应WM_PAINT消息的时候要进行复杂的图形处理,那么窗体在重绘时由于过频的刷新而引起闪烁现象。解决这一问题的有效方法就是双缓冲技术。因为窗体在刷新时,总要有一个擦除原来图象的过程OnEraseBkgnd,它利用背景色填充窗体绘图区,然后在调用新的绘图代码进行重绘,这样一擦一写造成了图...

Winform应用的多语言设置

VS 提供了一种简单的方式来为应用设置多种语言——resx文件。下面演示具体的操作步骤。 1.首先新建 Winform 项目,向其添加控件,并命名 2.将 Form1 的 Localizable 属性设为 true, 设置该属性后,.net 将根据不同的语言,为应用程序生成不同的资源文件(resx文件) 3.将 Form1 的 Language 属性修改为...