C# 輸入兩點並劃出直線(X、Y只顯示-10~10) for 大偉

使用者輸入兩點座標,並畫出直線(X、Y只顯示-10~10)。
執行結果。

參考答案。


  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace CSharp_DrawLine
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private void Form1_Load(object sender, EventArgs e)
  21. {
  22. //----textbox事件----
  23. foreach (Control ctrl in Controls)
  24. {
  25. if (ctrl.GetType().Name == "TextBox")
  26. {
  27. ctrl.Enter += TextBox_Enter;
  28. ctrl.MouseClick += TextBox_Enter;
  29. }
  30. }
  31. }
  32. //----選到textbox時,選取全部文字
  33. private void TextBox_Enter(object sender, EventArgs e)
  34. {
  35. TextBox tb = sender as TextBox;
  36. tb.SelectAll();
  37. }
  38.  
  39. float x0, x1, y0, y1;
  40. float ratio_x, ratio_y;
  41. float wid, hei;
  42. Bitmap bmp;
  43. Graphics g;
  44. Pen pen;
  45. SolidBrush brush;
  46.  
  47. private void draw_btn_Click(object sender, EventArgs e)
  48. {
  49. try
  50. {
  51. //----畫筆顏色----
  52. pen = new Pen(Color.Black);
  53. brush = new SolidBrush(pen.Color);
  54. //----取得picturebox寬度與高度----
  55. wid = pictureBox1.Width;
  56. hei = pictureBox1.Height;
  57. //----是否有上一次的圖片,如果有就清除----
  58. if (pictureBox1.Image != null)
  59. pictureBox1.Image = null;
  60. if (bmp != null)
  61. bmp.Dispose();
  62. //----轉換使用者輸入的資料----
  63. x0 = float.Parse(x0_tb.Text);
  64. y0 = float.Parse(y0_tb.Text);
  65. x1 = float.Parse(x1_tb.Text);
  66. y1 = float.Parse(y1_tb.Text);
  67. //----計算放大倍率----
  68. ratio_x = (wid - 50) / 20;
  69. ratio_y = (hei - 50) / 20;
  70. //----開新的Bitmap----
  71. bmp = new Bitmap((int)wid, (int)hei);
  72. //----使用上面的Bitmap畫圖----
  73. g = Graphics.FromImage(bmp);
  74. //----清除Bitmap為某顏色----
  75. g.Clear(Color.White);
  76. //----更改原點位置----
  77. g.TranslateTransform(pictureBox1.Width / 2, pictureBox1.Height / 2);
  78. //----畫坐標軸----
  79. g.DrawLine(pen, -1000, 0, 1000, 0);//x軸
  80. g.DrawLine(pen, 0, -1000, 0, 1000);//y軸
  81. g.DrawString("X", this.Font, brush, wid / 2 - 20, 20);
  82. g.DrawString("Y", this.Font, brush, 20, -hei / 2);
  83. g.DrawLine(pen, wid / 2, 0, wid / 2 - 10, 5);//x軸箭頭
  84. g.DrawLine(pen, wid / 2, 0, wid / 2 - 10, -5);
  85. g.DrawLine(pen, 0, -hei / 2, 5, -hei / 2 + 10);//y軸箭頭
  86. g.DrawLine(pen, 0, -hei / 2, -5, -hei / 2 + 10);
  87. for (int i = -10; i <= 10; i++)//畫X Y軸座標位置
  88. {
  89. g.DrawLine(pen, i * ratio_x, -5, i * ratio_x, 5);
  90. g.DrawString(i.ToString().PadLeft(2, ' '), this.Font, brush, i * ratio_x - 9, 10);
  91. g.DrawLine(pen, -5, i * ratio_y, 5, i * ratio_y);
  92. if (i != 0)
  93. g.DrawString(i.ToString(), this.Font, brush, 15, i * ratio_y - 8);
  94. }
  95. //----換顏色----
  96. pen = new Pen(Color.Red);
  97. brush = new SolidBrush(pen.Color);
  98. //----畫線----
  99. g.DrawLine(pen, x0 * ratio_x, -y0 * ratio_y, x1 * ratio_x, -y1 * ratio_y);
  100. //----畫兩點----
  101. g.FillEllipse(brush, new RectangleF(x0 * ratio_x - 2.5f, -y0 * ratio_y - 2.5f, 5, 5));
  102. g.FillEllipse(brush, new RectangleF(x1 * ratio_x - 2.5f, -y1 * ratio_y - 2.5f, 5, 5));
  103. //----釋放Graphics資源----
  104. g.Dispose();
  105. //----將Bitmap顯示在Picture上
  106. pictureBox1.Image = bmp;
  107. }
  108. catch (Exception x)
  109. {
  110. MessageBox.Show(x.Message);//錯誤視窗
  111. }
  112. finally
  113. {
  114. GC.Collect();//清除垃圾
  115. }
  116. }
  117. }
  118. }

專案下載位置:檔案名稱為CSharp_DrawLine

留言

這個網誌中的熱門文章

C# 井字遊戲