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

📄 uicommon.cpp

📁 EVC环境下用SDK开发WINCE的应用程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "StdAfx.h"
#include "resource.h"
#include "NWnd.h"

#include "UICommon.h"
#include "SettingDlg.h"
#include "Commdlg.h"

// Point to the main window of the application.
CNWnd		*g_pMainWnd = NULL;

// current instance
HINSTANCE	g_hInstance = NULL;

extern "C" 
{  
	// When drag map, UI set this event and ask MD(map dislpay) to redraw map,
	// MD will set the event signal when it finished redawing map.
	HANDLE	g_hMDEvent = NULL;
//	HANDLE	g_GPS_Event = NULL;
	extern HANDLE g_hSmoothEvent = NULL;
	// when draggin map begins, set the flag TRUE, when done, set it FALSE.
	BOOL	g_bIsDraggingMap = FALSE;
	void SetDraggingMap(BOOL bDrag) { g_bIsDraggingMap = bDrag; }	
	BOOL IsDraggingMap() { return g_bIsDraggingMap; }

	// save the map scale before searching route, when return from
	// route overveiw, restore to this scale. 
	// when start guidance, Guidance will retrieve this value and 
	// redraw the map with this scale and back to car POS.
	UINT g_uPrevMapScale = 0;
	UINT GetPrevMapScale() { return g_uPrevMapScale; }
	void SetPrevMapScale(UINT scale) { g_uPrevMapScale = scale;}
	// when we need to blt a bitmap, we can use the following mem DC
	// other than creating another one.
	HDC		g_hTempMemDC = NULL;
	HDC	NGetTempMemDC() 
	{ 
		ASSERT(NULL!=g_hTempMemDC); 
		return g_hTempMemDC; 
	}

	HINSTANCE NGetInstanceHandle()
	{
		ASSERT( NULL != g_hInstance );
		return g_hInstance; 
	}

	// create the memdc for UI. all windows are drawed in the memdc,
	// when finished, copy it to  screen.
	HDC		g_hdcUI = NULL;
	HBITMAP g_hbmpUIOld;

}	// end of extern "C"

HRGN	g_hRgnNULL = NULL;
// The font is used for UI text in most case.
HFONT	g_hNavFont = NULL;
// Cursor for all windows in this app.
HCURSOR g_hNavCursor = NULL;

// when program boots, initialize this global variable by
// system time and  then refresh it by timer(every 2 seconds). 
// if GPS singal is reliable, we adjust it by GPS time periodically.
static SYSTEMTIME g_GPSTime;
LPSYSTEMTIME GetGPSTime() 
{
	return &g_GPSTime;
}

// retreive the pointer to the main window of the application.
CNWnd* NGetMainWnd() 
{ 
	ASSERT( g_pMainWnd!=NULL );
	ASSERT( ::IsWindow(g_pMainWnd->m_hWnd) );
	return g_pMainWnd; 
}

HFONT NGetNavFont()
{
	ASSERT( NULL != g_hNavFont );
	return g_hNavFont;
}

// Create font used for UI
// try to create all fonts here, don't create same font 
// in different class or defferent function. if we need to 
// create font somewhere, declare a global variable or static member 
// variable for class, and try to initialize them here.

static HFONT s_hFont8Heavy = NULL; 
static HFONT s_hFont15Heavy = NULL; 
static HFONT s_hFont12Heavy = NULL;
static HFONT s_hFont25Heavy = NULL;
static HFONT s_hFont18Heavy = NULL;

HFONT NavGetFont(int nIndex)
{
	HFONT hfont = NULL;
	switch(nIndex)
	{
	case 8:
		hfont = s_hFont8Heavy;
		break;
	case 12:
		hfont = s_hFont12Heavy;
		break;
	case 15:
		hfont = s_hFont15Heavy;
		break;
	case 18:
		hfont = s_hFont18Heavy;
		break;
	case 25:
		hfont = s_hFont25Heavy;
		break;
	default:
		TRACE(_T("Error index, there no such font.\n"));
		ASSERT(FALSE);
		break;
	}

	ASSERT( hfont != NULL);
	return hfont;
}

