画图软件C程序代码
绘图程序代码,用C#实现

一个很好的绘图程序代码,用C#实现。
using System;using System.IO;//用于文件存取using System.Data;//用于数据访问using System.Drawing;//提供画GDI+图形的基本功能using System.Drawing.Text;//提供画GDI+图形的高级功能using System.Drawing.Drawing2D;//提供画高级二维,矢量图形功能using System.Drawing.Imaging;//提供画GDI+图形的高级功能namespace Drawpicture{/// <summary>/// BarChart 的摘要说明。
/// </summary>public class BarChart{private int Pic_Width = 410;private int Pic_Height = 320;public static bool hasNeg = false;private const string FontWord = \"Arial\";private const int FontSize = 9;public int SIDE_WIDTH = 400;public int SIDE_HEIGHT = 400;private const int CHART_TOP = 60;private int CHART_HEIGHT = 300;private const int CHART_LEFT = 60;private const int CHART_WIDTH = 300;public void Render(string[] word,int[] data, out string fileName) {for(int i=0;i<data.Length;i++){if(data[i]<0){Pic_Height *=2;hasNeg = true;SIDE_HEIGHT = (int)(SIDE_HEIGHT*1.5);CHART_HEIGHT =(int)(CHART_HEIGHT*0.3);break;}}ChartUtil cu = new ChartUtil();PointF[] zuoBiaoStart;PointF[] zuoBiaoEnd;Graphics g; [Page]Bitmap bm;createCanvas(out bm, out g, out zuoBiaoEnd, out zuoBiaoStart,cu);//画坐标轴int chiDu = drawAxes(data, zuoBiaoStart, g, zuoBiaoEnd,cu);//画条形图float barWidth = CHART_WIDTH / (2 * 2);PointF barOrigin = new PointF(CHART_LEFT + (barWidth / 2),0);float barHeight = 2;for(int i=0;i<2;i++){barHeight = ((float)data[i]/chiDu) *(CHART_HEIGHT/5) ;if(barHeight<0){barOrigin.Y = CHART_TOP + CHART_HEIGHT;}else{barOrigin.Y = CHART_TOP + CHART_HEIGHT - barHeight;}g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)),barOrigin.X,barOrigin. Y,barWidth,Math.Abs(barHeight));barOrigin.X = barOrigin.X + (barWidth * 2);}//画右上角的说明图形drawInstruction(CHART_LEFT, CHART_HEIGHT, CHART_TOP, g, word, data);//输出图形fileName = cu.PicPath + Guid.NewGuid().ToString() + \".gif\";bm.Save(fileName, ImageFormat.Gif);//资源回收bm.Dispose();g.Dispose();}private int drawAxes(int[] data, PointF[] zuoBiaoStart, Graphics g, PointF[] zuoBiaoEnd,C hartUtil cu){int chiDu =1;int[] t = cu.getZuoBiaoValue(data,out chiDu); [Page]for(int i=0;i<zuoBiaoStart.Length;i++){PointF txtPos = new PointF();txtPos.X = zuoBiaoStart[i].X -50;txtPos.Y = zuoBiaoStart[i].Y - 5;g.DrawString(t[i].ToString(),new Font(FontWord,8),Brushes.Black,txtPos);g.DrawLine(Pens.Black,zuoBiaoStart[i],zuoBiaoEnd[i]);}return chiDu;}private void createCanvas(out Bitmap bm, out Graphics g, out PointF[] zuoBiaoEnd, ou t PointF[] zuoBiaoStart,ChartUtil cu){//建立一个Graphics对象实例bm = new Bitmap(Pic_Width,Pic_Height);g = Graphics.FromImage(bm);//设置条图图形和文字属性g.ScaleTransform((Convert.ToSingle(Pic_Width))/SIDE_WIDTH,(Convert.ToSingle(Pic_ Height))/SIDE_HEIGHT);g.SmoothingMode = SmoothingMode.Default;g.TextRenderingHint = TextRenderingHint.AntiAlias;//设定画布和边g.Clear(Color.White);g.DrawRectangle(Pens.Black,0,0,SIDE_WIDTH-1,SIDE_HEIGHT-1);//设置条形图的边g.DrawRectangle(new Pen(Color.Black,1),CHART_LEFT,CHART_TOP,CHART_WIDTH, C HART_HEIGHT);if(hasNeg){g.DrawRectangle(new Pen(Color.Black,1),CHART_LEFT,CHART_TOP+CHART_HEIGHT, CHART_WIDTH, CHART_HEIGHT);}zuoBiaoEnd = null;zuoBiaoStart = cu.getZuoBiaoPoint(new PointF(CHART_TOP,CHART_LEFT),CHART_HEI GHT,CHART_WIDTH,out zuoBiaoEnd,hasNeg);}private void drawInstruction(int CHART_LEFT, int CHART_HEIGHT, int CHART_TOP, Gr aphics g, string[] word, int[] data){PointF colsNamePoint = new PointF(CHART_LEFT + 55,CHART_HEIGHT + CHART_TO P + 2); [Page]if(hasNeg){colsNamePoint = new PointF(CHART_LEFT + 55,CHART_HEIGHT*2 + CHART_TOP + 2);}PointF boxOrigin = new PointF(Pic_Width*5/6-30,CHART_TOP*1/3);PointF textOrigin = new PointF(Pic_Width*4/5+8,CHART_TOP*1/3 -3 );for(int i=0;i<2;i++){g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)),boxOrigin.X,boxOrigin. Y,20,10);//g.DrawRectangle(Pens.Black,boxOrigin.X,boxOrigin.Y,20,10);g.DrawString(word[i],new Font(FontWord,FontSize),Brushes.DarkBlue,colsNamePoint);g.DrawString(data[i].ToString(),new Font(FontWord,FontSize),Brushes.Black,textOrigi n);colsNamePoint.X += 150;boxOrigin.Y += 15;textOrigin.Y += 15;}}}public class ChartUtil{public readonly string PicPath = System.AppDomain.CurrentDomain.BaseDirectory + \ "Temp/\";public PointF[] getZuoBiaoPoint(PointF orign,int height,int width,out PointF[] endPoint, bool hasNeg){int num = 6;if(hasNeg) num = 11;PointF[] startPoint = new PointF[num];endPoint = new PointF[num];for(int i =0;i<num;i++){PointF pfStart = new PointF();PointF pfEnd = new PointF();pfStart.X = orign.X;pfEnd.X = orign.X + width;pfStart.Y = orign.Y + i*height/5;pfEnd.Y = pfStart.Y; [Page]startPoint[i] = pfStart;endPoint[i] = pfEnd;}return startPoint;public int[] getZuoBiaoValue(int[] data,out int chidu){int[] result = new Int32[11];int Max = data[0];for(int i=0;i<data.Length;i++){if(Max < data[i])Max = data[i];}string strMax = Max + \"\";chidu =(int)((Convert.ToInt32(strMax.Substring(0,1)) +1)* Math.Pow(10,strMax.Lengt h-1))/5;for(int i=0;i<result.Length;i++){result[i] = chidu* (5-i) ;}return result;}#region get colorpublic static Color GetChartItemColor(int itemIndex){Color selectedColor;switch(itemIndex){case 0:selectedColor = Color.Blue;break;case 1:selectedColor = Color.Red;break;case 2:selectedColor = Color.Yellow;break;case 3:selectedColor = Color.Purple;break;default:selectedColor = Color.Green;break;}return selectedColor; [Page]#endregion }}。
C#绘图程序

