C# 程式设计.ppt

上传人:赵齐羽 文档编号:379225 上传时间:2018-10-09 格式:PPT 页数:82 大小:989.50KB
下载 相关 举报
C# 程式设计.ppt_第1页
第1页 / 共82页
C# 程式设计.ppt_第2页
第2页 / 共82页
C# 程式设计.ppt_第3页
第3页 / 共82页
C# 程式设计.ppt_第4页
第4页 / 共82页
C# 程式设计.ppt_第5页
第5页 / 共82页
亲,该文档总共82页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、1,C#.NET 程式設計,第十一章 Windows Forms 控制項,2,11-1 Windows Forms 簡介,“Windows Forms” 是全新的視窗應用程式介面 “Web Forms” 與 “Web Services” 組成了全新的網際網路應用程式介面,.NET Framework 將之統稱為 “ASP.NET” 查看 System.Windows.Forms 命名空間提供的成員,3,11-2 設計階段的表單,11-2-1 建立表單 啟動 Visual Studio.NET。 點取 起始頁 的 新增專案 按鈕。 選擇 Visual C# 專案 類型及 Windows 應用程式

2、 範本,並輸入專案的名稱,然後按 確定。,4,11-2-2 設定表單的格線大小,格線可以幫助我們對齊控制項,如果要設定格線的大小或顯示與否,可以選取 工具 選項,然後點取 Windows Forms 設計工具,再依照下圖操作:,5,11-2-3 設定起始表單,在方案總管內的專案名稱按一下滑鼠右鍵,然後從快顯功能表中選取 屬性,再於如下對話方塊的 啟始物件 欄位選取該表單:,6,11-2-4 設定表單的屬性,只要選取表單,屬性視窗就會列出被選取之表單的常用屬性供您查看或修改,7,表單的外觀屬性 BackColor BackgroundImage Cursor ForeColor RightToL

3、eft Text Font FormBorderStyle,表單的視窗樣式屬性 ControlBox HelpButton Icon IsMdiContainer MaximizeBox Menu MaximizeBox Opacity ShowInTaskBar SizeGripStyle TopMost TransparencyKey,8,表單的配置方式:單一文件介面(SDI) 、多重文件介面(MDI) 、總管樣式介面(Explorer-Style Interface) 表單的配置屬性 AutoScale AutoScroll AutoScrollMargin AutoScrollMinS

4、ize DockPadding Location MaximumSize MinimumSize Size StartPosition WindowState,表單的行為屬性 AllowDrop ContextMenu Enabled ImeMode 表單的其他屬性 AcceptButton CancelButton KeyPreview Name,9,10,執行階段的表單就像所有物件,會有建立與終止的時候。 相關的控制項事件:,11-3 執行階段的表單,11,11-4-1 TextBox (文字方塊) 文字方塊的外觀屬性 BackColor BorderStyle Cursor Font F

5、oreColor Lines RightToLeft ScrollBars Text TextAlign,11-4 文字編輯控制項,12,文字方塊的行為屬性 AcceptsReturn AcceptsTab AllowDrop AutoSize CharacterCasting ContextMenu Enabled HideSelection ImeMode MaxLength Multiline PasswordChar ReadOnly TabIndex TabStop Visible WordWrap,文字方塊的配置屬性 Anchor Dock Location Size Name,1

6、3,11-4-2 RichTextBox,RichTextBox 的屬性 AutoWordSelection BulletIndent DetectUrls ShowSelectionMarginZoomFactor,14,RichTextBox 的格式化屬性 SelectedText SelectionAlignment SelectionBullet SelectionCharOffset SelectionColor SelectionFont SelectionIndent SelectionHangingIndent SelectionRightIndent SelectionSta

7、rt SelectionLength SelectionProtected SelectionType SelectionTabs,15,private void richTextBox1_Enter(object sender, System.EventArgs e) richTextBox1.SelectionBullet = true;richTextBox1.BulletIndent = 10; private void richTextBox2_Enter(object sender, System.EventArgs e) richTextBox2.SelectionHanging

8、Indent = 15; ,16,11-5-1 Button (按鈕) Button 的外觀屬性 BackColor BackgroundImage Cursor FlatStyle Font ForeColor Image ImageAlign ImageIndex ImageList RightToLeft Text TextAlign,11-5 命令控制項,17,Button 的行為屬性 AllowDrop ContextMenu DialogResult Enabled TabIndex TabStop Visible Name,18,11-5-2 NotifyIcon (告知圖示),

9、比較重要的屬性有 Icon 、Visible 、Text,19,11-5-3 ToolBar (工具列),ToolBar 的外觀屬性 BorderStyle ButtonSize Divider DropDownArrows Font TextAlign,20,Toolbar 的行為屬性 AllowDrop Appearance AutoSize Buttons ContextMenu Enabled ImageList ShowToolTips TabIndex TabStop Visible Wrappable Name,21,22,讓程式根據使用者點取第幾個工具列按鈕,秀出對應的對話方塊

10、:,private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) switch(toolBar1.Buttons.IndexOf(e.Button)case 0:MessageBox.Show(“您按下的是第一個按鈕“); break;case 1:MessageBox.Show(“您按下的是第二個按鈕“);break;case 2:MessageBox.Show(“您按下的是第三個按鈕“);break;case 3:MessageBox.Show(“您按

11、下的是第四個按鈕“); break;case 4:MessageBox.Show(“您按下的是第五個按鈕“);break; ,23,11-6 文字顯示控制項,11-6-1 Label (標籤) Label 的外觀屬性 BackColor BorderStyle Cursor FlatStyle ForeColor Font Image ImageAlign ImageIndex ImageList RightToLeft Text TextAlign UseMnemonic,24,Label 的行為屬性 AllowDrop AutoSize ContextMenu Enabled TabInd

12、ex Visible Name,第一個 Label 將第一個 TextBox 的快速存取鍵設定為 Alt + 1,也就是當使用者按下 Alt 鍵加上第一個 Label 內有底線的字元 1 時,就會將焦點移至第一個 TextBox;同理,第二個 Label 將第二個 TextBox 的快速存取鍵設定為 Alt + 2。,25,11-6-2 LinkLabel (超連結標籤),ActiveLinkColor DiabledLinkColor LinkColor LinkVisited VisitedLinkColor LinkBehavior LinkArea,26,private void li

13、nkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e) / 將 LinkVisited 屬性設定為 true, 將使藍色的超連結變成已瀏覽的超連結色彩紫色linkLabel1.LinkVisited = true;/ 呼叫 Process.Start() 方法以預設的瀏覽器開啟指定的網址System.Diagnostics.Process.Start(“http:/ private void linkLabel2_LinkClicked(object sender, Sy

14、stem.Windows.Forms.LinkLabelLinkClickedEventArgs e) Form Form2 = new Form(); / 建立另一個表單Form2.Show(); / 呼叫 Show() 方法將表單顯示出來linkLabel2.LinkVisited = true; ,27,11-6-3 StatusBar (狀態列),StatusBar 的外觀屬性 Cursor Font Panels RightToLeft SizingGrip Text,StatusBar 的行為屬性 AllowDrop ContextMenu Enabled ShowPanels T

15、abIndex TabStop Visible Name,28,private void statusBar1_PanelClick(object sender, System.Windows.Forms.StatusBarPanelClickEventArgs e) switch(statusBar1.Panels.IndexOf(e.StatusBarPanel)case 0:MessageBox.Show(“您點取的是第一個面板“); /您可以在此定義其它動作break;case 1:MessageBox.Show(“您點取的是第二個面板“); /您可以在此定義其它動作break; ,2

16、9,11-7 圖片控制項,11-7-1 PictureBox (圖片方塊)PictureBox 的外觀屬性 BackColor Background Image BorderStyle Cursor Image PictureBox 的行為屬性 ContextMenu Enabled SizeMode Visible Name,30,private void pictureBox1_Click(object sender, System.EventArgs e) pictureBox3.Visible = true; / 顯示第三個 PictureBox 控制項的圖片 ,private voi

17、d pictureBox2_Click(object sender, System.EventArgs e) pictureBox3.Visible = false; / 不顯示第三個 PictureBox 控制項的圖片 ,31,11-7-2 ImageList (圖片清單),ImageList 控制項可以用來儲存圖片清單,以便稍後經由其它控制項顯示,例如 ToolBar、ListView、TreeView、TabControl、Button、CheckBox、RadioButton 和 Label 等控制項都可以搭配 ImageList 控制項來儲存圖片清單。,32,11-8 清單控制項,1

18、1-8-1 CheckBox (核取方塊) 屬性 Appearance CheckAlign Checked CheckState AutoCheck ThreeState,33,private void button1_Click(object sender, System.EventArgs e) string Prefer = “您喜愛的早餐有 “;if (checkBox1.Checked = true) Prefer = Prefer + “三明治 “;if (checkBox2.Checked = true) Prefer = Prefer + “潛水艇 “;if (checkBo

19、x3.Checked = true) Prefer = Prefer + “燒餅 “;if (checkBox4.Checked = true) Prefer = Prefer + “飯糰 “;if (checkBox5.Checked = true) Prefer = Prefer + “羅蔔糕 “;MessageBox.Show(Prefer); ,34,11-8-2 RadioButton (選項按鈕),private void button1_Click(object sender, System.EventArgs e) string Prefer = “您喜愛的早餐有 “;if (

20、radioButton1.Checked = true) Prefer = Prefer + “三明治 “;if (radioButton2.Checked = true) Prefer = Prefer + “潛水艇 “;if (radioButton3.Checked = true) Prefer = Prefer + “燒餅 “;if (radioButton4.Checked = true) Prefer = Prefer + “飯糰 “;if (radioButton5.Checked = true) Prefer = Prefer + “羅蔔糕 “;MessageBox.Show(

21、Prefer); ,35,11-8-3 ListBox (清單方塊),屬性 ColumnWidth DrawMode HorizontalExtent HorizontalScrollBar IntegralHeight ItemHeight MultiColumn ScrollAlwaysVisible SelectedIndex SelectedIndices SelectedItem SelectedItems SelectionMode Sorted,36,private void button1_Click(object sender, System.EventArgs e) Mes

22、sageBox.Show(“這個清單方塊內總共有“ + listBox1.Items.Count + “個項目n“ + “您所選擇的最高學歷為“ + listBox1.SelectedItem); ,37,private void button1_Click(object sender, System.EventArgs e) string Msg = “這個清單方塊內總共有“ + listBox1.Items.Count + “個項目n“ + “您所選擇的休閒活動為“;if (listBox1.SelectedItems.Count != 0) for(int i = 0; i listBo

23、x1.SelectedItems.Count; i+)Msg = Msg + listBox1.SelectedItemsi.ToString() + “ “;MessageBox.Show(Msg); ,38,11-8-4 CheckedListBox (核取清單方塊),private void button1_Click(object sender, System.EventArgs e) string Msg = “這個清單方塊內總共有“ + checkedListBox1.Items.Count + “個項目n“ + “您所選擇的休閒活動為“;if (checkedListBox1.C

24、heckedItems.Count != 0) for(int i = 0; i checkedListBox1.CheckedItems.Count; i+)Msg = Msg + checkedListBox1.CheckedItemsi.ToString() + “ “;MessageBox.Show(Msg); ,39,11-8-5 ComboBox (下拉式清單),private void button1_Click(object sender, System.EventArgs e) MessageBox.Show(“這個清單方塊內總共有“ + comboBox1.Items.Co

25、unt + “個項目n“ +“您所選擇的年薪為“ + comboBox1.SelectedItem); ,40,11-8-6 DomainUpDown,private void button1_Click(object sender, System.EventArgs e) MessageBox.Show(“這個清單方塊內總共有“ + domainUpDown1.Items.Count + “個項目n“ + “您所選擇的年薪為“ + domainUpDown1.SelectedItem); ,41,11-8-7 NumericUpDown,private void button1_Click(

26、object sender, System.EventArgs e) MessageBox.Show(“您輸入的年齡為“ + numericUpDown1.Value); ,42,11-8-8 ListView (清單檢視),屬性CheckBoxes、CheckedIndices、CheckedItems、FullRowSelect、GridLines、View、Activation、AllowColumnReorder、AutoArrange、Columns、HeaderStyle、HideSelection、HoverSelection、Items、LabelEdit、LabelWrap、

27、LargeImageList、MultiSelectScrollable、SmallImageList、Sorting、StateImageList,43,設計一個 ListView 控制項,使其大型圖示、小型圖示、清單、詳細資料等四種檢視模式如下所示:,44,11-8-9 TreeView (樹狀檢視),屬性CheckBoxes、ItemHeight、FullRowSelect、HideSelection、HotTracking、Indent、LabelEdit、Nodes、PathSeparator、Scrollable、ShowLines、ShowPlusMinus、ShowRootLi

28、nes、Sorted,45,46,11-9 功能表控制項,11-9-1 MainMenu (主功能表) MainMenu 控制項可以用來在表單上插入主功能表,如下圖所示:,47,11-9-2 ContextMenu (快顯功能表),48,11-10-1 FontDialog (字型對話方塊) 屬性AllowSimulations、AllowVectorFonts、AllowVerticalFonts、Color、Font、FontMustExist、MaxSize、MinSize、ScriptsOnly、ShowApply、ShowColor、ShowEffects、ShowHelp,11-1

29、0 其它控制項,49,private void button1_Click(object sender, System.EventArgs e) fontDialog1.ShowDialog(); / 呼叫 ShowDialog() 方法來顯示對話方塊label1.Font = fontDialog1.Font; / 將標籤的字型設定為選取的字型label1.ForeColor = fontDialog1.Color; / 將標籤的前景色彩設定為選取的色彩 ,50,屬性 AllowFullOpen AnyColor Color FullOpen ShowHelp SolidColorOnly,

30、11-10-2 ColorDialog (色彩對話方塊),51,請在表單上放置按鈕、文字方塊和色彩對話方塊,使其執行結果如下:,52,11-10-3 TrackBar (滑動軸),屬性 Orientation LargeChange SmallChange TickFrequency TickStyle Maximum Minimum Value,53,請設計一個表單,使其執行結果如下:,private void button1_Click(object sender, System.EventArgs e) MessageBox.Show(“您設定的音量大小為“ + trackBar1.Va

31、lue); ,54,屬性 Format ShowCheckBox ShowUpDown MaxDate MinDate Value,11-10-4 DateTimePicker (日期時間清單),55,請設計一個表單,使其執行結果如下,56,11-10-5 MonthCalendar (月曆) 屬性CalendarDimentions、TitleBackcolor、TitleForecolor、TrailingForeColor、FirstDayOfWeek、MaxDate、MinDate、MaxSelectionCount、MonthlyBoldDates、ScrollChange、Sele

32、ctionEnd、SelectionStart、SelectionRange、ShowToday、ShowTodayCircle、ShowWeekNumbers、TodayDate,57,請設計如下表單,使其執行結果如下,58,11-10-6 GroupBox (群組方塊),請設計如右表單,內有兩個 GroupBox 控制項,而 GroupBox 控制項裡面又有 Label、RadioBox、TextBox、CheckBox 等控制項。,59,11-10-7 Panel (面板),60,11-10-8 TabControl (標籤),屬性 ItemSize Multiline Padding

33、ShowToolTips TabCount TabPages,61,請設計如下表單,使其 TabControl 控制項有 6 個標籤:,62,11-10-9 Timer (計時器),Interval 屬性 Enabled 屬性 Tick 事件處理程序 Start() 方法 Stop() 方法,63,11-10-10 ProgressBar (進度列),屬性 Maximum Minimum Step Value,64,請設計如右表單,進度列的最大值為 100、最小值為 0,每隔 3 秒鐘會自動增量 10,而多顯示一個藍色矩形,直到藍色矩形填滿整個進度列。,65,11-10-11 SaveFile

34、Dialog (儲存檔案 對話方塊),屬性AddExtension、CheckFileExists、CheckPathExists、CreatePrompt、DefaultExt、DereferenceLinks、FileName、FileNames、Filter、FilterIndex、InitialDirectory、OverwritePrompt、RestoreDirectory、ShowReadOnly、ShowHelp、Title、ValidateNames,66,請設計一個表單,使其執行結果如下:,67,11-10-12 OpenFileDialog (開啟舊檔 對話方塊),68,

35、11-11-1 建立 Graphics 物件 方式一 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) Graphics MyGraphics = e.Graphics;/接下來可以撰寫進行繪圖的程式碼 方式二 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) Graphics MyGraphics = this.CreateGraphics();/接下來可以撰寫進行繪圖的程式碼

36、,11-11 Windows Forms 的 GDI+ 繪圖,69,11-11-2 建立色彩、畫筆與筆刷,建立色彩Dim MyColor as Color : MyColor = Color.FromArgb(0, 255, 0) 建立筆刷,類型如下: SolidBrush HatchBrush TextureBrush LinearGradientBrush PathGradientBrush,70,11-11-3 繪製線條與形狀,Point 結構和 Rectangle 結構 Point MyPoint = new Point(x1, y1), new Point(x2, y2), new

37、Point(x3, y3).; Rectangle MyRec = new Rectangle(x1, y1, x2, y2);方法 DrawBezier(pen, point1, point2, point3, point4) DrawCurve(pen, point() DrawClosedCurve(pen, point() DrawEllipse(pen, x, y, width, height) DrawLine(pen, x1, y1, x2, y2) DrawPolygon(pen, point() DrawRectangle(pen, x1, y1, x2, y2) DrawA

38、rc(pen, x, y, width, height, startAngle, sweepAngle),71,DrawPie(pen, x, y, width, height, startAngle, sweepAngle) FillClosedCurve(brush, point() FillEllipse(brush, x, y, width, height) FillPie(brush, x, y, width, height, startAngle, sweepAngle) FillPolygon(brush, point() FillRectangle(brush, x1, y1,

39、 x2, y2) ResetTransform() RotateTransform(angle) ScaleTransform(sw, sh) TranslateTransform (dx, dy) Clear(color) Dispose() Save(),72,請依照指示在表單上繪製如下的線條或形狀,裡面定義了三種畫筆,色彩分別為 Red、Blue、Green,寬度分別為 1、3、5 個像素點:,73,請依照指示將前一個隨堂練習的封閉圖形填滿色彩:,74,請依照指示在表單上繪製如下圖形,前者是繪製 10 個圓形,每個圓形之間位移 10 像素點,後者是繪製 36 個矩形,每個矩形之間旋轉 1

40、0 度:,75,11-11-4 繪製文字,首先,建立 Graphices 物件。 接著,建立用來繪製文字的筆刷。 繼續,建立用來繪製文字的字型,例如: Font MyFont = new Font(“標楷體”, 24, FontStyle.Italic); 呼叫 Graphics 物件提供的 DrawString() 方法 繪製文字,例如 : MyGraphics.DrawString(“VC#.NET程式設計“, MyFont, MyBrush, 10, 10);,76,將前面的四個步驟整合在一起,可以得到如下結果:,private void Form1_Paint(object sende

41、r, System.Windows.Forms.PaintEventArgs e) Graphics MyGraphics = e.Graphics;LinearGradientBrush MyBrush =new LinearGradientBrush(ClientRectangle, Color.Red, Color.Yellow, LinearGradientMode.Horizontal);Font MyFont = new Font(“標楷體“, 20, FontStyle.Italic);MyGraphics.DrawString(“VC#.NET程式設計“, MyFont, My

42、Brush, 10, 10); ,77,11-11-5 顯示圖形影像,首先,建立 Graphices 物件。 接著,建立用來表示所要顯示之圖形影像的物件,如: Bitmap MyBitmap = new Bitmap(“img1.bmp“); 呼叫Graphics物件提供的DrawImage() 方法繪製文字,例如 : MyGraphics.DrawImage(MyBitmap, 10, 10);,78,將前面的三個步驟整合在一起,可以得到如下結果:,private void Form1_Paint(object sender, System.Windows.Forms.PaintEventA

43、rgs e) Graphics MyGraphics = e.Graphics;Bitmap MyBitmap = new Bitmap(“img1.bmp“);MyGraphics.DrawImage(MyBitmap, 10, 10); ,79,11-12 Windows Forms的列印支援,80,1.,2.,3. 將 PrintPreviewDialog1、PrintDialog1 的 Document 屬性設定為 PrintDocument1 ,表示要進行預覽及列印的文件為 PrintDocument1。,81,4. 撰寫 PrintDocument1 的 PrintPage 事件處

44、理程序,private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) Graphics MyGraphics = e.Graphics;SolidBrush MyBrush = new SolidBrush(Color.Black);Font MyFont = new Font(“新細明體“, 12);MyGraphics.DrawString(textBox1.Text, MyFont, MyBrush, 10, 10); ,5. 撰寫 預覽列印 按鈕的 C

45、lick 事件處理程序,private void button1_Click(object sender, System.EventArgs e) printPreviewDialog1.ShowDialog(); ,82,6. 在程式碼視窗中撰寫 列印 按鈕 (Button2) 的 Click 事件處理程序,private void button2_Click(object sender, System.EventArgs e) DialogResult Result = printDialog1.ShowDialog();if (Result = DialogResult.OK) printDocument1.Print(); ,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 教学课件 > 大学教育

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1