📄 vtl.cpp
字号:
void DLine(HDC hDC,RECT *rect)
{
DrawEdge(hDC,rect,EDGE_ETCHED,BF_TOPLEFT);
}
RECT *PointToRect(int x1,int y1,int x2,int y2)
{
static RECT PointRect;
PointRect.left = x1; PointRect.top = y1;
PointRect.right = x2; PointRect.bottom = y2;
return (&PointRect);
}
void DrawBorlandFrame(HDC hDC,RECT *rt)
{
FillRect(hDC,rt,GetStockObject(LTGRAY_BRUSH));
DrawEdge(hDC,rt,EDGE_SUNKEN,BF_RECT);
}
void DrawFrame(HDC hDC,RECT *rect,HBRUSH hBrush)
{
FillRect(hDC,rect,hBrush);
DrawEdge(hDC,rect,EDGE_RAISED,BF_RECT);
}
char *cstrncpy(char *dest,char *sour,int n)
{
int i,flag;
for(i=0,flag=0;i<n;i++){
dest[i] = sour[i];
if(dest[i] == 0)break ;
else if(dest[i] &0x80)flag = !flag;
}
if(flag) dest[i-1] = 0;
else dest[i] = 0;
return (dest);
}
/* 删除左右空白的串拷贝 */
char *bstrncpy(char *dest,char *sour,int n)
{
int i,j,flag;
for(i=j=0,flag=0;j<n;i++){
if(j==0){// 删除左空白
if(sour[i] == 0x20 || sour[i] == '\t')
continue;
}
else if(sour[i] == '\r' || sour[i] == '\n')break;
if(sour[i] == 0)break ;
else if(sour[i] &0x80)flag = !flag;
dest[j++] = sour[i];
}
if(flag) {dest[j-1] = 0; j=j-1;}
else dest[j] = 0;
for(j=j-1;j>=0;j--){ // 删除右空白
if(dest[j] == 0x20 || dest[j] == '\t')
dest[j]=0;
else break;
}
return (dest);
}
//检查文件存在
int CheckFileExist(char *name)
{
int rc;
rc = GetFileAttributes(name);
if(rc==MAXDWORD || (rc&FILE_ATTRIBUTE_DIRECTORY)!=0)
rc=FALSE;
else rc=TRUE;
return(rc);
}
//锁住整个文件
int LockWholeFile(HANDLE hand,int bWait)
{
int rc ;
DWORD nSize;
nSize=GetFileSize(hand,NULL);
rc = LockPartFile(hand,bWait,0l,nSize);
return(rc);
}
//解锁整个文件
int UnlockWholeFile( HANDLE hand)
{
int rc ;
DWORD nSize;
nSize=GetFileSize(hand,NULL);
rc = UnlockPartFile(hand,0l,nSize);
return(rc);
}
//锁住文件的某部分
int LockPartFile(HANDLE hand,int bWait,
DWORD nFileOffset,DWORD nLockBytes)
{
int rc;
DWORD nTime;
MSG msg ;
while(1) {
rc = LockFile(hand,nFileOffset,0l,nLockBytes,0l);
if (rc)break;
else if(!bWait)break;
nTime=GetCurrentTime()+1000;/* wait a second & try lock again */
while (GetCurrentTime() <= nTime){
/* Give some other application a chance to run. */
if ( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) ){
TranslateMessage((LPMSG)&msg) ;
DispatchMessage((LPMSG)&msg) ;
if( msg.message == WM_DESTROY)
return(FALSE) ;
}
}
}
return(rc);
}
//解锁文件的某部分
int UnlockPartFile(HANDLE hand,
DWORD nFileOffset,DWORD nLockBytes)
{
int rc;
rc = UnlockFile(hand,nFileOffset,0l,nLockBytes,0l);
return(rc);
}
//获取字符宽度
int GetWidthPerChar(HDC hDC)
{
TEXTMETRIC tm;
int nVersion;
int nWidthPerChar;
GetTextMetrics(hDC,&tm);
nVersion =LOWORD(GetVersion());
if(nVersion>4) //Windows98时,字间稍宽
nWidthPerChar = tm.tmAveCharWidth;
else nWidthPerChar = tm.tmAveCharWidth+1;
return(nWidthPerChar);
}
//获取字符高度
int GetHeightPerChar(HDC hDC)
{
TEXTMETRIC tm;
int nHeightPerChar;
GetTextMetrics(hDC,&tm);
nHeightPerChar = tm.tmHeight+tm.tmInternalLeading;
return(nHeightPerChar);
}
//创造打印字体
HFONT CreatePrintFont(int nWidth,int nHeight,char *pszName)
{
LOGFONT lf;
HFONT hFont;
if(nWidth<=0)nWidth=13;
if(nHeight<=0)nHeight=24;
memset(&lf,0,sizeof(LOGFONT));
lf.lfQuality = PROOF_QUALITY;
lf.lfPitchAndFamily = DEFAULT_PITCH;
lf.lfClipPrecision = CLIP_EMBEDDED;
lf.lfOutPrecision = OUT_TT_ONLY_PRECIS;
if(pszName==NULL)
strcpy(lf.lfFaceName, "宋体");
else strcpy(lf.lfFaceName, pszName);
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfWidth =nWidth; lf.lfHeight = nHeight;
hFont = CreateFontIndirect(&lf);
return(hFont);
}
//显示内存分配错误
void ShowNoMemory(HWND hWnd,char *pszTitle)
{
static char pszText[]="没有足够内存可使用!";
MessageBeep(MB_ICONSTOP);/*发声*/
MessageBox(hWnd,pszText,pszTitle,
MB_OK|MB_ICONHAND|MB_APPLMODAL);
}
//判断字符串是否为字母符号组成
int IsStrAlphaNumeric(char *szBuf)
{
int i,ch,rc;
rc=TRUE;
for(i=0;rc!=FALSE;i++){
ch = szBuf[i];
if(ch==0)break;
rc=IsCharAlphaNumeric(ch);
}
return(rc);
}
//判断字符串是否为字母符号组成
int IsStrNumeric(char *szBuf)
{
int i,ch,rc;
rc=TRUE;
for(i=0;rc!=FALSE;i++){
ch = szBuf[i];
if(ch==0)break;
if(ch<'0' || ch>'9')rc=FALSE;
}
return(rc);
}
//延时每单位1/1000秒 ******************//
void DelayMilliseconds(int ts)
{
DWORD nTime;
nTime = GetTickCount()+ts;
while(GetTickCount()<nTime)
ProcessMessage();
}
//延时每单位1/100秒 ******************//
void Msmydelay(int ts)
{ DelayMilliseconds(ts*10); }
// 延时每单位1/10秒 ******************//
void Mydelay(int ts)
{ DelayMilliseconds(ts*100); }
//********************SYRIS Controller CRC verify Initialization Function ******************************************************************************************************************************************************************************************//
#define CRCCCITT 0x1021 /* CCITT polynomial */
#define CRCCCITT_REV 0x8408 /* Reverse CCITT polynomial */
#define CRC16 0x8005 /* CRC16 polynomial */
#define CRC16_REV 0xa001 /* Reverse CRC16 polynomial 0x8005 */
#define crcupdate(d,a,t) (((a)<<8)^(t)[((a)>>8)^(d)])
#define crcupdate_rev(d,a,t) (((a)>>8)^(t)[((a)^(d))&0x0ff])
static WORD CRCHWare_Rev(WORD Data,WORD GenPoly,WORD Accum);
static WORD CRCHWare(WORD Data,WORD GenPoly,WORD Accum);
static WORD CRCTable[256];
static BYTE nCRCMode=0;
//初始化CRC表:nCCITT=>CRC模型(0=>CRC-16,!0=>CRC-CCITT)
//nRec=>CRC计算方法(0=>正序,!0=>反序)
WORD *InitCRC(int nCCITT,int nRec)
{
int i;
WORD nPoly;
i=0x01; //已经计算CRC表
if(nCCITT)i |=0x02; //CRC-CCITT模型
if(nRec)i|=0x04; //反序算法
if((nCRCMode&i)!=i){ //重新计算CRC表
nCRCMode=(BYTE)i;
if(nRec){//反序算法
if(nCCITT)nPoly=CRCCCITT_REV;//CRC-CCITT模型
else nPoly=CRC16_REV;//CRC-16模型
for(i=0;i<256;i++)
CRCTable[i] = CRCHWare_Rev(i,nPoly,0);
}
else{//正序算法
if(nCCITT)nPoly=CRCCCITT;//CRC-CCITT模型
else nPoly=CRC16;//CRC-16模型
for(i=0;i<256;i++)
CRCTable[i] = CRCHWare(i,nPoly,0);
}
}
return(CRCTable);
}
//正序CRC表算法
WORD CRCHWare(WORD Data,WORD GenPoly,WORD Accum)
{
int i;
Data <<=8; /* data to high byte */
for(i=8;i>0;i--){
if((Data^Accum)&0x8000) /* If MSB of (Data XOR Accum) is TRUE */
Accum = (Accum<<1)^GenPoly;/* Shift and subtract poly */
else Accum <<=1; /* Otherwise,transparent shift */
Data <<=1; /* Move up next bit for XOR */
}
return(Accum);
}
//反序CRC表算法
WORD CRCHWare_Rev(WORD Data,WORD GenPoly,WORD Accum)
{
int i;
for(i=8;i>0;i--){
if((Data^Accum)&0x01) /* If MSB of (Data XOR Accum) is TRUE */
Accum = (Accum>>1)^GenPoly;/* Shift and subtract poly */
else Accum >>=1; /* Otherwise,transparent shift */
Data >>=1; /* Move up next bit for XOR */
}
return(Accum);
}
WORD GetCRCWithBuf(BYTE *pszBuf,WORD nSize)
{
WORD i,Accum;
int nCCITT,nRec;
nCCITT=0;nRec=1; //CRC-16模型,反序算法
InitCRC(nCCITT,nRec); //CRC-16模型,反序算法
if(nRec){//反序算法
for(i=Accum=0;i<nSize;i++,pszBuf++)
Accum = crcupdate_rev(*pszBuf,Accum,CRCTable);
}
else{//正序算法
for(i=Accum=0;i<nSize;i++,pszBuf++)
Accum = crcupdate(*pszBuf,Accum,CRCTable);
}
if(nRec==0)
Accum = (Accum>>8)+(Accum<<8);
return(Accum);
}
#ifdef __cplusplus
} // std
#endif /* __cplusplus */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -