Wince常用操作

#pragma once


template<class T>
void DeleteEx(T* pData)
{
 if( NULL != pData)
  delete pData;
 pData = NULL;
}

void getVesionTime(char cTime[21]);
char* UnicodToeChar(CString &i_szDest);
void deleteHeapData(char *i_pDest);
bool getCurFilePath(CString& o_szDest);
bool writeFile(const CString i_szPath, const bool i_bAppend, CString& i_szData);
bool readFile(CString& o_szData, const CString i_szPath);
bool findFile(const CString i_szPath);
COLORREF getRGB(const float i_fMin, const float i_fMax, const float i_fValue);
bool isLeapYear(int iYear);
int getDaysofMonth(int iYear, int iMonth);
void BrowerFile(CString szSrc);

//----------MFC abort--------
void assertDebug(bool i_bSign, CString i_strError = L"Error!");
//---------------------------


//----------MFC control----------
void zoomDlg(CDialog* io_pDlg, const float i_fRateW, const float i_fRateH);
void zoomText(CDialog* io_pDlg, const float i_fRateW, const float i_fRateH);
void moveDlg(CDialog* io_pDlg, const int i_iW, const int i_iH);
void showTaskBar(int iTaskBarStatus);
void maximizeWindow(CDialog* i_pDlg, int iTaskBarStatus = SW_HIDE, int iScreenY = 0);
void KeyBoard();
void doEvent();
//-------------------------------

 

 

#include "StdAfx.h"
#include <assert.h>
#include "WinCE600.h"
#include "AFX.h"


void getVesionTime(char cTime[21])
{
 strcpy(cTime,__DATE__ "-" __TIME__);
}

char* UnicodToeChar(CString &i_szDest)
{
 char *pchBuff;
 int nLen = WideCharToMultiByte(CP_ACP, 0, i_szDest, -1, NULL, 0, NULL, NULL);
 pchBuff = new char[nLen+1];
 WideCharToMultiByte (CP_ACP, 0,i_szDest, -1,pchBuff , nLen, NULL,NULL);
 return pchBuff;
}

void deleteHeapData(char *i_pDest)
{
 delete[] i_pDest;
}

void assertDebug(bool i_bSign, CString i_strError)
{
#ifdef _DEBUG
 if( !i_bSign)
 {
  AfxMessageBox(i_strError);
  assert(false);
 }
#endif
}

void zoomDlg(CDialog* io_pDlg, const float i_fRateW, const float i_fRateH)
{
 CRect rect;
 io_pDlg->GetWindowRect(&rect);
 int iW = rect.Width()*i_fRateW;
 int iH = rect.Height()*i_fRateH;
 io_pDlg->MoveWindow(rect.left,rect.top,iW,iH);
}

void zoomText(CDialog* io_pDlg, const float i_fRateW, const float i_fRateH)
{
 CFont* pFont = io_pDlg->GetFont();
 LOGFONT lf;
 pFont->GetLogFont(&lf);
 lf.lfWidth = lf.lfWidth * i_fRateW;
 lf.lfHeight = lf.lfHeight * i_fRateH;
 CFont* pfontNew = new CFont();
 pfontNew->CreateFontIndirect(&lf);
 io_pDlg->SetFont(pfontNew);
 pfontNew = NULL;
 delete pFont;
 pFont = NULL;
}

void moveDlg(CDialog* io_pDlg, const int i_iW, const int i_iH)
{
 CRect rect;
 io_pDlg->GetWindowRect(&rect);
 rect.bottom = rect.bottom+i_iH;
 rect.top = rect.top+i_iH;
 rect.left = rect.left+i_iW;
 rect.right = rect.right+i_iW;
 io_pDlg->MoveWindow(rect);
}

void KeyBoard()
{
 static bool bShowKeyBoard = false;
 bShowKeyBoard ? SipShowIM(SIPF_OFF) : SipShowIM(SIPF_ON);
 bShowKeyBoard = bShowKeyBoard ? false : true;
}

bool getCurFilePath(CString& i_szDest)
{
 TCHAR szCurPath[100];
 memset(szCurPath , 0 ,100);
 if( 0 == GetModuleFileName(NULL , szCurPath , 100) )
 {
  return false;
 }
 i_szDest = szCurPath;
 for(int i = i_szDest.GetLength() ; i > 0 ;i-- )
 {
  if(i_szDest[i] == '//')
  {
   i_szDest = i_szDest.Left(i+1);
   break;
  }
 }
 return true;
}

bool writeFile(const CString i_szPath, const bool i_bAppend, CString& i_szData)
{
 char *pContent = NULL;
 pContent = UnicodToeChar(i_szData);
 assertDebug(NULL != pContent, L"alloc space failed");

 CFile file;
 if( i_bAppend)
 {
  if( !file.Open(i_szPath , CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite ) )
  {
   deleteHeapData(pContent);
   file.Close();
   return false;
  }
  file.SeekToEnd();
 }
 else
 {
  if( !file.Open(i_szPath , CFile::modeCreate | CFile::modeWrite ) )//| CFile::typeText
  {
   deleteHeapData(pContent);
   file.Close();
   return false;
  }
 }

 file.Write(pContent , strlen(pContent) + 1 );
 deleteHeapData(pContent);
 file.Close();
 return true;
}

bool readFile(CString& o_szData, const CString i_szPath)
{
 CFile file;
 if( file.Open(i_szPath, CFile::modeRead))
 {
  ULONGLONG iLen = file.GetLength();
  BYTE* pBuf = new BYTE[iLen];
  file.Read(pBuf,iLen);
  CString szRet = CString((char*)pBuf);
  delete[] pBuf;
  pBuf = NULL;
  file.Close();

  o_szData = szRet;
  return true;
 }
 return false;
}

