⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 windows mobile读取GPS数据
💻 CS
📖 第 1 页 / 共 4 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlServerCe;
using System.Xml;
using Microsoft.WindowsMobile.Samples.Location;
namespace QXPDA
{
    public partial class Form1 : Form
    {
        //自定义变量
        /// <summary>
        /// GPS设备状态对象
        /// </summary>
        GpsDeviceState device = null;
        /// <summary>
        /// GPS位置对象
        /// </summary>
        GpsPosition position = null;
        /// <summary>
        /// GPS对象
        /// </summary>
        Gps gps = new Gps();
        /// <summary>
        /// 数据源路径
        /// </summary>
        private string strConnectString = "";
        private string strIniFilePath = "";
        private bool bLoadOver = false;
        /// <summary>
        /// 标识干线线路名称数据装载到combox控件是否完毕
        /// </summary>
        private bool bLineNameLoadOver = false;
        private bool bPtComboxDataLoadFirst = true;
        private bool bLcComboxDataLoadFirst = true;
        /// <summary>
        /// 数据库连接
        /// </summary>
        private SqlCeConnection sqlconn = null;
 
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            if (qxpdaDataSetUtil.DesignerUtil.IsRunTime())
            {
                // TODO: 删除此行代码以移除“qxpdaDataSet.bdz_base”的默认 AutoFill。
                this.bdz_baseTableAdapter.Fill(this.qxpdaDataSet.bdz_base);
            }

            //GPS事件
            gps.DeviceStateChanged += new DeviceStateChangedEventHandler(gps_DeviceStateChanged);
            gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged);

            //连接数据库
            strConnectString = "data source ="+ GetCurrentDirectory() +@"\qxpda.sdf";
            //strConnectString = @"Data Source=\Program Files\qxpda\qxpda.sdf";
            sqlconn = new SqlCeConnection(strConnectString);
            sqlconn.Open();
            if (sqlconn.State != ConnectionState.Open)
            {
                MessageBox.Show("数据库连接失败!");
                return;
            }

            //启动定时备份功能
            strIniFilePath = "data source =" + GetCurrentDirectory() + @"\qxpda.ini";
            //strIniFilePath = @"Data Source=\Program Files\qxpda\qxpda.ini";
            //SetTimeCtrl(strIniFilePath);
            
