zoukankan      html  css  js  c++  java
  • c#轻松实现磁性窗口【原】


    /// <summary>
    /// 磁性窗体函数
    /// </summary>
    /// <param name="form">窗体控件(一般传this即可)</param>
    /// <param name="space">自定义的与屏幕边缘的距离</param>
    /// <param name="isWorkingArea">是否在屏幕工作区进行该操作(true表示不包括任务栏,false则包括整个屏幕的范围)</param>
    public void Form_Welt(Control form, int space, bool isWorkingArea)
    {
        
    //获取窗体的左上角的x,y坐标
        int x = form.Location.X;
        
    int y = form.Location.Y;

        
    int sW = 0;
        
    int sH = 0;

        
    if (isWorkingArea)
        {
            
    //获取屏幕的工作区(不包括任务栏)的宽度和高度
            sW = Screen.PrimaryScreen.WorkingArea.Width;
            sH 
    = Screen.PrimaryScreen.WorkingArea.Height;
        }
        
    else
        {
            
    //获取整个屏幕(包括任务栏)的宽度和高度
            sW = Screen.PrimaryScreen.Bounds.Width;
            sH 
    = Screen.PrimaryScreen.Bounds.Height;
        }

        
    //如果窗体的左边缘和屏幕左边缘的距离在用户定义的范围内,则执行左贴边
        if ((x <= space && x > 0|| (Math.Abs(x) <= space && x < 0))  //Math.Abs(x)是取绝对值
        {
            form.Location 
    = new Point(0, y);
        }

        
    //如果窗体的上边缘和屏幕上边缘的距离在用户定义的范围内,则执行上贴边
        if ((y <= space && y > 0|| (Math.Abs(y) <= space && y < 0))
        {
            form.Location 
    = new Point(x, 0);
        }


        
    //窗体右边缘跟屏幕右边缘的距离
        int rightW = sW - form.Right;
        
    //窗体下边缘跟屏幕下边缘的距离
        int bottomW = sH - form.Bottom;

        
    //判断右边的情况
        if ((rightW <= space && form.Right < sW) || (Math.Abs(rightW) <= space && rightW < 0))
        {
            form.Location 
    = new Point(sW - form.Width, y);
        }
        
    //判断下边的情况
        if ((bottomW <= 10 && form.Bottom < sH) || (Math.Abs(bottomW) <= space && bottomW < 0))
        {
            form.Location 
    = new Point(x, sH - form.Height);
        }
    }
    看到千千静听的窗口可以在接近屏幕边缘时贴在边缘上觉得不错,自己也有这个需要,所以写了这个方法,测试了感觉还蛮不错的,哈哈~
    使用的时候只要在想应用的窗体的Form_Move(object sender,EventAges e)事件里面调用即可
    ps:不过有时窗体可能会比较闪,这个可能是代码还有待改善,或者是在Form_Move事件里面来调用不大合适,反正功能是实现了,要是哪位有更好的方法,欢迎回复交流一下啊~
  • 相关阅读:
    读写分离,就该这么改进
    使用HttpHandler来监控HTML页面请求
    半DDD架构 1
    WebForm开发中的路由功能
    如何让代码可测试化(C#)
    ParamQuery(jQuery Grid Plugin PQGrid, jQuery插件)
    通用Login功能自动化测试
    Top 10 Security Issue Solution
    KO学习重点
    OWASP Top 10 – 2013, 最新十大安全隐患(ASP.NET解决方法)
  • 原文地址:https://www.cnblogs.com/linyc/p/1547320.html
Copyright © 2011-2022 走看看