当前位置:编程学习 > C#/ASP.NET >>

Dotnet WinForm 建立 FAQ2(转贴)(编程技巧)

答案:9.     如何制作一个MDI的窗体
1.     建立一个新的Windows Application项目
2.     分别加入两个窗体Form1 、Form2
3.     设置Form1的IsMdiContainer属性为True。使它成为MDI主窗体。
4.     在Form2中加入一个RichTextBox控件,并设置Dock为:Fill
5.     在Tools 窗体中拖一个MainMenu到窗体Form1,然后建立一个菜单File|Windows|Help三个菜单项,File中包括New、Exit菜单项;Windows中包括Cascade、Horizontal等。
6.     设置Windows菜单项的MdiList属性=True, 这样每一个MDI子窗口将自动加在Windows菜单项下面。
7.     双击New菜单项,然后加入以下代码:
     private void menuNew_Click(object sender, System.EventArgs e)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
     {
         Form2  NewMdiChild ;
         NewMdiChild = new Form2() ;
         NewMdiChild.MdiParent = this ;
         NewMdiChild.Show() ;
  
     }
8.     在Windows的Cascade等菜单项中加入以下代码:
     private void menuWindowCasca_Click(object sender, System.EventArgs e)
     {
         this.LayoutMdi( MdiLayout.Cascade) ;
        
     }
另外还有以下常用的:
this.LayoutMdi(MdiLayout.TileHorizontal);
this.LayoutMdi(MdiLayout.TileVertical);
9.     F5运行。
最终版的VS.NET 不知是否会有一个通用的模板,不过用完全手工的方式产生一个MDI的窗口,显得有些繁琐,不如VS.NET的IDE方式下那么简洁。
  
10.     如何将你的窗体不显示在任务条上.
当窗体的边界风格是Tools Window时它都不会出现在任务条上的.另外上面标题5中介绍的方法不仅窗体看不见,也不会出现在任务条上.
    如果你现在在Dotnet的世界,这件事也变的简单,任何的Winform窗体现在都有ShowInTaskbar属性,所以你只要简单的设置这个属性就可以了。同样你可以选择在属性窗口中将ShowInTaskbar由True改为False。或是用代码的方式:
    MyTaskBarFrm.ShowInTaskbar = false ; ( C# )
  
11.     如何制作一个带启动屏幕的窗体.
需要你准备两个Winform的窗体,一个叫它:SplashScreen,把它做成一个漂亮的窗体。然后你需要一个主窗体叫它:Form1吧,然后在这个窗体加入下面的代码。
     // ( C# )
     protected override void OnLoad ( System.EventArgs e )
     {
         //make load take a long time
         Thread.Sleep(2000);
  
         base.OnLoad(e);
  
     }
然后在Main中加入这样的代码:
     [STAThread]
     static void <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /> Main ()
     {
         SplashScreen splashForm = new SplashScreen();
         splashForm.Show();
  
         Form1 mainForm = new Form1() ;
         mainForm.Load += new EventHandler(splashForm.MainScreen_Load);
         Application.Run(mainForm);
  
     }
不要忘了加上对Threading的引用: using System.Threading;
  
12.     如何使你的窗体TrayIcon.
实现这个功能你可以运用NotifyIcon控件来达到,从Tools Windows中将NotifyIcon拖到你的窗体上然后在下面的事件加入如下代码,F5。
    
   ' // VB.NET
    Private mIconA As Icon = New Icon("Icon1.ico")
    Private mIconB As Icon = New Icon("Icon2.ico")
    Private mIconDisplayed As Boolean
   
    Public Sub New()
        MyBase.New
  
        Form1 = Me
  
        'This call is required by the Win Form Designer.
        InitializeComponent
  
        'TODO: Add any initialization after the InitializeComponent() call
       
        'this form isn't used directly so hide it immediately
        Me.Hide()
       
        'setup the tray icon
        Initializenotifyicon()
    End Sub
   
    Private Sub Initializenotifyicon()
        'setup the default icon
        notifyicon = New System.Windows.Forms.NotifyIcon()
        NotifyIcon.Icon = mIconA
        NotifyIcon.Text = "Right Click for the menu"
        NotifyIcon.Visible = True
        mIconDisplayed = True
  
        'Insert all MenuItem objects into an array and add them to
        'the context menu simultaneously
        Dim mnuItms(3) As MenuItem
        mnuItms(0) = New MenuItem("Show Form...", New EventHandler(AddressOf Me.ShowFormSelect))
        mnuItms(0).DefaultItem = True
        mnuItms(1) = New MenuItem("Toggle Image", New EventHandler(AddressOf Me.ToggleImageSelect))
        mnuItms(2) = New MenuItem("-")
        mnuItms(3) = New MenuItem("Exit", New EventHandler(AddressOf Me.ExitSelect))
        Dim notifyiconMnu As ContextMenu = New ContextMenu(mnuItms)
        notifyicon.ContextMenu = notifyiconMnu
    End Sub
  
    Public Sub ShowFormSelect(ByVal sender As Object, ByVal e As System.EventArgs)
        'Display the settings dialog
        Dim SettingsForm As New SettingsForm()
        SettingsForm.ShowDialog()
  
    End Sub
  
    Public Sub ToggleImageSelect(ByVal sender As Object, ByVal e As System.EventArgs)
        'called when the user selects the 'Toggle Image' context menu
 &

上一个:捕捉窗口关闭事件(转贴)
下一个:选择文件夹的对话框!

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,