Dotnet WinForm 建立 FAQ (2)
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時代那樣去
從源代碼中找尋祕密和內幕了。
當窗體的邊界風格是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時代那樣去
從源代碼中找尋祕密和內幕了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.