乞丐中的霸主(ぃ枫.net)(天行健,君子以自强不息;地势坤,君子以厚德载物! -《周易》)QQ讨论群(交流EVC++,VC++,USB驱动程序开发,上下机位通讯,无线网络通讯技术,Mobile开发)帮主QQ号:414885058

C#,C++ 学习

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

#include "stdafx.h"
#include "OnLine.h"
#include "Public.h"
void ConvToIntFFT(const short *inWaveData,int DataLength,short *outWaveData,float *yMax,float *yMin)
{
 int i;
 int PowerNum;
 float meanVal;
 float PPVal;
 short *ReArray=new short[DataLength];
 short *ImArray=new short[DataLength];
 short *TempArray=new short[DataLength];
 short *indexArray =new short[DataLength];

 CFile bitRevFile;
 int startAddress;
// iPublicDll BitRev = (iPublicDll)GetProcAddress(g_hPublicDll,_T("BitRev"));
 //计算最大值、最小值、峰峰值、均值
 *yMax=*(inWaveData);
 *yMin=*(inWaveData);
 meanVal=(float)*(inWaveData);
 *(ReArray)=*(inWaveData);
 for(i=1;i<DataLength;i++)
 {
  *(ReArray+i)=*(inWaveData+i);
  if(*(ReArray+i)>*yMax)
  {
   *yMax=*(ReArray+i);
  }
  if(ReArray[i]<*yMin)
  {
   *yMin=*(ReArray+i);
  }
  meanVal+=*(ReArray+i);
 }
 meanVal=meanVal/DataLength; //均值
 PPVal=*yMax-*yMin;   //峰峰值

 //数据计算
 switch(DataLength)
 {
 case 1024:
  PowerNum=10;
  startAddress = 0;
  break;
 case 2048:
  PowerNum=11;
  startAddress = 2048;
  break;
 case 4096:
  PowerNum=12;
  startAddress = 6144;
  break;
 case 8192:
  PowerNum=13;
  startAddress = 14336;
  break;
 case 16384:
  PowerNum=14;
  break;
 case 32768:
  PowerNum=15;
  break;
 }
 
 for(i=0;i<DataLength;i++)
 {
  TempArray[i]=(short)((ReArray[i]-meanVal)*(pow(2,BITNUM-1)-1)/PPVal);
 }
 if(bitRevFile.Open(BITREVPATH,CFile::modeRead))
 {
  bitRevFile.Seek(startAddress,CFile::begin);
  bitRevFile.Read(indexArray,DataLength*sizeof(short));
  bitRevFile.Close();
 }
 else
 {
  for(i=0; i<DataLength; i++)
   *(indexArray+i)=BitRev(i,PowerNum);   //到位序算法
 }
 for(i=0; i<DataLength; i++)
 {
  ReArray[i]=TempArray[*(indexArray+i)];
 }
 
 //INTFFT计算
 INTFFTCalc(ReArray,ImArray,DataLength);

 //输出
 DataLength=(int)(DataLength/2.56f);
 for(i=0;i<DataLength;i++)
 {
  *(outWaveData+i)=static_cast<short>((float)((PPVal * *(ReArray+i))/(pow(2,BITNUM-1)-1)));
 }
 
 *yMin=*outWaveData;
 *yMax=*outWaveData;
 for(i=1;i<DataLength;i++)
 {  
  if(*(outWaveData+i)>*yMax)
  {
   *yMax= *(outWaveData+i);
  }
  if(*(outWaveData+i)<*yMin)
  {
   *yMin= *(outWaveData+i);
  }
 }

 delete [] ReArray;
 delete [] ImArray;
 delete []TempArray;
 delete []indexArray;
 //return;
}