bool findFile(const CString i_szPath)
{
 CFile file;
 if( file.Open(i_szPath, CFile::modeRead))
 {
  file.Close();
  return true;
 }
 return false;
}


COLORREF getRGB(const float i_fMin, const float i_fMax, const float i_fValue)
{
 float fRate = (float)(i_fValue-i_fMin)/(i_fMax-i_fMin);
 fRate = (fRate<1)? fRate: 1;
 fRate = (fRate>0)? fRate: 0;
 int R,G,B;
 if( fRate >= 0.75)
 {
  fRate -= 0.75;
  R = 255;
  G = 255-fRate/0.25*255;
  B = 0;
 }
 else if( fRate >= 0.5)
 {
  fRate -= 0.5;
  R = fRate/0.25*255;
  G = 255;
  B = 0;
 }
 else if( fRate >= 0.25)
 {
  fRate -= 0.25;
  R = 0;
  G = 255;
  B = 255-fRate/0.25*255;
 }
 else if( fRate >= 0)
 {
  R = 0;
  G = fRate/0.25*255;
  B = 255;
 }
 else
 {
  R = 0;
  G = 0;
  B = 0;
 }
 return RGB(R,G,B);
}

void showTaskBar(int iTaskBarStatus)
{
 //隐藏状态栏
 HWND lpClassName;
 lpClassName = ::FindWindow(TEXT("HHTaskBar"), NULL);
 if(lpClassName != NULL)
  ::ShowWindow(lpClassName, iTaskBarStatus);
}

void maximizeWindow(CDialog* i_pDlg, int iTaskBarStatus , int iScreenY)
{
 //隐藏状态栏
 showTaskBar(iTaskBarStatus);

 //更改工作区域大小
 int screenx=GetSystemMetrics(SM_CXSCREEN);//得到屏幕的大小
 int screeny=GetSystemMetrics(SM_CYSCREEN);

 CRect rcWorkArea;
 rcWorkArea.left = 0;
 rcWorkArea.right = screenx;
 rcWorkArea.top = 0;
 rcWorkArea.bottom = screeny + iScreenY;
 ::SystemParametersInfo( SPI_SETWORKAREA, 0, &rcWorkArea, SPIF_SENDCHANGE );
 Sleep(100);
 //扩大工作窗口
 i_pDlg->MoveWindow(0,0,screenx,screeny,TRUE); 
}

void doEvent()
{
 //用于中断函数,处理消息事件,再返回这里。
 MSG msg;
 while (PeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE) )//判断是否有消息
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
}

ThreadEx::ThreadEx(HANDLE& o_hCloseEvent, LPTHREAD_START_ROUTINE lpStartAddr,LPVOID lpvThreadParam)
{
 m_hEventThread = CreateThread(NULL,0,lpStartAddr,lpvThreadParam,0,&m_dwEventThreadID);
 CString strEvent;
 strEvent.Format(L"EventThread_%d_%d",m_hEventThread,m_dwEventThreadID);
 m_hEventCloseEvent = CreateEvent(NULL,FALSE,FALSE,strEvent);
 o_hCloseEvent = m_hEventCloseEvent;
 TRACE(_T("Thread!/n"));
}

ThreadEx::~ThreadEx()
{
 SetEvent(m_hEventCloseEvent);
 if( WAIT_TIMEOUT == WaitForSingleObject(m_hEventThread,4000))
 {
  TRACE(_T("~Thread WAIT_TIMEOUT/n"));
  TerminateThread(m_hEventThread,0);
 }
 m_hEventThread = NULL;
 CloseHandle(m_hEventCloseEvent);
 TRACE(_T("~Thread!/n"));
}

bool isLeapYear(int iYear)
{
    if( iYear%4==0 && iYear%100!=0 || iYear%400==0)
        return true;
    else 
        return false;
}

int getDaysofMonth(int iYear, int iMonth)
{
 if( 1 == iMonth || 3 == iMonth || 5 == iMonth || 7 == iMonth || 8 == iMonth || 10 == iMonth || 12 == iMonth)
  return    31;
 else if( 2 == iMonth)
 {
  if( isLeapYear(iYear))
            return 29;
  else
   return 28;
 }
 else
  return 30;
}

void BrowerFile(CString szSrc)
{
 WIN32_FIND_DATA wfd;
 HANDLE hFile=::FindFirstFile(szSrc+_T("//*"),&wfd);
 if(hFile!=INVALID_HANDLE_VALUE)
 {
  do
  {
   if(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
   {
    CString strFolderName=wfd.cFileName;
    if(strFolderName != _T(".") && strFolderName != _T(".."))
    {
     CString szSubDir;
     szSubDir=szSrc+_T("//")+strFolderName;
     BrowerFile(szSubDir);
    }
   }
   else
   {
    CString strType = wfd.cFileName;
    strType = strType.Right(strType.GetLength()-strType.ReverseFind('.')-1);
    strType = strType.MakeUpper();
    if( L"BMP" == strType || L"PNG" == strType
     || L"GIF" == strType || L"JPG" == strType )
    {
     CString strPath = L"";
     strPath += szSrc;
     strPath += L"//";
     strPath += wfd.cFileName;
     strPath += "/n";
     TRACE(strPath);
    }
   }
  }while(::FindNextFile(hFile,&wfd));
  ::FindClose(hFile);
 }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值