zoukankan      html  css  js  c++  java
  • Windows Forms编程实战学习:第一章 初识Windows Forms

    1. 初识Windows Forms

    1,用C#编程

    using System.Windows.Forms;

     

    [assembly: System.Reflection.AssemblyVersion("1.0")]

     

    namespace MyNamespace

    {

    public class MyForm : Form

    {

    public MyForm()

    {

    this.Text = "Hello Form";

    }

    [System.STAThread]

    public static void Main()

    {

    Application.EnableVisualStyles();

    Application.Run(new MyForm());

    }

    }

    }

    Application类

    Application类用于管理应用程序、线程和Windows消息所需要的静态方法,这个类被声明为密封的(sealed),也就是说无法被继承。

    公共静态属性

    AllowQuit

    是否退出了应用程序

    CommonAppDataRegister

    获取所有用户共享的应用程序数据的注册表键(HKLM)

    CurrentCulture

    获取或设置当前线程的本地化设置

    OpenForms

    获取这个应用程序活动Form对象的合集

    ProductName

    获取应用程序产品名

    ProductVersion

    获取应用程序的版本

    StartupPath

    启动应用程序的可执行文件路径

    UserAppDataRegister

    获取当前用户指定的应用程序数据的注册表键(HK_USER)

    公共静态方法

    AddMessageFilter

    安装一个IMessageFilter接口,对当前线程的窗口消息进行监视。它可以用于拦截到达这个窗口的消息

    DoEvents

    处理当前位于消息队列中的任何Windows消息

    EnableVisualStyles

    允许应用程序的可视化风格

    Exit

    停止应用程序中所有的消息循环,并关闭这个应用程序的所有窗口。注意,它可能无法强制应用程序退出。

    ExitThread

    只停止当前线程中的消息循环,并关闭当前线程的所有窗口

    Run

    在当前线程中启动一个标准的消息循环。如提供一个Form,就使用它。

    公共静态事件

    ApplicationExit

    当应用程序将要关闭时发生

    Idle

    当应用程序将要进入空闲状态时发生

    ThreadException

    当一个线程中出现一个未捕获异常时发生

    ThreadExit

    当应用程序中的一个线程将要关闭时发生

     

    2,添加控件

    using System;

    using System.Drawing;

    using System.Windows.Forms;

     

    [assembly: System.Reflection.AssemblyVersion("1.3")]

     

    namespace MyNamespace

    {

    public class MyForm : Form

    {

    private Button btnLoad;

    private PictureBox pbxPhoto;

     

    public MyForm()

    {

    this.Text = "Hello Form 1.2";

     

    //create and configure a button

    btnLoad = new Button();

    btnLoad.Text = "&load";

    btnLoad.Left = 10;

    btnLoad.Top = 10;

     

    //create and configure a picture box

    pbxPhoto = new PictureBox();

    pbxPhoto.BorderStyle = BorderStyle.Fixed3D;

    pbxPhoto.Width = this.Width / 2;

    pbxPhoto.Height = this.Height / 2;

    pbxPhoto.Left = (this.Width - pbxPhoto.Width) / 2;

    pbxPhoto.Top = (this.Height - pbxPhoto.Height) / 2;

     

    //add our new controls to the form

    this.Controls.Add(btnLoad);

    this.Controls.Add(pbxPhoto);

    }

     

     

    [System.STAThread]

    public static void Main()

    {

    Application.EnableVisualStyles();

    Application.Run(new MyForm());

    }

    }

    }

     

    当一个控件被添加到窗体后,它位于这个窗体控件堆栈Z序的最后。

     

    3,加载文件

    using System;

    using System.Drawing;

    using System.Windows.Forms;

     

    [assembly: System.Reflection.AssemblyVersion("1.3")]

     

    namespace MyNamespace

    {

    public class MyForm : Form

    {

    private Button btnLoad;

    private PictureBox pbxPhoto;

     

    public MyForm()

    {

    this.Text = "Hello Form 1.2";

     

    //create and configure a button

    btnLoad = new Button();

    btnLoad.Text = "&load";

    btnLoad.Left = 10;

    btnLoad.Top = 10;

    btnLoad.Click += new EventHandler(this.HandleLoadClick);

    //create and configure a picture box

    pbxPhoto = new PictureBox();

    pbxPhoto.BorderStyle = BorderStyle.Fixed3D;

    pbxPhoto.Width = this.Width / 2;

    pbxPhoto.Height = this.Height / 2;

    pbxPhoto.Left = (this.Width - pbxPhoto.Width) / 2;

    pbxPhoto.Top = (this.Height - pbxPhoto.Height) / 2;

    pbxPhoto.SizeMode = PictureBoxSizeMode.Zoom;

     

    //add our new controls to the form

    this.Controls.Add(btnLoad);

    this.Controls.Add(pbxPhoto);

    }

     

    private void HandleLoadClick(object sender, EventArgs e)

    {

    OpenFileDialog dlg = new OpenFileDialog();

    dlg.Title = "Open Photo";

    dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";

     

    if (dlg.ShowDialog() == DialogResult.OK)

    {

    pbxPhoto.Image = new Bitmap(dlg.OpenFile());

    }

     

    dlg.Dispose();

    }

     

    [System.STAThread]

    public static void Main()

    {

    Application.EnableVisualStyles();

    Application.Run(new MyForm());

    }

    }

    }

    OpenFileDialog类允许用户选择一个需要打开的文件。

    Filter字符串每部分使用|分隔,每一对值分别表示在对话框中所显示的字符串以及显示文件时所用的正则表达式。

     

    公共属性

    AddExtension

    如果用户省略扩展名,这个属性获取或设置文件对话框是否会自动添加文件扩展名。

    CheckFileExists

    如果指定的文件不存在,这个属性获取或者设置文件对话框是否显示警告信息。

    DefaultExt

    获取或设置默认的文件扩展名

    FileName

    获取或设置包含了被选择文件名的字符串

    FileNames

    获取一个字符串数组,包含了被选择的文件名集合

    Filter

    获取或设置文件名过滤字符串

    InitialDirectory

    获取或显示文件对话框所显示的初始目录

    RestoreDirectory

    获取或设置文件对话框在关闭前是否把目录恢复到原先的值

    ShowHelp

    获取或设置文件对话框是否出现帮助按钮

    Title

    获取或者设置文件对话框标题栏字符串

    公共方法

    Dispose

    释放对话框所使用的资源

    Reset

    将对话框属性重设为默认值

    ShowDialog

    显示一个常用对话框,并返回用户所选所选择按钮的DialogResult枚举值

    公共事件

    HelpRequest

    点击"帮助"按钮时发生

     

    4,更改窗体大小

    using System;

    using System.Drawing;

    using System.Windows.Forms;

     

    [assembly: System.Reflection.AssemblyVersion("1.4")]

     

    namespace MyNamespace

    {

    public class MyForm : Form

    {

    private Button btnLoad;

    private PictureBox pbxPhoto;

     

    public MyForm()

    {

    this.Text = "Hello Form 1.4";

     

    //create and configure a button

    btnLoad = new Button();

    btnLoad.Text = "&load";

    btnLoad.Left = 10;

    btnLoad.Top = 10;

    btnLoad.Click += new EventHandler(this.HandleLoadClick);

    btnLoad.Anchor = AnchorStyles.Top | AnchorStyles.Left;

     

    //create and configure a picture box

    pbxPhoto = new PictureBox();

    pbxPhoto.BorderStyle = BorderStyle.Fixed3D;

    pbxPhoto.Width = this.Width / 2;

    pbxPhoto.Height = this.Height / 2;

    pbxPhoto.Left = (this.Width - pbxPhoto.Width) / 2;

    pbxPhoto.Top = (this.Height - pbxPhoto.Height) / 2;

    pbxPhoto.SizeMode = PictureBoxSizeMode.Zoom;

    pbxPhoto.Anchor = AnchorStyles.Top | AnchorStyles.Bottom

    | AnchorStyles.Left | AnchorStyles.Right;

     

    //add our new controls to the form

    this.Controls.Add(btnLoad);

    this.Controls.Add(pbxPhoto);

    }

     

    private void HandleLoadClick(object sender, EventArgs e)

    {

    OpenFileDialog dlg = new OpenFileDialog();

    dlg.Title = "Open Photo";

    dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";

     

    if (dlg.ShowDialog() == DialogResult.OK)

    {

    pbxPhoto.Image = new Bitmap(dlg.OpenFile());

    }

     

    dlg.Dispose();

    }

     

    [System.STAThread]

    public static void Main()

    {

    Application.EnableVisualStyles();

    Application.Run(new MyForm());

    }

    }

    }

    所有控件都支持Anchor属性,属性值是AnchorStyles枚举的值

    枚举值

    Bottom

    被锚定到底部边缘

    Left

    被锚定到左侧边缘

    None

    没被锚定,当容器大小改变时,控件在各个方向移动的距离是容器各个方向所改变幅度的一般。

    Right

    被锚定到右侧边缘

    Top

    被锚定到顶部边缘

    锚定:保持距离不变

     

    Dock:固定到边缘

    Control类中均可使用。如果控件设定了Dock属性None以外的属性值,Anchor值被锚定到顶部和左侧。

    枚举值

    Bottom

    停靠到底部边缘

    Fill

    停靠到所有边缘

    Left

    停靠到左侧边缘

    None

    没有停靠,由Anchor属性决定位置

    Right

    停靠到右侧边缘

    Top

    停靠到顶部边缘

  • 相关阅读:
    1137. 第 N 个泰波那契数
    486. 预测赢家
    python函数—函数的参数+递归函数
    python函数—调用函数+定义函数
    Seize the day
    数学建模基础学习2-matlab + lingo
    Python基础--使用list和tuple+条件判断+使用dict和set
    经济学人精读丨中国的电子商务
    数学建模基础学习1
    C盘今天爆掉了,罪魁祸首--百度云管家
  • 原文地址:https://www.cnblogs.com/xyb930826/p/5296577.html
Copyright © 2011-2022 走看看