BOOL InitNavFont()
{

	// to be sure to create the font only once.
	ASSERT( g_hNavFont == NULL );

	LOGFONT lf;
	memset(&lf, 0, sizeof(LOGFONT));
	lf.lfWeight = FW_HEAVY;

	// Create 8 heavy font
	lf.lfHeight = -8;
	s_hFont8Heavy = CreateFontIndirect(&lf);
	if( NULL == s_hFont8Heavy )
	{
		ASSERT(_T("Failing to create font."));
		return FALSE;
	}

	// Create 12 heavy font
	lf.lfHeight = -12;
	s_hFont12Heavy = CreateFontIndirect(&lf);
	if( NULL == s_hFont12Heavy )
	{
		ASSERT(_T("Failing to create font."));
		return FALSE;
	}

	// create 15 heavy font
	lf.lfHeight = -15;
	s_hFont15Heavy = CreateFontIndirect(&lf);
	if( NULL == s_hFont15Heavy )
	{
		ASSERT(_T("Failing to create font."));
		return FALSE;
	}

	// create 18 heavy font
	lf.lfHeight = -18;
	s_hFont18Heavy = CreateFontIndirect(&lf);
	if( NULL == s_hFont18Heavy )
	{
		ASSERT(_T("Failing to create font."));
		return FALSE;
	}


	// Create 25 heavy font
	lf.lfHeight = -35;
	s_hFont25Heavy = CreateFontIndirect(&lf);
	if( NULL == s_hFont25Heavy )
	{
		ASSERT(_T("Failing to create font."));
		return FALSE;
	}

	g_hNavFont = s_hFont12Heavy;
	CSettingDlg::sm_hCaptionFont = s_hFont15Heavy;

	return TRUE;
}


void FreeNavFont()
{
	DeleteObject(s_hFont12Heavy);
	s_hFont12Heavy = NULL;
	DeleteObject(s_hFont15Heavy);
	s_hFont15Heavy = NULL;
	DeleteObject(s_hFont18Heavy);
	s_hFont18Heavy = NULL;
	DeleteObject(s_hFont25Heavy);
	s_hFont25Heavy = NULL;

}

