資源簡介
這個程序是用C#編寫的,用GDI+繪制一個動態的二維平面坐標,用戶能通過輸入X軸和Y軸的最小刻度來控制平面坐標的大小,并通過產生一個隨機數波形,來動態顯示平面坐標。
希望這個小程序能對學習使用C#GDI+繪圖的朋友帶來幫助。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Drawing;
using?System.Windows.Forms;
namespace?DrawPlane
{
????class?Draw
????{
????????public?Bitmap?mybitmap;//用于雙緩沖的位圖,和畫布等大
????????Random?rm?=?new?Random();
????????public?void?DrawLineS(Color?color?float?Xmark?float?Ymark?PictureBox?picboxPoint[]?ptlist)
????????{
????????????mybitmap?=?new?Bitmap(picbox.Width?picbox.Height);//設定位圖大小
????????????Graphics?doublebufferg?=?Graphics.FromImage(mybitmap);//從位圖上獲取畫布
????????????doublebufferg.Clear(Color.White);//用背景色刷新
????????????//pictureBox1填充為白色,便于顯示圖像?500*300
????????????Rectangle?rect?=?new?Rectangle(0?0?picbox.Width?picbox.Height);
????????????doublebufferg.FillRectangle(new?SolidBrush(Color.White)?rect);
????????????//畫X軸和Y軸
????????????DrawXY(ref?doublebufferg?picbox);
????????????//X軸上的刻度
????????????SetXAxis(ref?doublebufferg?picbox?Xmark);
????????????//Y軸上的刻度
????????????SetYAxis(ref?doublebufferg?picbox?Ymark);
????????????//繪制實時隨機曲線
????????????Point?temp?=?new?Point();
????????????for?(int?j?=?0;?j?????????????{
????????????????temp?=?ptlist[j?+?1];
????????????????ptlist[j]?=?new?Point(temp.X?-?5?temp.Y);
????????????}
????????????Point?lastpt?=?new?Point();
????????????lastpt.X?=?picbox.Width;
????????????lastpt.Y?=?rm.Next(DateTime.Now.Millisecond)?%?picbox.Height;
????????????ptlist[picbox.Width?/?5?-1]?=?lastpt;
????????????doublebufferg.DrawLines(new?Pen(Color.Red2)?ptlist);
????????????//將緩沖中的位圖繪制到窗體上
????????????Graphics?g1?=?picbox.CreateGraphics();//創建?PictureBox窗體的畫布
????????????g1.Clear(Color.White);
????????????g1.DrawImage(mybitmap?0?0);
????????}
????????//畫X軸和Y軸的基本部分,包括間隔、方向箭頭
????????public?void?DrawXY(ref?Graphics?g?PictureBox?picbox)
????????{
????????????Pen?pen?=?new?Pen(Color.Green?2);//畫筆
????????????SolidBrush?sb?=?new?SolidBrush(Color.Green);//畫刷
????????????//X軸上的方向箭頭,實際上是繪制了一個三角形
????????????Point[]?xpts?=?new?Point[3]{
????????????????new?Point(picbox.Width-35picbox.Height-32)
????????????????new?Point(picbox.Width-35picbox.Height-28)
????????????????new?Point(picbox.Width-30picbox.Height-30)
???????????????????????????????????????};
????????????g.DrawLine(pen?30?picbox.Height?-?30?picbox.Width?-?30?picbox.Height?-?30);//繪制X坐標軸
????????????g.DrawPolygon(penxpts);//繪制X軸的方向箭頭
????????????g.DrawString(“X軸“?new?Font(“宋體“?9)?sb?picbox.Width?-?25?picbox.Height?-?35);//標注X軸
????????????//Y軸的箭頭,實際上是繪制了一個三角形
????????????Point[]?ypts?=?new?Point[3]{
??????????????????new?Point(2835)
??????????????????new?Point(3030)
??????????????????new?Point(3235)???};
????????????g.DrawLine(pen?30?picbox.Height?-?30?30?30);
????????????g.DrawPolygon(pen?ypts);
????????????g.DrawString(“Y軸“?new?Font(“宋體“?9)?sb?20?10);
????????}
????????//繪制平行Y軸的豎線,相當于X軸上的刻度
????????public?void?SetXAxis(ref?Graphics?g?PictureBox?picbox
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????12800??2011-03-27?11:37??DrawPlane\DrawPlane\bin\Debug\DrawPlane.exe
?????文件??????30208??2011-03-27?11:37??DrawPlane\DrawPlane\bin\Debug\DrawPlane.pdb
?????文件??????14328??2011-03-27?11:45??DrawPlane\DrawPlane\bin\Debug\DrawPlane.vshost.exe
?????文件????????490??2007-07-21?01:33??DrawPlane\DrawPlane\bin\Debug\DrawPlane.vshost.exe.manifest
?????文件???????5521??2011-03-27?11:37??DrawPlane\DrawPlane\Draw.cs
?????文件???????3764??2011-03-23?10:22??DrawPlane\DrawPlane\DrawPlane.csproj
?????文件???????3020??2011-03-27?11:22??DrawPlane\DrawPlane\Form1.cs
?????文件???????5867??2011-03-27?11:06??DrawPlane\DrawPlane\Form1.Designer.cs
?????文件???????6008??2011-03-27?11:06??DrawPlane\DrawPlane\Form1.resx
?????文件????????618??2011-03-27?11:45??DrawPlane\DrawPlane\obj\Debug\DrawPlane.csproj.FileListAbsolute.txt
?????文件????????847??2011-03-27?11:06??DrawPlane\DrawPlane\obj\Debug\DrawPlane.csproj.GenerateResource.Cache
?????文件??????12800??2011-03-27?11:37??DrawPlane\DrawPlane\obj\Debug\DrawPlane.exe
?????文件????????180??2011-03-27?11:06??DrawPlane\DrawPlane\obj\Debug\DrawPlane.Form1.resources
?????文件??????30208??2011-03-27?11:37??DrawPlane\DrawPlane\obj\Debug\DrawPlane.pdb
?????文件????????180??2011-03-23?10:22??DrawPlane\DrawPlane\obj\Debug\DrawPlane.Properties.Resources.resources
?????文件????????490??2011-03-23?09:42??DrawPlane\DrawPlane\Program.cs
?????文件???????1374??2011-03-23?09:42??DrawPlane\DrawPlane\Properties\AssemblyInfo.cs
?????文件???????2868??2011-03-23?09:42??DrawPlane\DrawPlane\Properties\Resources.Designer.cs
?????文件???????5612??2011-03-23?09:42??DrawPlane\DrawPlane\Properties\Resources.resx
?????文件???????1094??2011-03-23?09:42??DrawPlane\DrawPlane\Properties\Settings.Designer.cs
?????文件????????249??2011-03-23?09:42??DrawPlane\DrawPlane\Properties\Settings.settings
?????文件????????917??2011-03-23?09:42??DrawPlane\DrawPlane.sln
????..A..H.?????15872??2011-03-27?12:03??DrawPlane\DrawPlane.suo
?????目錄??????????0??2011-03-23?12:33??DrawPlane\DrawPlane\obj\Debug\Refactor
?????目錄??????????0??2011-03-23?09:42??DrawPlane\DrawPlane\obj\Debug\TempPE
?????目錄??????????0??2011-03-23?10:22??DrawPlane\DrawPlane\bin\Debug
?????目錄??????????0??2011-03-27?11:37??DrawPlane\DrawPlane\obj\Debug
?????目錄??????????0??2011-03-23?09:42??DrawPlane\DrawPlane\bin
?????目錄??????????0??2011-03-23?09:42??DrawPlane\DrawPlane\obj
?????目錄??????????0??2011-03-23?09:42??DrawPlane\DrawPlane\Properties
............此處省略5個文件信息
- 上一篇:獲取鼠標所在窗口的句柄、類名c#
- 下一篇:C#和西門子PLC1200 以太網通訊
評論
共有 條評論