窗體對(duì)話框組件與微軟視窗操作系統(tǒng)中的對(duì)話框是一樣的;也就是說(shuō),PrintDialog 組件是“打印”對(duì)話框,OpenFileDialog 組件是 “打開(kāi)文件”對(duì)話框,依此類推。 與以往的 Microsoft Visual Basic 6.0 等 Windows 程序設(shè)計(jì)語(yǔ)言相似,.NET 框架提供了 Windows 用戶耳熟能詳?shù)膶?duì)話框。對(duì)話框的具體用途(如 Printdialog 可用于文件打印等)通常是多種多樣的。故而在 .NET 框架提供的基礎(chǔ)類中不包含用于文件打印、顏色選擇等具體操作的代碼,而你卻可以根據(jù)應(yīng)用程序的需要靈活地實(shí)現(xiàn)它們。因此,在 .NET 框架下,你不但可以直接應(yīng)用標(biāo)準(zhǔn)對(duì)話框,而且能根據(jù)用戶的選擇作出不同的響應(yīng)。本文提供的代碼其用途就在于此。 注意,關(guān)于各種對(duì)話框的屬性、方法和事件的完整描述,可以在相應(yīng)類的 Members 頁(yè)面中找到。比如要查看 OpenFileDialog 組件的某一方法,就可以在文檔索引的“OpenFileDialog class, all members”欄目中找到相關(guān)的主題。 OpenFileDialog 組件 OpenFileDialog 對(duì)話框使得用戶能夠通過(guò)瀏覽本地或者遠(yuǎn)程的文件系統(tǒng)以打開(kāi)所選文件。它將返回文件路徑和所選的文件名。 OpenFileDialog 組件和 SaveFileDialog 組件(下文將會(huì)詳細(xì)描述)包含了用于瀏覽和選取文件所必需的基本代碼。有了它們,你就不必為這些功能編寫(xiě)任何代碼,進(jìn)而能夠?qū)P膶?shí)現(xiàn)打開(kāi)或者保存文件等具體操作。 注意,F(xiàn)ileDialog 類的 FilterIndex 屬性(由于繼承的原因,為 OpenFileDialog 和 SaveFileDialog 類所共有) 使用 one-based 索引(譯者注:指從 1 開(kāi)始編號(hào)的索引)。 此特性將在下文的代碼中用到(并且會(huì)在相應(yīng)位置再次強(qiáng)調(diào))。當(dāng)應(yīng)用程序通過(guò)類型過(guò)濾器打開(kāi)文件時(shí),或者需要保存為特定格式的文件(比如:保存為純文本文件而不是二進(jìn)制文件)時(shí),這一點(diǎn)是非常重要的。人們?cè)谑褂?FilterIndex 屬性時(shí)卻經(jīng)常忘了它,因此現(xiàn)在務(wù)必要把它記住。 下列代碼通過(guò) Button 控件的 Click 事件調(diào)用 OpenFileDialog 組件。當(dāng)用戶選中某個(gè)文件,并且單擊 OK 的時(shí)候,所選的文件將被打開(kāi)。在本例中,文件內(nèi)容將被顯示在消息框內(nèi),以證實(shí)文件流被讀入。 本例假設(shè)存在名為 Button1 的 Button 控件和名為 OpenFileDialog1 的 OpenFileDialog 控件。 ' Visual Basic ' NOTE: You must import the following namespace: ' Imports System.IO ' Without this import statement at the beginning ' of your code, the example will not function. Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click If OpenFileDialog1.ShowDialog() = DialogResult.OK Then Dim sr As New StreamReader(OpenFileDialog1.FileName) MessageBox.Show(sr.ReadToEnd) sr.Close() End If End Sub // C# // NOTE: You must import the following namespace: // using System.IO; // Without this import statement at the beginning // of your code, the example will not function. private void button1_Click(object sender, System.EventArgs e) { if(openFileDialog1.ShowDialog() == DialogResult.OK) { StreamReader sr = new StreamReader(openFileDialog1.FileName); MessageBox.Show(sr.ReadToEnd()); sr.Close(); } } 打開(kāi)文件還可以使用 OpenFileDialog 組件的 OpenFile 方法,它將返回文件的每一個(gè)字節(jié)。在下面的例子中,一個(gè) OpenFileDialog 組件將被實(shí)例化,它使用了 cursor 過(guò)濾器,以限定用戶只能選取光標(biāo)文件(擴(kuò)展名為 .cur)。一旦某個(gè) .cur 文件被選中,窗體的光標(biāo)就被設(shè)成該文件描繪的光標(biāo)形狀。 本例假設(shè)存在名為 Button1 的 Button 控件。 ' Visual Basic Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click ' Display an OpenFileDialog so the user can select a Cursor. Dim openFileDialog1 As New OpenFileDialog() openFileDialog1.Filter = "Cursor Files|*.cur" openFileDialog1.Title = "Select a Cursor File" ' Show the Dialog. ' If the user clicked OK in the dialog and ' a .CUR file was selected, open it. If openFileDialog1.ShowDialog() = DialogResult.OK Then If openFileDialog1.FileName <> "" Then ' Assign the cursor in the Stream to the Form's Cursor property. Me.Cursor = New Cursor(openFileDialog1.OpenFile()) End If End If End Sub // C# private void button1_Click(object sender, System.EventArgs e) { // Display an OpenFileDialog so the user can select a Cursor. OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Cursor Files|*.cur"; openFileDialog1.Title = "Select a Cursor File"; // Show the Dialog. // If the user clicked OK in the dialog and // a .CUR file was selected, open it. if (openFileDialog1.ShowDialog() == DialogResult.OK) { if(openFileDialog1.FileName != "") { // Assign the cursor in the Stream to the Form's Cursor property. this.Cursor = new Cursor(openFileDialog1.OpenFile()); } } } 關(guān)于讀取文件流的進(jìn)一步信息,請(qǐng)參閱FileStream.BeginRead 方法。 SaveFileDialog 組件 本對(duì)話框允許用戶瀏覽文件系統(tǒng)并且選取將被寫(xiě)入的文件。當(dāng)然,你還必須為文件寫(xiě)入編寫(xiě)具體代碼。 下列代碼通過(guò) Button 控件的 Click 事件調(diào)用 SaveFileDialog 組件。當(dāng)用戶選中某個(gè)文件,并且單擊 OK 的時(shí)候,RichTextBox 控件里的內(nèi)容將被保存到所選的文件中。 本例假設(shè)存在名為 Button1 的 Button 控件,名為 RichTextBox1 的 RichTextBox 控件和名為 OpenFileDialog1 的 SaveFileDialog 控件。 ' Visual Basic ' NOTE: You must import the following namespace: ' Imports System.IO ' Without this import statement at the beginning ' of your code, the code example will not function. Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click If SaveFileDialog1.ShowDialog() = DialogResult.OK Then RichTextBox1.SaveFile(SaveFileDialog1.FileName, _ RichTextBoxStreamType.PlainText) End If End Sub // C# // NOTE: You must import the following namespace: // using System.IO; // Without this import statement at the beginning // of your code, the code example will not function. private void button1_Click(object sender, System.EventArgs e) { if((saveFileDialog1.ShowDialog() == DialogResult.OK) { richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); } } 保存文件還可以用 SaveFileDialog 組件的 OpenFile 方法,它將提供一個(gè)用于寫(xiě)入的 Stream 對(duì)象。 在下面的例子中,有一個(gè)包含圖片的 Button 控件。 當(dāng)你單擊這個(gè)按鈕的時(shí)候,一個(gè) SaveFileDialog 組件將被打開(kāi),它將使用 .gif 、 .jpeg 和 .bmp 類型的文件過(guò)濾器。一旦用戶通過(guò) Save File 對(duì)話框內(nèi)選中此類文件,按鈕上的圖片將被存入其中。 本例假設(shè)存在名為 Button2 的 Button 控件,并且它的 Image 屬性被設(shè)為某個(gè)擴(kuò)展名為 .gif 、 .jpeg 或者 .bmp 的圖片文件。 'Visual Basic ' NOTE: You must import the following namespaces: ' Imports System.IO ' Imports System.Drawing.Imaging ' Without these import statements at the beginning of your code, ' the code example will not function. Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click ' Display an SaveFileDialog so the user can save the Image ' assigned to Button2. Dim saveFileDialog1 As New SaveFileDialog() saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif" saveFileDialog1.Title = "Save an Image File" saveFileDialog1.ShowDialog() ' If the file name is not an empty string open it for saving. If saveFileDialog1.FileName <> "" Then ' Save the Image via a FileStream created by the OpenFile method. Dim fs As FileStream = CType(saveFileDialog1.OpenFile(), FileStream) ' Save the Image in the appropriate ImageFormat based upon the ' file type selected in the dialog box. ' NOTE that the FilterIndex property is one-based. Select Case saveFileDialog1.FilterIndex Case 1 Me.button2.Image.Save(fs, ImageFormat.Jpeg) Case 2 Me.button2.Image.Save(fs, ImageFormat.Bmp) Case 3 Me.button2.Image.Save(fs, ImageFormat.Gif) End Select fs.Close() End If End Sub // C# // NOTE: You must import the following namespaces: // using System.IO; // using System.Drawing.Imaging; // Without these import statements at the beginning of your code, // the code example will not function. private void button2_Click(object sender, System.EventArgs e) { // Display an SaveFileDialog so the user can save the Image // assigned to Button2. SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"; saveFileDialog1.Title = "Save an Image File"; saveFileDialog1.ShowDialog(); // If the file name is not an empty string open it for saving. if(saveFileDialog1.FileName != "") { // Save the Image via a FileStream created by the OpenFile method. FileStream fs = (FileStream)saveFileDialog1.OpenFile(); // Save the Image in the appropriate ImageFormat based upon the // File type selected in the dialog box. // NOTE that the FilterIndex property is one-based. switch(saveFileDialog1.FilterIndex) { 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(); } } 關(guān)于寫(xiě)入文件流的進(jìn)一步信息,請(qǐng)參閱 FileStream.BeginWrite 方法。
ColorDialog 組件 此對(duì)話框顯示顏色列表,并且返回所選的顏色。 與前兩種對(duì)話框不同,ColorDialog 組件很容易實(shí)現(xiàn)其主要功能(挑選顏色)。選取的顏色將成為 Color 屬性的設(shè)定值。因此,使用顏色就和設(shè)定屬性值一樣簡(jiǎn)單。在下面的例子中,按鈕控制的 Click 事件將會(huì)開(kāi)啟一個(gè) ColorDialog 組件。一旦用戶選中某種顏色,并且單擊了 OK ,按鈕的背景將被設(shè)成所選的顏色。本例假設(shè)存在名為 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 屬性。當(dāng)其設(shè)為 False 的時(shí)候,Define Custom Colors 按鈕將會(huì)失效,此時(shí)用戶只能使用預(yù)定義的調(diào)色板。此外,它還有一個(gè) SolidColorOnly 屬性,當(dāng)其設(shè)為 true 時(shí),用戶將不能使用抖動(dòng)顏色。
FontDialog 組件 此對(duì)話框允許用戶選擇字體,以改變其 weight 和 size 等屬性。 被選中的字體將成為 Font 屬性的設(shè)定值。因此,使用字體也和設(shè)定屬性值一樣簡(jiǎn)單。在本例通過(guò) Button 控件的 Click 事件調(diào)用 FileDialog 組件。當(dāng)用戶選中一個(gè)字體,并且單擊 OK 的時(shí)候,TextBox 控件的 Font 屬性將被設(shè)成所選的字體。本例假設(shè)存在名為 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 屬性,它們決定了允許用戶選擇的字體的最小和最大點(diǎn)數(shù);還有一個(gè) ShowColor 屬性,當(dāng)其設(shè)為 True 時(shí),用戶可以從對(duì)話框的下拉列表中選取字體的顏色。 PrintDocument 類 以下三個(gè)會(huì)話框,PrintDialog 組件、 PageSetupDialog 組件和 PrintPreviewDialog 控件,都與 PrintDocument 類有關(guān)。PrintDocument 類用于文檔打印前的設(shè)置:設(shè)定其屬性,以改變文檔外觀和打印方式,再將其實(shí)例輸出到打印機(jī)。通常的步驟是: (1) 生成 PrintDocument 類的一個(gè)實(shí)例; (2) 設(shè)置 PageSetupDialog 組件的屬性; (3) 使用 PrintPreviewDialog 控件進(jìn)行預(yù)覽; (4) 通過(guò) PrintDialog 組件打印出來(lái)。 關(guān)于 PrintDocument 類的進(jìn)一步資料,請(qǐng)參閱 PrintDocument Class 。 PrintDialog 元件 此對(duì)話框允許用戶指定將要打印的文檔。除此之外,它還能用于選擇打印機(jī)、決定打印頁(yè),以及設(shè)置打印相關(guān)屬性。通過(guò)它可以讓用戶文檔打印更具靈活性:他們既能打印整個(gè)文檔,又能打印某個(gè)片斷,還能打印所選區(qū)域。 使用 PrintDialog 組件時(shí)要注意它是如何與 PrinterSettings 類進(jìn)行交互的。PrinterSettings 類用于設(shè)定紙張來(lái)源、分辨率和加倍放大等打印機(jī)特征屬性。每項(xiàng)設(shè)置都是 PrinterSettings 類的一個(gè)屬性。通過(guò) PrintDialog 類可以改變關(guān)聯(lián)到文檔的 PrinterSetting 類實(shí)例(由PrintDocument.PrinterSettings 指定)的特征屬性值。 PrintDialog 組件將包含特征屬性設(shè)置的 PrintDocument 類的實(shí)例提交到打印機(jī)。應(yīng)用 PrintDialog 組件進(jìn)行文檔打印的范例,請(qǐng)參見(jiàn) Creating Standard Windows Forms Print Jobs。 PageSetupDialog 組件 PageSetupDialog 組件用于顯示打印布局、紙張大小和其它頁(yè)面選項(xiàng)。如同其他對(duì)話框一樣,可以通過(guò) ShowDialog 方法調(diào)用 PageSetupDialog 組件。此外,必須生成一個(gè) PrintDocument 類的實(shí)例,也即被打印的文檔;而且必須安裝了一臺(tái)本地或者遠(yuǎn)程打印機(jī),否則,PageSetupDialog 組件將無(wú)法獲取打印格式以供用戶選擇。 使用 PageSetupDialog 組件時(shí)必須注意它是如何與 PageSettings 類進(jìn)行交互的。PageSettings 類決定頁(yè)面如何被打印,比如打印方向、頁(yè)面大小和邊距等。每項(xiàng)設(shè)置都是 PageSettings 類的一個(gè)屬性。PageSetupDialog 類可以改變 PageSettings 類實(shí)例(由 PrintDocument.DefaultPageSettings 指定)的上述選項(xiàng)。 在下列代碼中,Button 控件的 Click 事件處理程序開(kāi)啟一個(gè) PageSetupDialog 組件;其 Document 屬性被設(shè)成某個(gè)存在的文檔;其 Color 屬性被設(shè)成 false 。 本例假設(shè)存在名為 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(); } PrintPreviewDialog 控件 與其他對(duì)話框不同,PrintPreviewDialog 控件對(duì)整個(gè)應(yīng)用程序或者其它控件沒(méi)有影響,因?yàn)樗鼉H僅在對(duì)話框里顯示內(nèi)容。此對(duì)話框用于顯示文檔,主要是打印之前的預(yù)覽。 調(diào)用 PrintPreviewDialog 控件,也是使用 ShowDialog 方法。同時(shí),必須生成 PrintDocument 類的一個(gè)實(shí)例,也即被打印的文檔。 注意:當(dāng)使用 PrintPreviewDialog 控件時(shí),也必須已經(jīng)安裝了一臺(tái)本地或者遠(yuǎn)程打印機(jī),否則 PrintPreviewDialog 組件將無(wú)法獲取被打印文檔的外觀。 PrintPreviewDialog 控件通過(guò) PrinterSettings 類和 PageSettings 類進(jìn)行設(shè)置,分別與 PageDialog 組件和 PageSetupDialog 組件相似。此外,PrintPreviewDialog 控件的 Document 屬性所指定的被打印文檔,同時(shí)作用于 PrinterSettings 類和 PageSettings 類,其內(nèi)容被顯示在預(yù)覽窗口中。 在下列代碼中,通過(guò) Button 控件的 Click 事件調(diào)用 PrintPreviewDialog 控件。被打印文檔在 Document 屬性中指定。注意:代碼中沒(méi)有指定被打印文檔。 本例假設(shè)存在名為 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() } 小結(jié) .NET 框架里包含了 Windows 用戶所熟悉的各種公共對(duì)話框,于是在應(yīng)用程序中提供交互功能變得更加容易。通常,對(duì)話框的用途是多種多樣的;.NET 框架對(duì)此提供了開(kāi)放支持,你可以選擇最佳方案以適合應(yīng)用程序的需要。我們介紹了與對(duì)話框組件有關(guān)的一些簡(jiǎn)單應(yīng)用。你可以直接使用這些代碼,也可以對(duì)其稍加修改用于你的應(yīng)用程序。
|