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

📄 mainform.cs

📁 功能:基于windows mobile 的地图查看器。使用vs2005开发
💻 CS
📖 第 1 页 / 共 3 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Reflection;
using System.Drawing.Imaging;
using System.IO;

using Microsoft.WindowsCE.Forms;

using Microsoft.WindowsMobile.DirectX;
using Microsoft.WindowsMobile.DirectX.Direct3D;

using Microsoft.Samples.MD3DM;
using WorldWindow;

using PInvokeLibrary;
using GPSExample.Util;

namespace cfWorldWind
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			//this.Menu = mainMenu1; //wouldnt load

			miViewCrosshairs.Checked = Settings.Crosshairs;
			miViewPosition.Checked = Settings.Positions;
			miViewLatLon.Checked = Settings.LatLonLines;
			miViewEquator.Checked = Settings.Equator;
			miViewAxis.Checked = Settings.Axis;
			miDiagWorld.Checked = Settings.World;
			miViewBoundCount.Checked = Settings.BoundCount;
			miViewBoundCan.Checked = Settings.BoundCan;
			miViewBoundNor.Checked = Settings.BoundNor;
			miViewBoundSwe.Checked = Settings.BoundSwe;
			miViewBoundUs.Checked = Settings.BoundUs;
			miViewLandmark.Checked = Settings.Landmark;
			miViewFlags.Checked = Settings.Flag;
			
			miDiagWireFrame.Checked = Settings.WireFrame;
			miDiagInfo.Checked = Settings.Diagnostics;
			miDiagFps.Checked = Settings.FramesPerSecond;
			UpdateCullSetting(Settings.Culling);
			miDiagTexture.Checked = Settings.Texture;
			UpdateTileSetting(Settings.Tiles);
			miDiagTileOutline.Checked = Settings.TileOutline;
			miDiagRenderExc.Checked = Settings.RenderException;

			hardwareButton1.AssociatedControl = this;
			hardwareButton1.HardwareKey = Microsoft.WindowsCE.Forms.HardwareKeys.ApplicationKey1;
			//hardwareButton2.AssociatedControl = this;
			//hardwareButton2.HardwareKey = Microsoft.WindowsCE.Forms.HardwareKeys.ApplicationKey2;
			//hardwareButton3.AssociatedControl = this;
			//hardwareButton3.HardwareKey = Microsoft.WindowsCE.Forms.HardwareKeys.ApplicationKey3;
			hardwareButton4.AssociatedControl = this;
			hardwareButton4.HardwareKey = Microsoft.WindowsCE.Forms.HardwareKeys.ApplicationKey4;

			// Construct the reader
			// The two-parameter constructor uses default port settings.  It is equivalent to the following
			//     _gpsReader = new GPSReader("COM4:", 19200, ParitySetting.NoParity, 8, StopBitsSetting.OneStopBit)
			//_gpsReader = new GPSReader("COM4:", 19200);
			gpsReader = new GPSReader("COM2:", 4800);

			// Attach to GPSReader Events
			gpsReader.OnGPSMessage += new GPSEventHandler(gpsReader_OnGPSMessageLatLon);
		}

		static SplashForm splashForm = null;
		static public void SplashScreenShow()
		{
			splashForm = new SplashForm();
			//splashForm.Show();
			//System.Threading.Thread.Sleep(5000);
			Application.Run(splashForm);
		}

		static public void SplashScreenClose()
		{
			if (splashForm != null)
			{
				splashForm.Invoke(new EventHandler(splashForm.KillMe));
				splashForm.Dispose();
				splashForm = null;
			}
		}

		FpsTimerTool fpsTimer;
		PresentParameters presentParams = new PresentParameters();
		Device device = null;
		Material deviceMaterial;
		//Graphics graphics = null; //GDI+
		GPSReader gpsReader = null;

        public bool InitializeGraphics(out Exception exc)
        {
			exc = null;
            try
            {
                presentParams.Windowed = true;
                //presentParams.SwapEffect = SwapEffect.Discard;
				presentParams.SwapEffect = SwapEffect.Copy; //to save screen shots
				//presentParams.EnableAutoDepthStencil = true;
                //presentParams.AutoDepthStencilFormat = DepthFormat.D16;
                device = new Device(0, DeviceType.Default, this, CreateFlags.None, presentParams);
				//graphics = this.CreateGraphics();

				deviceMaterial = new Material();
				deviceMaterial.Diffuse = Color.White;
				deviceMaterial.Ambient = Color.White;
				device.Material = deviceMaterial;

                device.DeviceReset += new System.EventHandler(this.OnResetDevice);
                this.OnResetDevice(device, null);
            }
            catch (Exception ex)
            {
				exc = ex;
				return false;
            }

            return true;
        }

		MemoryStatus.MEMORYSTATUS memStatus = new MemoryStatus.MEMORYSTATUS();
		int megDivider = 1024; // * 1024;

		public uint GetAvailMem()
		{
			MemoryStatus.GlobalMemoryStatus(memStatus);
			return memStatus.dwAvailPhys;
		}

		World world;
		Camera camera;
		Crosshairs crosshairs;
		Diagnostic diagnostic;
		Position position;
		LatLon latLon;
		Tropics tropics;
		Axis axis;
		List<LatLon.LatLonLineList> alLatLon10;
		List<LatLon.LatLonLineList> alLatLon5;
		List<LatLon.LatLonLineList> alLatLon2;
		Boundary boundary;
		List<Boundary.BoundaryList> bCountry;
		List<Boundary.BoundaryList> bCanadian;
		List<Boundary.BoundaryList> bNorwegian;
		List<Boundary.BoundaryList> bSwedish;
		List<Boundary.BoundaryList> bUsState;
		Placename placename;
		Rectangle landmarkSize = new Rectangle(0, 0, 64, 64);
		Icons landmark;
		Rectangle flagSize = new Rectangle(0, 0, 64, 32);
		Icons flag;

        void OnResetDevice(object sender, EventArgs e)
        {
			uint memStart = GetAvailMem();

            Device device = (Device)sender;
			fpsTimer = new FpsTimerTool(device);

			//camera
			camera = new Camera();
			uint memCamera = GetAvailMem();

			//world
			world = new World();
			world.InitializeMesh(device);
			uint memWorldMesh = GetAvailMem();
			world.InitializeTiles(device);
			uint memWorldTiles = GetAvailMem();

			//axis
			axis = new Axis();
			axis.InitializeAxis(device);
			uint memAxis = GetAvailMem();

			//crosshairs
			crosshairs = new Crosshairs();
			crosshairs.InitializeVertex(device, this.Size, Color.White);
			uint memCrosshairs = GetAvailMem(); 

			//diagnostic
			diagnostic = Diagnostic.Instance(device);
			uint memDiagnostic = GetAvailMem();

			//position
			position = Position.Instance(device);
			uint memPosition = GetAvailMem();

			//lat lon tropics
			latLon = new LatLon();
			alLatLon10 = latLon.InitializeLatLonLines(device, 10);
			alLatLon5 = latLon.InitializeLatLonLines(device, 5);
			alLatLon2 = latLon.InitializeLatLonLines(device, 2);
			//alLatLon1 = world.InitializeLatLonLines(device, Settings.Radius, 1);
			tropics = new Tropics();
			tropics.InitializeVertex(device);
			uint memLatLon = GetAvailMem();

			//placenames
			placename = new Placename();
			//InitPlacenames();
			//uint memPlacename = GetAvailMem();	
			
			//landmark catalog
			landmark = new Icons();
			//InitLandmarks();
			//uint memLandmark = GetAvailMem();

			//boundaries
			boundary = new Boundary();
			//InitBoundaries();
			//uint memBoundary = GetAvailMem();

			//flags of the world
			flag = new Icons();
			//InitFlags();
			//uint memFlag = GetAvailMem();

			//lighting
			device.RenderState.Ambient = Color.White;
        }

		public void InitPlacenames()
		{
			placename.InitializeVertex(device);
		}

		public void DisposePlacenames()
		{
			placename.Dispose();
		}

		public void InitLandmarks()
		{
			LayerSet lsLandmark = LandmarkSettings.GetLayerSet();
			landmark.InitializeVertex(device, lsLandmark);
		}

		public void DisposeLandmarks()
		{
			landmark.Dispose();
		}

		public void InitBoundaries(Settings.BoundaryType boundaryType)
		{
			switch (boundaryType)
			{
				case Settings.BoundaryType.Country:
					bCountry = boundary.InitializeVertex(device, Settings.DirectoryBoundaries);
					boundary.PassthroughVertex(device, bCountry, Color.Yellow);
					break;
				case Settings.BoundaryType.Canadian:
					bCanadian = boundary.InitializeVertex(device, Settings.DirectoryBoundariesCanadian);
					boundary.PassthroughVertex(device, bCanadian, Color.Green);
					break;
				case Settings.BoundaryType.Norwegian:
					bNorwegian = boundary.InitializeVertex(device, Settings.DirectoryBoundariesNorwegian);
					boundary.PassthroughVertex(device, bNorwegian, Color.Red);
					break;
				case Settings.BoundaryType.Swedish:
					bSwedish = boundary.InitializeVertex(device, Settings.DirectoryBoundariesSwedish);
					boundary.PassthroughVertex(device, bSwedish, Color.Purple);
					break;
				case Settings.BoundaryType.UsState:
					bUsState = boundary.InitializeVertex(device, Settings.DirectoryBoundariesUS_State);
					boundary.PassthroughVertex(device, bUsState, Color.Cyan);
					break;
			}
		}

		public void DisposeBoundaries(Settings.BoundaryType boundaryType)
		{
			switch (boundaryType)
			{
				case Settings.BoundaryType.Country:
					boundary.Dispose(bCountry);
					break;
				case Settings.BoundaryType.Canadian:
					boundary.Dispose(bCanadian);
					break;
				case Settings.BoundaryType.Norwegian:
					boundary.Dispose(bNorwegian);
					break;
				case Settings.BoundaryType.Swedish:
					boundary.Dispose(bSwedish);
					break;
				case Settings.BoundaryType.UsState:
					boundary.Dispose(bUsState);
					break;
			}
		}

		public void InitFlags()
		{
			LayerSet lsFlag = FlagSettings.GetLayerSet();
			flag.InitializeVertex(device, lsFlag);
		}

		public void DisposeFlags()
		{
			flag.Dispose();
		}

        private void Render()
        {
            if (device == null) 
                return;

			fpsTimer.StartFrame();

			//defaults to CounterClockwise for LH, so LH vertices must be drawn Clockwise
			//WorldWind uses None or ClockWise
			device.RenderState.CullMode = Settings.Culling;

			// Render the sky according to view - example, close to earth, render sky blue, render space as black
			Color backgroundColor = Color.Black;
			if (camera.Altitude < 1000000f)
			{
				float percent = camera.Altitude / 1000000;
				if (percent > 1.0f)
					percent = 1.0f;
				else if (percent < 0.0f)
					percent = 0.0f;

				backgroundColor = System.Drawing.Color.FromArgb(
					(int)(Color.SkyBlue.R * (1.0f - percent)),
					(int)(Color.SkyBlue.G * (1.0f - percent)),
					(int)(Color.SkyBlue.B * (1.0f - percent)));
			}

			//see how much memory i'm sucking up
			Diagnostic.Instance(device).Add("mem", GetAvailMem() / megDivider);

			device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, backgroundColor, 1.0f, 0);
            device.BeginScene();

			camera.Upate(device);

			if (Settings.Axis == true)
			{
				axis.RenderAxis(device);
			}

			if (Settings.WireFrame == true)
			{
				device.RenderState.FillMode = FillMode.WireFrame;
			}
			else
			{
				device.RenderState.FillMode = FillMode.Solid;
			}

			if (Settings.World == true)
			{
				//draw the basic world 1st
				world.RenderMesh(device);
				if (Settings.Tiles != Settings.TileType.None)
				{
					//TODO update this on a separate thread
					//or only update this every second?
					//CalculateTiles will kick out early if no change
					world.CalculateTiles(device, camera);
					world.RenderTiles(device);
				}
				//else
				//{
				//	world.RenderMesh(device);
				//}
			}

			if (world.LevelChange == true)
			{
				//TODO only do this at level change
				//boundary.CalculateVertex(device, camera, bCountry, Color.Yellow);
				//boundary.CalculateVertex(device, camera, bCanadian, Color.Green);
				//boundary.CalculateVertex(device, camera, bNorwegian, Color.Red);
				//boundary.CalculateVertex(device, camera, bSwedish, Color.Purple);
				//boundary.CalculateVertex(device, camera, bUsState, Color.Cyan);
			}

			if (Settings.BoundCount == true)
			{
				boundary.RenderVertex(device, bCountry);
			}
			if (Settings.BoundCan == true)

⌨️ 快捷键说明

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