WinForm窗体屏幕边缘停靠

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace WZDH

{

    public partial class Form1 : Form

    {

        internal AnchorStyles StopAanhor = AnchorStyles.None;

        public Form1()

        {

            InitializeComponent();

        }

        /// <summary>

        /// 初始化窗体信息

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void Form1_Load(object sender, EventArgs e)

        {

            //设置窗体左边距离主显示器位置

            this.Width = 200;

            this.Left = Screen.PrimaryScreen.Bounds.Width - this.Width;

            //设置窗体顶边距离主显示器位置

            this.Top = 0;          

            //设置窗体宽度为主显示器宽度(不包括任务栏)

            this.Height = Screen.PrimaryScreen.WorkingArea.Height;

        }

        /// <summary>

        /// 淡入加载窗体

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void timer1_Tick(object sender, EventArgs e)

        {

            this.Opacity += 0.1;

            if (this.Opacity >= 1)

                this.timer1.Enabled = false;

        }

        /// <summary>

        /// 退出程序菜单

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void tsmiExit_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }

        /// <summary>

        /// 当窗体的位置改变时发生

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void Form1_LocationChanged(object sender, EventArgs e)

        {

            mStopAnhor();

        }

        private void timer2_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.Bottom:

                        this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);

                        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;

                }

            }

            else  //如果鼠标离开窗体,则根据停靠位置隐藏窗体,但须留出部分窗体边缘以便鼠标选中窗体

            {

                switch (this.StopAanhor)

                {

                    case AnchorStyles.Top:

                        this.Location = new Point(this.Location.X, (this.Height - 3) * (-1));

                        break;

                    case AnchorStyles.Bottom:

                        this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - 5);

                        break;

                    case AnchorStyles.Left:

                        this.Location = new Point((-1) * (this.Width - 3), this.Location.Y);

                        break;

                    case AnchorStyles.Right:

                        this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 2, this.Location.Y);

                        break;

                }

            }

        }

        

        private void mStopAnhor()

        {

            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

            {

                StopAanhor = AnchorStyles.None;

            }

        

    }

}

---转载:http://www.cnblogs.com/qinyun118/archive/2010/08/30/1812863.html

时间: 2024-10-13 06:29:01

WinForm窗体屏幕边缘停靠的相关文章

DockPanel与Winform窗体浮动和停靠

https://blog.csdn.net/jun502525164/article/details/9079481 原文地址:https://www.cnblogs.com/lihuali/p/9541626.html

C# WinForm窗体及其控件自适应各种屏幕分辨率

C# WinForm窗体及其控件自适应各种屏幕分辨率 一.说明  我们自己编写程序的界面,会遇到各种屏幕分辨率,只有自适应才能显的美观.实际上,做到这点也很简单,就是首先记录窗体和它上面控件的初始位置和大小,当窗体改变比例时,其控件的位置和大小也按此比例变化即可.因为窗体上控件的位置和大小是相对于自己所在的窗体的,也就是所谓的窗口坐标.  在这里我们只考虑相对于自己窗体的窗口坐标更简单,也就是成比例变化.为了多个窗体共用,我在这里创建一个类AutoSizeFormClass ,1.使用它去记录窗

WinForm实现类似QQ停靠,显示隐藏过程添加特效效果

原文:WinForm实现类似QQ停靠,显示隐藏过程添加特效效果 这可能是个老题长谈的问题了,只是在项目中会用到这个效果,所以今天做个记录.大家见了别喷我.在项目中的需求是这样的. 打开程序,在屏幕的右下角会显示一个窗体,一般情况下该窗体会隐藏停靠在右边,只露出很小部分,当鼠标移动到这个很小部分时,窗体全部显示,显示过程是从右边滑动到左边,当鼠标离开窗体时,窗体需要隐藏在右边,只露出很小部分,隐藏过程是从左边滑动到右边. 实现此类效果我碰到的连个难点是:1.如何判断鼠标离开了窗体?2.窗体显示隐藏

模拟窗体附顶停靠隐藏

using System.Runtime.InteropServices; /* Winform的 FormBorderStyle=None; */ namespace Windows_API_实现屏幕右下角_消息框_ { public partial class QQHide : Form { #region WinAPI http://msdn.microsoft.com/en-us/library/ms632669%28VS.85%29.aspx /// <summary> /// En

winForm窗体最大化的设置

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

WinForm窗体及其控件的自适应

3步骤: 1.在需要自适应的Form中实例化全局变量   AutoSizeFormClass.cs源码在下方 AutoSizeFormClass asc = new AutoSizeFormClass(); 2.Form_Load事件中   asc.controllInitializeSize(this); 3.Page_SizeChanged事件中 asc.controlAutoSize(this); AutoSizeFormClass.cs源码如下: class AutoSizeFormCl

获取Winform窗体、工作区 宽度、高度、命名空间、菜单栏高度等收集

MessageBox.Show("当前窗体标题栏高"+(this.Height - this.ClientRectangle.Height).ToString());//当前窗体标题栏高度 ClientRectangle//获取表示控件的工作区的矩形 Clientsize//获取表示窗体工作区的大小 MessageBox.Show(SystemInformation.PrimaryMonitorSize.ToString()); //获取主显示器屏幕的尺寸(像素) MessageBox

winform窗体中查找控件

private RichTextBox FindControl()        { RichTextBox ret = null;            try            {                Control[] controls = Application.OpenForms["MainForm"].Controls.Find("txtContent", false);                if (controls != nul

Android实现ListView或GridView首行/尾行距离屏幕边缘距离

Android上ListView&GridView默认行都是置顶的,这样会很丑. 一般为了解决这个问题都会在首行或尾行加上一个隐藏的View,那样实在是太麻烦了.在网上看博客的时候突然看到这个属性真的很有用! 直接上关键属性: 设置ListView或GridView的android:clipToPadding = true, 然后通过paddingTop和paddingBottom设置距离就好了. 博客原文: http://www.cnblogs.com/xitang/p/3606578.htm