教學課程 1:建立圖片檢視器Tutorial 1: Create a picture viewer

在本教程中,您將構建一個應用,該應用從檔中載入圖片並將其顯示在視窗中。In this tutorial, you build an app that loads a picture from a file and displays it in a window. 您將瞭解如何使用 Windows表單設計器將按鈕和圖片框等控制項拖動到表單上,設置其屬性,並使用容器平滑調整表單的大小。You learn how to use the Windows Forms Designer to drag controls like buttons and picture boxes on to your form, set their properties, and use containers to smoothly resize the form. 您也會開始撰寫程式碼。You also get started writing code.

注意

本教程將介紹 C# 和 Visual Basic,因此請關注特定于您使用的程式設計語言的資訊。This tutorial covers both C# and Visual Basic, so focus on the information that's specific to the programming language you're using.

本教程將引導您完成以下任務:This tutorial walks you through the following tasks:

  • 建立新專案。Create a new project.

  • 測試 (偵錯) 應用程式。Test (debug) an application.

  • 將基本控制項 (例如核取方塊和按鈕) 加入至表單。Add basic controls like check boxes and buttons to a form.

  • 使用佈局在表單上定位控制項。Position controls on a form by using layouts.

  • 將 [開啟檔案]**** 和 [色彩]**** 對話方塊新增至表單。Add Open File and Color dialog boxes to a form.

  • 使用 IntelliSense 和程式碼片段編寫代碼。Write code by using IntelliSense and code snippets.

  • 撰寫事件處理常式方法。Write event handler methods.

完成後,你的應用應類似于下圖:When you finish, your app should look similar to the following image:

您在本教程中創建的圖片檢視器應用

TitleTitle 描述Description
第 1 步:創建 Windows 表單應用專案Step 1: Create a Windows Forms App project 首先創建 Windows 表單應用專案。Begin by creating a Windows Forms App project.
第 2 步:運行圖片檢視器應用Step 2: Run your picture viewer app 運行您在上一步中創建的 Windows 表單應用專案。Run the Windows Forms App project that you created in the previous step.
第 3 步:設置表單內容Step 3: Set your form properties 使用 [屬性]**** 視窗變更表單的外觀。Change the way your form looks using the Properties window.
第 4 步:使用表佈局面板控制項佈局表單Step 4: Lay out your form with a TableLayoutPanel control 新增 TableLayoutPanel 控制項至表單。Add a TableLayoutPanel control to your form.
第 5 步:將控制項添加到表單Step 5: Add controls to your form 將控制項 (例如 PictureBox 控制項和 CheckBox 控制項) 新增至表單。Add controls, such as a PictureBox control and a CheckBox control, to your form. 將按鈕加入至表單。Add buttons to your form.
第 6 步:命名按鈕控制項Step 6: Name your button controls 將按鈕重新命名為更有意義的名稱。Rename your buttons to something more meaningful.
步驟 7:將對話方塊元件新增至您的表單Step 7: Add dialog components to your form 將一個 OpenFileDialog 元件和一個 ColorDialog 元件加入您的表單。Add an OpenFileDialog component and a ColorDialog component to your form.
步驟 8:為顯示圖片按鈕事件處理常式撰寫程式碼Step 8: Write code for the show a picture button event handler 使用 IntelliSense 工具編寫代碼。Write code by using the IntelliSense tool.
步驟 9:檢閱、註解和測試您的程式碼Step 9: Review, comment, and test your code 檢閱和測試程式碼。Review and test your code. 視需要加入註解。Add comments as needed.
步驟 10:為其他按鈕和核取方塊編寫代碼Step 10: Write code for additional buttons and a check box 使用 IntelliSense 撰寫程式碼讓其他按鈕和核取方塊發揮作用。Write code to make other buttons and a check box work using IntelliSense.
第 11 步:運行應用並嘗試其他功能Step 11: Run your app and try other features 運行應用並設置背景顏色。Run your app and set the background color. 嘗試其他功能,例如變更色彩、字型和框線。Try other features, such as changing colors, fonts, and borders.

也有偉大的,免費的視頻學習資源可供你使用。There are also great, free video learning resources available to you. 要瞭解有關 C# 中程式設計的更多詳細資訊,請參閱C# 基礎知識:絕對初學者的開發To learn more about programming in C#, see C# fundamentals: Development for absolute beginners. 若要深入了解 Visual Basic 中的程式設計,請參閱 Visual Basic 基礎:徹底初學者開發 (英文)。To learn more about programming in Visual Basic, see Visual Basic fundamentals: Development for absolute beginners.

後續步驟Next steps

要開始本教程,請從步驟 1 開始**:創建 Windows 表單應用程式專案**。To begin the tutorial, start with Step 1: Create a Windows Forms application project.

另請參閱See also