Dotnet WinForm 建立 FAQ (2)

来源:互联网 发布:数据解决方案 编辑:程序博客网 时间:2024/05/01 00:44
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”原则而使开发人员陷??尴尬,现在我们可?nbsp;很自然的享受这种特性,
但另一方面dotnet和Java或Delphi似乎靠得更近些,你几乎不能像MFC时代那样去
从源代码中找寻秘密和内幕了。
原创粉丝点击