case 1 :
this.button2.Image.Save(fs,ImageFormat.Jpeg);
break;
case 2 :
this.button2.Image.Save(fs,ImageFormat.Bmp);
break;
case 3 :
this.button2.Image.Save(fs,ImageFormat.Gif);
break;
}
fs.Close();
}
}
關于寫入文件流的進一步信息,請參閱 FileStream.BeginWrite 方法。
3.ColorDialog 組件
此對話框顯示顏色列表,并且返回所選的顏色。
與前兩種對話框不同,ColorDialog 組件很容易實現其主要功能(挑選顏色)。選取的顏色將成為 Color 屬性的設定值。因此,使用顏色就和設定屬性值一樣簡單。在下面的例子中,按鈕控制的 Click 事件將會開啟一個 ColorDialog 組件。一旦用戶選中某種顏色,并且單擊了 OK ,按鈕的背景將被設成所選的顏色。本例假設存在名為 Button1 的 Button 組件和名為 ColorDialog1 的 ColorDialog 組件。
' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If ColorDialog1.ShowDialog() = DialogResult.OK Then
Button1.BackColor = ColorDialog1.Color
End If
End Sub
// C#
private void button1_Click(object sender, System.EventArgs e)
{
if(colorDialog1.ShowDialog() == DialogResult.OK)
{
button1.BackColor = colorDialog1.Color;
}
}
ColorDialog 組件具有 AllowFullOpen 屬性。當其設為 False 的時候,Define Custom Colors 按鈕將會失效,此時用戶只能使用預定義的調色板。此外,它還有一個 SolidColorOnly 屬性,當其設為 true 時,用戶將不能使用抖動顏色。
4.FontDialog 組件
此對話框允許用戶選擇字體,以改變其 weight 和 size 等屬性。
被選中的字體將成為 Font 屬性的設定值。因此,使用字體也和設定屬性值一樣簡單。在本例通過 Button 控件的 Click 事件調用 FileDialog 組件。當用戶選中一個字體,并且單擊 OK 的時候,TextBox 控件的 Font 屬性將被設成所選的字體。本例假設存在名為 Button1 的 Button 控件,名為 TextBox1 的 TextBox 控件和名為 FontDialog1 的 FontDialog 組件。
' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If FontDialog1.ShowDialog() = DialogResult.OK Then
TextBox1.Font = FontDialog1.Font
End If
End Sub
// C#
private void button1_Click(object sender, System.EventArgs e)
{
if(fontDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Font = fontDialog1.Font;
}
}
FontDialog 元件還包括 MinSize 和 MaxSize 屬性,它們決定了允許用戶選擇的字體的最小和最大點數;還有一個 ShowColor 屬性,當其設為 True 時,用戶可以從對話框的下拉列表中選取字體的顏色。
5.PrintDocument 類
以下三個會話框,PrintDialog 組件、 PageSetupDialog 組件和 PrintPreviewDialog 控件,都與 PrintDocument 類有關。PrintDocument 類用于文檔打印前的設置:設定其屬性,以改變文檔外觀和打印方式,再將其實例輸出到打印機。通常的步驟是:
(1) 生成 PrintDocument 類的一個實例;
(2) 設置 PageSetupDialog 組件的屬性;
(3) 使用 PrintPreviewDialog 控件進行預覽;
(4) 通過 PrintDialog 組件打印出來。
關于 PrintDocument 類的進一步資料,請參閱 PrintDocument Class 。
6.PrintDialog 元件
此對話框允許用戶指定將要打印的文檔。除此之外,它還能用于選擇打印機、決定打印頁,以及設置打印相關屬性。通過它可以讓用戶文檔打印更具靈活性:他們既能打印整個文檔,又能打印某個片斷,還能打印所選區域。
使用 PrintDialog 組件時要注意它是如何與 PrinterSettings 類進行交互的。PrinterSettings 類用于設定紙張來源、分辨率和加倍放大等打印機特征屬性。每項設置都是 PrinterSettings 類的一個屬性。通過 PrintDialog 類可以改變關聯到文檔的 PrinterSetting 類實例(由PrintDocument.PrinterSettings 指定)的特征屬性值。
PrintDialog 組件將包含特征屬性設置的 PrintDocument 類的實例提交到打印機。應用 PrintDialog 組件進行文檔打印的范例,請參見 Creating Standard Windows Forms Print Jobs。
7.PageSetupDialog 組件
PageSetupDialog 組件用于顯示打印布局、紙張大小和其它頁面選項。如同其他對話框一樣,可以通過 ShowDialog 方法調用 PageSetupDialog 組件。此外,必須生成一個 PrintDocument 類的實例,也即被打印的文檔;而且必須安裝了一臺本地或者遠程打印機,否則,PageSetupDialog 組件將無法獲取打印格式以供用戶選擇。
使用 PageSetupDialog 組件時必須注意它是如何與 PageSettings 類進行交互的。PageSettings 類決定頁面如何被打印,比如打印方向、頁面大小和邊距等。每項設置都是 PageSettings 類的一個屬性。PageSetupDialog 類可以改變 PageSettings 類實例(由 PrintDocument.DefaultPageSettings 指定)的上述選項。
在下列代碼中,Button 控件的 Click 事件處理程序開啟一個 PageSetupDialog 組件;其 Document 屬性被設成某個存在的文檔;其 Color 屬性被設成 false 。
本例假設存在名為 Button1 的 Button 控件、名為 myDocument 的 PrintDocument 控件和名為 PageSetupDialog1 的 PageSetupDialog 組件。
' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' The print document 'myDocument' used below
' is merely for an example.
'You will have to specify your own print document.
PageSetupDialog1.Document = myDocument
' Set the print document's color setting to false,
' so that the page will not be printed in color.
PageSetupDialog1.Document.DefaultPageSettings.Color = False
PageSetupDialog1.ShowDialog()
End Sub
// C#
private void button1_Click(object sender, System.EventArgs e)
{
// The print document 'myDocument' used below
// is merely for an example.
// You will have to specify your own print document.
pageSetupDialog1.Document = myDocument;
// Set the print document's color setting to false,
// so that the page will not be printed in color.
pageSetupDialog1.Document.DefaultPageSettings.Color = false;
pageSetupDialog1.ShowDialog();
}
8.PrintPreviewDialog 控件
與其他對話框不同,PrintPreviewDialog 控件對整個應用程序或者其它控件沒有影響,因為它僅僅在對話框里顯示內容。此對話框用于顯示文檔,主要是打印之前的預覽。
調用 PrintPreviewDialog 控件,也是使用 ShowDialog 方法。同時,必須生成 PrintDocument 類的一個實例,也即被打印的文檔。
注意:當使用 PrintPreviewDialog 控件時,也必須已經安裝了一臺本地或者遠程打印機,否則 PrintPreviewDialog 組件將無法獲取被打印文檔的外觀。
PrintPreviewDialog 控件通過 PrinterSettings 類和 PageSettings 類進行設置,分別與 PageDialog 組件和 PageSetupDialog 組件相似。此外,PrintPreviewDialog 控件的 Document 屬性所指定的被打印文檔,同時作用于 PrinterSettings 類和 PageSettings 類,其內容被顯示在預覽窗口中。
在下列代碼中,通過 Button 控件的 Click 事件調用 PrintPreviewDialog 控件。被打印文檔在 Document 屬性中指定。注意:代碼中沒有指定被打印文檔。
本例假設存在名為 Button1 的 Button 控件,名為 myDocument 的 PrintDocument 組件和名為 PrintPreviewDialog1 的 PrintPreviewDialog 控件。
' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' The print document 'myDocument' used below
' is merely for an example.
' You will have to specify your own print document.
PrintPreviewDialog1.Document = myDocument
PrintPreviewDialog1.ShowDialog()
End Sub
// C#
private void button1_Click(object sender, System.EventArgs e)
{
// The print document 'myDocument' used below
// is merely for an example.
// You will have to specify your own print document.
printPreviewDialog1.Document = myDocument;
printPreviewDialog1.ShowDialog()
}
小結
.NET 框架里包含了 Windows 用戶所熟悉的各種公共對話框,于是在應用程序中提供交互功能變得更加容易。通常,對話框的用途是多種多樣的;.NET 框架對此提供了開放支持,你可以選擇最佳方案以適合應用程序的需要。我們介紹了與對話框組件有關的一些簡單應用。你可以直接使用這些代碼,也可以對其稍加修改用于你的應用程序。
文章來源于領測軟件測試網 http://www.kjueaiud.com/