📄 carform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GPSClassLibrary;
namespace GPSGuider
{
/* 整图 640*480 车 228*144, loca:208, 268 */
public partial class CarForm : Form
{
string InitFilePath = @"initial.ini";
static readonly Rectangle picRect = new Rectangle(203, 268, 228, 144); // 图像矩形
PointF curRealPoint = new PointF(100.0f, 100.0f);
#region 变量
// 本车的名字
string m_name = "闽C3566";
// 图像缓存,背景
private Bitmap m_backBmp = new Bitmap(640, 480);
// 图像设备
private Graphics m_myGraphic = null;
private Graphics m_backGraphic = null; // backBmp的图像设备
private int m_angle = 0; // 指定哪个角度
private int m_curMap = 0; // 指向哪个地图
// 属性变量
private int m_carSpeed = 50; // 速度
private int ispeed = 50;
private int m_carAngle = 0; // 车在地图上的角度
private double m_lati = 0; // 经度
private GPSClassLibrary.LatitudeType m_latiType = GPSClassLibrary.LatitudeType.North;
private double m_long = 0; // 纬度
private GPSClassLibrary.LongitudeType m_longType = GPSClassLibrary.LongitudeType.West;
private double m_high = 100; // 高度
private byte m_status = 0; // Gps控制属性
// 汽车信息
float m_enginTemperature = 0.0f;// 引擎温度
float m_oilSurplus = 100;// 剩余油量
int m_heaviyness = 2000; // 负重量
// 地图窗口
MapForm mapform;
// 传输类
GPSComport gpscomport;
// 发送日志
Log SendLog = new Log("SendLog");
// 接收日志
Log ReceiveLog = new Log("ReceiveLog");
#endregion
public CarForm()
{
InitializeComponent();
this.ClientSize = new Size(640, 600); // 窗体大小
// 资源初始化
new global::GPSGuider.Properties.Resources();
// 事件
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
this.KeyUp += new KeyEventHandler(Form1_KeyUp);
this.timer1.Tick += new EventHandler(timer1_Tick);
this.timer1.Interval = 80;
// 图像设备
m_angle = global::GPSGuider.Properties.Resources.car.Length / 2;
m_myGraphic = this.CreateGraphics();
m_backGraphic = Graphics.FromImage(m_backBmp);
if (m_myGraphic == null || m_backGraphic == null)
{
MessageBox.Show("图像设备建立失败");
Application.Exit();
}
CarDisplay();
}
// 汽车绘图事件
private void CarDisplay()
{
// 车的属性
//System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo( );
//provider.NumberDecimalDigits = 2;
if (timer1.Enabled==false)
{
label_speed.Text = "0";
ispeed = 50;
}
else
{
if (ispeed == -30)
label_speed.Text = "0";
else
{
label_speed.Text = ispeed.ToString();
}
}
label_lati.Text = Math.Round(m_lati,3).ToString() + ((char)m_latiType).ToString();
label_long.Text = Math.Round(m_long, 3).ToString() + ((char)m_longType).ToString();
label_high.Text = Math.Round(m_high,3).ToString();
// 车辆状态
label_status.Text = "";
if ((m_status & 0x07) == 0)
label_status.Text = "正常";
else
{
if ((m_status & 0x01) != 0) label_status.Text += "被指令关引擎 ";
if ((m_status & 0x02) != 0) label_status.Text += "被指令锁车匙 ";
if ((m_status & 0x04) != 0) label_status.Text += "被指令断油 ";
}
// 背景
m_backGraphic.DrawImage(global::GPSGuider.Properties.Resources.road[m_curMap], 0, 0);
// 作图开始
System.Drawing.Drawing2D.GraphicsContainer container = m_backGraphic.BeginContainer(); // 嵌套新容器
m_backGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
// 显示区域
if (!picRect.IsEmpty)
m_backGraphic.SetClip(picRect, System.Drawing.Drawing2D.CombineMode.Intersect); // 组合两个显示区域
m_backGraphic.DrawImage(global::GPSGuider.Properties.Resources.car[m_angle], picRect.X, picRect.Y);
// 作图结束
m_backGraphic.EndContainer(container); // 容器还原
m_myGraphic.DrawImage(m_backBmp, 0, 0);
}
// 键盘事件
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
m_angle = 4;
// 加速
m_carSpeed -= 40;
ispeed += 40;
if (ispeed > 180) ispeed = 170;
if (m_carSpeed < 20) m_carSpeed = 20;
// 重新计时
if (timer1.Enabled == true)
{
timer1.Stop();
timer1.Interval = m_carSpeed;
timer1.Start();
}
if (timer1.Enabled == false)
{
timer1.Interval = m_carSpeed;
timer1.Start();
}
break;
case Keys.Right:
if (m_angle < global::GPSGuider.Properties.Resources.car.Length - 1) m_angle++; // 车的图片转动
Position(1); // 车辆信息改变
break;
case Keys.Left:
if (m_angle > 0) m_angle--;
Position(2);
break;
case Keys.Down:
m_angle = 4;
// 减速
if (timer1.Enabled == true)
{
m_carSpeed += 40;
ispeed -=40;
if (m_carSpeed > 160)
{
m_carSpeed = 160;
}
// 重新计时
timer1.Stop();
timer1.Interval = m_carSpeed;
if (m_carSpeed < 160) timer1.Start();
}
break;
}
CarDisplay();
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
m_angle = 4;
break;
case Keys.Right:
m_angle = 4;
break;
case Keys.D:
case Keys.E:
case Keys.R:
case Keys.S:
case Keys.L:
AlarmProcess(e.KeyCode);
break;
}
CarDisplay();
}
private void timer1_Tick(object sender, EventArgs e)
{
// 是否停车
if ((m_status & 0x07) != 0||ispeed<0)
{
timer1.Stop();
m_carSpeed = 160;
return;
}
m_curMap = (m_curMap + 1) % global::GPSGuider.Properties.Resources.road.Length;
CarDisplay();
}
private void CarForm_Load(object sender, EventArgs e)
{
// 设置串口传输
using (SettingsForm form = new SettingsForm())
{
form.Text = "客户端端口设置!";
while (form.ShowDialog() == DialogResult.OK)
{
gpscomport = new GPSClassLibrary.GPSComport(form.PortName, form.BaudRate, form.DataBits, form.StopBits, form.Parity);
if (!gpscomport.OpenPort())
{
MessageBox.Show("打开串口不成功,请重新设置", "提示");
continue;
}
else
{
break;
}
}
if (form.DialogResult == DialogResult.Cancel)
this.Dispose();
}
// 开时钟
timer1.Enabled = true;
timer2.Enabled = true;
ReceiveTimer.Enabled = true;
// 显示地图
mapform = new MapForm();
mapform.Text = "客户端地图";
mapform.Show();
// 读取初始定位
ReadPositionInfo();
}
// 显示地图
private void timer2_Tick(object sender, EventArgs e)
{
// 车的属性改变
Position();
mapform.SetMapPosition(this.m_lati, this.m_latiType, this.m_long, this.m_longType, this.m_high,label_speed.Text);
GPPI gppi = new GPPI();
gppi.GPSState = GPSState.Located;
gppi.Height = m_high;
gppi.Latitude = m_lati;
gppi.LatitudeType = m_latiType;
gppi.Longitude = m_long;
gppi.LongitudeType = m_longType;
gppi.MachineName = m_name;
string str = gppi.ToString();
gpscomport.SendData(str);
SendLog.writeLog(str);
}
// 经纬变化规则
private void Position()
{
double time = ((double)timer2.Interval / 1000.0); // 经上次的时间
double speed = (160 - m_carSpeed)/100; // 当前车速
double roadLong = speed * time; // 经的路长
// 经纬度变换
double lati = Math.Sin(m_carAngle / 180) * roadLong; // 经度增量
double Long = Math.Cos(m_carAngle / 180) * roadLong; // 纬度增量
/* 计算 */
double lUnit = 4500/ 180; // 每度lati的长度
double rUnit = 5000 / 360; // 每度long的长度
// 纬度有变得情况
if (m_carAngle != 0)
{
if (m_latiType == LatitudeType.North)
{
// 向s行驶
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -