DotNET WinForm FAQ 16个(下)
发表于:2007-06-30来源:作者:点击数:
标签:
9. 如何制作一个MDI的窗体 1. 建立一个新的 Windows Application项目 2. 分别加入两个窗体Form1 、Form2 3. 设置Form1的IsMdiContainer属性为True。使它成为MDI主窗体。 4. 在Form2中加入一个RichTextBox控件,并设置Dock为:Fill 5. 在Tools 窗体中拖一个Mai
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;
this.mdiClient1.Name = "mdiClient1";
或是在窗口的属性窗口组合框中看到mdiClient1 System.Windows.Forms.mdiClient.这就是主MDI窗口,不过我没有在dotnet的文档中找到任何有关System.Windows.Forms.mdiClient的说明。然后你可以在InitializeComponent()或窗体的构造函数中加入这样的代码( C# ):
this.mdiClient1.BackgroundImage = new Bitmap("C:\\DotNetApp\\WinForm\\Tile.bmp" ) ;
网上有一个ImageView的例子,里面演示了给MDI主窗体中背景上加入一行Logo文字的方法,这样使你的MDI窗体看起来很商业化,具体的你可以这样做:
1. 先在VS.NET 自动产生代码的InitializeComponent中看是否有这样的语句( C# ):
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.mdiClient1});
又是这个mdiClient (haha)
2. 建立以下两个函数用于显示这个Logo字符:
// ( C# )
protected void Mdi_OnPaint ( Object s, System.Windows.Forms.PaintEventArgs e )
{
Control c = (Control)s;
Rectangle r1 = c.ClientRectangle;
r1.Width -= 4;
r1.Height -= 4;
Rectangle r2 = r1;
r2.Width -= 1;
r2.Height -= 1;
Font f = new Font("Tahoma", 8);
String str = "MyWinform.NET ?2001 MyWinform Application V1.0";
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Far;
sf.LineAlignment = StringAlignment.Far;
e.Graphics.DrawString(str, f, new SolidBrush(SystemColors.ControlDarkDark), r1, sf);
e.Graphics.DrawString(str, f, new SolidBrush(SystemColors.ControlLight), r2, sf);
}
protected void Mdi_OnResize ( Object s , System.EventArgs e )
{
Control c = (Control)s;
c.Invalidate();
}
3. 在InitializeComponent()或窗体的构造函数中加入这样的代码:
( C# )
this.Controls[0].Paint += new PaintEventHandler( Mdi_OnPaint ) ;
this.Controls[0].Resize += new EventHandler( Mdi_OnResize ) ;
注意将它加在InitializeComponent()后面或是在InitializeComponent函数中this.Controls.AddRange函数之后。
--------------------------------------------------------------------------------
总的看来,整个Winform部分始终透着Hejlsberg设计VJ++中WFC库的气息,现在还没有任何线索能证明dotnet是照搬WFC库,但我还是相信Delphi和VJ++的用户会更容易感受到Hejlsberg的设计风格,比如事件委托在几年前就被视为领先而严重违背“纯
Java”原则而使
开发人员陷入尴尬,现在我们可以很自然的享受这种特性,但另一方面dotnet和Java或Delphi似乎靠得更近些,你几乎不能像MFC时代那样去从源代码中找寻秘密和内幕了。
原文转自:http://www.ltesting.net