交互式绘图基础
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 9 namespace 交互绘图 10 { 11 public partial class Form1 : Form 12 { 13 public Form1() 14 { 15 InitializeComponent(); 16 } 17 int x1, y1; 18 int flat = 1; 19 bool b1, b2, b3, b4, b5; 20 21 private void pictureBox1_Click(object sender, EventArgs e) 22 { 23 24 } 25 26 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 27 { 28 int x2, y2; 29 Graphics g = pictureBox1.CreateGraphics(); 30 Pen p1 = new Pen(Color.Chocolate, 3); 31 if (b1 == true) 32 { 33 if (flat == 1) 34 { 35 x1 = e.X; y1 = e.Y; flat = 0; 36 } 37 else 38 { 39 x2 = e.X; y2 = e.Y; 40 g.DrawLine(p1, x1, y1, x2, y2); 41 flat = 1; 42 } 43 } 44 if (b2 == true) 45 { 46 if (flat == 1) 47 { 48 x1 = e.X; y1 = e.Y; flat = 0; 49 } 50 else 51 { 52 x2 = e.X; y2 = e.Y; 53 g.DrawEllipse(p1, x1, y1, (float)Math.Abs(x1 - x2), (float)Math.Abs(y1 - y2)); 54 flat = 1; 55 } 56 } 57 if (b3 == true) 58 { 59 if (flat == 1) 60 { 61 x1 = e.X; y1 = e.Y; flat = 0; 62 } 63 else 64 { 65 x2 = e.X; y2 = e.Y; 66 g.DrawRectangle(p1, x1, y1, (float)Math.Abs(x1 - x2), (float)Math.Abs(y1 - y2)); 67 flat = 1; 68 } 69 } 70 b5 = true; 71 } 72 73 private void 直线ToolStripMenuItem_Click(object sender, EventArgs e) 74 { 75 b1 = true; flat = 1; b2 = false; b3 = false; b4 = false; 76 } 77 78 private void 圆ToolStripMenuItem_Click(object sender, EventArgs e) 79 { 80 b1 = false; flat = 1; b2 = true; b3 = false; b4 = false; 81 } 82 83 private void 矩形ToolStripMenuItem_Click(object sender, EventArgs e) 84 { 85 b1 =false; flat = 1; b2 = false; b3 =true; b4 = false; 86 } 87 88 private void Form1_Load(object sender, EventArgs e) 89 { 90 pictureBox1.BackColor = Color.DarkGray; 91 } 92 } 93 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 9 namespace print 10 { 11 public partial class Form1 : Form 12 { 13 string str; 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 private void button1_Click(object sender, EventArgs e) 20 { 21 openFileDialog1.Multiselect = true; 22 openFileDialog1.ShowDialog(); 23 str = openFileDialog1.FileName; 24 //pictureBox1.Image = Image.FromFile(str); 25 try 26 { 27 pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); 28 } 29 catch (Exception EX) 30 { 31 MessageBox.Show(EX.Message); 32 } 33 } 34 35 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 36 { 37 try 38 { 39 Bitmap bitmap = new Bitmap(str); 40 e.Graphics.DrawImage(bitmap,100,200,600,800); 41 } 42 catch(Exception ex) 43 { 44 MessageBox.Show(ex.Message); 45 } 46 } 47 48 private void button2_Click(object sender, EventArgs e) 49 { 50 //printPreviewDialog1.Document = this.printDocument1; 51 printPreviewDialog1.ShowDialog(); 52 } 53 54 private void button3_Click(object sender, EventArgs e) 55 { 56 //Application.Exit(); 57 this.Close(); 58 } 59 60 private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) 61 { 62 MessageBox.Show("即将出现要打印的内容!"); 63 } 64 65 private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e) 66 { 67 //MessageBox.Show("恭喜您,打印成功!"); 68 } 69 70 private void Form1_Load(object sender, EventArgs e) 71 { 72 73 } 74 } 75 }
AutoCad中:运用dxf接口来开发。
win-touber-C编译,运行dxf文件,以下是代码。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include"stdio.h" 2 #include"conio.h" 3 FILE *fp; 4 void hsec(),endsec(); 5 void line(); 6 void arc(); 7 void text(); 8 void circle(); 9 main() 10 { 11 char a[8]="KEY40"; 12 clrscr(); 13 fp=fopen("d:\\wo.dxf","r"); 14 if(fp==NULL) 15 { 16 printf("can't open\n"); 17 exit(1); 18 } 19 hsec(fp); 20 line(fp,20.0,20.0,50.0,20.0); 21 line(fp,20.0,30.0,50.0,30.0); 22 arc(fp,20.0,25.0,5.0,90.0,270.0); 23 arc(fp,50.0,25.0,5.0,-90.0,90.0); 24 text(fp,30.0,45.0,3.5,0.0,0.0,a); 25 circle(fp,60.0,60.0,20.0); 26 text(fp,35.0,65.0,4.5,0.0,0.0,"我爱中国!"); 27 text(fp,50.0,85.0,3.0,0.0,0.0,"你好,欢迎访问!"); 28 circle(fp,40.0,60.0,10.0); 29 arc(fp,80.0,50.0,10.0,-90.0,90.0); 30 circle(fp,160.0,160.0,20.0); 31 line(fp,80.0,50.0,90.0,180.0); 32 circle(fp,60.0,60.0,20.0); 33 line(fp,60.0,60.0,100.0,100.0); 34 circle(fp,100.0,100.0,20.0); 35 line(fp,60.0,60.0,90.0,180.0); 36 endsec(fp); 37 fclose(fp); 38 } 39 40 void hsec(FILE *fp) 41 { 42 fprintf(fp,"0\nSECTION\n2\nENTITIES\n");/***\n为换行符***/ 43 } 44 void line(FILE *fp,double x1,double y1,double x2,double y2) 45 { 46 fprintf(fp,"0\nline\n8\nA\n10\n"); 47 fprintf(fp,"%9.4f\n",x1); 48 fprintf(fp,"20\n"); 49 fprintf(fp,"%9.4f\n",y1); 50 fprintf(fp,"11\n"); 51 fprintf(fp,"%9.4f\n",x2); 52 fprintf(fp,"21\n"); 53 fprintf(fp,"%9.4f\n",y2); 54 } 55 void arc(FILE *fp,double x,double y,double r,double sang,double eang) 56 { 57 fprintf(fp,"0\narc\n8\nA\n10\n"); 58 fprintf(fp,"%9.4f\n",x); 59 fprintf(fp,"20\n"); 60 fprintf(fp,"%9.4f\n",y); 61 fprintf(fp,"40\n"); 62 fprintf(fp,"%9.4f\n",r); 63 fprintf(fp,"50\n"); 64 fprintf(fp,"%9.4f\n",sang); 65 fprintf(fp,"51\n"); 66 fprintf(fp,"%9.4f\n",eang); 67 } 68 void circle(FILE *fp,double x,double y,double r) 69 { 70 fprintf(fp,"0\ncircle\n8\nA\n10\n"); 71 fprintf(fp,"%9.4f\n",x); 72 fprintf(fp,"20\n"); 73 fprintf(fp,"%9.4f\n",y); 74 fprintf(fp,"40\n"); 75 fprintf(fp,"%9.4f\n",r); 76 } 77 void text(FILE *fp,double x,double y,double h,double rota,double ang,char *tex) 78 { 79 fprintf(fp,"0\ntext\n8\nA\n10\n"); 80 fprintf(fp,"%9.4f\n",x); 81 fprintf(fp,"20\n"); 82 fprintf(fp,"%9.4f\n",y); 83 fprintf(fp,"40\n"); 84 fprintf(fp,"%9.4f\n",h); 85 fprintf(fp,"1\n"); 86 fprintf(fp,"%s\n",tex); 87 fprintf(fp,"50\n"); 88 fprintf(fp,"%9.4f\n",rota); 89 fprintf(fp,"51\n"); 90 fprintf(fp,"%9.4f\n",ang); 91 } 92 void endsec(FILE *fp) 93 { 94 fprintf(fp,"0\nENDSEC\n0\nEOF\n"); 95 }