首页 > 技术文章 > 1.窗体与界面设计-设置窗体大小

iwanc 2013-06-27 15:47 原文

用户打开软件后首先看到的就是窗体和窗体上的控件,如何设置窗体的大小及合理地设置窗体和控件的关系就变得十分重要。

041 获取桌面大小

C# 中提供了 Screen 对象,在该对象中封装了屏幕相关信息。可以通过读取 Screen 对象的相关属性来获取屏幕的信息,Screen.PrimaryScreen.WorkingArea 属性用于获取显示器的工作区。工作区是显示器的桌面区域,不包括任务栏、停靠窗口和停靠工具栏。Screen.PrimaryScreen.WorkingArea.Width 用于读取桌面宽度;Screen.PrimaryScreen.WorkingArea.Height 可以读取桌面的高度。

创建一个项目,默认窗体为 Form1,为 Form1 添加一个 Button 控件,用来获取桌面大小;添加两个 TextBox 控件,用来输出所获取的桌面大小。

namespace _041_DeskSize
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = Screen.PrimaryScreen.WorkingArea.Width.ToString(); //显示桌面的宽度
            textBox2.Text = Screen.PrimaryScreen.WorkingArea.Height.ToString(); //显示桌面的高度
        }
    }
}

042 在窗体间移动按钮

可视控件包含一个 Parent 属性,该属性表示控件的父对象。一般将此属性设置为一个窗体,通过该属性可以控制所属窗体。

创建一个项目,默认窗体为 Form1,为 Form1 添加一个 Button 控件,再添加一个窗体,默认窗体的 Name 属性为 Form2。

namespace _042_MoveButton
{
    public partial class Form1 : Form
    {
        Form2 f;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            f = new Form2();    //实例化一个Form2对象
            f.Show();           //显示Form2窗体
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Parent == this) //当控件button1的父容器为窗体Form1时
            {
                f.Controls.Add(this.button1);
                this.button1.Text = "返回原地";
            }
            else                        //当控件button1的父容器为窗体Form2时
            {
                this.Controls.Add(button1);
                this.button1.Text = "开始移动";
            }
        }
    }

043 实现 Office 助手

要实现 Office 助手效果,需要使用 Microsoft 公司提供的第三方控件。在工具箱中单击“选择项”,从弹出的对话框中选择 COM 组件选项卡中的 Microsoft Agent Control 组件并加入工具箱中,然后再添加到窗体中即可。

创建一个项目,默认窗体为 Form1,为 Form1 添加一个 ListBox 控件用来让用户选择人物的动作。

namespace _043_OfficeAssistant
{
public partial class Form1 : Form
{
    public partial class Form1 : Form
    {
        IAgentCtlCharacterEx ICCE;  //定义一个IAgentCtlCharacterEx类的对象
        IAgentCtlRequest ICR;       //定义一个IAgentCtlRequest类的对象
        string[] ws = new string[10] { "Acknowledge", "LookDown", "Sad", "Alert", "LookDownBlink", "Search", "Announce", "LookUp", "Think", "Blink"};   //定义一个字符串数组
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)    //循环遍历
            {
                listBox1.Items.Add(ws[i]);  //向控件listBox1中添加字符串数组中的内容
            }
            ICR = axAgent1.Characters.Load("merlin", "merlin.acs"); //加载指定文件
            ICCE = axAgent1.Characters.Character("merlin");         //设定模拟Office助手的表情
            ICCE.Show(0);                                           //显示模拟Office助手的表情
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ICCE.StopAll("");                       //停止所有模拟Office助手的表情
            ICCE.Play(ws[listBox1.SelectedIndex]);  //吓死你控件listBox1中选定的表情
        }
    }
}

 

推荐阅读