FormMain.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Runtime.InteropServices;
  12. using System.Threading;
  13. namespace metaldetector
  14. {
  15. public partial class FormMain : Form
  16. {
  17. [DllImport("kernel32.dll", SetLastError = true)]
  18. public static extern bool Beep(int frequency, int duration);
  19. public static Bitmap bmp0;
  20. public static bool SoundOn = false;
  21. public static int currentFreq = 1000;
  22. public static int realFreq = 1000;
  23. public static string cursorTextPos = "";
  24. public static string cursorTextColor = "";
  25. public static string cursorTextFreq = "";
  26. public static string cursorText = "";
  27. //Показываем отладочные данные на lblCursor
  28. public void ShowCursorText()
  29. {
  30. cursorText = cursorTextPos + "\n"
  31. + cursorTextColor + "\n"
  32. + cursorTextFreq + "\n";
  33. this.lblCursor.Text = cursorText;
  34. }
  35. public static void Sound()
  36. {
  37. while (SoundOn)
  38. {
  39. //Beep(currentFreq, 500);
  40. //Thread.Sleep(1);
  41. Beep(realFreq, 500);
  42. }
  43. }
  44. public void GenerateMap()
  45. {
  46. int start_x = 0;
  47. int end_x = 0;
  48. int start_y = 0;
  49. int end_y = 0;
  50. //высота и ширина квадрата в пикселях
  51. int rect_width = 20;
  52. int rect_height = 20;
  53. int ferro_n = 3; //количество ферромагнетиков
  54. int semi_n = 2; //количество полупроводников
  55. Bitmap bmp1 = new Bitmap(picMap.Width, picMap.Height, PixelFormat.Format24bppRgb);
  56. Graphics g = Graphics.FromImage(bmp1);
  57. g.Clear(Color.Black);
  58. // Создаем объекты-кисти для закрашивания фигур
  59. SolidBrush sbDia = new SolidBrush(Color.Red); //диамагнетик
  60. SolidBrush sbPara = new SolidBrush(Color.Green); //парамагнетик
  61. SolidBrush sbFerro = new SolidBrush(Color.Blue); //ферромагнетик
  62. SolidBrush sbSemi = new SolidBrush(Color.Black); //полупроводник
  63. int x_n = picMap.Width / rect_width;
  64. int y_n = picMap.Height / rect_height;
  65. int n_count = x_n * y_n; //количество квадратов
  66. rtbLog.Text = rtbLog.Text + "Генерация. x_n: " + x_n.ToString() + "; y_n: " + y_n.ToString() + "\n";
  67. Random rnd = new Random();
  68. int r = 0;
  69. for (int i = 0; i < x_n; i++)
  70. {
  71. for (int j = 0; j < y_n; j++)
  72. {
  73. start_x = i * rect_width;
  74. end_x = start_x + rect_width;
  75. start_y = j * rect_height;
  76. end_y = start_y + rect_height;
  77. r = rnd.Next(0, 2);
  78. //rtbLog.Text = rtbLog.Text + "i: " + i.ToString() + "; j: " + j.ToString() + "; start_x: " + start_x.ToString() + "; start_y: " + start_y.ToString() + "; r: " + r.ToString() + "\n";
  79. if (r == 0)
  80. {
  81. g.FillRectangle(sbDia, start_x, start_y, rect_width, rect_height);
  82. } else
  83. {
  84. g.FillRectangle(sbPara, start_x, start_y, rect_width, rect_height);
  85. }
  86. //рисуем ферромагнетик
  87. r = rnd.Next(0, n_count / ferro_n);
  88. if (r == 0)
  89. {
  90. g.FillRectangle(sbFerro, start_x, start_y, rect_width, rect_height);
  91. }
  92. //рисуем полупроводник
  93. r = rnd.Next(0, n_count / semi_n);
  94. if (r == 0)
  95. {
  96. g.FillRectangle(sbSemi, start_x, start_y, rect_width, rect_height);
  97. }
  98. }
  99. }
  100. bmp0 = (Bitmap)bmp1.Clone();
  101. picMap.Image = bmp1;
  102. }
  103. public FormMain()
  104. {
  105. InitializeComponent();
  106. }
  107. private void rbPass_CheckedChanged(object sender, EventArgs e)
  108. {
  109. if (rbPass.Checked) {
  110. trbFreqDelta.Enabled = true;
  111. lblFreqDelta.Enabled = true;
  112. } else {
  113. trbFreqDelta.Enabled = false;
  114. lblFreqDelta.Enabled = false;
  115. }
  116. }
  117. private void rbActive_CheckedChanged(object sender, EventArgs e)
  118. {
  119. if (rbPass.Checked)
  120. {
  121. trbFreqDelta.Enabled = true;
  122. lblFreqDelta.Enabled = true;
  123. }
  124. else
  125. {
  126. trbFreqDelta.Enabled = false;
  127. lblFreqDelta.Enabled = false;
  128. }
  129. }
  130. private void rbLocator_CheckedChanged(object sender, EventArgs e)
  131. {
  132. if (rbPass.Checked)
  133. {
  134. trbFreqDelta.Enabled = true;
  135. lblFreqDelta.Enabled = true;
  136. }
  137. else
  138. {
  139. trbFreqDelta.Enabled = false;
  140. lblFreqDelta.Enabled = false;
  141. }
  142. }
  143. private void picMap_MouseMove(object sender, MouseEventArgs e)
  144. {
  145. //позиция курсора
  146. //рисуем круг металлодетектора
  147. Bitmap bmp1 = (Bitmap)bmp0.Clone();
  148. Graphics g = Graphics.FromImage(bmp1);
  149. Pen myWind = new Pen(Color.Yellow, 4);
  150. //пассивный
  151. if (rbPass.Checked)
  152. {
  153. myWind = new Pen(Color.Yellow, 4);
  154. g.DrawEllipse(myWind, e.X - 11, e.Y - 11, 20, 20);
  155. }
  156. //активный
  157. if (rbActive.Checked)
  158. {
  159. myWind = new Pen(Color.Yellow, 1);
  160. g.DrawEllipse(myWind, e.X - 6, e.Y - 6, 10, 10);
  161. myWind = new Pen(Color.Magenta, 1);
  162. g.DrawEllipse(myWind, e.X - 11, e.Y - 11, 20, 20);
  163. }
  164. if (rbLocator.Checked)
  165. {
  166. myWind = new Pen(Color.Cyan, 1);
  167. g.DrawEllipse(myWind, e.X - 6, e.Y - 6, 10, 10);
  168. myWind = new Pen(Color.Cyan, 1);
  169. g.DrawEllipse(myWind, e.X - 11, e.Y - 11, 20, 20);
  170. }
  171. picMap.Image = bmp1;
  172. //цвет под курсором
  173. try
  174. {
  175. Color clr = bmp0.GetPixel(e.X, e.Y);
  176. byte r = clr.R;
  177. byte gr = clr.G;
  178. byte b = clr.B;
  179. cursorTextPos = "X: " + Convert.ToString(e.X) + "; Y: " + Convert.ToString(e.Y);
  180. cursorTextColor = "R: " + Convert.ToString(r) + "; G: " + Convert.ToString(gr) + "; B: " + Convert.ToString(b);
  181. //Перерасчет реальной частоты
  182. //пассивный
  183. if (rbPass.Checked)
  184. {
  185. if ((r==0) && (gr==0) && (b==0))
  186. {
  187. realFreq = currentFreq / 10;
  188. } else
  189. {
  190. realFreq = currentFreq + r / 255 * trbFreqDelta.Value * 3 - gr / 128 * trbFreqDelta.Value * 3;
  191. }
  192. }
  193. cursorTextFreq = "Base Freq: " + currentFreq.ToString() + "; Real Freq: " + realFreq.ToString();
  194. ShowCursorText();
  195. }
  196. catch
  197. {
  198. cursorTextPos = "X: ; Y:";
  199. ShowCursorText();
  200. }
  201. }
  202. private void button1_Click(object sender, EventArgs e)
  203. {
  204. GenerateMap();
  205. }
  206. private void FormMain_Load(object sender, EventArgs e)
  207. {
  208. GenerateMap();
  209. currentFreq = trbFreq.Value * 500 + 500;
  210. }
  211. private void btnSoundOn_Click(object sender, EventArgs e)
  212. {
  213. SoundOn = !SoundOn;
  214. if(SoundOn)
  215. {
  216. btnSoundOn.Text = "Выключить звук";
  217. // создаем новый поток
  218. Thread myThread = new Thread(new ThreadStart(Sound));
  219. myThread.Start(); // запускаем поток
  220. }
  221. else
  222. {
  223. btnSoundOn.Text = "Включить звук";
  224. }
  225. }
  226. private void trbFreq_Scroll(object sender, EventArgs e)
  227. {
  228. currentFreq = trbFreq.Value * 500 + 500;
  229. cursorTextFreq = "Base Freq: " + currentFreq.ToString();
  230. ShowCursorText();
  231. }
  232. }
  233. }