            //装载combox控件数据
            LoadComboxData();
            bLoadOver = true;
            
        }
        /// <summary>
        /// 重写OnClosed事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            MessageBox.Show("Closed");
            base.OnClosed(e);
        }

        //一些自定义函数
        /// <summary>
        /// 获取当前目录
        /// </summary>
        /// <returns></returns>
        private string GetCurrentDirectory()
        {
            return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
        }
        private void UpdateSubCtrlValue(BindingSource bs, ComboBox bdz_cb, ComboBox line_cb, int nParam)
        {
            int nBdzCode = 0;
            int nLineCode = 0;
            int nSublineCode = 0;
            //获取当前记录的bdz_code
            DataRowView drv = (DataRowView)bs.Current;
            nBdzCode = (int)drv.Row["bdz_code"];
            nLineCode = (int)drv.Row["line_code"];
            nSublineCode = (int)drv.Row["subline_code"];
            //MessageBox.Show(nBdzCode.ToString() + nLineCode.ToString() + nSublineCode.ToString());
            bdz_cb.SelectedValue = nBdzCode;
            string strSql = "select * from line_base where bdz_code =" + nBdzCode + " order by line_code asc";
            bLineNameLoadOver = false;
            ComboxBind(line_cb, strSql, "line_name", "line_code", nLineCode.ToString());
            bLineNameLoadOver = true;
            switch (nParam)
            {
                case 0:
                    break;
                case 1:
                    strSql = "select * from subline_base where bdz_code =" + nBdzCode + " and line_code=" + nLineCode + " order by subline_code asc";
                    ComboxBind(gt_subline_name, strSql, "subline_name", "subline_code", "");
                    gt_subline_name.SelectedValue = nSublineCode;
                    break;
                case 2:
                    strSql = "select * from subline_base where bdz_code =" + nBdzCode + " and line_code=" + nLineCode + " order by subline_code asc";
                    ComboxBind(lc_subline_name, strSql, "subline_name", "subline_code", "");
                    lc_subline_name.SelectedValue = nSublineCode;
                    break;
                default:
                    break;
            }

        }
        /// <summary>
        /// 初始化combox控件数据
        /// </summary>
        private void LoadComboxData()
        {
            string strSql = "";
            strSql = "select * from bdz_base order by bdz_code";
            ComboxBind(bdz_bdz_name, strSql, "bdz_name", "bdz_code", "");
            ComboxBind(sub_bdz_name, strSql, "bdz_name", "bdz_code", "");
            ComboxBind(lc_bdz_name, strSql, "bdz_name", "bdz_code", "");
            ComboxBind(gt_bdz_name, strSql, "bdz_name", "bdz_code", "");
            strSql = "select * from c_code where code_lb='dydj' order by code";
            ComboxBind(bdz_voltage_level, strSql, "name", "code", "");
        }
        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="strSQL"></param>
        private void ExcuteSql(string strSQL)
        {
            SqlCeCommand sqlcom = new SqlCeCommand();
            if (sqlconn.State == ConnectionState.Open)
            {
                sqlcom = sqlconn.CreateCommand();
                sqlcom.CommandText = strSQL;
                sqlcom.ExecuteNonQuery();
            }
        }
        /// <summary>
        /// 绑定combox数据源
        /// </summary>
        /// <param name="cbx"></param>
        /// <param name="strSql"></param>
        /// <param name="DisplayName"></param>
        /// <param name="ValueCode"></param>
        /// <param name="SelectedCode"></param>
        public void ComboxBind(ComboBox cbx, string strSql, string DisplayName, string ValueCode, string SelectedCode)
        {
            DataTable dt = new DataTable();
            SqlCeDataAdapter scda = new SqlCeDataAdapter(strSql, sqlconn);
            scda.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                cbx.DataSource = dt.DefaultView;
                cbx.DisplayMember = DisplayName;
                cbx.ValueMember = ValueCode;
                if (SelectedCode == null || SelectedCode == "")
                {
                    cbx.SelectedValue = 0;
                }
                else
                {
                    cbx.SelectedValue = SelectedCode;
                }
            }
        }
        /// <summary>
        /// 统计记录的数目
        /// </summary>
        /// <param name="strSql">必须为select count(*) from ...等语句</param>
        /// <returns></returns>
        private int GetRecordCount(string strSql)
        {
            int nRowNum = 0;
            SqlCeCommand sqlCom = new SqlCeCommand();
            sqlCom = sqlconn.CreateCommand();
            sqlCom.CommandText = strSql;
            nRowNum = (int)sqlCom.ExecuteScalar();
            return nRowNum;
        }

        //实现gps功能的处理
        /// <summary>
        /// gps位置发生改变时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        protected void gps_LocationChanged(object sender, LocationChangedEventArgs args)
        {
            position = args.Position;
        }
        /// <summary>
        /// gps设备状态发生改变时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        void gps_DeviceStateChanged(object sender, DeviceStateChangedEventArgs args)
        {
            device = args.DeviceState;
        }
        /// <summary>
        /// 连接GPS设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void open_gps_Click(object sender, EventArgs e)
        {
            if (!gps.Opened)
            {
                gps.Open();
            }
            open_gps.Enabled = false;
            close_gps.Enabled = true;
        }
        /// <summary>
        /// 关闭GPS设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param> 
        private void close_gps_Click(object sender, EventArgs e)
        {
            if (gps.Opened)
            {
                gps.Close();
            }
            open_gps.Enabled = true;
            close_gps.Enabled = false;
        }
        /// <summary>
        /// 获取坐标并设置到文本框
        /// </summary>
        /// <param name="x">显示经度的文本框</param>
        /// <param name="y">显示纬度的文本框</param>
        private void GetGpsLocationData(TextBox x, TextBox y)
        {
            string str;
            if (!gps.Opened)
            {
                str = "GPS设备没有打开,请点击打开GPS菜单后重试!";
                MessageBox.Show(str);
                return;
            }
            if (device == null)
            {
                str = "GPS设备打开错误,请重新插拔GPS卡后重试!";
                MessageBox.Show(str);
                return;
            }
            if (position != null)
            {
                string strJd, strWd;
                strJd = FormatString(position.DoubleLongtitude, 7);
                strWd = FormatString(position.DoubleLatitude, 7);
                DialogResult result;

                str = "经度:" + strJd + "\n纬度:" + strWd;
                result = MessageBox.Show(str, "当前坐标", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (result == DialogResult.OK)
                {
                    x.Text = strWd;
                    y.Text = strJd;
                    return;
                }
            }
        }
        /// <summary>
        /// 将double型数据格式化到小数点后特定长度
        /// </summary>
        /// <param name="nLength">小数点后的长度</param>
        /// <param name="dbTemp"></param>
        /// <returns></returns>
        private string FormatString(double dbTemp, int nLength)
        {
            string strTemp;
            int nDotPos = 0;
            nDotPos = dbTemp.ToString().IndexOf('.');
            strTemp = dbTemp.ToString().Substring(0, nDotPos + nLength + 1);
            return strTemp;
        }

        //上下翻按钮处理
        private void bdz_pre_Click(object sender, EventArgs e)
        {
            this.bdz_baseBindingSource.MovePrevious();
            UpdateButtonState();
        }
        private void bdz_next_Click(object sender, EventArgs e)
        {
            this.bdz_baseBindingSource.MoveNext();
            UpdateButtonState();
        }
        
        private void sub_pre_Click(object sender, EventArgs e)
        {
            this.subline_baseBindingSource.MovePrevious();
            UpdateSubCtrlValue(subline_baseBindingSource,sub_bdz_name,sub_line_name,0);
            UpdateButtonState();
        }
        private void sub_next_Click(object sender, EventArgs e)
        {
            this.subline_baseBindingSource.MoveNext();
            UpdateSubCtrlValue(subline_baseBindingSource, sub_bdz_name, sub_line_name,0);
            UpdateButtonState();
        }
        
        private void gt_pre_Click(object sender, EventArgs e)
        {
            this.pole_towerBindingSource.MovePrevious();
            UpdateSubCtrlValue(pole_towerBindingSource, gt_bdz_name, gt_line_name,1);
            UpdateButtonState();
        }
        private void gt_next_Click(object sender, EventArgs e)
        {
            this.pole_towerBindingSource.MoveNext();
            UpdateSubCtrlValue(pole_towerBindingSource, gt_bdz_name, gt_line_name,1);
            UpdateButtonState();
        }
        
        private void lc_pre_Click(object sender, EventArgs e)
        {
            this.line_crossBindingSource.MovePrevious();
            UpdateSubCtrlValue(line_crossBindingSource, lc_bdz_name, lc_line_name,2);
            UpdateButtonState();
        }
        private void lc_next_Click(object sender, EventArgs e)
        {
            this.line_crossBindingSource.MoveNext();
            UpdateSubCtrlValue(line_crossBindingSource, lc_bdz_name, lc_line_name,2);
            UpdateButtonState();
        }
        /// <summary>
        /// 更新前后翻按钮状态
        /// </summary>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -