C#结合VS开发WinForm学习笔记
自己最近在学习C#,了解到了一些空间以及GUI开发相关方面的东西,觉得挺不错的,于是就记到自己的博客上,顺便分享下。。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 7 using System.Windows.Forms; 8 9 namespace Example002 10 { 11 public partial class Form1 : Form 12 { 13 public Form1() 14 { 15 InitializeComponent(); 16 this.WindowState = FormWindowState.Minimized; 17 this.StartPosition = FormStartPosition.CenterScreen; 18 } 19 20 private void openChildFormToolStripMenuItem_Click( object sender, EventArgs e ) 21 { 22 Form2 f = new Form2(); 23 f.MdiParent = this; 24 f.Show(); 25 f.Resize += new EventHandler( f_Resize ); 26 f.FormClosed += new FormClosedEventHandler( f_Close ); 27 } 28 29 void f_Resize( object sender, EventArgs e ) 30 { 31 Form2 f = ( Form2 ) sender; 32 ToolStripMenuItem item = new ToolStripMenuItem(); 33 item.Text = "子窗口右键菜单"; 34 item.Name = "children"; 35 for ( int i = 0; i < f.contextMenuStrip2.Items.Count; i++ ) 36 { 37 item.DropDownItems.Add( f.contextMenuStrip2.Items[ i ] ); 38 } 39 this.contextMenuStrip1.Items.Add( item ); 40 f.ContextMenuStrip = contextMenuStrip1; 41 } 42 43 void f_Close( object sender, EventArgs e ) 44 { 45 Form2 f = ( Form2 ) sender; 46 ToolStripItem[] item = this.contextMenuStrip1.Items.Find( "children" ,true ); 47 foreach ( var i in item ) 48 { 49 this.contextMenuStrip1.Items.Remove( i ); 50 } 51 f.ContextMenuStrip = f.contextMenuStrip2; 52 } 53 54 private void openToolStripMenuItem_Click( object sender, EventArgs e ) 55 { 56 this.Visible = true; 57 } 58 59 private void notifyIcon1_MouseDoubleClick( object sender, MouseEventArgs e ) 60 { 61 this.Show(); 62 this.WindowState = FormWindowState.Normal; 63 this.notifyIcon1.Visible = false; 64 } 65 66 67 Icon icon1 = new Icon( System.Environment.CurrentDirectory + "\\icon.ico"); 68 69 private void Form1_Load( object sender, EventArgs e ) 70 { 71 if ( this.WindowState == FormWindowState.Minimized ) 72 { 73 this.Visible = false; 74 this.notifyIcon1.Visible = true; 75 this.notifyIcon1.Icon = icon1; 76 this.notifyIcon1.ShowBalloonTip( 100, "Tips: ", "Hi,this is a test of notifyIcon..", ToolTipIcon.Info ); 77 } 78 } 79 80 private void openToolStripMenuItem1_Click( object sender, EventArgs e ) 81 { 82 this.Show(); 83 this.WindowState = FormWindowState.Normal; 84 this.notifyIcon1.Visible = false; 85 } 86 87 private void hideMainWindowToolStripMenuItem_Click( object sender, EventArgs e ) 88 { 89 this.Hide(); 90 } 91 92 //最大化按钮事件 93 private int WM_SYSCOMMAND = 0x112; 94 private long SC_MAXIMIZE = 0xF030; 95 private long SC_MINIMIZE = 0xF020; 96 private long SC_CLOSE = 0xF060; 97 protected override void WndProc( ref Message m ) 98 { 99 if ( m.Msg == WM_SYSCOMMAND ) 100 { 101 if ( m.WParam.ToInt64() == SC_MAXIMIZE ) 102 { 103 //MessageBox.Show("MAXIMIZE "); 104 return; 105 } 106 if ( m.WParam.ToInt64() == SC_MINIMIZE ) 107 { 108 //MessageBox.Show("MINIMIZE "); 109 this.notifyIcon1.Visible = true; 110 this.Hide(); 111 return; 112 } 113 if ( m.WParam.ToInt64() == SC_CLOSE ) 114 { 115 //MessageBox.Show("CLOSE "); 116 return; 117 } 118 } 119 base.WndProc( ref m ); 120 }//end method Maximum 121 122 }//end class 123 }//end namespace