學習.NET Winform開發 - 演示System.Windows.Forms.Form類的使用


學習.NET Winform開發 - 展示System.Windows.Forms.Form類的使用

撰寫日期:2016/04/20
更新日期:2016/04/22
發布地址:http://www.cnblogs.com/gibbonnet/p/5417191.html

任務場景

基本操作

  • 標題 Form.Text
  • 大小 Form.Size
  • 位置 Form.StartPosition
  • 圖標 Form.Icon
  • 總是最前 Form.TopMost

進階操作

  • 最小化至任務欄 System.Windows.Forms.NotifyIcon
  • 文件拖拽動作支持 Form.AllowDrop, DragOver, DragDrop

DemoForm.cs

效果:
DemoForm

編譯: csc DemoForm.cs /target:winexe /win32icon:images/title.ico

基本操作源碼

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

/**
 * DemoForm.cs
    - 標題
    - 大小
    - 位置
    - 圖標
 * csc DemoForm.cs /target:winexe /win32icon:images/title.ico 
 */
namespace gibbon.learning.winform
{
    public class DemoForm : Form
    {
        public DemoForm()
        {
            // set caption
            this.Text = "窗口的標題";
            // CenterParent, CenterScreen, Manual, WindowsDefaultBounds, WindowsDefaultLocation
            this.StartPosition = FormStartPosition.CenterScreen;
            // weight, height
            this.Size = new Size(500, 600);
            // icon
            this.Icon = new System.Drawing.Icon("images/Title.ico");
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new DemoForm());
        }
    }
}

最小化至托盤

// file: DemoMinToTray.cs
using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
{
    private System.Windows.Forms.NotifyIcon notifyIcon1;
    private System.Windows.Forms.ContextMenu contextMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.ComponentModel.IContainer components;

    [STAThread]
    static void Main()
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        this.components = new System.ComponentModel.Container();
        this.contextMenu1 = new System.Windows.Forms.ContextMenu();
        this.menuItem1 = new System.Windows.Forms.MenuItem();

        // Initialize contextMenu1
        this.contextMenu1.MenuItems.AddRange(
                    new System.Windows.Forms.MenuItem[] {this.menuItem1});

        // Initialize menuItem1
        this.menuItem1.Index = 0;
        this.menuItem1.Text = "E&xit";
        this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);

        // Set up how the form should be displayed.
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Text = "Notify Icon Example";

        // Create the NotifyIcon.
        this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

        // The Icon property sets the icon that will appear
        // in the systray for this application.
        notifyIcon1.Icon = new Icon("images/title.ico");

        // The ContextMenu property sets the menu that will
        // appear when the systray icon is right clicked.
        notifyIcon1.ContextMenu = this.contextMenu1;

        // The Text property sets the text that will be displayed,
        // in a tooltip, when the mouse hovers over the systray icon.
        notifyIcon1.Text = "Form1 (NotifyIcon example)";

        // Handle the DoubleClick event to activate the form.
        notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);

        this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
    }

    protected override void Dispose( bool disposing )
    {
        // Clean up any components being used.
        if( disposing )
            if (components != null)
                components.Dispose();

        base.Dispose( disposing );
    }

    #region 隱藏任務欄圖標、顯示托盤圖標
    private void Form1_SizeChanged(object sender, EventArgs e)
    {
        //判斷是否選擇的是最小化按鈕
        if (WindowState == FormWindowState.Minimized)
        {
            //托盤顯示圖標等於托盤圖標對象
            //注意notifyIcon1是控件的名字而不是對象的名字
            // notifyIcon1.Icon = ico;
            //隱藏任務欄區圖標
            this.ShowInTaskbar = false;
            //圖標顯示在托盤區
            notifyIcon1.Visible = true;
        }
    }
    #endregion

    private void notifyIcon1_DoubleClick(object Sender, EventArgs e)
    {
        // Show the form when the user double clicks on the notify icon.

        // Set the WindowState to normal if the form is minimized.
        if (this.WindowState == FormWindowState.Minimized)
            this.WindowState = FormWindowState.Normal;

        // Activate the form.
        this.Activate();

        //任務欄區顯示圖標
        this.ShowInTaskbar = true;
        //托盤區圖標隱藏
        notifyIcon1.Visible = false;
    }

    private void menuItem1_Click(object Sender, EventArgs e) {
        // Close the form, which closes the application.
        this.Close();
    }
}

拖拽操作

public enum DragDropEffects 指定拖拽操作的可能類型

  • All Copy, Move 和Scroll效果的組合
  • Copy drag的數據拷貝至drop的目標
  • Link drag的數據鏈接至drop的目標
  • Move drag的數據移動至drop的目標
  • None drop的目標不接受數據
  • Scroll The target can be scrolled while dragging to locate a drop position that is not currently visible in the target.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

/**
 * DemoForm.cs
    - 標題
    - 大小
    - 位置
    - 圖標
 * 演示拖拽效果
 * csc DemoForm.cs /target:winexe /win32icon:images/title.ico
 */
namespace gibbon.learning.winform
{
    public class DemoForm : Form
    {
        public DemoForm()
        {
            // set caption
            this.Text = "窗口的標題";
            // CenterParent, CenterScreen, Manual, WindowsDefaultBounds, WindowsDefaultLocation
            this.StartPosition = FormStartPosition.CenterScreen;
            // weight, height
            this.Size = new Size(500, 600);
            // icon
            this.Icon = new System.Drawing.Icon("images/Title.ico");
            // drap and drop
            this.AllowDrop = true;
            this.DragEnter += this.DemoForm_DragEnter;
            this.DragDrop += this.DemoForm_DragDrop;
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new DemoForm());
        }

        private void DemoForm_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            // Ensure that the list item index is contained in the data.
            string[] s=(string[])e.Data.GetData(DataFormats.FileDrop,false);

            for (int i = 0; i < s.Length; i++)
            {
                Console.WriteLine(s[i]);
            }
        }

        private void DemoForm_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.All;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
    }
}

外接

編譯工具CSC

參考:http://www.cnblogs.com/gibbonnet/p/5420548.html

常用命令:http://www.cnblogs.com/changweihua/archive/2011/04/02/2003844.html

輸出圖標:/win32icon,例如:csc /win32icon:myicon.ico my.cs

IDE設置圖標:http://jingyan.baidu.com/article/7f41ececf50f4c593d095c11.html

為什么雙擊生成的exe文件時會執行命令行?
csc的編譯目標:/target: exe(default)/winexe/library/module/appcontainerexe/winmdobj


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM