popwindow 6.0 不消失Mobile6.0关于游戏...

+ windows mobile6.0
环境搭建 文档资料
下载量:0次
阅读量:34次
搜索该词的网友还关注
下载:499次 阅读:19999次
下载:499次 阅读:19999次
下载:499次 阅读:11724次
下载:499次 阅读:11577次
下载:499次 阅读:4112次
下载:499次 阅读:3392次
下载:499次 阅读:3308次
+ windows mobile6.0
分类:&&&&&&
下载: 0次 |
阅读:56次
分类:&&&&&&
下载: 0次 |
阅读:25次
分类:&&&&&&
下载: 27次 |
阅读:838次
分类:&&&&&&
下载: 2次 |
阅读:177次
分类:&&&&&&
下载: 0次 |
阅读:22次
分类:&&&&&&
下载: 0次 |
分类:&&&&&&
下载: 0次 |
阅读:76次
分类:&&&&&&
下载: 11次 |
阅读:267次
分类:&&&&&&
下载: 5次 |
阅读:986次
分类:&&&&&&
下载: 2次 |
阅读:130次
精华文档合集
下载:6次 阅读:459次 文档:23
下载:6次 阅读:248次 文档:5
下载:7次 阅读:562次 文档:25
下载:4次 阅读:232次 文档:11
下载:1次 阅读:78次 文档:12
下载:16次 阅读:571次 文档:65
下载:5次 阅读:225次 文档:22
下载:13次 阅读:355次 文档:44Windows Mobile6.0 入门学习(一)
来源:博客园
刚接触Windows Mobile是同事写的程序,在手机上开启GPRS定位时获取手机的经纬度坐标,由于获取不到卫星,在执行程序的时候报空异常。由此开始接触Windows Moblile,看了看同事的代码,然后再往上搜索资料,MSDN,CSDN,博客园,连百度文库也看了。呵呵。最后,还是在Windows Mobile6.0自带的Samples\PocketPC\CS里面看到例子和微软DLL文件的内部类。下面是GpsSample.cs例子代码,这个例子是获取GPS经纬度的例子,用到Microsoft.WindowsMobile.Samples.Location这个DLL文件,希望给其他初学者带来一些方便。 View Code
2 // Copyright (c) Microsoft Corporation.
All rights reserved.
5 // Use of this sample source code is subject to the terms of the Microsoft
6 // license agreement under which you licensed this sample source code. If
7 // you did not accept the terms of the license agreement, you are not
8 // authorized to use this sample source code. For the terms of the license,
9 // please see the license agreement between you and Microsoft or, if applicable, 10 // see the LICENSE.RTF on your install media or the root of your tools installation. 11 // THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES. 12 // 13 // 14 // Copyright (c) Microsoft Corporation.
All rights reserved. 15 // 16 // 17 // Use of this source code is subject to the terms of the Microsoft end-user 18 // license agreement (EULA) under which you licensed this SOFTWARE PRODUCT. 19 // If you did not accept the terms of the EULA, you are not authorized to use 20 // this source code. For a copy of the EULA, please see the LICENSE.RTF on your 21 // install media. 22 using S 23 using System.D 24 using System.C 25 using System.Windows.F 26 using System.D 27 using Microsoft.WindowsMobile.Samples.L 28
29 namespace GpsTest 30 { 31
/// &summary& 32
/// Summary description for Form1. 33
/// &/summary& 34
public class Form1 : System.Windows.Forms.Form 35
private System.Windows.Forms.MenuItem exitMenuI 37
private System.Windows.Forms.MainMenu mainMenu1; 38
private System.Windows.Forms.L 39
private MenuItem menuItem2; 40
private MenuItem startGpsMenuI 41
private MenuItem stopGpsMenuI 42
private EventHandler updateDataH 45
GpsDeviceState device = null; 46
GpsPosition position = null; 47
Gps gps = new Gps(); 49
public Form1() 51
// Required for Windows Form Designer support 54
InitializeComponent(); 56
// TODO: Add any constructor code after InitializeComponent call 59
/// &summary& 62
/// Clean up any resources being used. 63
/// &/summary& 64
protected override void Dispose( bool disposing ) 65
base.Dispose( disposing ); 67
#region Windows Form Designer generated code 69
/// &summary& 70
/// Required method for Designer support - do not modify 71
/// the contents of this method with the code editor. 72
/// &/summary& 73
private void InitializeComponent() 74
this.mainMenu1 = new System.Windows.Forms.MainMenu(); 76
this.exitMenuItem = new System.Windows.Forms.MenuItem(); 77
this.menuItem2 = new System.Windows.Forms.MenuItem(); 78
this.startGpsMenuItem = new System.Windows.Forms.MenuItem(); 79
this.stopGpsMenuItem = new System.Windows.Forms.MenuItem(); 80
this.status = new System.Windows.Forms.Label(); 81
// mainMenu1 83
this.mainMenu1.MenuItems.Add(this.exitMenuItem); 85
this.mainMenu1.MenuItems.Add(this.menuItem2); 86
// exitMenuItem 88
this.exitMenuItem.Text = "Exit"; 90
this.exitMenuItem.Click += new System.EventHandler(this.exitMenuItem_Click); 91
// menuItem2 93
this.menuItem2.MenuItems.Add(this.startGpsMenuItem); 95
this.menuItem2.MenuItems.Add(this.stopGpsMenuItem); 96
this.menuItem2.Text = "GPS"; 97
// startGpsMenuItem 99
// // // this.stopGpsMenuItem.Text = "Stop GPS"; // // this.status.Size = new System.Drawing.Size( // Form1 this.ClientSize = new System.Drawing.Size( this.Menu = this.mainMenu1; this.Closed += new System.EventHandler(this.Form1_Closed); } /// The main entry point for the application. { } private void exitMenuItem_Click(object sender, EventArgs e) if (gps.Opened) gps.Close(); } private void Form1_Load(object sender, System.EventArgs e) updateDataHandler = new EventHandler(UpdateData); status.Text = ""; status.Width = Screen.PrimaryScreen.WorkingArea.W gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged); { // update the UI on the UI thread { // update the UI on the UI thread } void UpdateData(object sender, System.EventArgs args) if (gps.Opened) string str = ""; { { if (position.LatitudeValid) str += "Latitude (DD):\n
" + position.Latitude + "\n"; " + position.LatitudeInDegreesMinutesSeconds + "\n"; { " + position.Longitude + "\n"; " + position.LongitudeInDegreesMinutesSeconds + "\n"; position.SatellitesInViewValid && { " + position.GetSatellitesInSolution().Length + "/" + { " + position.Time.ToString() + "\n"; } status.Text = } { { } { { } startGpsMenuItem.Enabled = true; } private void startGpsMenuItem_Click(object sender, EventArgs e) if (!gps.Opened) gps.Open(); stopGpsMenuItem.Enabled = true; } 15 using S 16
17 #endregion 18
19 namespace Microsoft.WindowsMobile.Samples.Location 20 { 21
/// &summary& 22
/// class that represents a gps coordinate in degrees, minutes, and seconds.
/// &/summary& 24
public class DegreesMinutesSeconds 25
bool isP 28
/// &summary& 29
/// Returns true if the degrees, minutes and seconds refer to a positive value, 30
/// false otherwise. 31
/// &/summary& 32
public bool IsPositive 33
get { return isP } 35
/// &summary& 39
/// The degrees unit of the coordinate 40
/// &/summary& 41
public uint Degrees 42
get { return } 44
/// &summary& 48
/// The minutes unit of the coordinate 49
/// &/summary& 50
public uint Minutes 51
get { return } 53
/// &summary& 57
/// The seconds unit of the coordinate 58
/// &/summary& 59
public double Seconds 60
get { return } 62
/// &summary& 65
/// Constructs a new instance of DegreesMinutesSeconds converting
/// from decimal degrees 67
/// &/summary& 68
/// ¶m name="decimalDegrees"&Initial value as decimal degrees&/param& 69
public DegreesMinutesSeconds(double decimalDegrees) 70
isPositive = (decimalDegrees & minutes = (uint) doubleM 77
seconds = (doubleMinutes - (double)minutes) * /// are negative coordinates.&/param& 86
/// ¶m name="degrees"&Degrees unit of the coordinate&/param& 87
/// ¶m name="minutes"&Minutes unit of the coordinate&/param& 88
/// ¶m name="seconds"&Seconds unit of the coordinate. This should be a positive value.&/param& 89
public DegreesMinutesSeconds(bool isPositive, uint degrees, uint minutes, double seconds) 90
this.isPositive = isP 92
this.degrees = 93
this.minutes = 94
this.seconds = 95
/// &summary& 98
/// Converts the decimal, minutes, seconds coordinate to
/// decimal degrees /// &returns&&/returns& { val = isPositive ? val : val * - return /// Converts the instance to a string in format: D M' S" /// &returns&string representation of degrees, minutes, seconds&/returns& { } /// &summary& /// &/summary& { { } /// &summary& /// &/summary& { { } } public delegate void LocationChangedEventHandler(object sender, LocationChangedEventArgs args); /// Summary description for GPS. public class Gps // handle to the gps device // devices gets a new location // device state changes // thread System.Threading.Thread gpsEventThread = null; View Code
event LocationChangedEventHandler locationC
/// &summary&
/// Event that is raised when the GPS locaction data changes
/// &/summary&
public event LocationChangedEventHandler LocationChanged
locationChanged += 11
// create our event thread only if the user decides to listen 13
CreateGpsEventThread(); 14
locationChanged -= 18
event DeviceStateChangedEventHandler deviceStateC 23
/// &summary& 25
/// Event that is raised when the GPS device state changes 26
/// &/summary& 27
public event DeviceStateChangedEventHandler DeviceStateChanged 28
deviceStateChanged += 32
// create our event thread only if the user decides to listen 34
CreateGpsEventThread(); 35
deviceStateChanged -= 39
/// &summary& 43
/// True: The GPS device has been opened. False: It has not been opened 44
/// &/summary& 45
public bool Opened 46
get { return gpsHandle != IntPtr.Z } 48
public Gps() 51
// make sure that the GPS was closed. 57
Close(); 58
/// &summary& 61
/// Opens the GPS device and prepares to receive data from it. 62
/// &/summary& 63
public void Open() 64
if (!Opened) 66
// create handles for GPS events 68
newLocationHandle = CreateEvent(IntPtr.Zero, deviceStateChangedHandle = CreateEvent(IntPtr.Zero, stopHandle = CreateEvent(IntPtr.Zero, gpsHandle = GPSOpenDevice(newLocationHandle, deviceStateChangedHandle, null, // block until our event thread is finished before lock (this) if (newLocationHandle != IntPtr.Zero) CloseHandle(newLocationHandle); } if (deviceStateChangedHandle != IntPtr.Zero) CloseHandle(deviceStateChangedHandle); } if (stopHandle != IntPtr.Zero) CloseHandle(stopHandle); } } /// &summary& /// &/summary& public GpsPosition GetPosition() return GetPosition(TimeSpan.Zero); /// &summary& /// the maxAge passed in /// ¶m name="maxAge"&Max age of the gps position data that you want back. /// if maxAge == TimeSpan.Zero, then the age of the data is ignored&/param& public GpsPosition GetPosition(TimeSpan maxAge) GpsPosition gpsPosition = null; { // has the same memory layout as its native counterpart gpsPosition = new GpsPosition(); Marshal.StructureToPtr(gpsPosition, ptr, false); // call native method passing in our native buffer if (result == { gpsPosition = (GpsPosition)Marshal.PtrToStructure(ptr, typeof(GpsPosition)); if (maxAge != TimeSpan.Zero) // check to see if the data is recent enough. { } } // free our native memory } return gpsP
// write out GPS version of 1 16
Marshal.WriteInt32(pGpsDevice, // instantiate the GpsDeviceState class passing in the native pointer 23
device = new GpsDeviceState(pGpsDevice); 24
// free our native memory 27
Utils.LocalFree(pGpsDevice); 28
/// &summary& 33
/// Creates our event thread that will receive native events 34
/// &/summary& 35
private void CreateGpsEventThread() 36
// we only want to create the thread if we don't have one created already
// and we have opened the gps device 39
if (gpsEventThread == null && gpsHandle != IntPtr.Zero) 40
// Create and start thread to listen for GPS events 42
gpsEventThread = new System.Threading.Thread(new System.Threading.ThreadStart(WaitForGpsEvents)); 43
gpsEventThread.Start(); 44
/// &summary& 48
/// Method used to listen for native events from the GPS.
/// &/summary& 50
private void WaitForGpsEvents() 51
lock (this) 53
bool listening = true; 55
// allocate 3 handles worth of memory to pass to WaitForMultipleObjects 56
IntPtr handles = Utils.LocalAlloc( Marshal.WriteInt32(handles, while (listening) 64
int obj = WaitForMultipleObjects( // we've been signalled to stop 72
listening = false; 73
case // location has changed 83
if (locationChanged != null) 84
locationChanged(this, new LocationChangedEventArgs(GetPosition())); 86
// free the memory we allocated for the native handles 93
Utils.LocalFree(handles); 94
// clear our gpsEventThread so that we can recreate this thread again 96
// if the events are hooked up again. 97
gpsEventThread = null; 98
} #region PInvokes to gpsapi.dll #endregion #region PInvokes to coredll.dll 15 using S 16 using System.Runtime.InteropS 17
18 #endregion 19
20 public enum GpsServiceState : int 21 { 22
Off = StartingUp = Unloading = Unknown = - public static int GpsDeviceStructureSize = /// &summary& 53
/// State of the GPS Intermediate Driver service 54
/// &/summary& 55
public GpsServiceState ServiceState 56
get {return (GpsServiceState)serviceS} 58
int deviceState = /// &summary& 71
/// Friendly name of the real GPS device we are currently using. 72
/// &/summary& 73
public string FriendlyName 74
get {return friendlyN} 76
/// &summary& 79
/// Constructor of GpsDeviceState.
It copies values from the native pointer
/// passed in.
/// &/summary& 82
/// ¶m name="pGpsDevice"&Native pointer to memory that contains 83
/// the GPS_DEVICE data&/param& 84
public GpsDeviceState(IntPtr pGpsDevice) 85
// make sure our pointer is valid 87
if (pGpsDevice == IntPtr.Zero) 88
throw new ArgumentException(); 90
// read in the service state which starts at offset 8 93
serviceState = Marshal.ReadInt32(pGpsDevice, // the friendly name starts at offset 88 98
IntPtr pFriendlyName = (IntPtr)(pGpsDevice.ToInt32() + friendlyName = Marshal.PtrToStringUni(pFriendlyName); } public int this[int value]
if (value == else if (value == else if (value == else if (value == else if (value == else if (value == else throw new ArgumentOutOfRangeException("value must be 0 - 11");
#endregion
enum FixQuality : int
Unknown = enum FixSelection : int
Unknown = internal static int GPS_VALID_LATITUDE = internal static int GPS_VALID_SPEED = internal static int GPS_VALID_MAGNETIC_VARIATION = internal static int GPS_VALID_ALTITUDE_WRT_ELLIPSOID = internal static int GPS_VALID_HORIZONTAL_DILUTION_OF_PRECISION = internal static int GPS_VALID_SATELLITE_COUNT = internal static int GPS_VALID_SATELLITES_IN_VIEW = internal static int GPS_VALID_SATELLITES_IN_VIEW_ELEVATION = internal static int GPS_VALID_SATELLITES_IN_VIEW_SIGNAL_TO_NOISE_RATIO = // Current version of GPSID client is using.
internal int dwSize = // Additional information about this location structure (GPS_DATA_FLAGS_XXX)
internal int dwFlags = // Degrees latitude.
North is positive
internal double dblLongitude = // Speed in knots
internal float flHeading = // Magnetic variation.
East is positive
internal float flAltitudeWRTSeaLevel = //** Quality of this fix
// Where did we get fix from?
internal FixQuality fixQuality = FixQuality.U
// Is this 2d or 3d fix?
internal FixType fixType = FixType.U
// Auto or manual selection between 2d or 3d mode
internal FixSelection selectionType = FixSelection.U
// Position Dilution Of Precision
internal float flPositionDilutionOfPrecision = // Vertical Dilution Of Precision
internal float flVerticalDilutionOfPrecision = // PRN numbers of satellites used in the solution
internal SatelliteArray rgdwSatellitesUsedPRNs = new SatelliteArray();
// Number of satellites in view.
From 0-GPS_MAX_SATELLITES
internal int dwSatellitesInView = /// &summary&
/// Satellites used in the solution
/// &/summary&
/// &returns&Array of Satellites&/returns&
public Satellite[] GetSatellitesInSolution()
Satellite[] inViewSatellites = GetSatellitesInView();
ArrayList list = new ArrayList();
for (int index = if (rgdwSatellitesUsedPRNs[index] == inViewSatellites[viewIndex].Id)
found = inViewSatellites[viewIndex];
list.Add(found);
return (Satellite[])list.ToArray(typeof(Satellite));
/// &summary&
/// True if the SatellitesInSolution property is valid, false if invalid
/// &/summary&
public bool SatellitesInSolutionValid
get { return (dwValidFields & GPS_VALID_SATELLITES_USED_PRNS) != satellites = new Satellite[dwSatellitesInView];
for (int index = /// &summary&
/// Number of satellites used in solution
/// &/summary&
public int SatelliteCount
get { return dwSatelliteC }
/// &summary&
/// True if the SatelliteCount property is valid, false if invalid
/// &/summary&
public bool SatelliteCountValid
get { return (dwValidFields & GPS_VALID_SATELLITE_COUNT) != /// &summary&
/// Speed in knots
/// &/summary&
public float Speed
get { return flS }
/// &summary&
/// True if the Speed property is valid, false if invalid
/// &/summary&
public bool SpeedValid
get { return (dwValidFields & GPS_VALID_SPEED) != /// &summary&
/// Altitute with regards to sea level, in meters
/// &/summary&
public float SeaLevelAltitude
get { return flAltitudeWRTSeaL }
/// &summary&
/// True if the SeaLevelAltitude property is valid, false if invalid
/// &/summary&
public bool SeaLevelAltitudeValid
get { return (dwValidFields & GPS_VALID_ALTITUDE_WRT_SEA_LEVEL) != /// &summary&
/// Longitude in decimal degrees.
East is positive
/// &/summary&
public double Longitude
get { return dblL }
/// &summary&
/// Longitude in degrees, minutes, seconds.
East is positive
/// &/summary&
public DegreesMinutesSeconds LongitudeInDegreesMinutesSeconds
get { return new DegreesMinutesSeconds(dblLongitude); }
/// &summary&
/// True if the Longitude property is valid, false if invalid
/// &/summary&
public bool LongitudeValid
get { return (dwValidFields & GPS_VALID_LONGITUDE) != /// &summary&
/// Position Dilution Of Precision
/// &/summary&
public float PositionDilutionOfPrecision
get { return flPositionDilutionOfP }
/// &summary&
/// True if the PositionDilutionOfPrecision property is valid, false if invalid
/// &/summary&
public bool PositionDilutionOfPrecisionValid
get { return (dwValidFields & GPS_VALID_POSITION_DILUTION_OF_PRECISION) != /// &summary&
/// Vertical Dilution Of Precision
/// &/summary&
public float VerticalDilutionOfPrecision
get { return flVerticalDilutionOfP }
/// &summary&
/// True if the VerticalDilutionOfPrecision property is valid, false if invalid
/// &/summary&
public bool VerticalDilutionOfPrecisionValid
get { return (dwValidFields & GPS_VALID_VERTICAL_DILUTION_OF_PRECISION) != /// &summary&
/// Satellites used in the solution
/// &/summary&
/// &returns&Array of Satellites&/returns&
public Satellite[] GetSatellitesInSolution()
Satellite[] inViewSatellites = GetSatellitesInView();
ArrayList list = new ArrayList();
for (int index = if (rgdwSatellitesUsedPRNs[index] == inViewSatellites[viewIndex].Id)
found = inViewSatellites[viewIndex];
list.Add(found);
return (Satellite[])list.ToArray(typeof(Satellite));
/// &summary&
/// True if the SatellitesInSolution property is valid, false if invalid
/// &/summary&
public bool SatellitesInSolutionValid
get { return (dwValidFields & GPS_VALID_SATELLITES_USED_PRNS) != satellites = new Satellite[dwSatellitesInView];
for (int index = /// &summary&
/// Number of satellites used in solution
/// &/summary&
public int SatelliteCount
get { return dwSatelliteC }
/// &summary&
/// True if the SatelliteCount property is valid, false if invalid
/// &/summary&
public bool SatelliteCountValid
get { return (dwValidFields & GPS_VALID_SATELLITE_COUNT) != /// &summary&
/// Speed in knots
/// &/summary&
public float Speed
get { return flS }
/// &summary&
/// True if the Speed property is valid, false if invalid
/// &/summary&
public bool SpeedValid
get { return (dwValidFields & GPS_VALID_SPEED) != /// &summary&
/// Altitute with regards to sea level, in meters
/// &/summary&
public float SeaLevelAltitude
get { return flAltitudeWRTSeaL }
/// &summary&
/// True if the SeaLevelAltitude property is valid, false if invalid
/// &/summary&
public bool SeaLevelAltitudeValid
get { return (dwValidFields & GPS_VALID_ALTITUDE_WRT_SEA_LEVEL) != /// &summary&
/// Longitude in decimal degrees.
East is positive
/// &/summary&
public double Longitude
get { return dblL }
/// &summary&
/// Longitude in degrees, minutes, seconds.
East is positive
/// &/summary&
public DegreesMinutesSeconds LongitudeInDegreesMinutesSeconds
get { return new DegreesMinutesSeconds(dblLongitude); }
/// &summary&
/// True if the Longitude property is valid, false if invalid
/// &/summary&
public bool LongitudeValid
get { return (dwValidFields & GPS_VALID_LONGITUDE) != /// &summary&
/// Position Dilution Of Precision
/// &/summary&
public float PositionDilutionOfPrecision
get { return flPositionDilutionOfP }
/// &summary&
/// True if the PositionDilutionOfPrecision property is valid, false if invalid
/// &/summary&
public bool PositionDilutionOfPrecisionValid
get { return (dwValidFields & GPS_VALID_POSITION_DILUTION_OF_PRECISION) != /// &summary&
/// Vertical Dilution Of Precision
/// &/summary&
public float VerticalDilutionOfPrecision
get { return flVerticalDilutionOfP }
/// &summary&
/// True if the VerticalDilutionOfPrecision property is valid, false if invalid
/// &/summary&
public bool VerticalDilutionOfPrecisionValid
get { return (dwValidFields & GPS_VALID_VERTICAL_DILUTION_OF_PRECISION) != /// &summary& /// &/summary& { { } /// &summary& /// &/summary& { { } View Code 1 // 2 // Copyright (c) Microsoft Corporation.
All rights reserved. 3 // 4 // 5 // Use of this sample source code is subject to the terms of the Microsoft 6 // license agreement under which you licensed this sample source code. If 7 // you did not accept the terms of the license agreement, you are not 8 // authorized to use this sample source code. For the terms of the license, 9 // please see the license agreement between you and Microsoft or, if applicable, /// &summary& /// &/summary& { { { if (ptr == IntPtr.Zero) throw new OutOfMemoryException(); } public static void LocalFree(IntPtr hMem) IntPtr ptr = Win32.LocalFree(hMem); { } } public class Win32 public const int LMEM_ZEROINIT = [System.Runtime.InteropServices.DllImport("coredll.dll", EntryPoint = "#33", SetLastError = true)] }

参考资料

 

随机推荐