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

📄 gpsutil.cpp

📁 很好的GPS应用源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#include "stdafx.h"
#include "resource.h"
#include "gps.h"
#include <aygshell.h>

//REGENTRY myRegInfo = {HKEY_LOCAL_MACHINE, L"SOFTWARE\\Digiark\\Eztrip", L"traceno", L"1"};

long datel;
__int8 dateh;
long f_tracePos;   //进行GPS轨迹回放时轨迹记录文件指针所在的位置

// Return true if the COM exists, false if the COM does not exist
bool COMAvailable(int COMNum) {
	wchar_t szPort[15];

	pIDComDev = INVALID_HANDLE_VALUE;
	wsprintf( szPort, _T("COM%d:"), COMNum );
	pIDComDev = CreateFile( szPort,//指向端口名的Pointer 
		GENERIC_READ | GENERIC_WRITE,//允许读和写
		0,//独占方式(共享模式)
		NULL,
		OPEN_EXISTING,//打开而不是创建(创建方式)
		0,	//FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,//重叠方式(文件属性和标志)
		NULL 
		);

	if( pIDComDev == INVALID_HANDLE_VALUE) {
		return false;
    } else {
		CloseHandle( pIDComDev );
        return true;
	}
}

//以下为GPS的一些函数
//GPS 设置对话框.
LRESULT CALLBACK SetGPSSetup(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	RECT rt, rt1;
	int DlgWidth, DlgHeight;	// dialog width and height in pixel units
	int NewPosX, NewPosY;

	switch (message)
	{
		case WM_INITDIALOG:
			// trying to center the About dialog
			if (GetWindowRect(hDlg, &rt1))
			{
				GetClientRect(GetParent(hDlg), &rt);
				DlgWidth	= rt1.right - rt1.left;
				DlgHeight	= rt1.bottom - rt1.top ;
				NewPosX		= (rt.right - rt.left - DlgWidth)/2;
				NewPosY		= (rt.bottom - rt.top - DlgHeight)/2;
				
				// if the About box is larger than the physical screen 
				if (NewPosX < 0) NewPosX = 0;
				if (NewPosY < 0) NewPosY = 0;
				SetWindowPos(hDlg, 0, NewPosX, NewPosY,
					0, 0, SWP_NOZORDER | SWP_NOSIZE);
			}

			{
				HWND hwListBox = GetDlgItem(hDlg, IDC_COMPORT); 
				TCHAR szBufW[16];
				for(int i = 1; i<=16; i++)
				{
					if (COMAvailable(i)==true) {
					  wsprintf(szBufW, L"COM %d", i);
					  nPort = i;
					} else {
					  wsprintf(szBufW, L"COM %d不可用", i);
					}
				    ::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);
				}
				/*wsprintf(szBufW, L"COM %d", 1);
				::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);
				wsprintf(szBufW, L"COM %d", 2);
				::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);
				wsprintf(szBufW, L"COM %d", 3);
				::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);
				wsprintf(szBufW, L"COM %d", 4);
				::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);*/

				::SendMessage(hwListBox, CB_SETCURSEL, nPort-1, 0);

				hwListBox = GetDlgItem(hDlg, IDC_COMBO2); 
				for(i = 0; i<15; i++)
				{
					wsprintf(szBufW, L"%d", arrayBaud[i]);
					::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);
				}
				::SendMessage(hwListBox, CB_SETCURSEL, nBaud, 0);

				hwListBox = GetDlgItem(hDlg, IDC_COMBO3);  
				for(i = 4; i<=8; i++)
				{
					wsprintf(szBufW, L"%d", i);
					::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);
				}
				::SendMessage(hwListBox, CB_SETCURSEL, nByteSize-4, 0);

				hwListBox = GetDlgItem(hDlg, IDC_COMBO4); 
				wsprintf(szBufW, L"无");
				::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);
				wsprintf(szBufW, L"奇校验");
				::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);
				wsprintf(szBufW, L"偶校验");
				::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);
				wsprintf(szBufW, L"标记");
				::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);
				wsprintf(szBufW, L"空格");
				::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);
				::SendMessage(hwListBox, CB_SETCURSEL, nParity, 0);

				hwListBox = GetDlgItem(hDlg, IDC_COMBO5); 
				wsprintf(szBufW, L"1");
				::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);

				wsprintf(szBufW, L"1.5");
				::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);
				wsprintf(szBufW, L"2");
				::SendMessage(hwListBox, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBufW);
				::SendMessage(hwListBox, CB_SETCURSEL, nStopBits, 0);
			}
			return true;

		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
			case IDC_COMPORT:
				break;
			case IDOK:
			{
				HWND hwListBox = GetDlgItem(hDlg, IDC_COMPORT); 
				int nSelPort = ::SendMessage(hwListBox, CB_GETCURSEL, 0, 0);
				if(nSelPort != LB_ERR)
				{
					nPort = nSelPort+1;
				}

				hwListBox = GetDlgItem(hDlg, IDC_COMBO2); 
				nSelPort = ::SendMessage(hwListBox, CB_GETCURSEL, 0, 0);
				if(nSelPort)
				{
					nBaud = nSelPort;
				}

				hwListBox = GetDlgItem(hDlg, IDC_COMBO3); 
				nSelPort = ::SendMessage(hwListBox, CB_GETCURSEL, 0, 0);
				if(nSelPort)
				{
					nByteSize = nSelPort+4;
				}

				hwListBox = GetDlgItem(hDlg, IDC_COMBO4); 
				nSelPort = ::SendMessage(hwListBox, CB_GETCURSEL, 0, 0);
				if(nSelPort)
				{
					nParity = nSelPort;
				}

				hwListBox = GetDlgItem(hDlg, IDC_COMBO5); 
				nSelPort = ::SendMessage(hwListBox, CB_GETCURSEL, 0, 0);
				if(nSelPort)
				{
					nStopBits = nSelPort;
				}
			}

				EndDialog(hDlg, LOWORD(wParam));
				return true;
			case IDCANCEL:
				EndDialog(hDlg, LOWORD(wParam));
				return true;
			}
			break;
	}
    return false;
}

//GPS状态对话框
LRESULT CALLBACK SetGPSStatus(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  HDC hdc;
  PAINTSTRUCT ps;

  //SHINITDLGINFO shidi;

  switch (message)
	{
		case WM_INITDIALOG:
            // Create a Done button and size it.  
			/*
            shidi.dwMask = SHIDIM_FLAGS;
            shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
            shidi.hDlg = hDlg;
            SHInitDialog(&shidi);
			UpdateGPSStatus(hDlg);
			GPSStatushWnd = hDlg;*/
			return true;

		case GPSINFO_CHANGED:
			UpdateGPSStatus(hDlg);
			break;
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
			case IDOK:
				GPSStatushWnd = NULL;
				EndDialog(hDlg, LOWORD(wParam));
				return true;
			case IDCANCEL:
				GPSStatushWnd = NULL;
				EndDialog(hDlg, LOWORD(wParam));
				return true;
			}
			break;
		case WM_PAINT:
			hdc = BeginPaint(hDlg, &ps);
			PaintPanent(hdc,hDlg);
			EndPaint(hDlg, &ps);
			break;
	}
    return false;
}

//绘制星的位置
void  PaintPanent(HDC hdc, HWND hWnd)
{
	RECT rt;
	POINT pt;
	POINT drawpt[2];
	int x0,y0;
	int length,height;
	int r;
	WCHAR str[10];
	int ww ;

	HBRUSH hBlueBrush, hYellowBrush, hRedBrush, hGrayBrush;

	HPEN hBlackPen, hWhitePen, hNullPen;
	HBRUSH hBlackBrush,hNullBrush;

	hBlackBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
	hNullBrush = (HBRUSH)GetStockObject(NULL_BRUSH);
	hGrayBrush = (HBRUSH)GetStockObject(GRAY_BRUSH);

    hBlueBrush = ::CreateSolidBrush(RGB(200, 200, 255));
	hYellowBrush = ::CreateSolidBrush(RGB(250, 250, 200));
	hRedBrush = ::CreateSolidBrush(RGB(255, 0, 0));

	hBlackPen = (HPEN)GetStockObject(BLACK_PEN);
	hWhitePen = (HPEN)GetStockObject(WHITE_PEN);
	hNullPen = (HPEN)GetStockObject(NULL_PEN);

	HWND subhWnd = GetDlgItem(hWnd, IDC_MAP);
	GetWindowRect(subhWnd, &rt);
	pt.x = (rt.left + rt.right)/2;
	pt.y = (rt.top + rt.bottom)/2;
	r = abs(rt.right - rt.left);

	ScreenToClient(hWnd, &pt);

	ww = 10;
	if(hCompDC == NULL)
	{
		hCompDC = CreateCompatibleDC(hdc);
	    hCompBuff = CreateCompatibleBitmap(hdc, r+20, r+20);
		hCompBuff = (HBITMAP) SelectObject(hCompDC, hCompBuff);
	}

	HPEN pOldPen = (HPEN)SelectObject(hCompDC, hBlackPen);
	HBRUSH pOldBrush = (HBRUSH)SelectObject(hCompDC, hBlueBrush);

	//画一个方块
	Rectangle(hCompDC, 0, 0, r + 2*ww, r + 2*ww);
	//画外园
	Ellipse(hCompDC, ww, ww, ww + r, ww + r);
	//画中心园
	Ellipse(hCompDC, ww + r/4, ww + r/4, ww + r*3/4, ww + r*3/4);

	//画十字线
	drawpt[0].x = ww;
	drawpt[0].y = r/2 + ww;
	drawpt[1].x = r + ww;
	drawpt[1].y = r/2 + ww;
	Polyline(hCompDC, drawpt, 2);

	drawpt[0].x = r/2 + ww;
	drawpt[0].y = ww;
	drawpt[1].x = r/2 + ww;
	drawpt[1].y = r + ww;
	Polyline(hCompDC, drawpt, 2);

	//画方向
	//SelectObject(hCompDC, hGrayBrush);
	SelectObject(hCompDC, hYellowBrush);

	//'W'
	//一个字的小小
	wcscpy(str, _T("W"));
	DrawText(hCompDC, str, wcslen(str), &rt, DT_CALCRECT|DT_SINGLELINE);
	length = abs(rt.right - rt.left);
	height = abs(rt.bottom - rt.top);

	rt.left = 2;
	rt.right = rt.left + length;
	rt.top = ww + r/2 - height/2;
	rt.bottom = rt.top + height;
	SetBkMode(hCompDC, TRANSPARENT);
	Rectangle(hCompDC, rt.left-1, rt.top, rt.right+1, rt.bottom);
	DrawText(hCompDC, _T("W"), 1, &rt, DT_SINGLELINE|DT_CENTER);

	//'E'
	wcscpy(str, _T("E"));
	DrawText(hCompDC, str, wcslen(str), &rt, DT_CALCRECT|DT_SINGLELINE);
	length = abs(rt.right - rt.left);
	height = abs(rt.bottom - rt.top);

	rt.left = r + ww + 2;
	rt.right = rt.left + length;
	rt.top = ww + r/2 - height/2;
	rt.bottom = rt.top + height;
	Rectangle(hCompDC, rt.left-1, rt.top, rt.right+1, rt.bottom);
	DrawText(hCompDC, _T("E"), 1, &rt, DT_SINGLELINE|DT_CENTER);

	//'N'
	wcscpy(str, _T("N"));
	DrawText(hCompDC, str, wcslen(str), &rt, DT_CALCRECT|DT_SINGLELINE);
	length = abs(rt.right - rt.left);
	height = abs(rt.bottom - rt.top);

	rt.left = ww + r/2 - length/2;
	rt.right = rt.left + length;
	rt.top = 2;
	rt.bottom = rt.top + height;
	Rectangle(hCompDC, rt.left-1, rt.top, rt.right+1, rt.bottom);
	DrawText(hCompDC, _T("N"), 1, &rt, DT_SINGLELINE|DT_CENTER);

	//'S'
	wcscpy(str, _T("S"));
	DrawText(hCompDC, str, wcslen(str), &rt, DT_CALCRECT|DT_SINGLELINE);
	length = abs(rt.right - rt.left);
	height = abs(rt.bottom - rt.top);

	rt.left = ww + r/2 - length/2;
	rt.right = rt.left + length;
	rt.bottom = r + 2*ww - 2;
	rt.top = rt.bottom - height;
	Rectangle(hCompDC, rt.left - 1, rt.top, rt.right+1, rt.bottom);
	DrawText(hCompDC, _T("S"), 1, &rt, DT_SINGLELINE|DT_CENTER);

	//画星的位置
	for(int i = 0; i<nplanet; i++)
	{
		//星的中心点
		length = int(((double)r/2.) * (1.0-planet[i].Elevation/90.));
		x0 = ww + r/2 + length*sin(planet[i].Azimuth*3.1415/180.);
		y0 = ww + r/2 - length*cos(planet[i].Azimuth*3.1415/180.);

		SelectObject(hCompDC, hNullPen);

		if(planet[i].bActive)
		{
			SelectObject(hCompDC, hRedBrush);
		}
		else
		{
			SelectObject(hCompDC, hYellowBrush);
		}

		//画星的位置的园
		//画一个园,首先确定大小
		wcscpy(str, _T("00"));
		DrawText(hCompDC, str, wcslen(str), &rt, DT_CALCRECT|DT_SINGLELINE);
		length = abs(rt.right - rt.left);
		height = abs(rt.bottom - rt.top);

		Ellipse(hCompDC, x0 - length/2 - 1, y0 - length/2 - 1,  x0 + length/2 + 1, y0 + length/2 + 1);

		//星的标号
		_itow(planet[i].PRVNo, str, ww);
		rt.left = x0 - length/2;
		rt.right =  x0 + length/2;
		rt.top = y0 - height/2;
		rt.bottom =  y0 + height/2 ;

		SetBkMode(hCompDC, TRANSPARENT);
		DrawText(hCompDC, str, wcslen(str), &rt, DT_SINGLELINE|DT_CENTER);
	}

	SetROP2(hdc, R2_COPYPEN);
	BitBlt(hdc, pt.x - r/2 - ww, pt.y - r/2 - ww, r+20, r+20, hCompDC,0,0,SRCCOPY);

	SelectObject(hCompDC, pOldPen);
	SelectObject(hCompDC, pOldBrush);

	DeleteObject(hBlueBrush);
	DeleteObject(hYellowBrush);
	DeleteObject(hRedBrush);
	DeleteObject(hBlackPen);
	DeleteObject(hWhitePen);
	DeleteObject(hNullPen);
}

