|

楼主 |
发表于 2024-7-25 15:17:10
|
显示全部楼层
本帖最后由 gxg0000 于 2024-7-25 15:19 编辑
窗口应用程序C#书写,有注释,仅供参考!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Threading;
- using System.Drawing.Imaging;
- using 琴键开关;
- using System.Diagnostics;
- namespace 扫频仪
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- public const int GWL_WNDPROC = (-4); //nIndex的参数:为窗口过程设定一个新的地址
- public IntPtr OldProc = IntPtr.Zero;
- [DllImport("coredll.dll")] //API声明,将消息信息传送给指定的窗口过程
- private extern static int CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hwnd, uint msg, uint wParam, int lParam);
- [DllImport("coredll.dll")] //API声明,改变指定窗口的属性
- public static extern IntPtr SetWindowLong(IntPtr hwnd, int nIndex, IntPtr dwNewLong);
- public delegate int WndProcHandler(IntPtr hwnd, uint msg, uint wParam, int lParam);//定义委托(Delegate)的类型函数
- WndProcHandler myproc = null; //声明委托类型对象
- int hd; //声明流驱动设备句柄
- int hDrv; //声明打开设备句柄
- bool button1_state=true;
- bool loop1= true; //数据刷新标记
- bool loop2 = false; //图形上显示带宽标记
- int dwCode; //控制码
- uint dstart = 0;
- public const int OPEN_EXISTING = 3;
- public const int INVALID_HANDLE_VALUE = -1;
- public const int GENERIC_READ = -0;
- public const int GENERIC_WRITE = 0x40000000;
- public static uint[] pBuffIn1 = new uint[16]; //声明写入数组
- public static byte[] pBuffIn2 = new byte[2];
- public static uint[] pBufOut1 = new uint[310];//声明读出数组
- public static uint[] pBufOut2 = new uint[310];
- uint[] f1 = { 455000, 465000, 4000000, 8000000, 10700000, 12000000, 16000000, 20000000, 24000000, 30000000 };
- uint[] f2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 100 };
- KeySwitch keySwitch5;
- KeySwitch keySwitch6;
- KeySwitch keySwitch7;
- [DllImport("Coredll")]
- public static extern uint GetTickCount();
- [DllImport("Coredll")]
- public static extern int CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFLagsAndAttributes, int hTemplateFile);
- [DllImport("Coredll")]
- public static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, byte[] pNumberOfBytesWritten, int lpOverlapped);
- [DllImport("coredll")]
- public static extern bool ReadFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToRead, byte[] lpNumberOfBytesRead, int lpOverlapped);
- [DllImport("Coredll")]
- public static extern int DeviceIoControl(int hDevice, int dwIoControlCode, uint[] lpInBuffer, int nInBufferSize, uint[] lpOutBuffer, int nOutBufferSize, IntPtr lpBytesReturned, int lpOverlapped);
- [DllImport("coredll")]
- public static extern bool CloseHandle(int hObject);
- [DllImport("coredll")] //加载流驱动
- public static extern int ActivateDeviceEx(string lpszDevKey, IntPtr lpRegEnts, UInt32 cRegEnts, IntPtr dwParam);
- [DllImport("coredll")] //卸载流
- public static extern bool DeactivateDevice(int hDevice);
-
- Pen p1 = new Pen(Color.LimeGreen, 1); //创建画笔p1(色,像数)
- Pen p2 = new Pen(Color.DimGray, 1);
- Pen p3 = new Pen(Color.FromArgb(30, 30, 30), 1);
- Brush brush1= new SolidBrush(Color.Brown);
- Bitmap tup1; Graphics g1; //画曲线
- Bitmap tup2; Graphics g2; //画网格和坐标
-
- Thread ThreadAd;
- /// <summary>
- /// 画显示电池电压图形
- /// </summary>
- /// <param name="level">电量等级</param>
- private void UV(int level)
- {
- if (level > 715) //欠压7.18V时对应AD值715
- {
- level = (level - 715) / 17 + 1;
- level = (level > 4) ? 4 : level; //限幅
- }
- else
- {
- level = 0;
- }
- Bitmap tup3; Graphics g3; //画电池图形
- tup3 = new Bitmap(22, 14); g3 = Graphics.FromImage(tup3);
- Brush brush2 = new SolidBrush(Color.Gray);
- g3.DrawRectangle(new Pen(Color.FromArgb(50, 50, 50), 1), 0, 4, 2, 5);
- g3.DrawRectangle(new Pen(Color.FromArgb(50, 50, 50), 1), 2, 2, 19, 9);
- g3.FillRectangle(brush2, 20 - 4 * level, 4, 4 * level, 6);
- pictureBox2.Image = tup3;
- g3.Dispose();
- }
- /// <summary>
- /// 频率显示格式化
- /// </summary>
- /// <param name="f">频率</param>
- /// <param name="str1">返回频率值</param>
- /// <param name="str2">返回频率单位</param>
- private void Freq_Format(int f, out string str1, out string str2)
- {
- string length1 = f.ToString();
- if (length1.Length < 4) { str1 = length1; str2 = "Hz"; }
- else if (length1.Length == 4) { str1 = length1.Insert(1, "."); str2 = "KHz"; }
- else if (length1.Length == 5) { str1 = length1.Insert(2, ".").Remove(5, 1); str2 = "KHz"; }
- else if (length1.Length == 6) { str1 = length1.Insert(3, ".").Remove(5, 2); str2 = "KHz"; }
- else if (length1.Length == 7) { str1 = length1.Insert(1, ".").Remove(5, 3); str2 = "MHz"; }
- else if (length1.Length == 8) { str1 = length1.Insert(2, ".").Remove(5, 4); str2 = "MHz"; }
- else { str1 = "0"; str2 = "0"; }
- }
- /// <summary>
- /// 图像提亮函数
- /// </summary>
- /// <param name="sur">源图像</param>
- /// <param name="light">背景图像提亮(=0亮度不变、正数亮度增加、负数亮度降低</param>
- /// <returns></returns>
- private Bitmap ImageLight(Bitmap sur, int light)
- {
- if (light == 0) { return sur; }
- Bitmap tempCur = (Bitmap)sur.Clone(); //创建临时背景位图副本
- BitmapData bmpData1 = tempCur.LockBits(new Rectangle(0, 0, tempCur.Width, tempCur.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
- int stride = bmpData1.Stride; //背景图像扫描宽度
- byte[] Data1 = new byte[stride * tempCur.Height + 3]; //背景图像数组
- Marshal.Copy(bmpData1.Scan0, Data1, 0, stride * tempCur.Height);//读取背景图像数据(拷贝到托管数组)
- for (int i = 0; i < tempCur.Height; i++) //高内循环
- {
- for (int j = 0; j < tempCur.Width; j++) //行内循环
- {
- int m = i * stride + j * 3;
- int b = Data1[m] + light;
- int g = Data1[m + 1] + light;
- int r = Data1[m + 2] + light;
- if (light > 0)
- {
- b = b > 255 ? 255 : b;
- g = g > 255 ? 255 : g;
- r = r > 255 ? 255 : r;
- }
- else if (light < 0)
- {
- b = b < 0 ? 0 : b;
- g = g < 0 ? 0 : g;
- r = r < 0 ? 0 : r;
- }
- Data1[m] = (byte)b;
- Data1[m + 1] = (byte)g;
- Data1[m + 2] = (byte)r;
- }
- }
- Marshal.Copy(Data1, 0, bmpData1.Scan0, stride * tempCur.Height); //将数据写入图像(拷贝到bmpData1)
- tempCur.UnlockBits(bmpData1);
- return tempCur;
- }
- /// <summary>
- /// 生成窗口上固定的字符串
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Form1_Paint(object sender, PaintEventArgs e)
- {
- Graphics g3 = e.Graphics;
- Font font = new Font("Arial", 9f, FontStyle.Regular);
- Brush brush = new SolidBrush(Color.DimGray);
- //画显示图形区矩形框
- g3.DrawRectangle(p2, 21, 19, 311, 217);
- //y轴刻度字符串生成
- g3.DrawString("电压(V)", font, brush, 1, 4);
- g3.DrawString(" 3", font, brush, 1, 18);
- g3.DrawString("2.5", font, brush, 1, 51);
- g3.DrawString(" 2", font, brush, 1, 87);
- g3.DrawString("1.5", font, brush, 1, 123);
- g3.DrawString(" 1", font, brush, 1, 159);
- g3.DrawString("0.5", font, brush, 1, 195);
- g3.DrawString(" 0", font, brush, 1, 227);
- //频率轴刻度字符串生成
- g3.DrawString("0", font, brush, 19, 238);
- g3.DrawString("1", font, brush, 51, 238);
- g3.DrawString("2", font, brush, 82, 238);
- g3.DrawString("3", font, brush, 113, 238);
- g3.DrawString("4", font, brush, 144, 238);
- g3.DrawString("5", font, brush, 175, 238);
- g3.DrawString("6", font, brush, 206, 238);
- g3.DrawString("7", font, brush, 237, 238);
- g3.DrawString("8", font, brush, 268, 238);
- g3.DrawString("9", font, brush, 299, 238);
- g3.DrawString("10", font, brush, 326, 238);
- //
- //g3.DrawString("gxg0000", font, brush, 1, 252);
- g3.DrawString("扫描频率", font, brush, 142, 252);
- //
- g3.FillRectangle(new SolidBrush(Color.DarkKhaki), 340, 0, 138, 98);
- g3.DrawString("谐振频率", font, new SolidBrush(Color.DarkRed), 342, 5);
- g3.DrawLine(new Pen(Color.MediumOrchid, 2), 346, 49, 472, 49);
- g3.DrawString("峰值电压 V", font, new SolidBrush(Color.Indigo), 342, 55);
- //
- g3.FillRectangle(new SolidBrush(Color.YellowGreen), 340, 100, 138, 82);
- g3.DrawString("带宽", font, new SolidBrush(Color.Purple), 342+24, 103);
- g3.DrawString("下边频", font, new SolidBrush(Color.Purple), 342+12, 119);
- g3.DrawString("上边频", font, new SolidBrush(Color.Purple), 342+12, 135);
- g3.DrawString("Q值", font, new SolidBrush(Color.Purple), 342+30, 151);
- g3.DrawString("矩形系数", font, new SolidBrush(Color.Purple), 342, 167);
- //
- g3.FillRectangle(new SolidBrush(Color.LightSeaGreen), 340, 184, 138, 51);
- g3.DrawString("开始频率", font, new SolidBrush(Color.Navy), 342, 188);
- g3.DrawString("步进", font, new SolidBrush(Color.Navy), 342+24, 203);
- g3.DrawString("终止频率", font, new SolidBrush(Color.Navy), 342, 218);
- }
- private void Form1_Load(System.Object sender, System.EventArgs e) //初始化
- {
- Cursor.Hide();
- this.Width = 480;
- this.Height = 267;
- label18.Width = 1; //竖线
- label18.Height = 216 - 12;
- label18.Top = 20;
- label18.Left = 22;
- label18.Visible = false;
- label19.Width = 310 - 32; //横向
- label19.Height = 1;
- label19.Top = 20;
- label19.Left = 22 + 32;
- label19.Visible = false;
- label31.Width = 32; //频率刻度
- label31.Height = 12;
- label31.Top = 20 - 12 + 216;
- label31.Left = 22;
- label31.Visible = false;
- label32.Width = 32; //增益刻度
- label32.Height = 12;
- label32.Top = 20;
- label32.Left = 22;
- label32.Visible = false;
- hd = ActivateDeviceEx("\\Drivers\\SWEEP", IntPtr.Zero, 0, IntPtr.Zero); //加载驱动
- hDrv = CreateFile("SWE1:", GENERIC_READ + GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
- if (INVALID_HANDLE_VALUE == hDrv) //获得句柄hDrv,句柄=-1,说明设备未打开
- { MessageBox.Show("不能打开SWE1"); }
- tup1 = new Bitmap(310, 216); g1 = Graphics.FromImage(tup1);
- tup2 = new Bitmap(310, 216); g2 = Graphics.FromImage(tup2);
- dwCode = 0; //控制码
- pBuffIn1[0] = 441750; //扫描开始频率
- pBuffIn1[1] = 155; //步进
- pBuffIn1[2] = 1; //AD转换增加延时1ms
- pBuffIn1[3] = 1; //AD转换选择1通道
- label2.Text = "0";
- label3.Text = "0";
- label4.Text = "0 Hz";
- label5.Text = "0 Hz";
- label6.Text = "0 Hz";
- label8.Text = "";
- //生成预置频率控件
- keySwitch5 = new KeySwitch();
- keySwitch5.SwichValue = 2;
- keySwitch5.Left = 1;
- keySwitch5.Top = 17;
- keySwitch5.SwichText = new string[] { "455KHz", "465KHz", "4MHz", "8MHz", "10.7MHz", "12MHz", "16MHz", "20MHz", "24MHz", "手动"};
- this.panel1.Controls.Add(keySwitch5); //添加到控件集合
- this.keySwitch5.MouseDown += new KeySwitch.MouseDownEventHandler(this.keySwitch5_MouseDown);
- //生成扫频范围控件
- keySwitch6 = new KeySwitch();
- keySwitch6.SwichValue = 5;
- keySwitch6.Left = 62;
- keySwitch6.Top = 17;
- keySwitch6.SwichText = new string[] { "±1%", "±2%", "±3%", "±4%", "±5%", "±6%", "±7%", "±8%", "±9%", "手动" };
- this.panel1.Controls.Add(keySwitch6); //添加到控件集合
- this.keySwitch6.MouseDown += new KeySwitch.MouseDownEventHandler(this.keySwitch6_MouseDown);
- //生成AD转换通道号控件
- keySwitch7 = new KeySwitch();
- keySwitch7.SwichNumber = 2;
- keySwitch7.Left = 123;
- keySwitch7.Top = 17;
- keySwitch7.Height = 50;
- keySwitch7.SwichText = new string[] { "ch1", "ch2" };
- this.panel1.Controls.Add(keySwitch7); //添加到控件集合
- //生成显示速度切换控件
- //keySwitch4 = new KeySwitch();
- //keySwitch4.SwichNumber = 2;
- //keySwitch4.Left = 184;
- //keySwitch4.Top = 17;
- //keySwitch4.Height = 50;
- //keySwitch4.SwichText = new string[] { "快", "慢" };
- //this.panel1.Controls.Add(keySwitch4); //添加到控件集合
- //
- ThreadAd = new Thread(myTask); //创建读取电压采集数据的线程
- ThreadAd.Start(); //启动线程
- }
复制代码
待续
|
|