//INT16FFT计算
void INTFFTCalc(short *ReArray,short *ImArray,int DataLength)
{
 int CosVal;
 int SinVal;
 int TempImA;
 int TempImB;
 int TempReA;
 int TempReB;
 int TempReA2;
 int TempReB2;

 int TempL;
 long ReTwid;
 long ImTwid;
 short *SineTable=new short[DataLength];

 int i;
 int Gcnt;
 int Scnt;
 int Group;
 int Stage;
 int IndexA;
 int IndexB;
 int SinIndex;
 CFile sinFile;
 int startAddress;
 float fTemp;
 
 if(sinFile.Open(SINDATAPATH,CFile::modeRead))
 {
  switch(DataLength)
  {
  case 1024:
   startAddress = 0;
   break;
  case 2048:
   startAddress = 2048;
   break;
  case 4096:
   startAddress = 6144;
   break;
  case 8192:
   startAddress = 14336;
   break;
  default:
   startAddress = 0;
   break;
  }
  sinFile.Seek(startAddress,CFile::begin);
  sinFile.Read(SineTable,DataLength*sizeof(short));
  sinFile.Close();
 }
 else
 {
  for(i=0;i<DataLength;i++)
  {
   *(SineTable+i)=(short)(32767*sin(6.2832f*i/DataLength));
  }
 }
 

 Stage=2;
 Group=DataLength/4;
 IndexA=0;

 for(Gcnt=0;Gcnt<DataLength/2;Gcnt++)
 {
  IndexB=IndexA+1;
  TempReA=ReArray[IndexA];
  TempReB=ReArray[IndexB];

  //Calculate new value for ReArray[IndexA]
  TempL=TempReA+TempReB;
  ReArray[IndexA]=TempL/2;

  //Calculate new value for ReArray[IndexB]
  TempL=TempReA-TempReB;
  ReArray[IndexB]=TempL/2;

  ImArray[IndexA]=0;
  ImArray[IndexB]=0;

  IndexA=IndexB+1;
 }

 while(Stage<=DataLength/2)
 {
  IndexA=0;
  SinIndex=0;
  for(Gcnt=0;Gcnt<Group;Gcnt++)
  {
   for(Scnt=0;Scnt<Stage;Scnt++)
   {
    IndexB=IndexA+Stage;

    TempReA=ReArray[IndexA];
    TempReB=ReArray[IndexB];
    TempImA=ImArray[IndexA];
    TempImB=ImArray[IndexB];

    if(SinIndex==0)
    {
     TempL=TempReA+TempReB;
     TempReA2=TempL/2;

     TempL=TempReA-TempReB;
     TempReB2=TempL/2;

     TempL=TempImA-TempImB;
     TempImB=TempL/2;

     TempL=TempImA+TempImB;
     TempImA=TempL/2;
    }
    else if(SinIndex==DataLength/4)
    {
     TempL=TempReA-TempImB;
     TempReB2=TempL/2;

     TempL=TempReA+TempImB;
     TempReA2=TempL/2;

     TempL=TempImA+TempReB;
     TempImB=TempL/2;

     TempL=TempImA-TempReB;
     TempImA=TempL/2;
    }
    else
    {
     if(SinIndex>DataLength/4)
     {
      SinVal=SineTable[DataLength/2-SinIndex];
      CosVal=-1*SineTable[SinIndex-DataLength/4];
     }
     else
     {
      SinVal=SineTable[SinIndex];
      CosVal=SineTable[DataLength/4-SinIndex];
     }

     ReTwid=TempReB*CosVal+TempImB*SinVal;
     ImTwid=TempImB*CosVal-TempReB*SinVal;

     ReTwid = ReTwid / 32767;
     ImTwid = ImTwid / 32767;

     TempL=TempReA;
     TempL=TempL+ReTwid;
     TempReA2=TempL/2;

     TempL=TempReA;
     TempL=TempL-ReTwid;
     TempReB2=TempL/2;

     TempL=TempImA;
     TempL=TempL-ImTwid;
     TempImB=TempL/2;

     TempL=TempImA;
     TempL=TempL+ImTwid;
     TempImA=TempL/2;
    }

    ReArray[IndexA]=TempReA2;
    ReArray[IndexB]=TempReB2;
    ImArray[IndexA]=TempImA;
    ImArray[IndexB]=TempImB;

    IndexA=IndexA+1;
    SinIndex=SinIndex+Group;
   }//end for(Scnt=0;Scnt<Stage;Scnt++)
   IndexA=IndexB+1;
   SinIndex=0;
  }//end for(Gcnt=0;Gcnt<Group;Gcnt++)
  Group=Group/2;
  Stage=Stage*2;
 }// end while(Stage<=DataLength/2)

 for(i=0;i<DataLength;i++)
 {
  fTemp=(float)(2*sqrt(pow(ReArray[i],2)+pow(ImArray[i],2)));
  if(fTemp>=32767)
  {
   ReArray[i]=32767;
  }
  else
  {
   ReArray[i]=(short)fTemp;
  }
 }

 if(*SineTable!=NULL)
 {
  delete SineTable;
 }
 return ;
}

int BitRev(int index,int bitNum)
{
 int *iBool=new int[bitNum];
 int i;
 int result;

 for(i=bitNum-1;i>=0;i--)
 {
  if(index>=pow(2,i))
  {
   index=(int)(index-pow(2,i));
   iBool[i]=1;
  }
  else
  {
   iBool[i]=0;
  }
 }

 result=0;
 for(i=0;i<bitNum;i++)
 {
  result=(int)(result+pow(2,(bitNum-1-i))*iBool[i]);
 }

 delete iBool;
 return result;
}
void CalcIndexValue(const float *waveData,int dataLength,DataIndexVal *tDataIndex)
{
 //计算指标值,目前包括有效值,峰峰值,峭度
 int i;
 float temp1;
 float temp2;
 float minVal;
 float maxVal;
 
 temp1=0;
 temp2=0;
 minVal=*waveData;
 maxVal=*waveData;
 for(i=0;i<dataLength;i++)
 {
  temp1=(float)(temp1+pow(*(waveData+i),2));
  temp2=(float)(temp2+pow(*(waveData+i),4));
  if(*(waveData+i)<minVal)
  {
   minVal=*(waveData+i);
  }
  if(*(waveData+i)>maxVal)
  {
   maxVal=*(waveData+i);
  }
 }
 //有效值
 temp1=temp1/dataLength;
 tDataIndex->RMSValue=(float)sqrt(temp1);
 //峭度
 temp2=temp2/dataLength;
 tDataIndex->KurValue=(float)(temp2/pow(tDataIndex->RMSValue,4));
 //峰峰值
 tDataIndex->PPValue=maxVal-minVal;
 return;
}
int  CheckTmp(int meaTmp)   //温度校正
{
 int tmpMea = meaTmp;
 int tmpCheck = tmpMea/100;
/* switch(tmpCheck)
 {
 case 3:
  tmpMea -= 0;
  break;
 case 4:
  tmpMea -= 60;
  break;
 case 5:
  tmpMea -= 65;
  break;
 case 6:
  tmpMea -= 75;
  break;
 case 7:
  tmpMea -= 80;
  break;
 case 8:
  tmpMea -= 80;
  break;
 case 9:
  tmpMea -= 75;
  break;
 case 10:
  tmpMea -= 70;
  break;
 case 11:
  tmpMea -= 60;
  break;
 case 12:
  tmpMea -= 50;
  break;
 case 13:
  tmpMea -= 35;
  break;
 case 14:
  tmpMea -= 25;
  break;
 default:
  break;
 }*/
 return tmpMea;
}
CString StringTrim(CString strInput,int strWide ,int leftPoint)  //按宽度精确截取字符串。
{
 int iTemp;
 CString result;
 
 iTemp = GetStringWide(strInput);
 if(iTemp <= strWide)
 {
  result = strInput;
 }
 else
 {
  result = strInput;
  iTemp = GetStringWide(result);
  if(leftPoint !=2)  //为2时省略左边3个点
  {
   while(iTemp > strWide-3)
   {
    result.Delete(0);
    iTemp = GetStringWide(result);
   }   
   result = _T("...")+result;
  }
  else
  {
   while(iTemp > strWide)
   {
    result.Delete(0);
    iTemp = GetStringWide(result);
   }
  }
  
 }
 return result;
}