//更新GPS的某一些值
void UpdateGPSStatus(HWND hWnd)
{
	TCHAR szBufW[200];

	HWND subhWnd = GetDlgItem(hWnd, IDC_STATUS); 
	if(bGPSStart)
	{
		if(available != 0)
		{
			wsprintf(szBufW, _T("GPS定位成功!"));
		}
		else
		{
			wsprintf(szBufW, _T("GPS正在定位..."));
		}
	}
	else
	{
		wsprintf(szBufW, _T("GPS未启动!"));
	}

	::SendMessage(subhWnd, WM_SETTEXT, 0, (LPARAM)(LPCTSTR)(szBufW));

	subhWnd = GetDlgItem(hWnd, IDC_DATE); 
	wsprintf(szBufW, _T("%d.%d.%d"), nyear, nmonth, nday);
	::SendMessage(subhWnd, WM_SETTEXT, 0, (LPARAM)(LPCTSTR)(szBufW));

	subhWnd = GetDlgItem(hWnd, IDC_TIME); 
	wsprintf(szBufW, _T("%d:%d:%d"), nhour, nminute, nsecond);
	::SendMessage(subhWnd, WM_SETTEXT, 0, (LPARAM)(LPCTSTR)(szBufW));

	int n1, n2;
	double d3;
	subhWnd = GetDlgItem(hWnd, IDC_LATITUDE); 
	n1 = int(latitude)/100;
	n2 = int(latitude) - n1*100;
	d3 = (latitude - n1*100 - n2)*100;
	wsprintf(szBufW, _T("%d度%d分%f秒(%c) %f"), n1,n2,d3,dire_latitude, latiX);
	::SendMessage(subhWnd, WM_SETTEXT, 0, (LPARAM)(LPCTSTR)(szBufW));

	subhWnd = GetDlgItem(hWnd, IDC_LONGITUDE); 
	n1 = int(longitude)/100;
	n2 = int(longitude) - n1*100;
	d3 = (longitude - n1*100 - n2)*100;

⌨️ 快捷键说明

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