123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Threading;
- namespace metaldetector
- {
- public partial class FormMain : Form
- {
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern bool Beep(int frequency, int duration);
- public static Bitmap bmp0;
- public static bool SoundOn = false;
- public static int currentFreq = 1000;
- public static int realFreq = 1000;
- public static string cursorTextPos = "";
- public static string cursorTextColor = "";
- public static string cursorTextFreq = "";
- public static string cursorText = "";
- //Показываем отладочные данные на lblCursor
- public void ShowCursorText()
- {
- cursorText = cursorTextPos + "\n"
- + cursorTextColor + "\n"
- + cursorTextFreq + "\n";
- this.lblCursor.Text = cursorText;
- }
- public static void Sound()
- {
- while (SoundOn)
- {
- //Beep(currentFreq, 500);
- //Thread.Sleep(1);
- Beep(realFreq, 500);
- }
- }
- public void GenerateMap()
- {
- int start_x = 0;
- int end_x = 0;
- int start_y = 0;
- int end_y = 0;
- //высота и ширина квадрата в пикселях
- int rect_width = 20;
- int rect_height = 20;
- int ferro_n = 3; //количество ферромагнетиков
- int semi_n = 2; //количество полупроводников
- Bitmap bmp1 = new Bitmap(picMap.Width, picMap.Height, PixelFormat.Format24bppRgb);
- Graphics g = Graphics.FromImage(bmp1);
- g.Clear(Color.Black);
- // Создаем объекты-кисти для закрашивания фигур
- SolidBrush sbDia = new SolidBrush(Color.Red); //диамагнетик
- SolidBrush sbPara = new SolidBrush(Color.Green); //парамагнетик
- SolidBrush sbFerro = new SolidBrush(Color.Blue); //ферромагнетик
- SolidBrush sbSemi = new SolidBrush(Color.Black); //полупроводник
- int x_n = picMap.Width / rect_width;
- int y_n = picMap.Height / rect_height;
- int n_count = x_n * y_n; //количество квадратов
- rtbLog.Text = rtbLog.Text + "Генерация. x_n: " + x_n.ToString() + "; y_n: " + y_n.ToString() + "\n";
- Random rnd = new Random();
- int r = 0;
- for (int i = 0; i < x_n; i++)
- {
- for (int j = 0; j < y_n; j++)
- {
- start_x = i * rect_width;
- end_x = start_x + rect_width;
- start_y = j * rect_height;
- end_y = start_y + rect_height;
- r = rnd.Next(0, 2);
- //rtbLog.Text = rtbLog.Text + "i: " + i.ToString() + "; j: " + j.ToString() + "; start_x: " + start_x.ToString() + "; start_y: " + start_y.ToString() + "; r: " + r.ToString() + "\n";
- if (r == 0)
- {
- g.FillRectangle(sbDia, start_x, start_y, rect_width, rect_height);
- } else
- {
- g.FillRectangle(sbPara, start_x, start_y, rect_width, rect_height);
- }
- //рисуем ферромагнетик
- r = rnd.Next(0, n_count / ferro_n);
- if (r == 0)
- {
- g.FillRectangle(sbFerro, start_x, start_y, rect_width, rect_height);
- }
- //рисуем полупроводник
- r = rnd.Next(0, n_count / semi_n);
- if (r == 0)
- {
- g.FillRectangle(sbSemi, start_x, start_y, rect_width, rect_height);
- }
- }
- }
- bmp0 = (Bitmap)bmp1.Clone();
- picMap.Image = bmp1;
- }
- public FormMain()
- {
- InitializeComponent();
- }
- private void rbPass_CheckedChanged(object sender, EventArgs e)
- {
- if (rbPass.Checked) {
- trbFreqDelta.Enabled = true;
- lblFreqDelta.Enabled = true;
- } else {
- trbFreqDelta.Enabled = false;
- lblFreqDelta.Enabled = false;
- }
- }
- private void rbActive_CheckedChanged(object sender, EventArgs e)
- {
- if (rbPass.Checked)
- {
- trbFreqDelta.Enabled = true;
- lblFreqDelta.Enabled = true;
- }
- else
- {
- trbFreqDelta.Enabled = false;
- lblFreqDelta.Enabled = false;
- }
- }
- private void rbLocator_CheckedChanged(object sender, EventArgs e)
- {
- if (rbPass.Checked)
- {
- trbFreqDelta.Enabled = true;
- lblFreqDelta.Enabled = true;
- }
- else
- {
- trbFreqDelta.Enabled = false;
- lblFreqDelta.Enabled = false;
- }
- }
- private void picMap_MouseMove(object sender, MouseEventArgs e)
- {
- //позиция курсора
- //рисуем круг металлодетектора
- Bitmap bmp1 = (Bitmap)bmp0.Clone();
- Graphics g = Graphics.FromImage(bmp1);
- Pen myWind = new Pen(Color.Yellow, 4);
- //пассивный
- if (rbPass.Checked)
- {
- myWind = new Pen(Color.Yellow, 4);
- g.DrawEllipse(myWind, e.X - 11, e.Y - 11, 20, 20);
- }
- //активный
- if (rbActive.Checked)
- {
- myWind = new Pen(Color.Yellow, 1);
- g.DrawEllipse(myWind, e.X - 6, e.Y - 6, 10, 10);
- myWind = new Pen(Color.Magenta, 1);
- g.DrawEllipse(myWind, e.X - 11, e.Y - 11, 20, 20);
- }
- if (rbLocator.Checked)
- {
- myWind = new Pen(Color.Cyan, 1);
- g.DrawEllipse(myWind, e.X - 6, e.Y - 6, 10, 10);
- myWind = new Pen(Color.Cyan, 1);
- g.DrawEllipse(myWind, e.X - 11, e.Y - 11, 20, 20);
- }
- picMap.Image = bmp1;
- //цвет под курсором
- try
- {
- Color clr = bmp0.GetPixel(e.X, e.Y);
- byte r = clr.R;
- byte gr = clr.G;
- byte b = clr.B;
- cursorTextPos = "X: " + Convert.ToString(e.X) + "; Y: " + Convert.ToString(e.Y);
- cursorTextColor = "R: " + Convert.ToString(r) + "; G: " + Convert.ToString(gr) + "; B: " + Convert.ToString(b);
- //Перерасчет реальной частоты
- //пассивный
- if (rbPass.Checked)
- {
- if ((r==0) && (gr==0) && (b==0))
- {
- realFreq = currentFreq / 10;
- } else
- {
- realFreq = currentFreq + r / 255 * trbFreqDelta.Value * 3 - gr / 128 * trbFreqDelta.Value * 3;
- }
- }
- cursorTextFreq = "Base Freq: " + currentFreq.ToString() + "; Real Freq: " + realFreq.ToString();
- ShowCursorText();
- }
- catch
- {
- cursorTextPos = "X: ; Y:";
- ShowCursorText();
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- GenerateMap();
- }
- private void FormMain_Load(object sender, EventArgs e)
- {
- GenerateMap();
- currentFreq = trbFreq.Value * 500 + 500;
- }
- private void btnSoundOn_Click(object sender, EventArgs e)
- {
- SoundOn = !SoundOn;
- if(SoundOn)
- {
- btnSoundOn.Text = "Выключить звук";
- // создаем новый поток
- Thread myThread = new Thread(new ThreadStart(Sound));
- myThread.Start(); // запускаем поток
- }
- else
- {
- btnSoundOn.Text = "Включить звук";
- }
- }
- private void trbFreq_Scroll(object sender, EventArgs e)
- {
- currentFreq = trbFreq.Value * 500 + 500;
- cursorTextFreq = "Base Freq: " + currentFreq.ToString();
- ShowCursorText();
- }
- }
- }
|