zoukankan      html  css  js  c++  java
  • 学习.NET Winform开发

    学习.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

  • 相关阅读:
    [HDU 3038] How Many Answers Are Wrong
    [BZOJ 4977][Lydsy1708月赛]跳伞求生
    [BZOJ4974] 字符串大师
    总结-exCRT
    [luogu 4777] exCRT
    [AHOI 2009] 中国象棋
    JavaScript MVC框架PK:Angular、Backbone、CanJS与Ember
    十一黄金周 加班加点随笔
    从两个设计模式到前端MVC-洪宇
    Todo&Rocket
  • 原文地址:https://www.cnblogs.com/gibbonnet/p/5417191.html
Copyright © 2011-2022 走看看