1.窗体与界面设计-设置窗体位置

在很多软件中,都会对窗体的大小、位置和移动进行限定。在不同分辨率的显示器中如何正确显示窗体,如何设置窗体始终在最上面...

038 设置窗体在屏幕中的位置

设置窗体在屏幕中的位置,可以通过设置窗体的属性来实现。窗体的 Left 属性表示窗体距屏幕左侧的距离,Top 属性表示窗体距屏幕上方的距离。

1.窗体与界面设计-设置窗体位置

创建一个项目,默认窗体为 Form1,为 Form1 添加 Label 控件,添加 TextBox 控件用来输入距屏幕的距离,添加 Button 控件用来设置窗体在屏幕上的位置。

namespace _038_SetLocation

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void button1_Click(object sender, EventArgs e)

        {

            this.Left = Convert.ToInt32(textBox1.Text); //设置窗体左边缘与屏幕左边缘之间的距离

            this.Top = Convert.ToInt32(textBox2.Text);  //设置窗体上边缘与屏幕上边缘之间的距离

        }

    }

}

039 始终在最上面的窗体

实现窗体始终在最上面只需要将窗体的 TopMost 属性设为 True 即可。

1.窗体与界面设计-设置窗体位置

创建一个项目,默认窗体为 Form1,为 Form1 窗体添加背景图片,并设置窗体 TopMost 属性为 True。


040 从桌面右下角显示的窗体

主要用到动画显示窗体的 API 函数 AnimateWindow,在该函数的重载方法中,依据参数值的不同窗体会以不同的形式显示和隐藏。

1.窗体与界面设计-设置窗体位置1.窗体与界面设计-设置窗体位置

1.创建一个项目,默认窗体为 Form1,添加一个 Windows 窗体,将其命名为 MainForm。

2.在 Form1 窗体上添加一个 GroupBox 控件和两个 Button 控件。在 MainForm 窗体中添加一个 PictureBox 控件和一个 ImageList 控件。

//Form1 窗体代码

namespace _040_SoutheastDisplayWindow

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void display_Click(object sender, EventArgs e)

        {

            MainForm.Instance().ShowForm();//显示窗体

        }



        private void close_Click(object sender, EventArgs e)

        {

            MainForm.Instance().CloseForm();//隐藏窗体

        }

    }

}

//MainForm 窗体代码

namespace _040_SoutheastDisplayWindow

{

    public partial class MainForm : System.Windows.Forms.Form

    {

        #region 声明的变量

        private System.Drawing.Rectangle Rect;//定义一个存储矩形框的数组

        private FormState InfoStyle = FormState.Hide;//定义变量为隐藏

        static private MainForm dropDownForm = new MainForm();//实例化当前窗体

        private static int AW_HIDE = 0x00010000; //该变量表示动画隐藏窗体

        private static int AW_SLIDE = 0x00040000;//该变量表示出现滑行效果的窗体

        private static int AW_VER_NEGATIVE = 0x00000008;//该变量表示从下向上开屏

        private static int AW_VER_POSITIVE = 0x00000004;//该变量表示从上向下开屏

        #endregion



        #region 该窗体的构造方法

        public MainForm()

        {

            InitializeComponent();

            //初始化工作区大小

            System.Drawing.Rectangle rect = System.Windows.Forms.Screen.GetWorkingArea(this);//实例化一个当前窗口的对象

            this.Rect = new System.Drawing.Rectangle(rect.Right - this.Width - 1, rect.Bottom - this.Height - 1, this.Width, this.Height);//为实例化的对象创建工作区域

        }

        #endregion



        #region 调用API函数显示窗体

        [DllImportAttribute("user32.dll")]

        private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);

        #endregion



        #region 鼠标控制图片的变化

        private void pictureBox1_MouseEnter(object sender, EventArgs e)

        {

            pictureBox1.Image = imageList1.Images[1];//设定当鼠标进入PictureBox控件时PictureBox控件的图片

        }



        private void pictureBox1_MouseLeave(object sender, EventArgs e)

        {

            pictureBox1.Image = imageList1.Images[0]; //设定当鼠标离开PictureBox控件时PictureBox控件的图片

        }

        #endregion



        #region 定义标识窗体移动状态的枚举值

        protected enum FormState

        {

            //隐藏窗体

            Hide = 0,

            //显示窗体

            Display = 1,

            //显示窗体中

            Displaying = 2,

            //隐藏窗体中

            Hiding = 3

        }

        #endregion



        #region 用属性标识当前状态

        protected FormState FormNowState

        {

            get { return this.InfoStyle; }   //返回窗体的当前状态

            set { this.InfoStyle = value; }  //设定窗体当前状态的值

        }

        #endregion



        #region 显示窗体

        public void ShowForm()

        {

            switch (this.FormNowState)      //判断窗体当前处于何种状态

            {

                case FormState.Hide:        //当窗体处于隐藏状态时

                    if (this.Height <= this.Rect.Height - 192)//当窗体没有完全显示时

                        this.SetBounds(Rect.X, this.Top - 192, Rect.Width, this.Height + 192);//使窗体不断上移

                    else

                    {

                        this.SetBounds(Rect.X, Rect.Y, Rect.Width, Rect.Height);//设置当前窗体的边界

                    }

                    AnimateWindow(this.Handle, 800, AW_SLIDE + AW_VER_NEGATIVE);//动态显示本窗体

                    break;

            }

        }

        #endregion



        #region 关闭窗体

        public void CloseForm()

        {

            AnimateWindow(this.Handle, 800, AW_SLIDE + AW_VER_POSITIVE + AW_HIDE); //动画隐藏窗体

            this.FormNowState = FormState.Hide;//设定当前窗体的状态为隐藏

        }

        #endregion



        #region 返回当前窗体的实例化对象

        static public MainForm Instance()

        {

            return dropDownForm; //返回当前窗体的实例化对象

        }

        #endregion

    }

}

你可能感兴趣的:(界面设计)