1.[图片]曲线图效果.[代码]完整代码 DrawingCurve.cs1 2 3 4 5 6 7 8 9101112131415161718192021222324252627 using System;using System.Collections.Generic;using System.Text;using System.Drawing;using System.Data;using System.Drawing.Drawing2D;namespace SarchPMS.Business.Draw{public class DrawingCurve : DrawingChart{/// <summary>/// 画曲线图/// </summary>/// <param name="dsParameter"></param>/// <returns></returns>public override Bitmap DrawImage(DataSet dsParameter){Curve2D cuv2D = new Curve2D();cuv2D.Fit();return cuv2D.CreateImage();}}public class Curve2D{private Graphics objGraphics; //Graphics 类提供将对象绘制到显示设备的方法2829303132333435363738394041424344454647484950515253545556575859606162636465666768697071private Bitmap objBitmap; //位图对象private float fltWidth = 480; //图像宽度private float fltHeight = 248; //图像高度private float fltXSlice = 50; //X轴刻度宽度private float fltYSlice = 50; //Y轴刻度宽度private float fltYSliceValue = 20; //Y轴刻度的数值宽度private float fltYSliceBegin = 0; //Y轴刻度开始值private float fltTension = 0.5f;private string strTitle = "曲线图"; //标题private string strXAxisText = "月份"; //X轴说明文字private string strYAxisText = "万元"; //Y轴说明文字private string[] strsKeys = new string[] { "一月", "二月", "三月", "四月"private float[] fltsValues = new float[] { 20.0f, 30.0f, 50.0f, 55.4f, 21 20.0f, 30.0f, 50.0f, 55.4f, 21.6f, 12.8f }; //值private Color clrBgColor = Color.Snow; //背景色private Color clrTextColor = Color.Black; //文字颜色private Color clrBorderColor = Color.Black; //整体边框颜色private Color clrAxisColor = Color.Black; //轴线颜色private Color clrAxisTextColor = Color.Black; //轴说明文字颜色private Color clrSliceTextColor = Color.Black; //刻度文字颜色private Color clrSliceColor = Color.Black; //刻度颜色private Color[] clrsCurveColors = new Color[] { Color.Red, Color.Blue };private float fltXSpace = 100f; //图像左右距离边缘距离private float fltYSpace = 100f; //图像上下距离边缘距离private int intFontSize = 9; //字体大小号数private float fltXRotateAngle = 30f; //X轴文字旋转角度private float fltYRotateAngle = 0f; //Y轴文字旋转角度private int intCurveSize = 2; //曲线线条大小private int intFontSpace = 0; //intFontSpace 是字体大小和距离调整出来的一#region 公共属性/// <summary>/// 图像的宽度/// </summary>public float Width{set{if (value < 100){fltWidth = 100;}72737475767778798081828384858687888990919293949596979899 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115else{fltWidth = value;}}get{if (fltWidth <= 100){return 100;}else{return fltWidth;}}}/// <summary>/// 图像的高度/// </summary>public float Height{set{if (value < 100){fltHeight = 100;}else{fltHeight = value;}}get{if (fltHeight <= 100){return 100;}else{return fltHeight;}117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 }/// <summary>/// X轴刻度宽度/// </summary>public float XSlice{set { fltXSlice = value; }get { return fltXSlice; }}/// <summary>/// Y轴刻度宽度/// </summary>public float YSlice{set { fltYSlice = value; }get { return fltYSlice; }}/// <summary>/// Y轴刻度的数值宽度/// </summary>public float YSliceValue{set { fltYSliceValue = value; }get { return fltYSliceValue; } }/// <summary>/// Y轴刻度开始值/// </summary>public float YSliceBegin{set { fltYSliceBegin = value; }get { return fltYSliceBegin; } }/// <summary>/// 张力系数/// </summary>public float Tension{161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203{if (value < 0.0f && value > 1.0f){fltTension = 0.5f;}else{fltTension = value;}}get{return fltTension;}}/// <summary>/// 标题/// </summary>public string Title{set { strTitle = value; }get { return strTitle; }}/// <summary>/// 键,X轴数据/// </summary>public string[] Keys{set { strsKeys = value; }get { return strsKeys; }}/// <summary>/// 值,Y轴数据/// </summary>public float[] Values{set { fltsValues = value; }get { return fltsValues; }}204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 /// <summary>/// 背景色/// </summary>public Color BgColor{set { clrBgColor = value; }get { return clrBgColor; }}/// <summary>/// 文字颜色/// </summary>public Color TextColor{set { clrTextColor = value; }get { return clrTextColor; }}/// <summary>/// 整体边框颜色/// </summary>public Color BorderColor{set { clrBorderColor = value; }get { return clrBorderColor; } }/// <summary>/// 轴线颜色/// </summary>public Color AxisColor{set { clrAxisColor = value; }get { return clrAxisColor; }}/// <summary>/// X轴说明文字/// </summary>public string XAxisText{set { strXAxisText = value; }get { return strXAxisText; }}248249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 /// <summary>/// Y轴说明文字/// </summary>public string YAxisText{set { strYAxisText = value; }get { return strYAxisText; }}/// <summary>/// 轴说明文字颜色/// </summary>public Color AxisTextColor{set { clrAxisTextColor = value; }get { return clrAxisTextColor; } }/// <summary>/// 刻度文字颜色/// </summary>public Color SliceTextColor{set { clrSliceTextColor = value; }get { return clrSliceTextColor; } }/// <summary>/// 刻度颜色/// </summary>public Color SliceColor{set { clrSliceColor = value; }get { return clrSliceColor; }}/// <summary>/// 曲线颜色/// </summary>public Color[] CurveColors{set { clrsCurveColors = value; }get { return clrsCurveColors; }292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 }/// <summary>/// X轴文字旋转角度/// </summary>public float XRotateAngle{get { return fltXRotateAngle; }set { fltXRotateAngle = value; } }/// <summary>/// Y轴文字旋转角度/// </summary>public float YRotateAngle{get { return fltYRotateAngle; }set { fltYRotateAngle = value; } }/// <summary>/// 图像左右距离边缘距离/// </summary>public float XSpace{get { return fltXSpace; }set { fltXSpace = value; }}/// <summary>/// 图像上下距离边缘距离/// </summary>public float YSpace{get { return fltYSpace; }set { fltYSpace = value; }}/// <summary>/// 字体大小号数/// </summary>public int FontSize{get { return intFontSize; }336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379set { intFontSize = value; }}/// <summary>/// 曲线线条大小/// </summary>public int CurveSize{get { return intCurveSize; }set { intCurveSize = value; }}#endregion/// <summary>/// 自动根据参数调整图像大小/// </summary>public void Fit(){//计算字体距离intFontSpace = FontSize + 5;//计算图像边距float fltSpace = Math.Min(Width / 6, Height / 6);XSpace = fltSpace;YSpace = fltSpace;//计算X轴刻度宽度XSlice = (Width - 2 * XSpace) / (Keys.Length - 1);//计算Y轴刻度宽度和Y轴刻度开始值float fltMinValue = 0;float fltMaxValue = 0;for (int i = 0; i < Values.Length; i++){if (Values[i] < fltMinValue){fltMinValue = Values[i];}else if (Values[i] > fltMaxValue){fltMaxValue = Values[i];}}if (YSliceBegin > fltMinValue){YSliceBegin = fltMinValue;380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423}int intYSliceCount = (int)(fltMaxValue / YSliceValue);if (fltMaxValue % YSliceValue != 0){intYSliceCount++;}YSlice = (Height - 2 * YSpace) / intYSliceCount;}/// <summary>/// 生成图像并返回bmp图像对象/// </summary>/// <returns></returns>public Bitmap CreateImage(){InitializeGraph();int intKeysCount = Keys.Length;int intValuesCount = Values.Length;if (intValuesCount % intKeysCount == 0){int intCurvesCount = intValuesCount / intKeysCount;for (int i = 0; i < intCurvesCount; i++){float[] fltCurrentValues = new float[intKeysCount];for (int j = 0; j < intKeysCount; j++){fltCurrentValues[j] = Values[i * intKeysCount + j];}DrawContent(ref objGraphics, fltCurrentValues, clrsCurveColor }}else{objGraphics.DrawString("发生错误,Values的长度必须是Keys的整数倍/ 2)));}return objBitmap;}/// <summary>/// 初始化和填充图像区域,画出边框,初始标题/// </summary>424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 private void InitializeGraph(){//根据给定的高度和宽度创建一个位图图像objBitmap = new Bitmap((int)Width, (int)Height);//从指定的 objBitmap 对象创建 objGraphics 对象 (即在objBitmap对象中画objGraphics = Graphics.FromImage(objBitmap);//根据给定颜色(LightGray)填充图像的矩形区域 (背景)objGraphics.DrawRectangle(new Pen(BorderColor, 1), 0, 0, Width - 1, H objGraphics.FillRectangle(new SolidBrush(BgColor), 1, 1, Width - 2, H//画X轴,注意图像的原始X轴和Y轴计算是以左上角为原点,向右和向下计算float fltX1 = XSpace;float fltY1 = Height - YSpace;float fltX2 = Width - XSpace + XSlice / 2;float fltY2 = fltY1;objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor), 1), fltX1, fl//画Y轴fltX1 = XSpace;fltY1 = Height - YSpace;fltX2 = XSpace;fltY2 = YSpace - YSlice / 2;objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor), 1), fltX1, fl//初始化轴线说明文字SetAxisText(ref objGraphics);//初始化X轴上的刻度和文字SetXAxis(ref objGraphics);//初始化Y轴上的刻度和文字SetYAxis(ref objGraphics);//初始化标题CreateTitle(ref objGraphics);}/// <summary>/// 初始化轴线说明文字/// </summary>/// <param name="objGraphics"></param>468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 private void SetAxisText(ref Graphics objGraphics){float fltX = Width - XSpace + XSlice / 2 - (XAxisText.Length - 1) * i float fltY = Height - YSpace - intFontSpace;objGraphics.DrawString(XAxisText, new Font("宋体", FontSize), new SolfltX = XSpace + 5;fltY = YSpace - YSlice / 2 - intFontSpace;for (int i = 0; i < YAxisText.Length; i++){objGraphics.DrawString(YAxisText[i].ToString(), new Font("宋体",fltY += intFontSpace; //字体上下距离}}/// <summary>/// 初始化X轴上的刻度和文字/// </summary>/// <param name="objGraphics"></param>private void SetXAxis(ref Graphics objGraphics){float fltX1 = XSpace;float fltY1 = Height - YSpace;float fltX2 = XSpace;float fltY2 = Height - YSpace;int iCount = 0;int iSliceCount = 1;float Scale = 0;float iWidth = ((Width - 2 * XSpace) / XSlice) * 50; //将要画刻度的长float fltSliceHeight = XSlice / 10; //刻度线的高度objGraphics.TranslateTransform(fltX1, fltY1); //平移图像(原点)objGraphics.RotateTransform(XRotateAngle, MatrixOrder.Prepend); //旋转objGraphics.DrawString(Keys[0].ToString(), new Font("宋体", FontSize) objGraphics.ResetTransform(); //重置图像for (int i = 0; i <= iWidth; i += 10) //以10为单位{Scale = i * XSlice / 50;//即(i / 10) * (XSlice / 5),将每个刻度分if (iCount == 5){objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor)), fltX//画网格虚线512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555Pen penDashed = new Pen(new SolidBrush(AxisColor));penDashed.DashStyle = DashStyle.Dash;objGraphics.DrawLine(penDashed, fltX1 + Scale, fltY1, fltX2 +//这里显示X轴刻度if (iSliceCount <= Keys.Length - 1){objGraphics.TranslateTransform(fltX1 + Scale, fltY1);objGraphics.RotateTransform(XRotateAngle, MatrixOrder.PreobjGraphics.DrawString(Keys[iSliceCount].ToString(), newobjGraphics.ResetTransform();}else{//超过范围,不画任何刻度文字}iCount = 0;iSliceCount++;if (fltX1 + Scale > Width - XSpace){break;}}else{objGraphics.DrawLine(new Pen(new SolidBrush(SliceColor)), flt }iCount++;}}/// <summary>/// 初始化Y轴上的刻度和文字/// </summary>/// <param name="objGraphics"></param>private void SetYAxis(ref Graphics objGraphics){float fltX1 = XSpace;float fltY1 = Height - YSpace;float fltX2 = XSpace;float fltY2 = Height - YSpace;int iCount = 0;float Scale = 0;int iSliceCount = 1;float iHeight = ((Height - 2 * YSpace) / YSlice) * 50; //将要画刻度的556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599float fltSliceWidth = YSlice / 10; //刻度线的宽度string strSliceText = string.Empty;objGraphics.TranslateTransform(XSpace - intFontSpace * YSliceBegin.To objGraphics.RotateTransform(YRotateAngle, MatrixOrder.Prepend); //旋转objGraphics.DrawString(YSliceBegin.ToString(), new Font("宋体", FontS objGraphics.ResetTransform(); //重置图像for (int i = 0; i < iHeight; i += 10){Scale = i * YSlice / 50; //即(i / 10) * (YSlice / 5),将每个刻度分if (iCount == 5){objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor)), fltX//画网格虚线Pen penDashed = new Pen(new SolidBrush(AxisColor));penDashed.DashStyle = DashStyle.Dash;objGraphics.DrawLine(penDashed, XSpace, fltY1 - Scale, Width//这里显示Y轴刻度strSliceText = Convert.ToString(YSliceValue * iSliceCount + YobjGraphics.TranslateTransform(XSpace - intFontSize * strSlicobjGraphics.RotateTransform(YRotateAngle, MatrixOrder.PrependobjGraphics.DrawString(strSliceText, new Font("宋体", FontSizobjGraphics.ResetTransform(); //重置图像iCount = 0;iSliceCount++;}else{objGraphics.DrawLine(new Pen(new SolidBrush(SliceColor)), flt }iCount++;}}/// <summary>/// 画曲线/// </summary>/// <param name="objGraphics"></param>private void DrawContent(ref Graphics objGraphics, float[] fltCurrentValu {Pen CurvePen = new Pen(clrCurrentColor, CurveSize);600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620PointF[] CurvePointF = new PointF[Keys.Length];float keys = 0;float values = 0;for (int i = 0; i < Keys.Length; i++){keys = XSlice * i + XSpace;values = (Height - YSpace) + YSliceBegin - YSlice * (fltCurrentVaCurvePointF[i] = new PointF(keys, values);}objGraphics.DrawCurve(CurvePen, CurvePointF, Tension);}/// <summary>/// 初始化标题/// </summary>/// <param name="objGraphics"></param>private void CreateTitle(ref Graphics objGraphics){objGraphics.DrawString(Title, new Font("宋体", FontSize), new SolidBru/ 2 - intFontSpace)));}}}最近需求要将数据保存到.csv文件(逗号分隔值 )中然后上传到ftp服务器上,供系统调用。
c#画图程序完整代码

C#画图程序,可以话直线,可以画圆,可以使用橡皮檫,可以新建文件,打开文件,保存文件,退出,可画矩形,可旋转。
下面为程序全整代码。
using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Drawing.Imaging;using System.IO;using System.Threading;using System.Drawing.Drawing2D;using System.Text.RegularExpressions;using System.Collections;namespace mydraw{public partial class Form1 : Form{Pen p = new Pen(Color.Black, 5);int mdb2 = 1;Point b2start = new Point(0, 0);Point b2stop = new Point(0, 0);int mdb3 = 1;Point b3start = new Point(0, 0);Point b3stop = new Point(0, 0);int mdb4 = 1;Point b4start = new Point(0, 0);Point b4stop = new Point(0, 0);int mdb10= 1;Point b10start = new Point(0, 0);Point b10stop = new Point(0, 0);public Form1(){InitializeComponent();}private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) {Application.Exit();}private void 新建图形ToolStripMenuItem_Click(object sender, EventArgs e) {pictureBox1.Refresh();}private void button1_Click(object sender, EventArgs e){if (button1.BackColor == Color.White){button1.BackColor = Color.MistyRose;button2.BackColor = Color.White;button3.BackColor = Color.White;button4.BackColor = Color.White;button5.BackColor = Color.White;button10.BackColor = Color.White;pictureBox1.Cursor = Cursors.Cross;}else{button1.BackColor = Color.White;pictureBox1.Cursor = Cursors.Default;}}private void 清除图形ToolStripMenuItem_Click(object sender, EventArgs e) {}private void button6_Click(object sender, EventArgs e){ColorDialog ColorDialog1 = new ColorDialog();ColorDialog1.AllowFullOpen = true;ColorDialog1.FullOpen = true;ColorDialog1.ShowHelp = true;ColorDialog1.Color = Color.Black;if (ColorDialog1.ShowDialog() != DialogResult.Cancel) button6.BackColor = ColorDialog1.Color;}private void button2_Click(object sender, EventArgs e){if (button2.BackColor == Color.White){button1.BackColor = Color.White;button2.BackColor = Color.MistyRose;button3.BackColor = Color.White;button4.BackColor = Color.White;button5.BackColor = Color.White;button10.BackColor = Color.White;pictureBox1.Cursor = Cursors.Cross;MessageBox.Show("我们一起来画直线吧");}else{button2.BackColor = Color.White;pictureBox1.Cursor = Cursors.Default;}}private void button3_Click(object sender, EventArgs e){if (button3.BackColor == Color.White){button1.BackColor = Color.White;button2.BackColor = Color.White;button3.BackColor = Color.MistyRose;button4.BackColor = Color.White;button5.BackColor = Color.White;button10.BackColor = Color.White;pictureBox1.Cursor = Cursors.Cross;MessageBox.Show("先选择矩形左上角的点,再选择矩形的右下角的点");}else{button3.BackColor = Color.White;pictureBox1.Cursor = Cursors.Default;}}private void button4_Click(object sender, EventArgs e){if (button4.BackColor == Color.White){button1.BackColor = Color.White;button2.BackColor = Color.White;button3.BackColor = Color.White;button4.BackColor = Color.MistyRose;button5.BackColor = Color.White;button10.BackColor = Color.White;pictureBox1.Cursor = Cursors.Cross;MessageBox.Show("先选择椭圆左上角的点,再选择椭圆的右下角的点");}else{button4.BackColor = Color.White;pictureBox1.Cursor = Cursors.Default;}}private void button5_Click(object sender, EventArgs e){if (button5.BackColor == Color.White){button1.BackColor = Color.White;button2.BackColor = Color.White;button3.BackColor = Color.White;button4.BackColor = Color.White;button10.BackColor = Color.White;pictureBox1.Cursor = Cursors.Cross;}else{button5.BackColor = Color.White;pictureBox1.Cursor = Cursors.Default;}}private void button10_Click(object sender, EventArgs e){if (button10.BackColor == Color.White){button1.BackColor = Color.White;button2.BackColor = Color.White;button3.BackColor = Color.White;button4.BackColor = Color.White;button10.BackColor = Color.MistyRose;button5.BackColor = Color.White;pictureBox1.Cursor = Cursors.Cross;MessageBox.Show("先选择圆的圆心,再选择圆上的一点");}else{button10.BackColor = Color.White;pictureBox1.Cursor = Cursors.Default;}}private void 关于作者ToolStripMenuItem_Click(object sender, EventArgs e) {MessageBox.Show("作者:林元培学号:2011301610213");}private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {//画自由线的{Graphics g = pictureBox1.CreateGraphics();if (e.Button == MouseButtons.Left){p.Color = button6.BackColor;Point start = new Point(e.X, e.Y);Point stop = new Point(e.X, e.Y - 1);Point part = new Point((int)(start.X + stop.X) / 2, (int)(start.Y + stop.Y) /2);g.DrawLine(p, start, part);g.DrawLine(p, part, start);start = stop;}}//橡皮擦if (button5.BackColor == Color.MistyRose){Graphics g = pictureBox1.CreateGraphics();if (e.Button == MouseButtons.Left){p.Color = Color.White;p.Width = 30;Point start = new Point(e.X, e.Y);Point stop = new Point(e.X, e.Y - 1);Point part = new Point((int)(start.X + stop.X) / 2, (int)(start.Y + stop.Y) /2);g.DrawLine(p, start, part);g.DrawLine(p, part, start);start = stop;//画直线}}}private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {//画两点间直线if (button2.BackColor == Color.MistyRose){if (this.mdb2 == 1){b2start.X = e.X;b2start.Y = e.Y;mdb2++;}else if (this.mdb2 == 2){b2stop.X = e.X;b2stop.Y = e.Y;mdb2--;Graphics g = pictureBox1.CreateGraphics();p.Color = button6.BackColor;g.DrawLine(p, b2start, b2stop);}}//画两点间矩形if (button3.BackColor == Color.MistyRose){if (this.mdb3 == 1){b3start.X = e.X;b3start.Y = e.Y;mdb3++;}else if (this.mdb3 == 2){b3stop.X = e.X;b3stop.Y = e.Y;mdb3--;Graphics g = pictureBox1.CreateGraphics();p.Color = button6.BackColor;if (b3stop.X - b3start.X < 0 || b3stop.Y - b3start.Y < 0) MessageBox.Show("孩子,我不是说过的吗~~~~~\n先指定左上一点", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);g.DrawRectangle(p, b3start.X, b3start.Y, b3stop.X - b3start.X, b3stop.Y - b3start.Y);}}//画两点间椭圆if (button4.BackColor == Color.MistyRose){if (this.mdb4 == 1){b4start.X = e.X;b4start.Y = e.Y;mdb4++;}else if (this.mdb4 == 2){b4stop.X = e.X;b4stop.Y = e.Y;mdb4--;Graphics g = pictureBox1.CreateGraphics();p.Color = button6.BackColor;if (b4stop.X - b4start.X < 0 || b4stop.Y - b4start.Y < 0) MessageBox.Show("小朋友,注意点的顺序!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);g.DrawEllipse(p, b4start.X, b4start.Y, b4stop.X - b4start.X, b4stop.Y - b4start.Y);}}//画圆的代码if (button10.BackColor == Color.MistyRose){if (this.mdb10 == 1){b10start.X = e.X;b10start.Y = e.Y;mdb10++;}else if (this.mdb10 == 2){b10stop.X = e.X;b10stop.Y = e.Y;mdb10--;p.Color = button6.BackColor;Graphics g = pictureBox1.CreateGraphics();int d = (int)Math.Sqrt((b10start.X - b10stop.X) * (b10start.X - b10stop.X) + (b10start.Y - b10stop.Y) * (b10start.Y - b10stop.Y));Rectangle rect = new Rectangle(b10start.X-d , b10start.Y-d, 2*d, 2*d);g.DrawEllipse(p ,rect );}}}美容养颜吧:private void button7_Click(object sender, EventArgs e){p.Width = 2;}private void button8_Click(object sender, EventArgs e){p.Width = 5;}private void button9_Click(object sender, EventArgs e){p.Width = 10;}private void 保存ToolStripMenuItem_Click(object sender, EventArgs e){SaveFileDialog sa = new SaveFileDialog();sa.Filter = "保存(*.bmp)|*.bmp";sa.FilterIndex = 2;sa.RestoreDirectory = true;if (DialogResult.OK == sa.ShowDialog()){if (pictureBox1.Image != null){Image im = this.pictureBox1.Image;Bitmap bit = new Bitmap(im);bit.Save(sa.FileName, System.Drawing.Imaging.ImageFormat.Bmp);}else{MessageBox.Show("已保存");}}}private void 旋转ToolStripMenuItem_Click(object sender, EventArgs e){}private void 度ToolStripMenuItem_Click(object sender, EventArgs e){}private void 度ToolStripMenuItem1_Click(object sender, EventArgs e){}private void 新建ToolStripMenuItem_Click(object sender, EventArgs e){}private void pictureBox1_Click(object sender, EventArgs e){}private void 关于ToolStripMenuItem_Click(object sender, EventArgs e){}private void Form1_Load(object sender, EventArgs e){}private void groupBox1_Enter(object sender, EventArgs e){}private void 缩放ToolStripMenuItem_Click(object sender, EventArgs e){}private void 放大ToolStripMenuItem_Click(object sender, EventArgs e){}private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e){MessageBox.Show("先选择矩形左上角的点,再选择矩形的右下角的点");}private void button11_Click(object sender, EventArgs e){}}}。
C语言画图函数

C语言画图函数与图形有关的函数GRAPHICS.H 库文件bar【功能】画出一条栏目。
【原型】void far bar(int left,int top,int right,int bottom)【位置】graphics.hgraphics.h【参见】bar3d,setfillstyle,rectanglebar3d【功能】画出一条3-D栏目。
【原型】void far bar3d(int left,int top,int right,int bottom,int depth,int topflag) 【位置】graphics.h【参见】barcircle【功能】以(x,y)为圆心按照指定的半径画出一个圆。
【原型】void far circle(int x,int y,int radius)【位置】graphics.h【参见】arccleardevice【功能】清除图形画面。
【原型】void far cleardevice(void)【位置】graphics.h【参见】clearviewportclearviewport【功能】清除当前可视区域。
【原型】clearviewport(void)【位置】graphics.h【参见】setviewport,cleardeviceclosegraph【功能】关闭图形系统。
【原型】void far closegraph(void)【位置】graphics.h【参见】initgraphdetecgraph【功能】通过检查硬件确定使用的图形驱动程序和模式。
【原型】void far detectgraph(int far *graphdriver,int far *graphmode)【位置】graphics.h【参见】initgraph,graphresultdrawpoly【功能】画出一个多边形的轮廓。
【原型】void far drawpoly(int numpoints,int far polypoints[])【位置】graphics.h【说明】polypoints中包括一共numpoints对数值。
c#画图程序完整代码

C#画图程序,可以话直线,可以画圆,可以使用橡皮檫,可以新建文件,打开文件,保存文件,退出,可画矩形,可旋转。
下面为程序全整代码。
using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Drawing.Imaging;using System.IO;using System.Threading;using System.Drawing.Drawing2D;using System.Text.RegularExpressions;using System.Collections;namespace mydraw{public partial class Form1 : Form{Pen p = new Pen(Color.Black, 5);int mdb2 = 1;Point b2start = new Point(0, 0);Point b2stop = new Point(0, 0);int mdb3 = 1;Point b3start = new Point(0, 0);Point b3stop = new Point(0, 0);int mdb4 = 1;Point b4start = new Point(0, 0);Point b4stop = new Point(0, 0);int mdb10= 1;Point b10start = new Point(0, 0);Point b10stop = new Point(0, 0);public Form1(){InitializeComponent();}private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) {Application.Exit();}private void 新建图形ToolStripMenuItem_Click(object sender, EventArgs e) {pictureBox1.Refresh();}private void button1_Click(object sender, EventArgs e){if (button1.BackColor == Color.White){button1.BackColor = Color.MistyRose;button2.BackColor = Color.White;button3.BackColor = Color.White;button4.BackColor = Color.White;button5.BackColor = Color.White;button10.BackColor = Color.White;pictureBox1.Cursor = Cursors.Cross;}else{button1.BackColor = Color.White;pictureBox1.Cursor = Cursors.Default;}}private void 清除图形ToolStripMenuItem_Click(object sender, EventArgs e) {}private void button6_Click(object sender, EventArgs e){ColorDialog ColorDialog1 = new ColorDialog();ColorDialog1.AllowFullOpen = true;ColorDialog1.FullOpen = true;ColorDialog1.ShowHelp = true;ColorDialog1.Color = Color.Black;if (ColorDialog1.ShowDialog() != DialogResult.Cancel) button6.BackColor = ColorDialog1.Color;}private void button2_Click(object sender, EventArgs e){if (button2.BackColor == Color.White){button1.BackColor = Color.White;button2.BackColor = Color.MistyRose;button3.BackColor = Color.White;button4.BackColor = Color.White;button5.BackColor = Color.White;button10.BackColor = Color.White;pictureBox1.Cursor = Cursors.Cross;MessageBox.Show("我们一起来画直线吧");}else{button2.BackColor = Color.White;pictureBox1.Cursor = Cursors.Default;}}private void button3_Click(object sender, EventArgs e){if (button3.BackColor == Color.White){button1.BackColor = Color.White;button2.BackColor = Color.White;button3.BackColor = Color.MistyRose;button4.BackColor = Color.White;button5.BackColor = Color.White;button10.BackColor = Color.White;pictureBox1.Cursor = Cursors.Cross;MessageBox.Show("先选择矩形左上角的点,再选择矩形的右下角的点");}else{button3.BackColor = Color.White;pictureBox1.Cursor = Cursors.Default;}}private void button4_Click(object sender, EventArgs e){if (button4.BackColor == Color.White){button1.BackColor = Color.White;button2.BackColor = Color.White;button3.BackColor = Color.White;button4.BackColor = Color.MistyRose;button5.BackColor = Color.White;button10.BackColor = Color.White;pictureBox1.Cursor = Cursors.Cross;MessageBox.Show("先选择椭圆左上角的点,再选择椭圆的右下角的点");}else{button4.BackColor = Color.White;pictureBox1.Cursor = Cursors.Default;}}private void button5_Click(object sender, EventArgs e){if (button5.BackColor == Color.White){button1.BackColor = Color.White;button2.BackColor = Color.White;button3.BackColor = Color.White;button4.BackColor = Color.White;button10.BackColor = Color.White;pictureBox1.Cursor = Cursors.Cross;}else{button5.BackColor = Color.White;pictureBox1.Cursor = Cursors.Default;}}private void button10_Click(object sender, EventArgs e){if (button10.BackColor == Color.White){button1.BackColor = Color.White;button2.BackColor = Color.White;button3.BackColor = Color.White;button4.BackColor = Color.White;button10.BackColor = Color.MistyRose;button5.BackColor = Color.White;pictureBox1.Cursor = Cursors.Cross;MessageBox.Show("先选择圆的圆心,再选择圆上的一点");}else{button10.BackColor = Color.White;pictureBox1.Cursor = Cursors.Default;}}private void 关于作者ToolStripMenuItem_Click(object sender, EventArgs e) {MessageBox.Show("作者:林元培学号:2011301610213");}private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {//画自由线的{Graphics g = pictureBox1.CreateGraphics();if (e.Button == MouseButtons.Left){p.Color = button6.BackColor;Point start = new Point(e.X, e.Y);Point stop = new Point(e.X, e.Y - 1);Point part = new Point((int)(start.X + stop.X) / 2, (int)(start.Y + stop.Y) /2);g.DrawLine(p, start, part);g.DrawLine(p, part, start);start = stop;}}//橡皮擦if (button5.BackColor == Color.MistyRose){Graphics g = pictureBox1.CreateGraphics();if (e.Button == MouseButtons.Left){p.Color = Color.White;p.Width = 30;Point start = new Point(e.X, e.Y);Point stop = new Point(e.X, e.Y - 1);Point part = new Point((int)(start.X + stop.X) / 2, (int)(start.Y + stop.Y) /2);g.DrawLine(p, start, part);g.DrawLine(p, part, start);start = stop;//画直线}}}private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {//画两点间直线if (button2.BackColor == Color.MistyRose){if (this.mdb2 == 1){b2start.X = e.X;b2start.Y = e.Y;mdb2++;}else if (this.mdb2 == 2){b2stop.X = e.X;b2stop.Y = e.Y;mdb2--;Graphics g = pictureBox1.CreateGraphics();p.Color = button6.BackColor;g.DrawLine(p, b2start, b2stop);}}//画两点间矩形if (button3.BackColor == Color.MistyRose){if (this.mdb3 == 1){b3start.X = e.X;b3start.Y = e.Y;mdb3++;}else if (this.mdb3 == 2){b3stop.X = e.X;b3stop.Y = e.Y;mdb3--;Graphics g = pictureBox1.CreateGraphics();p.Color = button6.BackColor;if (b3stop.X - b3start.X < 0 || b3stop.Y - b3start.Y < 0) MessageBox.Show("孩子,我不是说过的吗~~~~~\n先指定左上一点", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);g.DrawRectangle(p, b3start.X, b3start.Y, b3stop.X - b3start.X, b3stop.Y - b3start.Y);}}//画两点间椭圆if (button4.BackColor == Color.MistyRose){if (this.mdb4 == 1){b4start.X = e.X;b4start.Y = e.Y;mdb4++;}else if (this.mdb4 == 2){b4stop.X = e.X;b4stop.Y = e.Y;mdb4--;Graphics g = pictureBox1.CreateGraphics();p.Color = button6.BackColor;if (b4stop.X - b4start.X < 0 || b4stop.Y - b4start.Y < 0) MessageBox.Show("小朋友,注意点的顺序!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);g.DrawEllipse(p, b4start.X, b4start.Y, b4stop.X - b4start.X, b4stop.Y - b4start.Y);}}//画圆的代码if (button10.BackColor == Color.MistyRose){if (this.mdb10 == 1){b10start.X = e.X;b10start.Y = e.Y;mdb10++;}else if (this.mdb10 == 2){b10stop.X = e.X;b10stop.Y = e.Y;mdb10--;p.Color = button6.BackColor;Graphics g = pictureBox1.CreateGraphics();int d = (int)Math.Sqrt((b10start.X - b10stop.X) * (b10start.X - b10stop.X) + (b10start.Y - b10stop.Y) * (b10start.Y - b10stop.Y));Rectangle rect = new Rectangle(b10start.X-d , b10start.Y-d, 2*d, 2*d);g.DrawEllipse(p ,rect );}}}美容养颜吧:private void button7_Click(object sender, EventArgs e){p.Width = 2;}private void button8_Click(object sender, EventArgs e){p.Width = 5;}private void button9_Click(object sender, EventArgs e){p.Width = 10;}private void 保存ToolStripMenuItem_Click(object sender, EventArgs e){SaveFileDialog sa = new SaveFileDialog();sa.Filter = "保存(*.bmp)|*.bmp";sa.FilterIndex = 2;sa.RestoreDirectory = true;if (DialogResult.OK == sa.ShowDialog()){if (pictureBox1.Image != null){Image im = this.pictureBox1.Image;Bitmap bit = new Bitmap(im);bit.Save(sa.FileName, System.Drawing.Imaging.ImageFormat.Bmp);}else{MessageBox.Show("已保存");}}}private void 旋转ToolStripMenuItem_Click(object sender, EventArgs e){}private void 度ToolStripMenuItem_Click(object sender, EventArgs e){}private void 度ToolStripMenuItem1_Click(object sender, EventArgs e){}private void 新建ToolStripMenuItem_Click(object sender, EventArgs e){}private void pictureBox1_Click(object sender, EventArgs e){}private void 关于ToolStripMenuItem_Click(object sender, EventArgs e){}private void Form1_Load(object sender, EventArgs e){}private void groupBox1_Enter(object sender, EventArgs e){}private void 缩放ToolStripMenuItem_Click(object sender, EventArgs e){}private void 放大ToolStripMenuItem_Click(object sender, EventArgs e){}private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e){MessageBox.Show("先选择矩形左上角的点,再选择矩形的右下角的点");}private void button11_Click(object sender, EventArgs e){}}}。
c语言画图.

VC绘图/游戏简易教程--1:创建新项目[本期目标] 学会在VC 里面创建项目,并写简单的程序。
VC 写程序要有项目的概念,一个项目可以有多个 .cpp 文件,多个项目构成一个工作区。
先记住这两个英文单词吧:∙Workspace: 工作区∙Project: 项目现在开始创建一个新项目。
∙VC6 创建新项目请参考这个视频:/news/View.aspx?id=65∙VC2008 创建新项目请参考:/news/View.aspx?id=85∙VC2010 与VC2008 相似。
看明白后,自己动手建立项目试试,并输入以下代码:#include <stdio.h>void main(){printf("Hello World!");}尤其是之前用tc 的同学,请务必创建新项目试一试。
成功执行后,会看到屏幕上有“Hello World!”几个字符。
然后,再重新输入以下代码试试(无需理解代码含义):#include <graphics.h>#include <conio.h>void main(){initgraph(640, 480);line(200, 240, 440, 240);line(320, 120, 320, 360);getch();closegraph();}执行后应该可以看到屏幕正中央有一个十字。
看到该十字后,本节课结束。
VC绘图/游戏简易教程--2:简单绘图,学习单步执行[本期目标]学会简单绘图,并学会简单调试。
先看看上一课的代码,我加上了注释#include <graphics.h> // 绘图库头文件,绘图语句需要#include <conio.h> // 控制台输入输出头文件,getch()语句需要void main(){initgraph(640, 480); // 初始化640x480的绘图屏幕line(200, 240, 440, 240); // 画线(200,240) - (440,240)line(320, 120, 320, 360); // 画线(320,120) - (320,360)getch(); // 按任意键closegraph(); // 关闭绘图屏幕}解释一下:1. 创建的绘图屏幕640x480,表示横向有640个点,纵向有480个点。
win-tc(C语言)小游戏画图代码

/*这个打泡泡游戏感觉实在写得不怎么样,本来我想好好的写一个的,可我实在是静不下心来,为了实现游戏功能,我这里加一个语句,那里加一个语句,结果当然是当你再想调整或增加某些功能时,会变得异常繁杂,而且游戏有点小错误,我也不想改了,等哪天实在闲得没事做的时候,再好好重新做一个.做个反面教材吧!*/#include<stdio.h>#include<conio.h>#include<graphics.h>#define LEFT 0x4b00#define RIGHT 0x4d00#define SPACE 0x3920struct PUBUNIT{int x;int y;}PubUnit[20][10];struct BBDATE{int x;int y;char state;}bb[10];int dflag=1;int PubState[20][10];int HeadLine;int HeadPubCount;int AllPubCount;int key;int bbx,bby;int BBState=0;void DownPub(void);void play_game(void);DrawPub(int i,int j,int state)/*画泡泡函数*/{if(state)setcolor(GREEN);else setcolor(BLACK);circle(PubUnit[i][j].x+7,PubUnit[i][j].y+7,7);}void DrawPPman(int j)/*画泡泡人函数*/{static oldj=4;static char flag=0;line(PubUnit[19][j].x+7,PubUnit[19][j].y,PubUnit[19][j].x+7,PubUnit[19][j].y+14);line(PubUnit[19][j].x+7,PubUnit[19][j].y+2,PubUnit[19][j].x,PubUnit[19][j].y+14);line(PubUnit[19][j].x+7,PubUnit[19][j].y+2,PubUnit[19][j].x+14,PubUnit[19][j].y+14);line(PubUnit[19][j].x,PubUnit[19][j].y+14,PubUnit[19][j].x+14,PubUnit[19][j].y+14);if(flag){setcolor(BLACK);line(PubUnit[19][oldj].x+7,PubUnit[19][oldj].y,PubUnit[19][oldj].x+7,PubUnit[19][oldj].y+14); line(PubUnit[19][oldj].x+7,PubUnit[19][oldj].y+2,PubUnit[19][oldj].x,PubUnit[19][oldj].y+14);line(PubUnit[19][oldj].x+7,PubUnit[19][oldj].y+2,PubUnit[19][oldj].x+14,PubUnit[19][oldj].y+1 4);line(PubUnit[19][oldj].x,PubUnit[19][oldj].y+14,PubUnit[19][oldj].x+14,PubUnit[19][oldj].y+14) ;oldj=j;setcolor(GREEN);}else flag=1;}void GameGraph(void)/*游戏画面和各项数据初始化函数*/{int i,j;int x,y;int driver=DETECT,gm;registerbgidriver(EGA VGA_driver);initgraph(&driver,&gm,"");rectangle(99,99,251,401);x=y=100;for(i=0;i<20;i++){for(j=0;j<20;j++){PubUnit[i][j].x=x;PubUnit[i][j].y=y;x+=15;}y+=15;x=100;}for(i=0;i<5;i++) /*画50个泡泡*/for(j=0;j<10;j++){PubState[i][j]=1;DrawPub(i,j,PubState[i][j]);}DrawPPman(4);HeadLine=4;AllPubCount=50;HeadPubCount=10;}void main(void)/*主函数*/{GameGraph();play_game();}void DownPub(void)/*泡泡下降函数,每调用一次泡泡就下降一次*/{int i,j;for(i=HeadLine+1;i>0;i--)for(j=0;j<10;j++){PubState[i][j]=PubState[i-1][j];DrawPub(i,j,PubState[i][j]);}for(j=0;j<10;j++){PubState[0][j]=1;DrawPub(0,j,PubState[0][j]);}AllPubCount+=10;HeadLine++;}void play_game(void){int time=0;int j=4;while(HeadLine!=19){while(time+72>clock()){ if(kbhit()){key=bioskey(0);switch(key){case LEFT:j--;break;case RIGHT:j++;break;case SPACE:if(dflag==1) {DrawBB(j);BBState=1;dflag=0;continue ;}else continue;default:continue;}if(j==-1) {j=0;continue;}else if(j==10) {j=9;continue;}DrawPPman(j);}if(BBState)DrawBB(-1);}DownPub();time=clock(); gotoxy(1,1);}}int DrawBB(int j)/*画子弹函数*/{int i;int c;static oldj;if(j>=0){bbx=PubUnit[19][j].x+7;bby=PubUnit[19][j].y;line(bbx,bby,bbx,bby+4);oldj=j;}if(getpixel(bbx,bby-5)!=BLACK){ if(getpixel(bbx,bby-5)==WHITE);{setcolor(BLACK);line(bbx,bby,bbx,bby-4);setcolor(GREEN);}dflag=1;BBState=0;for(i=19;i>=0;i--)if(PubState[i][oldj]==1){PubState[i][oldj]=0;DrawPub(i,oldj,0);AllPubCount--;if(AllPubCount==0){cleardevice();printf("YOU ARE WIN THE GAME,GOOD BOY!");printf("\nany key to exit!\n");getch();exit(1);}if(i==HeadLine){HeadPubCount--;if(HeadPubCount==0){HeadLine--;for(c=0;c<10;c++) if(PubState[HeadLine][c]==1) HeadPubCount++;}}setcolor(BLACK);line(bbx,bby,bbx,bby-4);setcolor(GREEN);return;}}putpixel(bbx,bby-5,GREEN);putpixel(bbx,bby,BLACK);bby--;delay(500);}/*这个东西不怎么完善,后面的如果和前面位置重合,就会被覆盖,不管十字鼠标或圆,以及它们的颜色,而且如何计算圆的半径,也只是粗略大概的计算一下,结果当然不会像Windows画图那样正确,把它就当作简单的鼠标应用学习学习吧*/ #include<stdio.h>#include<conio.h>#include<dos.h>#include<graphics.h>void DrawMouse(int x,int y,int color){setcolor(color);line(x-5,y,x+5,y);line(x,y-5,x,y+5);}main(){int flag=0;int first=0;int CircleX,CircleY,CircleDX;int MouseX,MouseY,MouseButton;int driver=DETECT,gm;registerbgidriver(EGA VGA_driver);initgraph(&driver,&gm,"");while(!kbhit()){_AX=0x03;geninterrupt(0x33);MouseX=_CX;MouseY=_DX;MouseButton=_BX;DrawMouse(MouseX,MouseY,WHITE);if(MouseButton==1){flag=1;if(first==0) {CircleX=MouseX;CircleY=MouseY,first=1;}delay(5000);setcolor(BLACK);circle(CircleX,CircleY,CircleDX);}else {flag=0;first=0;}if(flag==1){setcolor(WHITE);CircleDX=abs(MouseX-CircleX)+abs(MouseY-CircleY);circle(CircleX,CircleY,CircleDX);}delay(5000);DrawMouse(MouseX,MouseY,BLACK);}}/*下面这个计算器,是我第一次写的,有很多缺陷,被我改了很多,可是还是有几个缺陷,本来为了凑界面上的按键,我增加一个求余运算,到最后发现是一个错误,因为求余运算不支持浮点型的,所以,还要增加一个判断,如果是求余运算,是否有小数参与运算,前面我发现太多类似这样的问题,被我一个一个给改过来了,现在又来了一个,另外还有几个问题没有解决,如一开始,不按数字,直接按加减运算符的话,居然被承认,算了,不想改了,我打算以后另写一个好一点的.因为这个写得实在不怎么样,就算都改好了,也没什么价值.还不如另写一个.*/#include<dos.h>#include<graphics.h>void *pmouse;struct BLOCK{int x;int y;}block[5][4];int MouseX,MouseY;double FirstNumber,SecondNumber;char signal;char CurrentSig;char CS[20];char FristCS[10];char SecondCS[10];int position=0;DrawBlock(int x,int y){setfillstyle(1,LIGHTGRAY);bar(x,y,x+30,y+30);setcolor(WHITE);line(x,y,x+30,y);line(x,y,x,y+30);setcolor(BLACK);line(x,y+30,x+30,y+30);line(x+30,y,x+30,y+30);rectangle(x-1,y-1,x+31,y+31);}void Graph(){int x,y;int i,j;int size;int driver=DETECT,gm;registerbgidriver(EGA VGA_driver);initgraph(&driver,&gm,"");moveto(220,220);linerel(7,7);linerel(-5,-1);linerel(3,6);linerel(-3,1);linerel(-1,-6);linerel(-3,3);linerel(2,-10);size=imagesize(217,220,228,234);pmouse=malloc(size);getimage(217,220,228,234,pmouse);cleardevice();setfillstyle(1,LIGHTGRAY);bar(220,130,375,360);line(220,130,375,130);line(220,130,220,360);setcolor(LIGHTGRAY);line(375,325,220,360);line(375,325,375,130);setfillstyle(1,BLACK);bar(230,140,365,170);setcolor(WHITE);line(365,170,365,140);line(365,170,230,170);x=230;y=180;for(i=0;i<5;i++){for(j=0;j<4;j++){block[i][j].x=x;x+=35;block[i][j].y=y;DrawBlock(block[i][j].x,block[i][j].y);}x=230;y+=35;}setcolor(BLACK);settextstyle(0,0,1);outtextxy(block[0][0].x+5,block[0][0].y+10,"CE"); outtextxy(block[0][1].x+5,block[0][1].y+10,"BAK"); outtextxy(block[0][2].x+5,block[0][2].y+10,"ESC"); settextstyle(0,0,2);outtextxy(block[0][3].x+10,block[0][3].y+10,"%"); outtextxy(block[1][0].x+10,block[1][0].y+10,"7"); outtextxy(block[1][1].x+10,block[1][1].y+10,"8"); outtextxy(block[1][2].x+10,block[1][2].y+10,"9");outtextxy(block[1][3].x+10,block[1][3].y+10,"+");outtextxy(block[2][0].x+10,block[2][0].y+10,"4");outtextxy(block[2][1].x+10,block[2][1].y+10,"5");outtextxy(block[2][2].x+10,block[2][2].y+10,"6");outtextxy(block[2][3].x+10,block[2][3].y+10,"-");outtextxy(block[3][0].x+10,block[3][0].y+10,"1");outtextxy(block[3][1].x+10,block[3][1].y+10,"2");outtextxy(block[3][2].x+10,block[3][2].y+10,"3");outtextxy(block[3][3].x+10,block[3][3].y+10,"*");outtextxy(block[4][0].x+10,block[4][0].y+10,"0");outtextxy(block[4][1].x+10,block[4][1].y+10,".");outtextxy(block[4][2].x+10,block[4][2].y+10,"=");outtextxy(block[4][3].x+10,block[4][3].y+10,"/");setfillstyle(1,BLACK);settextstyle(0,0,1);}int CheckMouse(int x,int y){_AX=0x03;geninterrupt(0x33);while(x==_CX&&y==_DX&&!kbhit()){_AX=0x03;geninterrupt(0x33);if(_BX==1) return 1;}return 0;}void DrawUpKey(int x,int y);main(){int MouseButton;int x,y,i,j;Graph();while(!kbhit()){_AX=0x03;geninterrupt(0x33);MouseX=_CX;MouseY=_DX;MouseButton=_BX;putimage(MouseX,MouseY,pmouse,XOR_PUT);if(MouseButton==1&&MouseX>230&&MouseX<365&&MouseY>180&&MouseY<350) CheckKey(MouseX,MouseY);CheckMouse(MouseX,MouseY);putimage(MouseX,MouseY,pmouse,XOR_PUT);}}void DrawUpKey(int x,int y){_AX=0x03;geninterrupt(0x33);while(_BX==1){_AX=0x03;geninterrupt(0x33);}putimage(MouseX,MouseY,pmouse,XOR_PUT);setcolor(WHITE);line(x,y,x+30,y);line(x,y,x,y+30);setcolor(DARKGRAY);line(x+30,y+30,x,y+30);line(x+30,y+30,x+30,y);putimage(MouseX,MouseY,pmouse,XOR_PUT);}CheckKey(MouseX,MouseY){CurrentSig=0;if(MouseX>230&&MouseX<260&&MouseY>180&&MouseY<210){setcolor(BLACK);rectangle(230,180,260,210);DrawUpKey(230,180);CurrentSig='c';}else if(MouseX>265&&MouseX<295&&MouseY>180&&MouseY<210) {setcolor(BLACK);rectangle(265,180,295,210);DrawUpKey(265,180);CurrentSig='b';}else if(MouseX>300&&MouseX<330&&MouseY>180&&MouseY<210) {setcolor(BLACK);rectangle(300,180,330,210);DrawUpKey(300,180);CurrentSig='e';}else if(MouseX>335&&MouseX<365&&MouseY>180&&MouseY<210) {setcolor(BLACK);rectangle(335,180,365,210);DrawUpKey(335,180);CurrentSig='%';}else if(MouseX>230&&MouseX<260&&MouseY>215&&MouseY<245) {setcolor(BLACK);rectangle(230,215,260,245);DrawUpKey(230,215);CurrentSig='7';}else if(MouseX>265&&MouseX<295&&MouseY>215&&MouseY<245) {setcolor(BLACK);rectangle(265,215,295,245);DrawUpKey(265,215);CurrentSig='8';}else if(MouseX>300&&MouseX<330&&MouseY>215&&MouseY<245) {setcolor(BLACK);rectangle(300,215,330,245);DrawUpKey(300,215);CurrentSig='9';}else if(MouseX>335&&MouseX<365&&MouseY>215&&MouseY<245) {setcolor(BLACK);rectangle(335,215,365,245);DrawUpKey(335,215);CurrentSig='+';}else if(MouseX>230&&MouseX<260&&MouseY>250&&MouseY<280) {setcolor(BLACK);rectangle(230,250,260,280);DrawUpKey(230,250);CurrentSig='4';}else if(MouseX>265&&MouseX<295&&MouseY>250&&MouseY<280) {setcolor(BLACK);rectangle(265,250,295,280);DrawUpKey(265,250);CurrentSig='5';}else if(MouseX>300&&MouseX<330&&MouseY>250&&MouseY<280) {setcolor(BLACK);rectangle(300,250,330,280);DrawUpKey(300,250);CurrentSig='6';}else if(MouseX>335&&MouseX<365&&MouseY>250&&MouseY<280) {setcolor(BLACK);rectangle(335,250,365,280);DrawUpKey(335,250);CurrentSig='-';}else if(MouseX>230&&MouseX<260&&MouseY>285&&MouseY<315) {setcolor(BLACK);rectangle(230,285,260,315);DrawUpKey(230,285);CurrentSig='1';}else if(MouseX>265&&MouseX<295&&MouseY>285&&MouseY<315) {setcolor(BLACK);rectangle(265,285,295,315);DrawUpKey(265,285);CurrentSig='2';}else if(MouseX>300&&MouseX<330&&MouseY>285&&MouseY<315) {setcolor(BLACK);rectangle(300,285,330,315);DrawUpKey(300,285);CurrentSig='3';}else if(MouseX>335&&MouseX<365&&MouseY>285&&MouseY<315) {setcolor(BLACK);rectangle(335,285,365,315);DrawUpKey(335,285);CurrentSig='*';else if(MouseX>230&&MouseX<260&&MouseY>320&&MouseY<350) {setcolor(BLACK);rectangle(230,320,260,350);DrawUpKey(230,320);CurrentSig='0';}else if(MouseX>265&&MouseX<295&&MouseY>320&&MouseY<350) {setcolor(BLACK);rectangle(265,320,295,350);DrawUpKey(265,320);CurrentSig='.';}else if(MouseX>300&&MouseX<330&&MouseY>320&&MouseY<350) {setcolor(BLACK);rectangle(300,320,330,350);DrawUpKey(300,320);CurrentSig='=';}else if(MouseX>335&&MouseX<365&&MouseY>320&&MouseY<350) {setcolor(BLACK);rectangle(335,320,365,350);DrawUpKey(335,320);CurrentSig='/';}if(CurrentSig!=0){if(CurrentSig=='e') exit(0);CS[position]=CurrentSig;ManageResult();}}OutLine(char *s,int lines){int y;int length;length=strlen(s);switch(lines){case 0:y=141;break;case 1:y=151;break;case 2:y=161;break;outtextxy(364-length*8,y,s);}ManageResult(void){static int lines=0;static int fi=0;static flag=0,sflag=0;if(lines==0){if(position==1&&CS[0]=='0'&&CS[1]!='.')return;if(CS[position]=='.'&&flag==1) return;if(CS[position]=='.') flag=1;if(CS[position]=='c'){position=0;flag=0;}else if(CS[position]=='b'){if(position==0) return;position--;if(CS[position]=='.') flag=0;}else if(position==9){if(CS[position]!='%'&&CS[position]!='+'&&CS[position]!='-'&&CS[position]!='*'&&CS[position]!='/')return ;else {if(CS[position-1]=='.') return;lines=2;position++;fi=position;} }else if(CS[position]=='%'||CS[position]=='+'||CS[position]=='-' ||CS[position]=='*'||CS[position]=='/'){if(CS[position-1]=='.') return ;lines=2;position++;fi=position;}else if(CS[position]=='=') return;else position++;}else{if(position==fi+1&&CS[fi]=='0'&&CS[fi+1]!='.') return;if(CS[position]=='.'&&sflag==1) return;if(CS[position]=='.') sflag=1;if(CS[position]=='c'){position=0;lines=0;sflag=0;}else if(CS[position]=='b'){position--;if(CS[position]=='%'||CS[position]=='+'||CS[position]=='-'||CS[position]=='*'||CS[position]=='/')lines=0;else if(CS[position]=='.') sflag=0;}else if(position-fi==9&&CS[position]!='=')return ;else if(CS[position]>='0'&&CS[position]<='9'||CS[position]=='.') position++;else if(CS[position]=='='&&(CS[position-1]=='.'||CS[position-1]=='%' ||CS[position-1]=='+'||CS[position-1]=='-'||CS[position-1]=='*'||CS[position-1]=='/'))return;}OutResult();}OutResult(void){int fi=0, si=0;int pfi;int i;int state=0;setcolor(WHITE);FristCS[0]='\0';SecondCS[0]='\0';for(pfi=0;pfi<position;pfi++)if(CS[pfi]=='%'||CS[pfi]=='+'||CS[pfi]=='-'||CS[pfi]=='*'||CS[pfi]=='/') {state=1;signal=CS[pfi];break;}if(CS[position]=='=')state=2;for(fi=0;fi<pfi;fi++)FristCS[fi]=CS[fi];FristCS[fi]='\0';i=pfi+1;for(si=0;si<position-pfi-1;si++,i++)SecondCS[si]=CS[i];SecondCS[si]='\0';bar(230,140,364,169);if(state==0)OutLine(FristCS,2);else{switch(signal){case '%':outtextxy(262,150,"%");break;case '+':outtextxy(262,150,"+");break;case '-':outtextxy(262,150,"-");break;case '*':outtextxy(262,150,"*");break;case '/':outtextxy(262,150,"/");break;}OutLine(FristCS,0);if(state==1)OutLine(SecondCS,2);else{OutLine(SecondCS,1);outtextxy(262,160,"=");OutValue();}}}OutValue(void){double result;Change(&FirstNumber,FristCS);Change(&SecondNumber,SecondCS);switch(signal){case '+':result=FirstNumber+SecondNumber;break; case '-':result=FirstNumber-SecondNumber;break; case '*':result=FirstNumber*SecondNumber;break; case '/':result=FirstNumber/SecondNumber;break; }sprintf(FristCS,"%lf",result);FristCS[10]='\0';OutLine(FristCS,2);}Change(double *s,char *c){int i=0;double v=0.1;int flag=1;*s=0;while(c[i]){if(c[i]=='.') {flag=0;i++;continue;}if(flag)*s=10**s+c[i]-'0';else{*s+=(c[i]-'0')*v;v/=10;}i++;}}#include<stdio.h>#include<conio.h>#include<graphics.h>struct GRAPH{int x;int y;char color;}graph[56];DrawArrow(struct GRAPH graph){setcolor(graph.color);}void GainValue(void){int xx,yy;int i,j=0;xx=250;for(i=0;i<10;i++){graph[j].x=xx;xx+=15;graph[j].y=90;j++;} xx-=15;yy=105;for(i=0;i<19;i++){graph[j].x=xx;graph[j].y=yy;yy+=15;j++;} yy-=15;xx-=15;for(i=0;i<9;i++){graph[j].x=xx;xx-=15;graph[j].y=yy;j++;} yy-=15;xx+=15;for(i=0;i<18;i++){graph[j].x=xx;graph[j].y=yy;yy-=15;j++;} }void Init(void){int driver=DETECT,gm; registerbgidriver(EGA VGA_driver); initgraph(&driver,&gm,"");}main(){int i=0;int color;Init();GainV alue();graph[i].color=rand()%15;for(i=1;i<55;i++)do graph[i].color=rand()%15;while(graph[i].color==graph[i-1].color);do graph[i].color=rand()%15;while(graph[i].color==graph[i-1].color&&graph[i].color==graph[0].color);for(i=0;i<56;i++){setcolor(graph[i].color);circle(graph[i].x,graph[i].y,5);}delay(15000);while(!kbhit()){color=graph[56].color;for(i=56;i>0;i--)graph[i].color=graph[i-1].color;graph[0].color=color;for(i=0;i<56;i++){setcolor(graph[i].color);circle(graph[i].x,graph[i].y,5);}delay(15000);}getch();}/*这里有一个问题,不过幸好被我给解决了,但还是不知道是什么原因引起的就是你把刷新随机数的函数randomize();放到下面的PlayGame函数里,就会显示,调用示定义的函数在PlayGame里,本来我实在要崩溃了,明明是库函数,没有什么错的地方,怎么会出错了,无奈,我只好把它去掉,通过了.后来我发现把它放在主函数里则没有事.想问一下,有哪位高手知道是什么原因?关于为什么用printf函数输出分数,而不用outtextxy函数,你们自己可以去了解*/ #include<stdio.h>#include<conio.h>#include<graphics.h>#include<stdlib.h>#include<time.h>#define SPACE 0x3920#define UP 0x4800#define DOWN 0x5000#define LEFT 0x4b00#define RIGHT 0x4d00#define ESC 0x11b#define END 0#define MID 1int AllBlock[7][4][2]={{{1,4},{0,4},{2,4},{2,5}},{{1,4},{0,4},{2,4},{2,3}},{{1,4},{1,3},{1,5},{0,4}},{{1,4},{1,3},{0,4},{2,3}},{{1,4},{1,5},{0,4},{2,5}},{{0,3},{0,4},{1,3},{1,4}},{{2,4},{0,4},{1,4},{3,4}}};int CurrentBlock[4][2];int NextBlock[4][2];int key;int score=0;char s[10];struct BLOCKUNIT{int x;int y;char state;}BlockUnit[20][10];void PlayGame(void);DrawBlockUnit(int i,int j){ int x,y;x=BlockUnit[i][j].x;y=BlockUnit[i][j].y;if(BlockUnit[i][j].state)setcolor(GREEN);else setcolor(BLACK);rectangle(x+1,y+1,x+14,y+14);}void DrawGraph(void){char s[15];char fillpattern[9]={0x1b,0x33,0x44,0x55,0x67,0x33,0x01,0x29}; int i,j;int driver=DETECT,gm;registerbgidriver(EGA VGA_driver);initgraph(&driver,&gm,"");setfillpattern(fillpattern,7);bar(89,89,99,411);bar(99,89,330,99);bar(99,401,330,411);bar(320,99,330,411);bar(251,99,261,401);bar(251,180,320,190);setfillstyle(1,BLACK);setcolor(GREEN);outtextxy(270,192,"score:");sprintf(s,"UP: %c",24);outtextxy(263,232,s);sprintf(s,"DOWN:%c",25);outtextxy(263,272,s);sprintf(s,"LEFT:%c",27);outtextxy(263,312,s);sprintf(s,"RIGHT:%c",26);outtextxy(263,352,s);sprintf(s,"SPACE:");outtextxy(263,382,s);}void main(void){randomize();DrawGraph();PlayGame();}void MoveRotateBlock(int curk){int i;int nexti,nextj;int NewBlock[4][2];switch(key){case UP:for(i=0;i<4;i++){BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]].state=0;DrawBlockUnit(CurrentBlock[i][0],CurrentBlock[i][1]);}while(1){for(i=0;i<4;i++)if(BlockUnit[CurrentBlock[i][0]+1][CurrentBlock[i][1]].state==1||CurrentBlock[i][0]+1==20) i=4;if(i==4) for(i=0;i<4;i++) CurrentBlock[i][0]++;else break;}for(i=0;i<4;i++){BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]].state=1;DrawBlockUnit(CurrentBlock[i][0],CurrentBlock[i][1]);}break;case DOWN:for(i=0;i<4;i++){BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]].state=0;DrawBlockUnit(CurrentBlock[i][0],CurrentBlock[i][1]);}for(i=0;i<4;i++)if(BlockUnit[CurrentBlock[i][0]+1][CurrentBlock[i][1]].state==1||CurrentBlock[i][0]+1==20) i=4;if(i==4) for(i=0;i<4;i++) CurrentBlock[i][0]++;for(i=0;i<4;i++){BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]].state=1;DrawBlockUnit(CurrentBlock[i][0],CurrentBlock[i][1]);}break;case LEFT: for(i=0;i<4;i++){BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]].state=0;DrawBlockUnit(CurrentBlock[i][0],CurrentBlock[i][1]);}for(i=0;i<4;i++)if(BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]-1].state==1||CurrentBlock[i][1]==0) i=4;if(i==4)for(i=0;i<4;i++)CurrentBlock[i][1]--;for(i=0;i<4;i++){if(CurrentBlock[i][0]<0) continue;BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]].state=1;DrawBlockUnit(CurrentBlock[i][0],CurrentBlock[i][1]);}break;case RIGHT:for(i=0;i<4;i++){BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]].state=0;DrawBlockUnit(CurrentBlock[i][0],CurrentBlock[i][1]);}for(i=0;i<4;i++)if(BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]+1].state==1||CurrentBlock[i][1]==9) i=4;if(i==4)for(i=0;i<4;i++)CurrentBlock[i][1]++;for(i=0;i<4;i++){BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]].state=1;DrawBlockUnit(CurrentBlock[i][0],CurrentBlock[i][1]);}break;case SPACE: if(curk==5) break;for(i=0;i<4;i++){BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]].state=0;DrawBlockUnit(CurrentBlock[i][0],CurrentBlock[i][1]);}for(i=1;i<4;i++){nexti=CurrentBlock[i][1]-CurrentBlock[0][1]+CurrentBlock[0][0];nextj=CurrentBlock[0][0]-CurrentBlock[i][0]+CurrentBlock[0][1];if(BlockUnit[nexti][nextj].state==1||nexti==20||nextj==-1||nextj==10){i=4;continue;}NewBlock[i][0]=nexti;NewBlock[i][1]=nextj;}if(i==4)for(i=1;i<4;i++){CurrentBlock[i][0]=NewBlock[i][0];CurrentBlock[i][1]=NewBlock[i][1];}for(i=0;i<4;i++){BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]].state=1;DrawBlockUnit(CurrentBlock[i][0],CurrentBlock[i][1]);}break;}}void DrawNextBlock(int k){bar(263,101,309,163);switch(k){case 0:rectangle(263,101,277,115);rectangle(263,117,277,131);rectangle(263,133,277,147);rectangle(279,133,293,147);break;/*[][][][]*/ case 1:rectangle(279,101,293,115);rectangle(279,117,293,131);rectangle(279,133,293,147);rectangle(263,133,277,147);break; /*[][][][]*/case 2:rectangle(279,101,293,115);rectangle(279,117,293,131);rectangle(263,117,277,131);rectangle(295,117,309,131);break;/* [][][][]*/case 3:rectangle(279,101,293,115);rectangle(279,117,293,131);rectangle(263,117,277,131);rectangle(263,133,277,147);break; /*[][][][]*/ case 4:rectangle(263,101,277,115);rectangle(263,117,277,131);rectangle(279,117,293,131);rectangle(279,133,293,147);break;/*[][][][]*/ case 5:rectangle(263,101,277,115);rectangle(263,117,277,131);rectangle(279,101,293,115);rectangle(279,117,293,131);break; /*[][][][]*/ case 6:rectangle(263,101,277,115);rectangle(263,117,277,131);rectangle(263,133,277,147);rectangle(263,149,277,163);break;/*[][][][]*/}}OutScore(lines){switch(lines){case 1:score+=10;break;case 2:score+=30;break;case 3:score+=50;break;case 4:score+=80;break;default:score++;break;}gotoxy(34,14);sprintf(s,"%d",score);delline();printf("%s",s);}void PlayGame(void){int lines=0,time=0;int k,curk;int x,y;int i,j;int BlockState;gotoxy(34,14);printf("0");x=y=100;for(i=0;i<20;i++){for(j=0;j<20;j++){BlockUnit[i][j].x=x;BlockUnit[i][j].y=y;x+=15;}y+=15;x=100;}k=rand()%7;for(i=0;i<4;i++){NextBlock[i][0]=AllBlock[k][i][0];NextBlock[i][1]=AllBlock[k][i][1];} DrawNextBlock(k);while(key!=ESC){BlockState=MID;for(i=0;i<4;i++){CurrentBlock[i][0]=NextBlock[i][0];CurrentBlock[i][1]=NextBlock[i][1];}for(i=0;i<4;i++){BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]].state=1;DrawBlockUnit(CurrentBlock[i][0],CurrentBlock[i][1]);}curk=k;k=rand()%7;for(i=0;i<4;i++){NextBlock[i][0]=AllBlock[k][i][0];NextBlock[i][1]=AllBlock[k][i][1];} DrawNextBlock(k);while(BlockState!=END){while(time+10>clock())if(kbhit()){key=bioskey(0);MoveRotateBlock(curk);if(key==UP) {time=clock();time-=18;}else if(key==ESC){time-=18;BlockState=END;}}if(key==ESC) continue;BlockState=DownBlock();time=clock();}OutScore(10);lines=JudgeBlock();if(lines!=0){OutScore(lines);lines=0;}}}int DownBlock(void){int i;for(i=0;i<4;i++){BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]].state=0; DrawBlockUnit(CurrentBlock[i][0],CurrentBlock[i][1]);}for(i=0;i<4;i++)if(BlockUnit[CurrentBlock[i][0]+1][CurrentBlock[i][1]].state==1|| CurrentBlock[i][0]+1==20) i=4;if(i==5){for(i=0;i<4;i++){BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]].state=1;DrawBlockUnit(CurrentBlock[i][0],CurrentBlock[i][1]);}return END;}for(i=0;i<4;i++) CurrentBlock[i][0]++;for(i=0;i<4;i++){BlockUnit[CurrentBlock[i][0]][CurrentBlock[i][1]].state=1;DrawBlockUnit(CurrentBlock[i][0],CurrentBlock[i][1]);}return MID;}void ClearBlock(int i){int j;setcolor(BLACK);for(j=0;j<10;j++){BlockUnit[i][j].state=0;DrawBlockUnit(i,j);}setcolor(GREEN);for(;i>0;i--)for(j=0;j<10;j++){BlockUnit[i][j].state=BlockUnit[i-1][j].state;DrawBlockUnit(i,j);} }int JudgeBlock(void){int lines=0;int i,j;for(j=0;j<10;j++)if(BlockUnit[0][j].state==1) {key=ESC;return 0;}for(i=1;i<20;i++){for(j=0;j<10;j++)if(BlockUnit[i][j].state==0) j=10;if(j==10){lines++;ClearBlock(i);}}return lines;}/*游戏只想了五个关卡,有兴趣的朋友,可以自己想几个试试,因为功能所限地图的方框只能是8*8个.像箱子,金币只是简单的画了一下,如果有谁觉得实在是画得不好看的话,也可以在DrawBox,DrawCoin等去修改一下只要不超过20*20就行,OK先就这样吧*/#include<stdio.h>#include<dos.h>#include<graphics.h>#define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011benum MAP{wall,coin,blank,man,box,fullbox};enum MAP map[5][8][8]={{{wall,wall,wall,wall,wall,wall,wall,wall},{wall,coin,coin,coin,blank,blank,blank,wall},{wall,wall,wall,wall,wall,blank,blank,wall},{wall,blank,blank,man,wall,blank,blank,wall},{wall,blank,box,box,blank,blank,blank,wall},{wall,blank,blank,blank,box,blank,blank,wall},{wall,blank,blank,blank,wall,blank,blank,wall},{wall,wall,wall,wall,wall,wall,wall,wall},},{{wall,wall,wall,wall,wall,wall,wall,wall},{wall,blank,blank,blank,wall,blank,blank,wall},{wall,blank,blank,blank,blank,blank,blank,wall},。
利用VC编写简单的绘图程序

利用VC编写简单的绘图程序上机实验环境亦可选择Microsoft Visual C++〔以下简称VC〕。
VC是美国微软公司生产的基于其Windows系统的软件开发工具。
它具有使用灵活,并与32位Windows内核〔使用于Windows 2000/Windows XP〕高度兼容的特点,从而被Windows程序员们广泛使用。
VC 在图形图像处理方面有着广泛的应用,MFC中提供了大量的图形图像函数,下面我们将对使用VC/MFC编写简单的绘图程序。
一、使用VC编写MFC单文档应用程序很简单,只需要按照下面几个步骤进行:1.翻开MSVC集成开发环境。
双击桌面或“开始〞菜单中的Microsoft VisualC++6.0,不久将看到VC的编辑窗口,如图3-5:图1 VC启动界面2.选择菜单“File | New〞,在弹出的对话框中1)单击上方的选项卡“Project〞,2)选择“MFC AppWizard(exe)〞,3)在“Project name〞一栏中填写工程名,例如draw,4)在“Location〞一栏中填写你想把文件存放的位置〔目录〕。
然后按“OK〞。
见图2。
注意:第4〕步中指定你自己的目录,不要使用系统的缺省目录或者随便放在根目录或者其他的目录下。
这样便于你找到自己编写的程序。
图2 应用程序向导主界面3.在MFC Appwizard-Step 1中选择“Single Document〞,即单文档应用程序,点击“Finish〞,如图3所示。
注意:对于下面的操作我们可以忽略,直接点Finish即可。
图3 应用程序向导中选择单文档视图4.系统弹出一个当前工程信息的对话框,如图4所示,直接点“OK〞即可。
图4 新建工程信息5.进入程序编写的主界面,屏幕左下方为工作区,如图5所示,工作区中共有三种视图,分别是:1)文件视图〔“FileView〞〕,主要包括头文件,cpp文件以及资源文件。
我们在头文件中一般添加类的定义,类的成员变量和函数的声明,而在cpp文件中具体实现函数。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include <graphics.h>#include <stdlib.h>#include <conio.h>#include <stdio.h>#include <dos.h>#include <bios.h>#include <math.h>#include <alloc.h>/*定义常量*//*向上翻页移键*/#define PAGEUP 0x4900/*向下翻页移键*/#define PAGEDOWN 0x5100/*Escape键*/#define ESC 0x011b/*左移键*/#define LEFT 0x4b00/*右移键*/#define RIGHT 0x4d00/*下移键*/#define DOWN 0x5000/*上移键*/#define UP 0x4800/*空格键*/#define SPACE 0x3920#define NO_PRESSED 0#define LEFT_PRESSED 1#define RIGHT_PRESSED 2#define pi 3.1415926/*定义全局变量*/int Rx,Ry,R;int TOPx,TOPy,BOTTOMx,BOTTOMy;int Centx,Centy;int lineStartx,lineStarty,lineEndx,lineEndy;int linePoint_x[20],linePoint_y[20];/*这里的字模数组均由“点阵字模工具”生成,你可以用你自己需要的点阵信息来替换示例中的字模信息,注意字模大小要一致,否则显示会出问题。
*/char zhi16K[]={/* 以下是 '直' 的 16点阵楷体_GB2312 字模,32 byte */0x01,0x00,0x01,0x00,0x01,0xF0,0x1E,0x00,0x02,0x00,0x07,0xC0,0x08,0x40,0x0F,0x40,0x08,0x40,0x0F,0x40,0x08,0x40,0x0F,0x40,0x08,0x40,0x0F,0xFC,0x70,0x00,0x00,0x00,};char xian16K[]={/* 以下是 '线' 的 16点阵楷体_GB2312 字模,32 byte */ 0x00,0x80,0x00,0x90,0x08,0x88,0x10,0x80,0x24,0xF0,0x45,0x80,0x78,0xB0,0x11,0xC0,0x2C,0x88,0x70,0x50,0x04,0x60,0x18,0xA4,0x63,0x14,0x00,0x0C,0x00,0x04,0x00,0x00,};char ju16K[]={/* 以下是 '矩' 的 16点阵楷体_GB2312 字模,32 byte */ 0x00,0x00,0x08,0x00,0x08,0x78,0x10,0x80,0x1E,0x80,0x28,0xF8,0x48,0x88,0x0E,0x88,0xF8,0xF0,0x08,0x80,0x14,0x80,0x12,0x9E,0x20,0xE0,0x40,0x00,0x00,0x00,0x00,0x00,};char xing16K[]={/* 以下是 '形' 的 16点阵楷体_GB2312 字模,32 byte */ 0x00,0x00,0x07,0x88,0x3A,0x08,0x12,0x10,0x12,0x20,0x17,0x48,0xFA,0x10,0x12,0x20,0x12,0xC8,0x12,0x08,0x22,0x10,0x42,0x20,0x00,0x40,0x00,0x80,0x03,0x00,0x00,0x00,};char yuan16K[]={/* 以下是 '圆' 的 16点阵楷体_GB2312 字模,32 byte */ 0x00,0xF8,0x3F,0x08,0x23,0x88,0x24,0x88,0x27,0x08,0x21,0xC8,0x2E,0x48,0x29,0x48,0x29,0x48,0x22,0x88,0x24,0x48,0x28,0x08,0x3F,0xE8,0x00,0x10,0x00,0x00,0x00,0x00,};char qing16K[]={/* 以下是 '清' 的 16点阵楷体_GB2312 字模,32 byte */ 0x00,0x80,0x00,0xE0,0x33,0x80,0x10,0xE0,0x03,0x80,0x40,0xFC,0x2F,0x00,0x01,0xE0,0x12,0x20,0x13,0xA0,0x22,0x20,0x63,0xA0,0x42,0x20,0x02,0x60,0x00,0x20,0x00,0x00,};char ping16K[]={/* 以下是 '屏' 的 16点阵楷体_GB2312 字模,32 byte */ 0x00,0xF0,0x0F,0x30,0x08,0x60,0x0F,0x80,0x0A,0x20,0x09,0x40,0x08,0xF8,0x17,0x20,0x11,0x3E,0x2F,0xE0,0x21,0x20,0x42,0x20,0x82,0x20,0x04,0x20,0x08,0x20,0x00,0x00,};char bao16K[]={/* 以下是 '保' 的 16点阵楷体_GB2312 字模,32 byte */ 0x00,0x00,0x09,0xF0,0x0A,0x10,0x12,0x10,0x13,0xE0,0x30,0x80,0x50,0xFC,0x9F,0x80,0x11,0xC0,0x12,0xA0,0x14,0x98,0x18,0x8E,0x10,0x80,0x10,0x80,0x00,0x00,0x00,0x00,};char cun16K[]={/* 以下是 '存' 的 16点阵楷体_GB2312 字模,32 byte */ 0x01,0x00,0x01,0x00,0x01,0xF0,0x1E,0x00,0x02,0x70,0x05,0x90,0x08,0x20,0x08,0x40,0x18,0x7E,0x2B,0xA0,0xC8,0x20,0x08,0x20,0x08,0x20,0x08,0xA0,0x00,0x40,0x00,0x00,};char jia16K[]={/* 以下是 '加' 的 16点阵楷体_GB2312 字模,32 byte */ 0x00,0x00,0x08,0x00,0x08,0x00,0x08,0x00,0x0F,0x00,0x79,0x3C,0x09,0x44,0x11,0x44,0x11,0x44,0x22,0x44,0x22,0x78,0x4A,0x00,0x84,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};char zai16K[]={/* 以下是 '载' 的 16点阵楷体_GB2312 字模,32 byte */ 0x00,0x80,0x08,0xA0,0x08,0x90,0x0E,0x80,0x38,0xF0,0x0F,0x80,0x78,0x50,0x0E,0x50,0x34,0x20,0x1E,0x20,0x34,0x50,0x0E,0x92,0x75,0x0A,0x04,0x06,0x04,0x02,0x00,0x00,};char bang16K[]={/* 以下是 '帮' 的 16点阵楷体_GB2312 字模,32 byte */0x04,0x00,0x07,0x38,0x1C,0x48,0x06,0x50,0x1C,0x50,0x07,0x48,0x78,0x58,0x11,0x40,0x21,0xF0,0x4F,0x10,0x09,0x10,0x09,0x50,0x09,0x20,0x01,0x00,0x01,0x00,0x00,0x00,};char zhu16K[]={/* 以下是 '助' 的 16点阵楷体_GB2312 字模,32 byte */0x00,0x00,0x00,0x20,0x0C,0x20,0x34,0x20,0x24,0x20,0x34,0x38,0x25,0xC8,0x34,0x48,0x24,0x48,0x26,0x88,0x38,0x88,0xE1,0x28,0x02,0x10,0x04,0x00,0x00,0x00,0x00,0x00,};/*自定义函数*/void outChinese(char *mat,int matsize,int x,int y,int color); void fill(int startx,int starty,int endx,int endy,int color); void showHelp();void save();void load();int mouseStatus(int* x,int* y);int setMousePos(int x, int y);void DrawMouse(float x,float y);void DrawLine();void DrawRectangle();void LineToCircle(int x0,int y0,int r);void DrawCircle();long factorial(int n);float berFunction(int i,int n,double t);void DrawBezier();/*根据点阵信息显示中文函数*/void outChinese(char *mat,int matsize,int x,int y,int color) /*依次:字模指针、点阵大小、起始坐标(x,y)、颜色*/{int i, j, k, n;n = (matsize - 1) / 8 + 1;for(j = 0; j < matsize; j++)for(i = 0; i < n; i++)for(k = 0;k < 8; k++)if(mat[j * n + i] & (0x80 >> k))/*测试为1的位则显示*/putpixel(x + i * 8 + k, y + j, color);}/*填充函数*/void fill(int startx,int starty,int endx,int endy,int color){int i,j;for(i=startx;i<=endx;i++)for(j=starty;j<=endy;j++)/*在指定位置以指定颜色画一像素*/putpixel(i,j,color);}/*显示用户帮助函数*/void showHelp(){setcolor(14);outtextxy(45,50,"Line:");setcolor(WHITE);outtextxy(45,50," 1 Press left button to start until to line end.");outtextxy(45,65," 2 Use UP,DOWN,LEFT,RIGHT keys to move it.");outtextxy(45,80," 3 Use PAGEUP key to enlarge it, and PAGEDOWN key to shrink it.");outtextxy(45,95," 4 Use SPACE key to rotate it.");setcolor(14);outtextxy(45,120,"Rectangle:");setcolor(WHITE);outtextxy(45,120," 1 Press left button to start until to right corner.");outtextxy(45,135," 2 Use UP,DOWN,LEFT,RIGHT keys to move it.");outtextxy(45,150," 3 Use PAGEUP key to enlarge it, and PAGEDOWN key to shrink it.");setcolor(14);outtextxy(45,170,"Circle:");setcolor(WHITE);outtextxy(45,170," 1 Press left button to start until to end.");outtextxy(45,185," 2 Use PAGEUP key to enlarge it, and PAGEDOWN key toshrink it.");setcolor(14);outtextxy(45,205,"Bezier:");setcolor(WHITE);outtextxy(45,205," Press left button to start, and right button to end.");outtextxy(45,230,"Press ESC key to stop the operation function.");outtextxy(45,245,"Press right button to end the drawing works.");outtextxy(45,260,"Press any key to continue......");getch();fill(40,40,625,270,0);}/*保存函数*/void save(){int i,j;FILE *fp;char fileName[20];fill(0,447,630,477,2);gotoxy(1,25);printf("\n\n\n\n Input the file name[.dat]:");scanf("%s",fileName);fill(0,447,630,477,2);/*以读写的方式打开文件*/if((fp=fopen(fileName,"w+"))==NULL){outtextxy(260,455,"Failed to open file!");exit(0);}outtextxy(280,455,"saving...");/*保存像素到文件*/for(i=5;i<630;i++)for(j=30;j<=445;j++)fputc(getpixel(i,j),fp);fclose(fp);fill(0,447,630,477,2);outtextxy(260,455,"save over!");}/*打开函数*/void load(){int i,j;char fileName[20];FILE *fp;fill(0,447,630,477,2);gotoxy(1,25);printf("\n\n\n\n Input the file name[.dat]:");scanf("%s",fileName);/*打开指定的文件*/if((fp=fopen(fileName,"r+"))!=NULL){fill(0,447,630,477,2);outtextxy(280,455,"loading...");/*从文件中读出像素*/for(i=5;i<630;i++)for(j=30;j<=445;j++)putpixel(i,j,fgetc(fp));fill(0,447,630,477,2);outtextxy(280,455,"loading over !");}/*打开失败*/else{fill(0,447,630,477,2);outtextxy(260,455,"Failed to open file!");}fclose(fp);}/*获取鼠标状态函数*/int mouseStatus(int* x,int* y){/*定义两个寄存器变量,分别存储入口参数和出口参数*/union REGS inregs,outregs;int status;status=NO_PRESSED;/*入口参数AH=3,读取鼠标位置及其按钮状态*/ inregs.x.ax=3;int86(0x33,&inregs,&outregs);/*CX表示水平位置,DX表示垂直位置*/*x=outregs.x.cx;*y=outregs.x.dx;/*BX表示按键状态*/if(outregs.x.bx&1)status=LEFT_PRESSED;else if(outregs.x.bx&2)status=RIGHT_PRESSED;return (status);}/*设置鼠标指针位置函数*/int setMousePos(int x,int y){union REGS inregs,outregs;/*入口参数AH=4,设置鼠标指针位置*/inregs.x.ax=4;inregs.x.cx=x;inregs.x.dx=y;int86(0x33,&inregs,&outregs);}/*绘制鼠标函数*/void DrawMouse(float x,float y){line(x,y,x+5,y+15);line(x,y,x+15,y+5);line(x+5,y+15,x+15,y+5);line(x+11,y+9,x+21,y+19);line(x+9,y+11,x+19,y+21);line(x+22,y+19,x+20,y+21);}/*绘制直线函数*/void DrawLine(){int x0,y0,x1,y1;int last_x=0,last_y=0;int endFlag=0;int key;int temStartx,temStarty,temEndx,temEndy;int increment_x,increment_y,angle;DrawMouse(last_x,last_y);while(1){/*右键结束画直线*/while((mouseStatus(&x1,&y1)==RIGHT_PRESSED)) endFlag=1;if(endFlag==1)break;/*鼠标移动,没有单击,仅仅画移动的鼠标*/while(mouseStatus(&x1,&y1) == NO_PRESSED){if(last_x!=x1||last_y!=y1){DrawMouse(last_x,last_y);DrawMouse(x1,y1);last_x=x1;last_y=y1;}}/*单击左键后,开始画直线*/if(mouseStatus(&x0,&y0)==LEFT_PRESSED){DrawMouse(last_x,last_y);line(x0,y0,x1,y1);last_x=x1;last_y=y1;/*拉动过程中,画直线和鼠标*/while(mouseStatus(&x1, &y1)==LEFT_PRESSED) {if(last_x!=x1||last_y!=y1){line(x0,y0,last_x,last_y);line(x0,y0,x1,y1);last_x=x1;last_y=y1;}}/*松开左键后,画直线完成,记录直线的起始位置*/lineStartx=x0;lineStarty=y0;lineEndx=x1;lineEndy=y1;while(1){/*从键盘获取键值,开始操作(移动、放大、缩小、旋转)直线*/ key=bioskey(0);/*ESC键,退出操作*/if(key==ESC)break;/*旋转*/if(key==SPACE){/*计算旋转中心*//*如果直线示倾斜的*/if((lineStarty!=lineEndy)&& (lineStartx!=lineEndx)){Centx=(lineEndx-lineStartx)/2+lineStartx;Centy=(lineEndy-lineStarty)/2+lineStarty;}/*如果直线是竖直的*/if(lineStarty==lineEndy){Centx=(lineEndx-lineStartx)/2+lineStartx;Centy=lineStarty;}/*如果直线是水平的*/if(lineStartx==lineEndx){Centx=lineStartx;Centy=(lineEndy-lineStarty)/2+lineStarty;}temStartx=lineStartx;temStarty=lineStarty;temEndx=lineEndx;temEndy=lineEndy;/*旋转不能超过边界*/if(lineStartx>=10 && lineStarty>=40 && lineEndx <=620 && lineEndy <=445){/*清除原有的直线*/setwritemode(XOR_PUT);line(lineStartx,lineStarty,lineEndx,lineEndy);/*计算旋转30度后的起点坐标*/lineStartx=(temStartx-Centx)*cos(pi/6)-(temStarty-Centy)*sin(pi/6)+Centx; lineEndx=(temEndx-Centx)*cos(pi/6)-(temEndy-Centy)*sin(pi/6)+Centx;/*计算旋转30度后的终点坐标*/lineStarty=(temStartx-Centx)*sin(pi/6)+(temStarty-Centy)*cos(pi/6)+Centy; lineEndy=(temEndx-Centx)*sin(pi/6)+(temEndy-Centy)*cos(pi/6)+Centy;temStartx=lineStartx;temStarty=lineStarty;temEndx=lineEndx;temEndy=lineEndy;/*绘制旋转后的直线*/line(lineStartx,lineStarty,lineEndx,lineEndy);}}/*左移直线*/if(key==LEFT){if(lineStartx>=10 && lineStarty>=40 && lineEndx <=620 && lineEndy <=445){setwritemode(XOR_PUT);line(lineStartx,lineStarty,lineEndx,lineEndy);/*起始的横坐标减小*/lineStartx-=5;lineEndx-=5;line(lineStartx,lineStarty,lineEndx,lineEndy);}}/*右移直线*/if(key==RIGHT){if(lineStartx>=10 && lineStarty>=40 && lineEndx <=620 && lineEndy <=445){setwritemode(XOR_PUT);line(lineStartx,lineStarty,lineEndx,lineEndy);/*起始的横坐标增加*/lineStartx+=5;lineEndx+=5;line(lineStartx,lineStarty,lineEndx,lineEndy);}}/*下移直线*/if(key==DOWN){if(lineStartx>=10 && lineStarty>=40 && lineEndx <=620 && lineEndy <=445){setwritemode(XOR_PUT);line(lineStartx,lineStarty,lineEndx,lineEndy);/*起始的纵坐标增加*/lineStarty+=5;lineEndy+=5;line(lineStartx,lineStarty,lineEndx,lineEndy);}}/*上移直线*/if(key==UP){if(lineStartx>=10 && lineStarty>=40 && lineEndx <=620 && lineEndy <=445){setwritemode(XOR_PUT);line(lineStartx,lineStarty,lineEndx,lineEndy);/*起始的纵坐标减小*/lineStarty-=5;lineEndy-=5;line(lineStartx,lineStarty,lineEndx,lineEndy);}}/*放大直线*/if(key==PAGEUP){if(lineStartx>=10 && lineStarty>=40 && lineEndx <=620 && lineEndy <=445){setwritemode(XOR_PUT);line(lineStartx,lineStarty,lineEndx,lineEndy);/*如果直线是倾斜的*/if((lineStarty!=lineEndy)&& (lineStartx!=lineEndx)){/*计算直线的倾角*/angle=atan((fabs(lineEndy-lineStarty))/(fabs(lineEndx-lineStartx)));/*计算水平增量*/increment_x=cos(angle)*2;/*计算垂直增量*/increment_y=sin(angle)*2;/*计算放大后的起始坐标*/if(lineStartx<lineEndx){lineStartx-=increment_x;lineStarty-=increment_y;lineEndx+=increment_x;lineEndy+=increment_y;}if(lineStartx>lineEndx){lineEndx-=increment_x;lineEndy-=increment_y;lineStartx+=increment_x;lineStarty+=increment_y;}}/*如果直线竖直的*/if(lineStarty==lineEndy){lineStartx-=5;lineEndx+=5;}/*如果直线是水平的*/if(lineStartx==lineEndx){lineStarty-=5;lineEndy+=5;}line(lineStartx,lineStarty,lineEndx,lineEndy);}}/*缩小直线*/if(key==PAGEDOWN){if(lineStartx>=10 && lineStarty>=40 && lineEndx <=620 && lineEndy <=445){setwritemode(XOR_PUT);line(lineStartx,lineStarty,lineEndx,lineEndy);/*如果直线是倾斜的*/if((lineStarty!=lineEndy)&& (lineStartx!=lineEndx)){/*计算直线的倾角*/angle=atan((fabs(lineEndy-lineStarty))/(fabs(lineEndx-lineStartx)));/*计算水平减少量*/increment_x=cos(angle)*2;/*计算垂直减少量*/increment_y=sin(angle)*2;/*计算缩小后的起始坐标*/if(lineStartx<lineEndx){lineStartx+=increment_x;lineStarty+=increment_y;lineEndx-=increment_x;lineEndy-=increment_y;}if(lineStartx>lineEndx){lineEndx+=increment_x;lineEndy+=increment_y;lineStartx-=increment_x;lineStarty-=increment_y;}}/*如果直线竖直的*/if(lineStarty==lineEndy){lineStartx+=5;lineEndx-=5;}/*如果直线是水平的*/if(lineStartx==lineEndx){lineStarty+=5;lineEndy-=5;}line(lineStartx,lineStarty,lineEndx,lineEndy); }}}DrawMouse(x1,y1);}}DrawMouse(last_x,last_y);}/*绘制矩形函数*/void DrawRectangle(){int x0,y0,x1,y1;int last_x=0,last_y=0;int endFlag=0;int key;DrawMouse(last_x,last_y);while(1){/*单击右键,结束绘制矩形*/while((mouseStatus(&x1,&y1)==RIGHT_PRESSED))endFlag=1;if(endFlag==1)break;/*移动鼠标,仅仅绘制鼠标即可*/while(mouseStatus(&x1,&y1) == NO_PRESSED){if(last_x!=x1||last_y!=y1){DrawMouse(last_x,last_y);DrawMouse(x1,y1);last_x=x1;last_y=y1;}}/*单击左键开始绘制矩形*/if(mouseStatus(&x0,&y0)==LEFT_PRESSED){DrawMouse(last_x,last_y);rectangle(x0,y0,x1,y1);last_x=x1;last_y=y1;/*按着鼠标左键不动,绘制矩形*/while(mouseStatus(&x1,&y1)==LEFT_PRESSED) {if(last_x!=x1||last_y!=y1){rectangle(x0,y0,last_x,last_y); rectangle(x0,y0,x1,y1);last_x=x1;last_y=y1;}}/*绘制结束后,记录左上角和右下角的坐标*/ TOPx=x0;TOPy=y0;BOTTOMx=x1;BOTTOMy=y1;while(1){key=bioskey(0);if(key==ESC)break;/*放大矩形*/if(key==PAGEUP){if(TOPx>=10 && TOPy>=40 && BOTTOMx <=620 &&BOTTOMy <=445) {/*清除原有的直线*/setwritemode(XOR_PUT);rectangle(TOPx,TOPy,BOTTOMx,BOTTOMy);/*左上角坐标减小*/TOPx-=5;TOPy-=5;/*右下角坐标增加*/BOTTOMx+=5;BOTTOMy+=5;/*绘制放大后的矩形*/rectangle(TOPx,TOPy,BOTTOMx,BOTTOMy);}}/*缩小矩形*/if(key==PAGEDOWN){if(TOPx>=10 && TOPy>=40 && BOTTOMx <=620 &&BOTTOMy <=445) {setwritemode(XOR_PUT);rectangle(TOPx,TOPy,BOTTOMx,BOTTOMy);/*左上角坐标增加*/TOPx+=5;TOPy+=5;/*右下角坐标减小*/BOTTOMx-=5;BOTTOMy-=5;/*绘制缩小后的矩形*/rectangle(TOPx,TOPy,BOTTOMx,BOTTOMy);}}/*左移矩形*/if(key==LEFT){if(TOPx>=10 && TOPy>=40 && BOTTOMx <=620 &&BOTTOMy <=445) {setwritemode(XOR_PUT);rectangle(TOPx,TOPy,BOTTOMx,BOTTOMy);/*横坐标减小*/TOPx-=5;BOTTOMx-=5;rectangle(TOPx,TOPy,BOTTOMx,BOTTOMy);}}/*右移矩形*/if(key==RIGHT){if(TOPx>=10 && TOPy>=40 && BOTTOMx <=620 &&BOTTOMy <=445) {setwritemode(XOR_PUT);rectangle(TOPx,TOPy,BOTTOMx,BOTTOMy);/*横坐标增加*/TOPx+=5;BOTTOMx+=5;rectangle(TOPx,TOPy,BOTTOMx,BOTTOMy);}}/*下移矩形*/if(key==DOWN){if(TOPx>=10 && TOPy>=40 && BOTTOMx <=620 &&BOTTOMy <=445) {setwritemode(XOR_PUT);rectangle(TOPx,TOPy,BOTTOMx,BOTTOMy);/*纵坐标增加*/TOPy+=5;BOTTOMy+=5;rectangle(TOPx,TOPy,BOTTOMx,BOTTOMy);}}/*上移矩形*/if(key==UP){if(TOPx>=10 && TOPy>=40 && BOTTOMx <=620 &&BOTTOMy <=445) {setwritemode(XOR_PUT);rectangle(TOPx,TOPy,BOTTOMx,BOTTOMy);/*纵坐标减小*/TOPy-=5;BOTTOMy-=5;rectangle(TOPx,TOPy,BOTTOMx,BOTTOMy); }}}DrawMouse(x1,y1);}}DrawMouse(last_x,last_y);}/*用直线法生成圆*/void LineToCircle(int x0,int y0,int r){int angle;int x1,y1,x2,y2;angle=0;x1=r*cos(angle*pi/180);y1=r*sin(angle*pi/180);while(angle<45){angle+=5;x2=r*cos(angle*pi/180);y2=r*sin(angle*pi/180);while(x2==x1)x2++;while(y2==y1)y2++;line(x0+x1,y0+y1,x0+x2,y0+y2);line(x0-x1,y0+y1,x0-x2,y0+y2);line(x0+x1,y0-y1,x0+x2,y0-y2);line(x0-x1,y0-y1,x0-x2,y0-y2);line(x0+y1,y0-x1,x0+y2,y0-x2);line(x0+y1,y0+x1,x0+y2,y0+x2);line(x0-y1,y0-x1,x0-y2,y0-x2);line(x0-y1,y0+x1,x0-y2,y0+x2);x1=x2+1;y1=y2+1;}}/*绘制圆函数*/void DrawCircle(){int x0,y0,x1,y1,r,oldr;int last_x,last_y;int endFlag;int key;last_x=0;last_y=0;endFlag=0;DrawMouse(last_x,last_y);while(1){/*单击右键,绘制圆结束*/while((mouseStatus(&x1,& y1)==RIGHT_PRESSED)) {endFlag=1;}if(endFlag==1)break;/*移动鼠标,仅绘制鼠标即可*/while(mouseStatus(&x1,&y1) == NO_PRESSED){if(last_x!=x1||last_y!=y1){DrawMouse(last_x,last_y);DrawMouse(x1,y1);last_x=x1;last_y=y1;}}/*单击左键,开始绘制圆*/if(mouseStatus(&x0,&y0)==LEFT_PRESSED){/*计算半径*/r=sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)); DrawMouse(last_x,last_y);LineToCircle(x0,y0,r);last_x=x1;last_y=y1;oldr=r;/*按住鼠标左键不动,拖动鼠标绘制圆*/while(mouseStatus(&x1,&y1)==LEFT_PRESSED){if(last_x!=x1||last_y!=y1){r=sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1));LineToCircle(x0,y0,oldr);LineToCircle(x0,y0,r);last_x=x1;last_y=y1;oldr=r;}}/*绘制结束后,记录圆的圆心和半径*/Rx=x0;Ry=y0;R=r;while(1){key=bioskey(0);if(key==ESC)break;/*放大圆*/if(key==PAGEUP){if(Rx-R>10 && Ry-R>40 && Rx+R<620 && Ry+R<445) {/*如果半径和初始状态一样大,则保留原来的圆*/ if(R==r){setcolor(WHITE);R+=10;circle(Rx,Ry,R);}else{setcolor(BLACK);/*用背景色画圆,覆盖原有的*/circle(Rx,Ry,R);/*增加半径*/R+=10;setcolor(WHITE);/*绘制新圆*/circle(Rx,Ry,R);}}}/*缩小圆*/if(key==PAGEDOWN){if(Rx-R>10 && Ry-R>40 && Rx+R<620 && Ry+R<445) {/*如果半径和初始状态一样大,则保留原来的圆*/ if(R==r){setcolor(WHITE);R-=10;circle(Rx,Ry,R);}else{setcolor(BLACK);/*用背景色画圆,覆盖原有的*/circle(Rx,Ry,R);setcolor(WHITE);/*减小半径*/R-=10;circle(Rx,Ry,R);}}}}DrawMouse(x1,y1);}}DrawMouse(last_x,last_y);}/*求阶乘函数*/long factorial(int n){long s=1;if(n==0)return 1;while(n>0){s*=n;n--;}return s;}/*伯恩斯坦基函数*/float berFunction(int i,int n,double t){if(i==0&&t==0||t==1&&i==n)return 1;else if(t==0||t==1)return 0;return factorial(n)/(factorial(i)*factorial(n-i))*pow(t,i)*pow(1-t,n-i); }/*绘制Bezier曲线函数*/void DrawBezier(){int x,y,x0,y0,x1,y1;float j,t,dt;int i,n;int endFlag=0;int last_x=0,last_y=0;n=0;DrawMouse(last_x,last_y);while(mouseStatus(&x1,&y1)==LEFT_PRESSED);while(1){while((mouseStatus(&x1,&y1)==RIGHT_PRESSED))endFlag=1;if(endFlag==1)break;/*如果有两个以上的点,则将其连接,即画直线*/if(n>1)line(linePoint_x[n-1],linePoint_y[n-1],linePoint_x[n-2],linePoint_y[n-2]);/*移动鼠标*/while(mouseStatus(&x1,&y1) == NO_PRESSED) {if(last_x!=x1||last_y!=y1){DrawMouse(last_x,last_y);DrawMouse(x1,y1);last_x=x1;last_y=y1;}}/*单击左键时,绘制点*/while(mouseStatus(&x0,&y0)==LEFT_PRESSED); putpixel(x0,y0,14);/*记录每次鼠标左键单击的点坐标*/linePoint_x[n]=x0;linePoint_y[n]=y0;n++;}DrawMouse(x1,y1);dt=1.0/10;setwritemode(0);for(j=0;j<=10;j+=0.5){t=j*dt;x=0;y=0;i=0;while(i<n-1){x+=berFunction(i,n-2,t)*linePoint_x[i];y+=berFunction(i,n-2,t)*linePoint_y[i];i++;}if(j==0)moveto(x,y);lineto(x,y);}setwritemode(1);void main(){int gdriver,gmode;int x0,y0,x1,y1;int last_x,last_y;int i;x0=250;y0=250;gdriver=DETECT;while( 1){initgraph(&gdriver,&gmode,"");setbkcolor(0);setcolor(14);/*绘制画布*/rectangle(5,30,630,445);setfillstyle(1,2);/*填充画布以外的颜色,画布仍呈背景色*/ floodfill(10,10,14);/*绘制按钮框*/for(i=0;i<=7;i++){setcolor(RED);line(60*i+1,2,60*i+1,25);line(60*i+1,2,60*i+55,2);setcolor(RED);line(60*i+1,25,60*i+55,25);line(60*i+55,2,60*i+55,25);}setcolor(RED);line(0,446,639,446);line(0,478,639,478);setcolor(8);/*绘制退出按钮框*/rectangle(570,2,625,25);setfillstyle(1,RED);floodfill(620,5,8);setcolor(WHITE);outtextxy(585,10,"EXIT");/*显示“直线”*/outChinese(zhi16K, 16, 10,6, WHITE);outChinese(xian16K, 16, 28,6, WHITE);/*显示“矩形”*/outChinese(ju16K, 16, 70,6, WHITE);outChinese(xing16K, 16, 88,6, WHITE);/*显示“圆形”*/outChinese(yuan16K, 16, 130,6, WHITE);outChinese(xing16K, 16, 148,6, WHITE);outtextxy(185,10,"Bezier");/*显示“清屏”*/outChinese(qing16K, 16, 250,6, WHITE);outChinese(ping16K, 16, 268,6, WHITE);/*显示“保存”*/outChinese(bao16K, 16, 310,6, WHITE);outChinese(cun16K, 16, 328,6, WHITE);/*显示“加载”*/outChinese(jia16K, 16, 370,6, WHITE);outChinese(zai16K, 16, 388,6, WHITE);/*显示“帮助”*/outChinese(bang16K, 16, 430,6, WHITE);outChinese(zhu16K, 16, 448,6, WHITE);setMousePos(x0,y0);setwritemode(1);DrawMouse(x0,y0);last_x=x0;last_y=y0;while(!((mouseStatus(&x1,&y1)==NO_PRESSED) && x1>240 &&x1<295&&y1>1&&y1<25)){/*单击退出按钮*/if((mouseStatus(&x1,&y1)==NO_PRESSED) && x1>570 &&x1<625&&y1>1&&y1<25)exit(0);/*鼠标移动*/while(mouseStatus(&x1,&y1) == NO_PRESSED||y1>25) {if(last_x!=x1 && last_y!=y1){DrawMouse(last_x,last_y);DrawMouse(x1,y1);last_x=x1;last_y=y1;}}DrawMouse(last_x,last_y);/*在按钮框中单击左键后*/while(mouseStatus(&x1,&y1)==LEFT_PRESSED);/*绘制直线*/if(x1>0 && x1<60 && y1>1 && y1<25){setwritemode(0);setcolor(8);/*呈凹陷状态*/line(1,2,1,25);line(1,2,55,2);setcolor(15);line(1,25,55,25);line(55,2,55,25);setwritemode(1);DrawLine();setwritemode(0);setcolor(RED);/*还原成初始状态*/rectangle(1,2,55,25);setcolor(15);setwritemode(1);DrawMouse(last_x,last_y);}/*绘制矩形*/if(x1>60 && x1<115 && y1>1 && y1<25){setwritemode(0);setcolor(8);line(61,2,61,25);line(61,2,115,2);setcolor(15);line(61,25,115,25);line(115,2,115,25);setwritemode(1);DrawRectangle();setwritemode(0);setcolor(RED);rectangle(61,2,115,25);setcolor(15);setwritemode(1);DrawMouse(last_x,last_y);}/*绘制圆形*/if(x1>120 && x1<175 && y1>1 && y1<25) {setwritemode(0);setcolor(8);line(121,2,121,25);line(121,2,175,2);setcolor(15);line(121,25,175,25);line(175,2,175,25);setwritemode(1);DrawCircle();setwritemode(0);setcolor(RED);rectangle(121,2,175,25);setcolor(15);setwritemode(1);DrawMouse(last_x,last_y);}/*绘制Bezier曲线*/if(x1>180 && x1<235 && y1>1 && y1<25) {setwritemode(0);setcolor(8);line(181,2,181,25);line(181,2,235,2);setcolor(15);line(181,25,235,25);line(235,2,235,25);setwritemode(1);DrawBezier();setwritemode(0);setcolor(RED);rectangle(181,2,235,25);setcolor(15);setwritemode(1);DrawMouse(last_x,last_y);}/*保存文件*/if(x1>300 && x1<355 && y1>1 && y1<25) {setwritemode(0);setcolor(8);line(301,2,301,25);line(301,2,355,2);setcolor(15);line(301,25,355,25);line(355,2,355,25);setwritemode(1);save();setwritemode(0);setcolor(RED);rectangle(301,2,355,25);setcolor(15);setwritemode(1);DrawMouse(last_x,last_y);}/*加载已有的文件*/if(x1>360 && x1<415 && y1>1 && y1<25) {setwritemode(0);setcolor(8);line(361,2,361,25);line(361,2,415,2);setcolor(15);line(361,25,415,25);line(415,2,415,25);setwritemode(1);load();setwritemode(0);setcolor(RED);rectangle(361,2,415,25);setcolor(15);setwritemode(1);DrawMouse(last_x,last_y);}/*显示用户帮助*/if(x1>420 && x1<475 && y1>1 && y1<25) {setwritemode(0);setcolor(8);line(421,2,421,25);line(421,2,475,2);setcolor(15);line(421,25,475,25);line(475,2,475,25);setwritemode(1);showHelp();setwritemode(0);setcolor(RED);rectangle(421,2,475,25);setcolor(15);setwritemode(1);DrawMouse(last_x,last_y);}}closegraph();}}(注:文档可能无法思考全面,请浏览后下载,供参考。