// the returned bitmap will be created in this function, and 
// it's the calling function's reponsibility to delete the object.
HBITMAP NavLoadBitmap(int nWidth,int nHeight,UINT nIDResource)
{
	HDC		hdc  = GetDC(NULL);
//	CDC		*pdc = CDC::FromHandle(hdc);
	HDC		hDCMemory, hDCBtn;
	HBITMAP hbmMemory, hbmBtn;
	BITMAP	bminfo;

	// create memory dc and load bitmap from resources
	hDCMemory = CreateCompatibleDC(hdc);
	hbmMemory = LoadBitmap( NGetInstanceHandle(),(LPCTSTR)nIDResource );
	ASSERT( hbmMemory != NULL );
	GetObject(hbmMemory, sizeof(bminfo), &bminfo);
	SelectObject(hDCMemory, hbmMemory);

	// create memory dc and create memory bitmap
	hDCBtn = CreateCompatibleDC(hdc);
	hbmBtn = CreateCompatibleBitmap(hdc,nWidth,nHeight);
	SelectObject(hDCBtn, hbmBtn);

	// stretch the bmp to proper size.
	const int MARGIN = 6;
	BitBlt(hDCBtn,0,0,MARGIN,MARGIN,hDCMemory,0,0,SRCCOPY);	// left_top corner
	BitBlt(hDCBtn,nWidth-MARGIN,0,MARGIN,MARGIN,
		hDCMemory,bminfo.bmWidth-MARGIN,0,SRCCOPY);			// top_right corner
	BitBlt(hDCBtn,0,nHeight-MARGIN,MARGIN,MARGIN,
		hDCMemory,0,bminfo.bmHeight-MARGIN,SRCCOPY);			// left_bottom corner
	BitBlt(hDCBtn,nWidth-MARGIN,nHeight-MARGIN,MARGIN,MARGIN,
		hDCMemory,bminfo.bmWidth-MARGIN,bminfo.bmHeight-MARGIN,SRCCOPY);	// right_bottom corner

	StretchBlt(hDCBtn,0,MARGIN,MARGIN,nHeight-2*MARGIN,
		hDCMemory,0,MARGIN,MARGIN,bminfo.bmHeight-2*MARGIN,SRCCOPY);						// left bar
	StretchBlt(hDCBtn,nWidth-MARGIN,MARGIN,MARGIN,nHeight-2*MARGIN,
		hDCMemory,bminfo.bmWidth-MARGIN,MARGIN,MARGIN,bminfo.bmHeight-2*MARGIN,SRCCOPY);	// right bar
	StretchBlt(hDCBtn,MARGIN,0,nWidth-2*MARGIN,MARGIN,
		hDCMemory,MARGIN,0,bminfo.bmWidth-2*MARGIN,MARGIN,SRCCOPY);							// top bar
	StretchBlt(hDCBtn,MARGIN,nHeight-MARGIN,nWidth-2*MARGIN,MARGIN,
		hDCMemory,MARGIN,bminfo.bmHeight-MARGIN,bminfo.bmWidth-2*MARGIN,MARGIN,SRCCOPY);	// bottom bar

	StretchBlt(hDCBtn,MARGIN,MARGIN,nWidth-2*MARGIN,nHeight-2*MARGIN,
		hDCMemory,MARGIN,MARGIN,bminfo.bmWidth-2*MARGIN,bminfo.bmHeight-2*MARGIN,SRCCOPY);	// middle block

	// clean up
	DeleteDC(hDCMemory);
	DeleteObject(hbmMemory);
	DeleteDC(hDCBtn);
	ReleaseDC(NULL,hdc);
	return hbmBtn;
}
//
// process function key input, return TRUE if handled, otherwise FALSE.
//
BOOL ProcessKeyEvent(LONG lKeyCode)
{

	static BOOL bLCDPowerOn = TRUE;
	static DWORD dwVolume = 80;

	// send hardkey message to top window.
	HWND	hwnd = GetActiveWindow();//NGetMainWnd()->GetSafeHwnd();



	return FALSE;
}

// Retrieve the top window
CNWnd* NGetActiveWindow()
{
	CNWnd* pWnd;
	if( CNDialog::sm_nNextDlg>0  ) // stack not empty.
	{
		pWnd = (CNWnd*)CNDialog::sm_pDlg[CNDialog::sm_nNextDlg-1];
		ASSERT( pWnd != NULL);
	} else	// stack is empty, 
	{
		pWnd = NULL;
	}
/*
	HWND hwnd = ::GetForegroundWindow();
	int nIndex = (int)CNDialog::sm_nNextDlg -1;
	for(; nIndex>=0; nIndex--)
	{
		pWnd = (CNWnd*)CNDialog::sm_pDlg[nIndex];
		if( hwnd == pWnd->GetSafeHwnd() ) // found
			break;
	}

	if( nIndex<0 )	// not found.
		pWnd = NULL;
*/
	return pWnd;
}

// Check if the app is active.
BOOL IsActiveApp()
{
#ifndef PC_VERSION
	return TRUE;	// in target version, it's always active.
#else
	HWND hwnd = ::GetActiveWindow();
	HINSTANCE hInst = NULL;
	if( hwnd != NULL )
	{
		hInst = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);
	}
	return NGetInstanceHandle() == hInst;
#endif
}

// fill rectagle with specified color.
void FillSolidRect(HDC hDC, LPCRECT lpRect, COLORREF clr)
{
	ASSERT(hDC != NULL);

⌨️ 快捷键说明

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