DotNET WinForm FAQ 16个(下)


** 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)

{

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 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

'determine which icon is currently visible and switch it

If mIconDisplayed Then

'called when the user selects the 'Show Form' context menu

NotifyIcon.Icon = mIconB

NotifyIcon.Text = "Sad"

mIconDisplayed = False

Else

NotifyIcon.Icon = mIconA

NotifyIcon.Text = "Happy"

mIconDisplayed = True

End If

End Sub

Public Sub ExitSelect(ByVal sender As Object, ByVal e As System.EventArgs)

'called when the user selects the 'Exit' context menu

'hide the tray icon

NotifyIcon.Visible = False

'close up

Me.Close()

End Sub

'Form overrides dispose to clean up the component list.

Public Overloads Overrides Sub Dispose()

MyBase.Dispose()

components.Dispose()

End Sub

图标文件你自己准备了,如果成功你可以看到有关 NotifyIcond 的各种功能了。

** 13. 如何修改控制窗体的尺寸和长宽尺寸 . **

主要是修改 Winform 的 Size, Width 和 Height 属性。同样它们都是可以在设计和运行时刻进行修改和设置。

Form1.Size = New System.Drawing.Size(100, 100) ( VB.NET )

Form1.Width += 100 (VB.NET )

Form1.Height -= 20 (VB.NET )

** 14. 如何建立一个 Windows Explorer 风格的窗体 . **

1. 建立一个新的 Windows Application

2. 从 Toolbox 窗口拖一个 TreeView 控件、、一个 Splitterk 控件、一个 ListView 控件,分别在属性窗口中设置 TreeView 的 Dock 属性为:: Left ;设置 ListView 控件的 Dock 属性为: Fill

3 : F5 运行

** 15. 如何设置初始的启动窗体 **

无论是 C# 还是 Visual Basic 的 Winform 项目中你都可以在 Solution Explorer 窗口中右键你的 Project ,然后选择属性,从你 Project 的属性页中选择你启动的窗体或是 Main()方法。

有些不同的是在目前的 VS.NET Beta2中C#项目会自动产生Main() 方法,Visual Basic.Net 的项目中你必须自己添加Main()代码,C#中你可以将Form1改成任何你可以启动的窗体名:

// ( C# )

static void Main ()

{

Application.Run( new Form1());

}

** 16. 如何建立一个有背景图像的窗体 **

现在的 Winform 中所有的窗体都有一个 BackgroundImage 属性,只用对它赋值就可以了。普通窗体可以在运行模式也可以在运行模式完成这个设置。比如在 InitializeComponent() 或窗体的构造函数中加入这样的代码:

this.BackgroundImage = new Bitmap("C:\\DotNetApp\\WinForm\\Tile.bmp" ) ;

对于 MDI 的主窗体要麻烦一些,在 VS.NET 的 IDE 窗体中,当你设置完 IsMdiContainer 属性为 True 后,你需要查看一下 InitializeComponent() 中是否有这样的代码 ( C# ) :

this.mdiClient1.Dock = System.Windows.Forms.DockStyle.Fill;

<FONT face="Lucida

Published At
Categories with Web编程
Tagged with
comments powered by Disqus