C# : 如何撰寫微軟手機的 GPS 衛星接收器程式?

基礎篇

C# 簡介

開發環境

變數與運算

流程控制

陣列

函數

物件

例外處理

函式庫篇

檔案處理

資料結構

正規表達式

Thread

應用篇

視窗程式

媒體影音

網路程式

遊戲程式

手機程式

資料庫

雲端運算

特殊功能

委派

擴展方法

序列化

LinQ

WPF

網路資源

教學影片

投影片

教學文章

軟體下載

考題解答

101習題

專案程式下載 GpsWatcher.zip

簡介

GpsWatcher.jpg

Gps 衛星接收器的畫面

using System;
using System.IO.Ports;
using System.Windows.Forms;
using System.Data;
using System.Text.RegularExpressions;
using System.IO;
using Microsoft.WindowsMobile.Samples.Location;

namespace ccc.gps
{
    public partial class FormGps : Form
    {
        Gps gps = new Gps();
        private EventHandler locationChangedHandler, deviceStateChangedHandler;

        public FormGps()
        {
            InitializeComponent();
        }

        private void FormGps_Load(object sender, EventArgs e)
        {
         locationChangedHandler = new EventHandler(locationChanged);
         deviceStateChangedHandler = new EventHandler(deviceStateChanged);
         gps.DeviceStateChanged += new DeviceStateChangedEventHandler(gps_DeviceStateChanged);
         gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged);
        }

        private void buttonGetData_Click(object sender, EventArgs e)
        {
            listViewGps.Items.Clear();
         if (!gps.Opened) gps.Open();
            buttonGetData.Enabled = false;
            buttonStopGetData.Enabled = true;
        }

        private void buttonStopGetData_Click(object sender, EventArgs e)
        {
            if (gps.Opened) gps.Close();
            buttonGetData.Enabled = true;
            buttonStopGetData.Enabled = false;
        }

        GpsPosition    position;
        protected void gps_LocationChanged(object sender, LocationChangedEventArgs args)
        {
         position = args.Position;
         Invoke(locationChangedHandler);
        }

        GpsDeviceState deviceState;
        void gps_DeviceStateChanged(object sender, DeviceStateChangedEventArgs args)
        {
         deviceState = args.DeviceState;
         Invoke(deviceStateChangedHandler);
        }

        void deviceStateChanged(object sender, System.EventArgs args)
        {
        }

        int counter = 0;
        void locationChanged(object sender, System.EventArgs args)
        {
         if (gps.Opened && position != null)    {
         if (position.LatitudeValid && position.LongitudeValid && position.SatelliteCountValid && position.TimeValid)
         {
         counter++;
         ListViewItem item = new ListViewItem();
         item.Text = counter + "";
         item.SubItems.Add(String.Format("{0:F5}", position.Longitude));
         item.SubItems.Add(String.Format("{0:F5}", position.Latitude));
         item.SubItems.Add(String.Format("{0}", position.SatelliteCount));
         listViewGps.Items.Insert(0, item);
         if (listViewGps.Items.Count > 15)
         listViewGps.Items.RemoveAt(15);
         }
         }
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
         buttonStopGetData_Click(sender, e);
         Close();
        }

        private void FormGps_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
         buttonStopGetData_Click(sender, e);
        }
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License