C#程式設計學習(04):基本操作學習總結
一、對話方塊窗體的設計
(1)修改exe圖示:專案-->右鍵-->屬性-->應用程式-->圖示和清單-->圖示,選擇要新增的圖示
(2)修改對話方塊圖示: 點選對話方塊 --> 屬性 --> ICON
(3)固定對話方塊大小:點選對話方塊 --> 屬性 -->FormBorderStyle: FixedDialog
(4)最大化、最小化按鈕不可用:MinimizeBox: False; MaximizeBox: False
(5)修改標題:Text
二: 檔案文字的處理
(1)對於包含不確定數目空格的文字檔案讀入
首先新增引用:using System.Text.RegularExpressions;
忽略空格進行字串分割:string[] substrtmp = Regex.Split(nextLine, "\\s+", RegexOptions.IgnoreCase);
(2)彈出檔案選擇對話方塊
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
inputPath = ofd.FileName;
(3)開啟檔案度資料
System.IO.StreamReader inputFile = System.IO.File.OpenText(inputPath);
string firstLine = inputFile.ReadLine();
inputFile.Close();
//釋放物件
inputFile.Dispose();
(4)遍歷檔案
string nextLine;
while ((nextLine = inputFile.ReadLine()) != null)
{
//對資料及對檔案的操作
}
(5)寫檔案
System.IO.StreamWriter outputFile = System.IO.File.OpenText(inputPath);
三、繪圖API的使用
(1)根據寬度和高度建立點陣圖
System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);
(2)建立Graphics類物件
Graphics g = Graphics.FromImage(image);
(3)清空圖片背景色
g.Clear(Color.White);
(4)指定字型
Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
(5)繪製矩形
Point pt_leftUp = new Point(30, 30);
g.DrawRectangle(畫筆物件, 左上點X,左上點Y,寬度,高度);
示例:
g.DrawRectangle(new Pen(Color.Black), pt_leftUp.X, pt_leftUp.Y, image.Width - pt_leftUp.X * 2, image.Height - pt_leftUp.Y * 2);
(6) 繪製橢圓
DrawEllipse(畫筆,左上點x,左上點y,寬度,高度);
(7)繪製字串
DrawString(要標註的字串,字型,畫刷,x座標值,y座標值);
(8)填充橢圓
g.FillEllipse(new SolidBrush(item.Color), stlPoint.X - stlSize / 2, stlPoint.Y - stlSize / 2, stlSize, stlSize);