1.窗體與界面設計-設置窗體位置

在不少軟件中,都會對窗體的大小、位置和移動進行限定。在不一樣分辨率的顯示器中如何正確顯示窗體,如何設置窗體始終在最上面...數組

038 設置窗體在屏幕中的位置ide

設置窗體在屏幕中的位置,能夠經過設置窗體的屬性來實現。窗體的 Left 屬性表示窗體距屏幕左側的距離,Top 屬性表示窗體距屏幕上方的距離。函數

建立一個項目,默認窗體爲 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 始終在最上面的窗體this

實現窗體始終在最上面只須要將窗體的 TopMost 屬性設爲 True 便可。spa

建立一個項目,默認窗體爲 Form1,爲 Form1 窗體添加背景圖片,並設置窗體 TopMost 屬性爲 True。3d


040 從桌面右下角顯示的窗體orm

主要用到動畫顯示窗體的 API 函數 AnimateWindow,在該函數的重載方法中,依據參數值的不一樣窗體會以不一樣的形式顯示和隱藏。對象

1.建立一個項目,默認窗體爲 Form1,添加一個 Windows 窗體,將其命名爲 MainForm。blog

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
    }
}
相關文章
相關標籤/搜索