My Project
C:/Users/Lab_411_02/Desktop/Violet1 - dox/violet/violet/UserControl1.xaml.cs
查看本檔案說明文件.
1 
2 using ShapeLib.VShape;
3 using System;
4 using System.Collections;
7 using System.Collections.Generic;
8 using System.ComponentModel;
9 using System.Diagnostics;
10 using System.IO;
11 using System.Linq;
12 using System.Text;
13 using System.Threading;
14 using System.Threading.Tasks;
15 using System.Windows;
18 using System.Windows.Controls;
19 using System.Windows.Data;
20 using System.Windows.Documents;
21 using System.Windows.Input;
22 using System.Windows.Media;
23 using System.Windows.Media.Imaging;
24 using System.Windows.Navigation;
25 using System.Windows.Shapes;
26 using System.Windows.Threading;
27 using System.Xml;
28 using System.Xml.Serialization;
29 
30 
31 namespace violet
32 {
33 
37  public partial class UserControl1 : UserControl
38  {
39 
40  public UserControl1()
41  {
43  hiddenCanvas();
44 
47  // shapeLib.initControl(mygrid, myControl);
48 
49  }
53  public int drawtype = 1;
54  public String colortype = "black";
55  public int lineSpace = 9;
56  /*public GraphDoc gdc = new GraphDoc();*/
57  public checkHitDraw chd = new checkHitDraw();
58  public RUse ru = new RUse();
60  byte colorR = 0;
61  byte colorG = 0;
62  byte colorB = 0;
63  int strokeT = 3;
64 
65 
66  String Status = "rest";
67  // Point pStart, pEnd; /// <summary>
72  // Point tempStart;
73  Point p0, p1, p2, p3 = new Point(0, 0);
74  BezierSegment bezier = new BezierSegment();
77  PathFigure figure = new PathFigure();
78  PathGeometry geometry = new PathGeometry();
79 
80 
81 
82  Geometry tempGeo;
83  // gPath tempFPath;
84  // Ellipse myEllipse; /// <summary>
87  // Rectangle myRect, cornerRect, sideRect;
88  Line myLine;// controlLine; /// <summary>
91  // Polygon myTri;
92  System.Windows.Shapes.Path myPath = new System.Windows.Shapes.Path();
96  System.Windows.Shapes.Path controlPath = new System.Windows.Shapes.Path();
100 
101  bool bfirst = true;
102  // bool bCanMove = false; /// <summary>
106  // bool bhave = false; //you have choose
107  // bool gCanMove = false; /// <summary>
109  bool bConThing = false;
110  // bool OnIt = false; /// <summary>
113 
114 
115 
119  private Point correctPoint(Point p)
120  {
121  Point temp = p;
122  double tempDX = temp.X % lineSpace;
123  double tempDY = temp.Y % lineSpace;
124  if (temp.X % lineSpace != 0)
125  {
126  temp.X = lineSpace * Math.Round((temp.X / lineSpace), 0);
127  }
128  if (temp.Y % lineSpace != 0)
129  {
130  temp.Y = lineSpace * Math.Round((temp.Y / lineSpace), 0);
131  }
132  return temp;
133  }
134 
138  public void hiddenCanvas()
139  {
140  myControl.Visibility = Visibility.Hidden;
141  }
145  void showCanvas()
146  {
147  myControl.Visibility = Visibility.Visible;
148  }
149 
153  //void drawCurve(int xStart, int yStart, int xEnd, int yEnd)
154  //{
155  // if (bfirst)
156  // {
157  // if (Status.Equals("rest"))
158  // {
159  // bfirst = false;
160  // bezier = new BezierSegment();
161  // bezier.Point3 = new Point(xEnd, yEnd);
162  // figure = new PathFigure();
163  // figure.StartPoint = new Point(xStart, yStart);
164  // bezier.Point1 = figure.StartPoint;
165  // bezier.Point2 = bezier.Point3;
166  // p0 = figure.StartPoint;
167  // p1 = bezier.Point1;
168  // figure.Segments.Add(bezier);
169  // geometry = new PathGeometry();
170  // geometry.Figures.Add(figure);
171  // myPath = new System.Windows.Shapes.Path();
172  // myPath.Stroke = new SolidColorBrush(Color.FromRgb(colorR, colorG, colorB));
173  // myPath.StrokeThickness = strokeT;
174  // myPath.Data = geometry;
175 
176  // mygrid.Children.Add(myPath);
177  // Status = "work1";
178  // }
179  // else if (Status.Equals("work1"))
180  // {
181  // bfirst = false;
182  // mygrid.Children.Remove(myPath);
183  // bezier.Point1 = new Point(xEnd, yEnd);
184  // bezier.Point3 = p3;
185  // figure = new PathFigure();
186  // figure.StartPoint = p0;
187  // figure.Segments.Add(bezier);
188  // geometry = new PathGeometry();
189  // geometry.Figures.Add(figure);
190  // myPath.Data = geometry;
191 
192  // mygrid.Children.Add(myPath);
193  // Status = "work2";
194  // }
195  // else if (Status.Equals("work2"))
196  // {
197  // bfirst = false;
198  // mygrid.Children.Remove(myPath);
199  // bezier.Point2 = new Point(xEnd, yEnd);
200  // bezier.Point1 = p1;
201  // bezier.Point3 = p3;
202  // figure = new PathFigure();
203  // figure.StartPoint = p0;
204  // figure.Segments.Add(bezier);
205  // geometry = new PathGeometry();
206  // geometry.Figures.Add(figure);
207  // myPath.Data = geometry;
208  // tempGeo = geometry;
209  // mygrid.Children.Add(myPath);
210  // Status = "rest";
211  // }
212  // }
213  // else
214  // {
215  // if (Status.Equals("work1"))
216  // {
217  // bezier.Point3 = new Point(xEnd, yEnd);
218  // p3 = bezier.Point3;
219  // bezier.Point2 = bezier.Point3;
220  // p2 = bezier.Point2;
221  // }
222  // else if (Status.Equals("work2"))
223  // {
224  // bezier.Point1 = new Point(xEnd, yEnd);
225  // p1 = bezier.Point1;
226  // }
227  // else if (Status.Equals("rest"))
228  // {
229  // bezier.Point2 = new Point(xEnd, yEnd);
230  // p2 = bezier.Point2;
231  // }
232  // }
233  //}
237  //void Shapes_MouseEnter_Hands(object sender, MouseEventArgs e)
238  //{
239  // // OnIt = true;
240  // this.Cursor = System.Windows.Input.Cursors.Hand;
241  //}
242  //void Shapes_MouseEnter_SizeAll(object sender, MouseEventArgs e)
243  //{
245  // this.Cursor = System.Windows.Input.Cursors.SizeAll;
246  //}
247  //void Shapes_MouseLeave(object sender, MouseEventArgs e)
248  //{
249  // // OnIt = false;
250  // this.Cursor = System.Windows.Input.Cursors.Arrow;
251  //}
252 
253  /*-------------- 鍵盤事件 --------------*/
254  private void UserControl_KeyDown(object sender, KeyEventArgs e)
258  {
259  // shapeLib.InsertText(e.Key.ToString());
260 
261  /* if (e.Key == Key.Delete)
262  {
263  foreach(gPath gp in shapeLib.Data.multiSelList)
264  {
265  gp.isSel = false;
266  gp.IsDelete = true;
267 
268  }
269  shapeLib.Data.multiSelList.Clear();
270 
271  }*/
272 
273  if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
274  {
275  if (e.Key == Key.C)
276  {
277  shapeLib.copy();
278  }
279  else if (e.Key == Key.V)
280  {
281  shapeLib.paste();
282  }
283  }
284  }
285 
286  public void ClearBtnUse()
287  {
290  if (MessageBox.Show("你確定要清除畫布嗎? 若要你的檔案將會全部遺失!", "警告", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
291  {
292  mygrid.Children.Clear();
293  shapeLib.Data.gdc.sroot.PathList.Clear();
294  shapeLib.Data.gdc.FullList.Clear();
295  shapeLib.Data.gdc.UndoStack.Clear();
297  }
298  }
299  public void ClearDrawing()
300  {
303  mygrid.Children.Clear();
304  shapeLib.Data.gdc.sroot.PathList.Clear();
305  }
306  public void hideBackLine()
307  {
310  myBackground.Children.Clear();
311  }
312  public void drawBackLine(double w, double h, double opac)
316  {
317  int i;
318  int height = (int)h;
319  int width = (int)w;
320  int tempStroke = strokeT;
321  byte tmpR = colorR;
322  byte tmpG = colorG;
323  byte tmpB = colorB;
324  colorR = 0;
325  colorG = 0;
326  colorB = 0;
327  strokeT = 1;
328  for (i = 0; i <= height; i += lineSpace)
329  {
330  myLine = new Line();
331  myLine.Stroke = new SolidColorBrush(Color.FromRgb(colorR, colorG, colorB));
332  myLine.X1 = 0;
333  myLine.X2 = width;
334  myLine.Y1 = i;
335  myLine.Y2 = i;
336  myLine.HorizontalAlignment = HorizontalAlignment.Left;
337  myLine.VerticalAlignment = VerticalAlignment.Center;
338  myLine.StrokeThickness = strokeT;
339  myLine.Opacity = opac;
340  myBackground.Children.Add(myLine);
341  }
342  for (i = 0; i <= width; i += lineSpace)
343  {
344  myLine = new Line();
345  myLine.Stroke = new SolidColorBrush(Color.FromRgb(colorR, colorG, colorB));
346  myLine.X1 = i;
347  myLine.X2 = i;
348  myLine.Y1 = 0;
349  myLine.Y2 = height;
350  myLine.HorizontalAlignment = HorizontalAlignment.Left;
351  myLine.VerticalAlignment = VerticalAlignment.Center;
352  myLine.StrokeThickness = strokeT;
353  myLine.Opacity = opac;
354  myBackground.Children.Add(myLine);
355  }
356  colorR = tmpR;
357  colorG = tmpG;
358  colorB = tmpB;
359  strokeT = tempStroke;
360  }
361  public void stroke(int stroketype)
362  {
365  switch (stroketype)
366  {
367  case 1:
368  shapeLib.Data.strokeT = 1;
369  break;
370  case 3:
371  shapeLib.Data.strokeT = 3;
372  break;
373  case 5:
374  shapeLib.Data.strokeT = 5;
375  break;
376  case 8: //可考慮拿掉,有點太粗了
377  shapeLib.Data.strokeT = 8;
378  break;
379  }
380  }
381  public void color(String CName)
382  {
385  colortype = CName;
386  switch (colortype)
387  {
388  case "red":
389  colorR = 255;
390  colorG = 0;
391  colorB = 0;
392  break;
393  case "orange":
394  colorR = 255;
395  colorG = 165;
396  colorB = 0;
397  break;
398  case "yellow":
399  colorR = 255;
400  colorG = 230;
401  colorB = 0;
402  break;
403  case "green":
404  colorR = 0;
405  colorG = 128;
406  colorB = 0;
407  break;
408  case "blue":
409  colorR = 0;
410  colorG = 0;
411  colorB = 128;
412  break;
413  case "black":
414  colorR = 0;
415  colorG = 0;
416  colorB = 0;
417  break;
418  case "white":
419  colorR = 255;
420  colorG = 255;
421  colorB = 255;
422  break;
423  case "violet":
424  colorR = 138;
425  colorG = 43;
426  colorB = 226;
427  break;
428  case "gray":
429  colorR = 128;
430  colorG = 128;
431  colorB = 128;
432  break;
433  }
434  }
435  public void RUdo(int Act) //redo undo used
436  {
437  if (!bConThing)
438  {
439  if (Act == 0)
440  {
441  shapeLib.Data.gdc.unDo();
442  // reDraw(true);
443  }
444  if (Act == 1)
445  {
446  shapeLib.Data.gdc.reDo();
447  // reDraw(true);
448  }
449  }
450  }
451  private String pathDataToPoint(String Data)
455  {
456  String tempStr = Data;
457  int tmpMSeat = tempStr.IndexOf("M");
458  tempStr = tempStr.Remove(tmpMSeat, 1);
459 
460  String[] tempAry = tempStr.Split('C');
461  tempStr = tempAry[0] + "," + tempAry[1];
462  //Debug.WriteLine(tempStr);
463  return tempStr;
464  }
465 
466  /*-------------- 圖檔使用 --------------*/
467  public void initpath(string xml)
468  {
471  XmlSerializer serializer = new XmlSerializer(typeof(SVGRoot));
472  using (MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml)))
473  {
476  shapeLib.Data.gdc.sroot = (SVGRoot)serializer.Deserialize(XmlReader.Create(ms));
477  //reDraw(true);
478  }
479  }
480  private void UserControl_Unloaded(object sender, RoutedEventArgs e)
484  {
485  myControl.Children.Clear();
486  int margin = (int)mygrid.Margin.Left;
487  int width = (int)mygrid.ActualWidth + (int)mygrid.Margin.Left + (int)mygrid.Margin.Right;
488  int height = (int)mygrid.ActualHeight + (int)mygrid.Margin.Top + (int)mygrid.Margin.Bottom;
489  mygrid.Background = new SolidColorBrush(Color.FromArgb(255, 255, 0, 255));
490 
491  RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
492  DrawingVisual dv = new DrawingVisual();
493  using (DrawingContext dc = dv.RenderOpen())
494  {
495  Rect r = new Rect(new System.Windows.Point(0, 0), new System.Windows.Size(width, height));
496 
497  dc.DrawRectangle(new SolidColorBrush(Colors.White), new Pen(), r);
498  VisualBrush vb = new VisualBrush(mygrid);
499 
500  vb.Stretch = Stretch.UniformToFill;
501  dc.DrawRectangle(vb, null, r);
502 
503  }
504  rtb.Render(dv);
505  //save the ink to a memory stream
506  BmpBitmapEncoder encoder = new BmpBitmapEncoder();
507  encoder.Frames.Add(BitmapFrame.Create(rtb));
508  byte[] bitmapBytes;
509  using (MemoryStream ms = new MemoryStream())
510  {
511  encoder.Save(ms);
512  //get the bitmap bytes from the memory stream
513  ms.Position = 0;
514  bitmapBytes = ms.ToArray();
515  }
516 
517  Utility _utility = new Utility();
518 
519  _utility.BitmapBytes = bitmapBytes;
520 
521  using (MemoryStream stream = new MemoryStream())
522  {
523 
524  XmlSerializer s = new XmlSerializer(typeof(SVGRoot));
525 
526  s.Serialize(XmlWriter.Create(stream), shapeLib.Data.gdc.sroot);
527 
528  stream.Flush();
529  stream.Seek(0, SeekOrigin.Begin);
530 
531  StreamReader sr = new StreamReader(stream);
532  string myStr = sr.ReadToEnd();
533 
534  _utility.xml = myStr;
535  }
536 
537  // this.Dispose(true);
538  // this.Close();
539  // _utility.TagName = "test";// cbxTagName.SelectedItem.ToString();
541  ClearDrawing();
542  }
543 
544  }
545 }
Point p0
繪製曲線時的狀態
bool bfirst
紀錄控制後的曲線
static GModel Data
Definition: shapeLib.cs:42
void InitializeComponent()
InitializeComponent
internal void AddPictureContentControl(Utility _utility)
Definition: ThisAddIn.cs:114
void hiddenCanvas()
隱藏myControl
List< gPath > FullList
Definition: GraphDoc.cs:41
void UserControl_KeyDown(object sender, KeyEventArgs e)
繪製曲線
List< gPath > PathList
Definition: GraphDoc.cs:30
int drawtype
初始設定
internal System.Windows.Controls.Canvas myBackground
void color(String CName)
byte [] BitmapBytes
Definition: Utility.cs:14
void drawBackLine(double w, double h, double opac)
畫背景格線
使UNIX上開發的C程式移植到windows上 /summary>
Definition: Form1.cs:19
void stroke(int stroketype)
BezierSegment bezier
紀錄四個控制點使用
string xml
Definition: Utility.cs:21
System.Windows.Shapes.Path myPath
static void copy()
Definition: shapeLib.cs:55
static internal ThisAddIn ThisAddIn
System.Windows.Shapes.Path controlPath
紀錄曲線
internal System.Windows.Controls.Canvas mygrid
void unDo()
undo 回到前一狀態
Definition: GraphDoc.cs:171
String pathDataToPoint(String Data)
調整顏色
internal System.Windows.Controls.Canvas myControl
Point correctPoint(Point p)
是否有選取物件 ///
void UserControl_Unloaded(object sender, RoutedEventArgs e)
關閉時,轉成圖片
void reDo()
重作到目前狀態
Definition: GraphDoc.cs:138
static void paste()
Definition: shapeLib.cs:83
bool bConThing
判斷是否為繪製新圖形
void initpath(string xml)
void showCanvas()
顯示myControl