CString ManageFileName(CString oldName)
{
 CString newName;
 LPTSTR  tempName;
 int i,lenth;
 TCHAR c;
 lenth = oldName.GetLength();
 tempName = new TCHAR[lenth+1];
 if (tempName == NULL)
 {
  return _T("");
 }
 ZeroMemory(tempName,(lenth+1)*sizeof(TCHAR));
 for (i=0;i<lenth;i++)
 {
  c= oldName.GetAt(i);
  switch (oldName.GetAt(i))
  {
  case _T('/'):*(tempName+i) = _T('-');break;
  case _T('\\'):*(tempName+i) = _T('_');break;
  case _T('?'):*(tempName+i) = _T('%');break;
  case _T('*'):*(tempName+i) = _T('!');break;
  case _T('|'):*(tempName+i) = _T('~');break;
  case _T('"'):*(tempName+i) = _T('[');break;
  case _T(':'):*(tempName+i) = _T(';');break;
  case _T('<'):*(tempName+i) = _T('(');break;
  case _T('>'):*(tempName+i) = _T(')');break;
  default:*(tempName+i) = oldName.GetAt(i);break;
  }
 }
 newName = tempName;
 if (tempName!=NULL)
 {
  delete []tempName;
  tempName = NULL;
 }
 return  newName;
}
//---------------------------------------------
BOOL DeleteDirectory(const CString szDir)
{
 CString szFindDir=szDir;
 if(szFindDir.Right(1)!="\\")
 {
  szFindDir+="\\";
 }
 szFindDir+="*.*";
 
 WIN32_FIND_DATA fd;
 HANDLE hFind;
 hFind=FindFirstFile(szFindDir,&fd);
 if(hFind!=INVALID_HANDLE_VALUE)
 {
  do{
   if((fd.dwFileAttributes & 0x10) == FILE_ATTRIBUTE_DIRECTORY)  //文件可能还有其他属性,OK
   { 
    //it must be directory,get into
    DeleteDirectory(szDir+"http://www.cnblogs.com/jimmy998love/admin/file://%22+fd.cfilename/);  //递归调用
   }
   else
   {
    //it is file ,delete it
    if(DeleteFile(szDir+"http://www.cnblogs.com/jimmy998love/admin/file://%22+fd.cfilename)==false/)
    {
     return FALSE;
    }
   }
   
  }while(FindNextFile(hFind,&fd));
 } 
 //if you donot close the handle,the next step of Removing Directory would failed
 CloseHandle(hFind);
 //the root directory must be empty ,so remove it
 if(RemoveDirectory(szDir)==FALSE)  //只能移除空目录
 {
  return FALSE;
 } 
 return TRUE;
}
//----------------------------------copy screen to a bmp file ------------------------
LPSTR   FindDIBBits(LPSTR lpbi)  
{  
 return (lpbi + *(LPDWORD)lpbi + PaletteSize(lpbi));  
}  

WORD PaletteSize(LPSTR   lpbi)  
{  
 return (DIBNumColors(lpbi) * sizeof(RGBQUAD));  
}  

DWORD BytesPerLine(LPBITMAPINFOHEADER lpBMIH)  
{  
 return WIDTHBYTES(lpBMIH->biWidth * lpBMIH->biPlanes * lpBMIH->biBitCount);  
}  

WORD   DIBNumColors(LPSTR lpbi)  
{  
 WORD   wBitCount;  
 DWORD   dwClrUsed;  
   
 dwClrUsed = ((LPBITMAPINFOHEADER)lpbi)->biClrUsed;  
   
 if   (dwClrUsed)  
  return (WORD)dwClrUsed;  
   
 wBitCount   =   ((LPBITMAPINFOHEADER)   lpbi)->biBitCount;  
   
 switch(wBitCount)  
 {  
 case   8: return   256;  
 case   1:   return   2;  
 case   2:   return   4;  
 case   4:   return   16;     
 default:return   0;  
 }  
 return   0;  
 }

HBITMAP CopyScreenToBitmap(HWND   hPic,LPRECT   lpRect)  
{  
 
 HDC hScrDC;  
 HDC hMemDC;              
 //   屏幕和内存设备描述表    
 HBITMAP                   hBitmap;    
 HBITMAP                   hOldBitmap;           
 //   选定区域坐标  
 int nX,   nY,   nX2,   nY2;               
 //   位图宽度和高度  
 int nWidth,   nHeight;              
 //   屏幕分辨率  
 int xScrn,   yScrn;            
 //   确保选定区域不为空矩形  
 if   (IsRectEmpty(lpRect))  
  return   NULL;  
 //为屏幕创建设备描述表    
 //hScrDC   =   CreateDC("DISPLAY",   NULL,   NULL,   NULL);  
 hScrDC   =   GetDC(hPic);    
 //为屏幕设备描述表创建兼容的内存设备描述表    
 hMemDC   =   CreateCompatibleDC(hScrDC);    
 //   获得选定区域坐标    
 nX   =     lpRect->left;    
 nY   =     lpRect->top;    
 nX2   =   lpRect->right;    
 nY2   =   lpRect->bottom;    
 //   获得屏幕分辨率  
 RECT   rc1;  
 GetWindowRect(hPic,&rc1);  
 //       xScrn   =   GetDeviceCaps(hScrDC,   HORZRES);  
 xScrn=rc1.right-rc1.left;  
 
 //       yScrn   =   GetDeviceCaps(hScrDC,   VERTRES);  
 yScrn=rc1.bottom-rc1.top;  
 //确保选定区域是可见的  
 
 if   (nX   <   0)  
  
  nX   =   0;  
 
 if   (nY   <   0)  
  
  nY   =   0;  
 
 if   (nX2   >   xScrn)  
  
  nX2   =   xScrn;  
 
 if   (nY2   >   yScrn)  
  
  nY2   =   yScrn;  
 
 nWidth=nX2   -   nX;  
 
 nHeight=nY2   -   nY;  
 
 //创建一个与屏幕设备描述表兼容的位图  
 hBitmap   =   CreateCompatibleBitmap(hScrDC,   nWidth,   nHeight);  
 
 //把新位图选到内存设备描述表中  
 hOldBitmap   =   (HBITMAP)SelectObject(hMemDC,hBitmap);  
 
 //把屏幕设备描述表拷贝到内存设备描述表中  
 BitBlt(hMemDC,   0,   0,   nWidth,   nHeight,hScrDC,   nX,   nY,   SRCCOPY);  
 
 //得到屏幕位图的句柄  
 hBitmap=(HBITMAP)SelectObject(hMemDC,   hOldBitmap);  
 //清除    
 ReleaseDC(hPic,hScrDC);  
 DeleteDC(hMemDC);  
 //   返回位图句柄  
 return   hBitmap;  
}

LPSTR   GettingBits(HBITMAP   hSourceBitmap)    
{  
 LPSTR       lpDIB=NULL;  
 //1.   Initialize   source   bitmap.   For   example   load   from   resources.  
 //HBITMAP   hSourceBitmap   =   LoadBitmap(g_hInst,   MAKEINTRESOURCE(IDB_BITMAP1));  
   
 //2.   Getting   bimap   size.  
 BITMAP   bm;  
 GetObject(hSourceBitmap,   sizeof(BITMAP),   &bm);  
   
 lpDIB=(LPSTR)LocalAlloc(LPTR,bm.bmHeight*WIDTHBYTES(bm.bmWidth*24)+40);  
 
 if(lpDIB==NULL)  
 {  
  return   NULL;  
 }  
   
 //3.   Creating   new   bitmap   and   receive   pointer   to   it's   bits.  
 HBITMAP   hTargetBitmap;  
 void   *pBuffer;  
   
 //3.1   Initilize   DIBINFO   structure  
 DIBINFO     dibInfo;  
 dibInfo.bmiHeader.biBitCount   =   24;  
 dibInfo.bmiHeader.biClrImportant   =   0;  
 dibInfo.bmiHeader.biClrUsed   =   0;  
 dibInfo.bmiHeader.biCompression   =   0;  
 dibInfo.bmiHeader.biHeight   =   bm.bmHeight;  
 dibInfo.bmiHeader.biPlanes   =   1;  
 dibInfo.bmiHeader.biSize   =   40;  
 dibInfo.bmiHeader.biSizeImage   =   WIDTHBYTES(bm.bmWidth*24)*bm.bmHeight;  
 dibInfo.bmiHeader.biWidth   =   bm.bmWidth;  
 dibInfo.bmiHeader.biXPelsPerMeter   =   3780;  
 dibInfo.bmiHeader.biYPelsPerMeter   =   3780;  
 dibInfo.bmiColors[0].rgbBlue   =   0;  
 dibInfo.bmiColors[0].rgbGreen   =   0;  
 dibInfo.bmiColors[0].rgbRed   =   0;  
 dibInfo.bmiColors[0].rgbReserved   =   0;  
   
 //3.2   Create   bitmap   and   receive   pointer   to   points   into   pBuffer  
 HDC   hDC   =   ::GetDC(NULL);  
 ASSERT(hDC);  
 hTargetBitmap   =   CreateDIBSection(  
  hDC,  
  (const   BITMAPINFO*)dibInfo,  
  DIB_RGB_COLORS,  
  (void**)&pBuffer,  
  NULL,  
  0);  
   
 ::ReleaseDC(NULL,   hDC);  
   
   
 //4.   Copy   source   bitmap   into   the   target   bitmap.  
   
 //4.1   Create   2   device   contexts    
 HDC   memDc;  
 memDc=CreateCompatibleDC(NULL);  
 ASSERT(memDc!=NULL);  
   
 HDC   targetDc;  
 targetDc=CreateCompatibleDC(NULL);  
 ASSERT(targetDc!=NULL);  
   
 //4.2   Select   source   bitmap   into   one   DC,   target   into   another  
   
 HBITMAP   hOldBitmap1   =   (HBITMAP)::SelectObject(memDc,   hSourceBitmap);  
 HBITMAP   hOldBitmap2   =   (HBITMAP)::SelectObject(targetDc,   hTargetBitmap);  
   
 //4.3   Copy   source   bitmap   into   the   target   one  
 BitBlt(targetDc,0,   0,   bm.bmWidth,   bm.bmHeight,   memDc,   0,   0,   SRCCOPY);  
   
 //4.4   Restore   device   contexts  
 ::SelectObject(memDc,   hOldBitmap1);  
 ::SelectObject(targetDc,   hOldBitmap2);  
 DeleteDC(memDc);  
 DeleteDC(targetDc);  
   
 //Here   we   can   bitmap   bits:   pBuffer.   Note:  
 //   1.   pBuffer   contains   3   bytes   per   point  
 //   2.   Lines   ane   from   the   bottom   to   the   top!  
 //   3.   Points   in   the   line   are   from   the   left   to   the   right  
 //   4.   Bytes   in   one   point   are   BGR   (blue,   green,   red)   not   RGB  
 //   5.   Don't   delete   pBuffer,   it   will   be   automatically   deleted    
 //         when   delete   hTargetBitmap  
   
 LPSTR   lptmp=lpDIB;  
 CopyMemory(lptmp,&dibInfo.bmiHeader,40);  
 lptmp+=40;  
 CopyMemory(lptmp,pBuffer,dibInfo.bmiHeader.biSizeImage);  
   
 DeleteObject(hSourceBitmap);  
 DeleteObject(hTargetBitmap);  
   
 return   lpDIB;  
}

BOOL   SaveDIBToFile(LPSTR   hDib,   LPCTSTR   szFileName)  
{  
 HANDLE ghFile=NULL;  
 BOOL bResult=FALSE;  
 //   Bitmap文件头  
 BITMAPFILEHEADER   bmfHdr;  
 
 //   指向BITMAPINFOHEADER的指针  
 LPBITMAPINFOHEADER   lpBI=NULL;  
 
 //   DIB大小  
 DWORD       dwDIBSize=0;  
 
 DWORD       dwBytes=0;  
 
 if   (hDib   ==   NULL)  
 {  
  return   FALSE;  
 }  
 
 //   读取BITMAPINFO结构,并锁定  
   
 lpBI=(LPBITMAPINFOHEADER)hDib;  
   
 if   (lpBI   ==   NULL)  
 {  
  return   FALSE;  
 }  
 
 //   判断是否是WIN3.0   DIB  
 if   (!IS_WIN30_DIB(lpBI))  
 {  
  //   返回FALSE  
  return   FALSE;  
 }  
   
 //   文件类型"BM"  
 bmfHdr.bfType   =   DIB_HEADER_MARKER;  
 
 //文件头大小+颜色表大小  
 //BITMAPINFOHEADER和BITMAPCOREHEADER结构的第一个DWORD都是该结构的大小
 dwDIBSize   =   *(LPDWORD)lpBI   +   ::PaletteSize((LPSTR)lpBI);  
 
 //   计算图像大小  
 //   象素的大小  
 DWORD   dwBmBitsSize;  
 
 //大小为Width   *   Height  
 dwBmBitsSize   =   WIDTHBYTES((lpBI->biWidth)*((DWORD)lpBI->biBitCount))   *   lpBI->biHeight;    
 //计算出DIB真正的大小  
 dwDIBSize   +=   dwBmBitsSize;    
 //更新biSizeImage(很多BMP文件头中biSizeImage的值是错误的)  
 lpBI->biSizeImage   =   dwBmBitsSize;    
 //计算文件大小:DIB大小+BITMAPFILEHEADER结构大小  
 bmfHdr.bfSize   =   dwDIBSize   +   sizeof(BITMAPFILEHEADER);    
 //两个保留字  
 bmfHdr.bfReserved1   =   0;  
 bmfHdr.bfReserved2   =   0;  
 
 //计算偏移量bfOffBits,它的大小为Bitmap文件头大小+DIB头大小+颜色表大小  
 bmfHdr.bfOffBits   =   (DWORD)sizeof(BITMAPFILEHEADER)   +   lpBI->biSize  
  +   PaletteSize((LPSTR)lpBI);      
 //打开文件  
 ghFile=CreateFile(szFileName,   GENERIC_WRITE|GENERIC_READ,   FILE_SHARE_READ|FILE_SHARE_WRITE,   NULL,  
  CREATE_ALWAYS,   FILE_ATTRIBUTE_NORMAL,   NULL);  
   
 if(ghFile==INVALID_HANDLE_VALUE)  
 {  
  NKDbgPrintfW(_T("save   dib   to   file   fail.   \r\n"));  
  return   FALSE;  
 }  
 //写文件头  
 bResult=WriteFile(ghFile,(LPSTR)&bmfHdr,sizeof(BITMAPFILEHEADER),&dwBytes,NULL);    
 ASSERT(bResult!=FALSE);  
 //写DIB头和象素  
 bResult=WriteFile(ghFile,(LPSTR)lpBI,dwDIBSize,&dwBytes,NULL);  
 ASSERT(bResult!=FALSE);    
 CloseHandle(ghFile);       
 //返回TRUE  
 return   TRUE;  
}

HBITMAP CopyScreenToBitmap(int &nWidth,int &nHeight)
{
 // 屏幕和内存设备描述表
 HDC  hScrDC, hMemDC;     
 // 位图句柄
 HBITMAP  hBitmap, hOldBitmap;   
 // 屏幕分辨率
 int  xScrn, yScrn;        
 
 //为屏幕创建设备描述表
 hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
 //为屏幕设备描述表创建兼容的内存设备描述表
 hMemDC = CreateCompatibleDC(hScrDC);
 // 获得屏幕分辨率
 xScrn = GetDeviceCaps(hScrDC, HORZRES);
 yScrn = GetDeviceCaps(hScrDC, VERTRES);
 
 //存储屏幕的长宽
 nWidth = xScrn;
 nHeight = yScrn;
 
 // 创建一个与屏幕设备描述表兼容的位图
 hBitmap = CreateCompatibleBitmap(hScrDC, xScrn, yScrn);
 // 把新位图选到内存设备描述表中
 hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
 // 把屏幕设备描述表拷贝到内存设备描述表中
 BitBlt(hMemDC, 0, 0, xScrn,yScrn,hScrDC, 0, 0, SRCCOPY);
 //得到屏幕位图的句柄
 hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
 //清除
 DeleteDC(hScrDC);
 DeleteDC(hMemDC);
 // 返回位图句柄
 return hBitmap;
}

BOOL WriteWindowToDIB(LPCTSTR fileName)  
{  
 HBITMAP   SaveBitmap;  
 LPSTR   SaveLpstr;  
 int nWidth,nHeight;
 SaveBitmap = ::CopyScreenToBitmap(nWidth,nHeight);
 SaveLpstr = GettingBits(SaveBitmap);  
 SaveDIBToFile(SaveLpstr,fileName); 
 return TRUE;
}

//----------------------------------copy screen to a bmp file ------------------------

CString GetDescribe(const CString strDescribe, int numResult)  
{
 CString tempString;
 int stringLength;
 int startPos;
 int endPos;
 int count = 0;
 bool bFirst = TRUE;
 int i;
 stringLength = strDescribe.GetLength();  
 for(i=0; i<stringLength; i++)
 {
  tempString = strDescribe.Mid(i,1);
  if(tempString.Compare(_T("-"))==0)
  {
   count++;
  }
  if(count ==numResult)
  {
   if(bFirst)
   {
    startPos = i+1;
    bFirst = FALSE;
    if(count==0)
    {
     startPos--;
    }
   }  
   
  }
  if(count == numResult+1)
  {
   endPos =i;
   break;
  }
 }
 if(i==stringLength-1)
  endPos = stringLength;
 tempString = strDescribe.Mid(startPos,endPos-startPos); 
 return tempString;
}
int GetStringWide(const CString& strSource)   //尚有问题,汉字占两个字节,但是标点还是一个字节。
{
 int sum=0;
 for (int i=0; i<strSource.GetLength();i++)
 {
  if(strSource[i] >= 0x3000) //0x9fa5 && 0x4e00<=strSource[i]
   sum+=2;
  else
   sum++;
 }
 return sum;
 
}

void StringToChar(const CString s, char *Destination)
{
 int k=0;
 wchar_t *inf = NULL;
 inf = new wchar_t[MAX_STRINGLENGTH];
 wcscpy(inf,s);
 k=WideCharToMultiByte(CP_ACP,0,inf,-1,NULL,0,NULL,0);  
 k=WideCharToMultiByte(CP_ACP,0,inf,k,Destination,k,NULL,0);
 if(inf != NULL)
 {
  delete[]inf;
  inf = NULL;
 }
}
void IntToChar(char *p,const int TranInt,int count)
{
 CString str;
 str.Format(_T("%d"),TranInt);
 StringToChar(str,p);
}
void LongToChar(char *p,const long TranInt,int count)
{
 long iTemp;
 int i,j;
 iTemp = TranInt;
 p[0] = '0';
 for(i=0;i<count;i++)
 {
  if(iTemp)
  {
   for(j=i;j>0;j--)
   {
    p[j]=p[j-1];    
   }
   p[0] = iTemp%10+'0';
  }
  else
   break;
  iTemp = iTemp/10;
 }
}
CString HexToString(BYTE HexData)   
{
 CString hexStr;
 hexStr.Format(_T("%02x"),HexData);
 return hexStr;
}

CString CharToString(const char p[],int count)    //
{
 CString result;
 wchar_t *temp;       
 DWORD dwNum = MultiByteToWideChar(CP_ACP,0,p,count,NULL,0); 
 temp = new wchar_t[dwNum];
    dwNum = MultiByteToWideChar(CP_ACP,0,p,count,temp,dwNum);
 result.Format(_T("%s"),temp);
 if(!temp)
  delete []temp;
 temp =NULL;
 result= result.Left(dwNum);
    return result;
}

BOOL CharCompare(const char *pCode1,const char *pCode2)    //CHAR 数据比较,提高查找效率 只用于比较点检点编号
{
 if(*(pCode1+11) != *(pCode2+11))
 {
  return FALSE;
 }
 for(int i=0; i<11; i++)   //提高效率
 {
  if(*(pCode1+10-i) != *(pCode2+10-i))
   return FALSE;
 }
 return TRUE; 
}
CString TimeToString()
//函数功能:将系统时间转化为字符串
{
 CString timeStr;
 CTime   thetime   =   CTime::GetCurrentTime(); 
 timeStr.Format(_T("%04d%02d%02d"),thetime.GetYear(),thetime.GetMonth(),thetime.GetDay());
 return timeStr;
 
}

int CharToInt(const char *p,int count)    //CHAR 型转 int
{

 int iTemp,Length;
 const char *uTemp = p;
 Length =0;
 if(p[0] == '-')
 {
  for(int i=1;i<count;i++)
  {
   if((*(uTemp+i))>='0' && (*(uTemp+i))<='9')
    Length++;
   else
    break;  
  }
  iTemp = 0; 
  for (i=1;i<Length+1;i++)
   iTemp = iTemp *10 +(*(uTemp+i)-'0');
  iTemp = -iTemp;
 }
 else
 {
  for(int i=0;i<count;i++)
  {
   if((*(uTemp+i))>='0' && (*(uTemp+i))<='9')
    Length++;
   else
    break;  
  }
  iTemp = 0; 
  for (i=0;i<Length;i++)
   iTemp = iTemp *10 +(*(uTemp+i)-'0');
 }
 
    return iTemp;
}
long CharToLong(const char *p,int count)    //CHAR 型转 int
{
 long iTemp;
 int Length;
 const char *uTemp = p;
 Length =0;
 for(int i=0;i<count;i++)
 {
  if((*(uTemp+i))>='0' && (*(uTemp+i))<='9')
   Length++;
  else
   break;  
 }
 iTemp = 0; 
 for (i=0;i<Length;i++)
  iTemp = iTemp *10 +(*(uTemp+i)-'0');
    return iTemp;
}
/***********************
正负转换均可以 。
**************************/
float CharToFloat(const char *p, int count)
{
 float iTemp=0;
 const char *uTemp=p;
 int Length=0;
 int Length1=0;
 bool pointFlag=true;
 
 for(int i=0;i<count;i++)
 {  
  if(*(uTemp+i)=='.')
  {
   Length=i;
   pointFlag=false;
   break;
  } 
 }
 if(*uTemp=='-')
 {
  if(pointFlag)
  {
   for(int i=1;i<count;i++)
   {
    if((*(uTemp+i))>='0' && (*(uTemp+i))<='9')
     Length1++;
    else
     break; 
   }
   for (i=1;i<Length1;i++)
    iTemp = iTemp *10 +(*(uTemp+i)-'0');
   return (-1)*iTemp;
  }
  else
  {
   for (i=1;i<Length;i++)
    iTemp = iTemp *10 +(*(uTemp+i)-'0');
   for(i=Length+1;i<count;i++)
   {
    if((*(uTemp+i))>='0' && (*(uTemp+i))<='9')
     Length1++;
    else
     break;
   }
   for(i=Length+1;i<Length+1+Length1;i++)
   {
    iTemp = (float)(iTemp +(*(uTemp+i)-'0')/pow(10,i-Length));
   }
   return (-1)*iTemp;
  }

 }
 else
 {
  if(pointFlag)
  {
   for(int i=0;i<count;i++)
   {
    if((*(uTemp+i))>='0' && (*(uTemp+i))<='9')
     Length1++;
    else
     break; 
   }
   for (i=0;i<Length1;i++)
    iTemp = iTemp *10 +(*(uTemp+i)-'0');
   return iTemp;
  }
  else
  {
   for (i=0;i<Length;i++)
    iTemp = iTemp *10 +(*(uTemp+i)-'0');
   for(i=Length+1;i<count;i++)
   {
    if((*(uTemp+i))>='0' && (*(uTemp+i))<='9')
     Length1++;
    else
     break;
   }
   for(i=Length+1;i<Length+1+Length1;i++)
   {
    iTemp = (float)(iTemp +(*(uTemp+i)-'0')/pow(10,i-Length));
   }
   return iTemp;
  }
 }

}
void FloatToChar(char *p, float fValue) //new
{
 CString str;
 str.Format(_T("%.4f"),fValue);
 StringToChar(str,p); 
}
char AsciiToHex(char uAscii)
{
        char uTemp;
  if(uAscii>='0' && uAscii<= '9')
        {
                uTemp = uAscii-'0';
        }
        else
   uTemp =0;
        return uTemp;
}
char BinToAscii(char cTemp)
{
        char ucTemp;
        ucTemp = cTemp;  
        ucTemp += '0';   
        return ucTemp;
}
void TimeToChar(char *DateTime)
//函数功能:将系统时间转化为字符串
{
 CTime   thetime   =   CTime::GetCurrentTime(); 
 
 int y=thetime.GetYear();
 int d=thetime.GetDay();
 int m=thetime.GetMonth();
 char p[5];
 char q[3];
 char z[3];
 _itoa(y,p,10);
 _itoa(d,q,10);
 _itoa(m,z,10);
 for(int i=0;p[i]!='\0';i++)
  DateTime[i]=p[i];
 if(m<10)
 {
  DateTime[4]='0';
  DateTime[5]=z[0];
 }
 else
 {
  DateTime[4]=z[0];
  DateTime[5]=z[1];
 }
 if(d<10)
 {
  DateTime[6]='0';
  DateTime[7]=q[0];
 }
 else
 {
  DateTime[6]=q[0];
  DateTime[7]=q[1];
 }
 y = thetime.GetHour();
 d = thetime.GetMinute();
 m = thetime.GetSecond();
 _itoa(y,p,10);
 _itoa(d,q,10);
 _itoa(m,z,10);
 if(y<10)
 {
  DateTime[8]='0';
  DateTime[9]=p[0];
 }
 else
 {
  DateTime[8]=p[0];
  DateTime[9]=p[1];
 }
 if(d<10)
 {
  DateTime[10]='0';
  DateTime[11]=q[0];
 }
 else
 {
  DateTime[10]=q[0];
  DateTime[11]=q[1];
 }
 if(m<10)
 {
  DateTime[12]='0';
  DateTime[13]=z[0];
 }
 else
 {
  DateTime[12]=z[0];
  DateTime[13]=z[1];
 }
}

CString StrTrim(const CString strSource, int maxLength)
{
 CString strTemp;
 int length;

 strTemp = strSource;
 length = strSource.GetLength();

 if(length <= maxLength)
  return strTemp;

 strTemp.Delete(0, length-maxLength);
    strTemp = _T("...") + strTemp;
 
 return strTemp;
}

BOOL IsLeapyear(INT16 intYear)
{
 return (intYear%4==0&&intYear%100!=0)||(intYear%400==0);
}

int StringToInt(const CString strSource)
{
 int resultValue=0;
 if((int)strSource[0]==45)
 {
  for (int i=1; i<strSource.GetLength(); i++)
  {
   resultValue=resultValue*10+int(strSource[i]-48);
  }
  return (-1)*resultValue;
 }
 else
 {
  for (int i=0; i<strSource.GetLength(); i++)
  {
   resultValue=resultValue*10+int(strSource[i]-48);
  }
  return resultValue;
 }
 
}
float StringToFloat(const CString strSource,int count)
{
 float resultValue =0;
 int pointLength=0;

 bool pointFlag=true;
 
 for(int i=0;i<count;i++)
 {  
  if((int)strSource[i]==46)  //'.'
  {
   pointLength = i;
   pointFlag = false;
   break;
  } 
 }
 if((int)strSource[0]==45)    //'-'
 {
  if(pointFlag)
  {
   for(int i=1;i<count;i++)
   {
    resultValue=resultValue*10+int(strSource[i]-48); 
   }
   return (-1)*resultValue;
  }
  else
  {
   for (i=1; i<pointLength; i++)
    resultValue=resultValue*10+int(strSource[i]-48);
  
   for(i=pointLength+1; i<count; i++)
   {
    resultValue = resultValue +(float)((int(strSource[i]-48))/pow(10,i-pointLength));
   }
   return (-1)*resultValue;
  }  
 }
 else
 {
  if(pointFlag)
  {
   for(int i=0;i<count;i++)
   {
    resultValue=resultValue*10+int(strSource[i]-48); 
   }
   return resultValue;
  }
  else
  {
   for (i=0; i<pointLength; i++)
    resultValue=resultValue*10+int(strSource[i]-48);
   
   for(i=pointLength+1; i<count; i++)
   {
    resultValue = resultValue +(float)(int(strSource[i]-48)/pow(10,i-pointLength));
   }
   return resultValue;
  }
 }
}
CString CalFrontday(CString strSource)
{
 INT16 monthDaynumber[12]={31,28,31,30,31,30,31,31,30,31,30,31};
 INT16 year=StringToInt(strSource.Mid(0,4));
 INT16 month=StringToInt(strSource.Mid(4,2));
 INT16 day=StringToInt(strSource.Mid(6,2));
 if (IsLeapyear(year))
 {
  monthDaynumber[1]=29;
 }
 INT16 newYear=0;
 INT16 newMonth=0;
 INT16 newDay=day-1;
 if (newDay>0)
 {
  newYear=year;
  newMonth=month;
 }
 else if (newDay==0)
 {
  newMonth=month-1;
  if (newMonth>0)
  {
   newDay=monthDaynumber[newMonth-1];
   newYear=year;
  }
  else if(newMonth==0)
  {
   newMonth=12;
   newDay=monthDaynumber[newMonth-1];
   newYear=year-1;
  }
 }
 CString strNewday;
 CString strNewyear;
 CString strNewmonth;
 strNewday.Format(_T("%02d"), newDay);
 strNewmonth.Format(_T("%02d"), newMonth);
 strNewyear.Format(_T("%04d"), newYear);
    return strNewyear+strNewmonth+strNewday;
}

CString CalBackday(CString strSource)
{
 INT16 monthDaynumber[12]={31,28,31,30,31,30,31,31,30,31,30,31};
 INT16 year=StringToInt(strSource.Mid(0,4));
 INT16 month=StringToInt(strSource.Mid(4,2));
 INT16 day=StringToInt(strSource.Mid(6,2));
 if (IsLeapyear(year))
 {
  monthDaynumber[1]=29;
 }
 INT16 newYear=0;
 INT16 newMonth=0;
 INT16 newDay=day+1;
 if (newDay<=monthDaynumber[month-1])
 {
  newYear=year;
  newMonth=month;
 }
 else
 {
  newMonth=month+1;
  if (newMonth<=12)
  {
   newDay=1;
   newYear=year;
  }
  else
  {
   newMonth=1;
   newDay=1;
   newYear=year+1;
  }
 }
 CString strNewday;
 CString strNewyear;
 CString strNewmonth;
 strNewday.Format(_T("%02d"), newDay);
 strNewmonth.Format(_T("%02d"), newMonth);
 strNewyear.Format(_T("%04d"), newYear);
    return strNewyear+strNewmonth+strNewday;

}
CString GetnDay(const CString& currentDay, int n )
{
 CString beforeNDay("");
 CString afterNDay("");
 CString afterDay(currentDay);
 CString beforeDay(currentDay);
 for(int i = 0; i < n; i++)
 {
  CString afterFirstDay = CalBackday(afterDay);
  afterDay = afterFirstDay;
  afterNDay += afterFirstDay;
  
  CString beforeFirstDay = CalFrontday(beforeDay);
  beforeDay = beforeFirstDay;
  CString revbeforeDay = beforeDay;
  revbeforeDay.MakeReverse();
  beforeNDay += revbeforeDay;
 }
 beforeNDay.MakeReverse();
 //return  beforeNDay + currentDay + afterNDay;
 return  beforeNDay.Left(8);
}

CString GetCurtainDate( CString strSource )
{
 CString firstFrontday(CalFrontday(strSource));
 CString secondFrontday(CalFrontday(firstFrontday));
 CString threeFrontday(CalFrontday(secondFrontday));
 CString firstBackday(CalBackday(strSource));
 CString secondBackday(CalBackday(firstBackday));
 CString threeBackday(CalBackday(secondBackday));
 return threeFrontday+secondFrontday+firstFrontday+
  strSource+firstBackday+secondBackday+threeBackday;

}

HBRUSH GetBkBrush( HWND hWnd, UINT nID, HBITMAP hBmBk )
{
 HWND hWndCtrl;
 hWndCtrl = ::GetDlgItem( hWnd, nID ); 
 HBRUSH hBrushCtrl = NULL;
 if( NULL != hWndCtrl )
 {
  RECT rcCtrl;
  ::GetWindowRect( hWndCtrl, &rcCtrl );
  ::ScreenToClient(hWnd, (LPPOINT)&rcCtrl);
  ::ScreenToClient(hWnd, ((LPPOINT)&rcCtrl)+1);
  HDC hDC = ::GetDC(hWnd);
  HDC hMemDCBk = CreateCompatibleDC( hDC );
  HDC hMemDCCtrl = CreateCompatibleDC( hDC );
  HBITMAP hBmCtrl = CreateCompatibleBitmap( hDC, _W(rcCtrl), _H(rcCtrl) );
  HBITMAP hBmOldBk;
  HBITMAP hBmOldCtrl;
  hBmOldBk = (HBITMAP) ::SelectObject( hMemDCBk, hBmBk );
  hBmOldCtrl = (HBITMAP) ::SelectObject( hMemDCCtrl, hBmCtrl );
  ::BitBlt( hMemDCCtrl, 0, 0, _W(rcCtrl), _H(rcCtrl), hMemDCBk, _X(rcCtrl), _Y(rcCtrl), SRCCOPY );
  ::SelectObject(hMemDCCtrl, hBmOldCtrl );
  ::SelectObject(hMemDCBk, hBmOldBk );  
  hBrushCtrl = ::CreatePatternBrush( hBmCtrl );
  DeleteObject( hBmCtrl );
  ::DeleteDC( hMemDCBk );
  ::DeleteDC( hMemDCCtrl );
  ::ReleaseDC( hWnd, hDC );
 }
 return hBrushCtrl;
}

posted on 2009-03-20 09:50  arvin2012  阅读(705)  评论(0编辑  收藏  举报