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

📄 vtl.cpp

📁 企业员工考勤和工资管理系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "vtl.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

char *GetCOMName(int nComm)
 {
  static char szCom[8];
  char *p;

  if(nComm>=0 && nComm<10000){
    wsprintf(szCom,"COM%1d",nComm);
    p=szCom;
   }
  else p=NULL;
  return(p);
 }
//获取标题
char *GetCommTitle(void)
 {
  static char pszTitle[]="串行通讯";
  return(pszTitle);
 }
/*cbInue,cbOutQue 分别表示接收、发送队列大小*/
HANDLE InitComm(int nCom,DWORD BaudRate,DWORD nInQue,DWORD nOutQue)
 {
  SDCB dDcb;

  dDcb.nCom = nCom;
  dDcb.nInQue=nInQue;
  dDcb.nOutQue=nOutQue;
  dDcb.BaudRate = BaudRate;
  dDcb.Parity = NOPARITY;   /*无奇偶校验*/
  dDcb.ByteSize = DATABITS_8;
  dDcb.StopBits = ONESTOPBIT;  /*一个停止位*/

  return(InitCommWithSDCB(&dDcb));
 }

HANDLE InitCommWithSDCB(SDCB *d)
 {
  DCB dDcb;
  char *pszName;
  HANDLE hCom;
  COMMPROP CommProp;
  COMMTIMEOUTS to;
  int n;

  if(d!=NULL && d->nCom!=0){
    pszName = GetCOMName(d->nCom);
    hCom = CreateFile(pszName,GENERIC_READ|GENERIC_WRITE,0,NULL,
        OPEN_EXISTING,0,NULL);
   }
  else hCom = INVALID_HANDLE_VALUE;
  if(hCom!=INVALID_HANDLE_VALUE){
     //设置输入输出缓冲区长度
    CommProp.wPacketLength=sizeof(COMMPROP);
    GetCommProperties(hCom,&CommProp);
    if(d->nInQue==0)d->nInQue = 256;
    if(d->nOutQue==0)d->nOutQue= 256;
    n = CommProp.dwMaxRxQueue; // 最大输入缓冲区长度
    if(n>0 && n<(int)d->nInQue)d->nInQue=n;
    n = CommProp.dwMaxTxQueue; // 最大输出缓冲区长度
    if(n>0 && n<(int)d->nOutQue)d->nOutQue=n;
    SetupComm(hCom,(int)d->nInQue,(int)d->nOutQue);
     //设置通讯口状态
    dDcb.DCBlength = sizeof(DCB);
    GetCommState(hCom,&dDcb);
    dDcb.BaudRate = d->BaudRate;
    dDcb.Parity = d->Parity;
    dDcb.ByteSize = d->ByteSize;
    dDcb.StopBits = d->StopBits;
    if(d->Parity!=NOPARITY)dDcb.fParity = 1;
    else dDcb.fParity = 0;
    dDcb.fAbortOnError = FALSE;
    dDcb.ErrorChar = d->PeChar;
    dDcb.fOutX=FALSE; dDcb.fInX=FALSE;
    dDcb.fOutxCtsFlow=FALSE; dDcb.fOutxDsrFlow=FALSE;
    SetCommState(hCom,&dDcb); /*设置串行口状态*/
     // 设置超时设定
    ZeroMemory(&to,sizeof(COMMTIMEOUTS));
    to.ReadIntervalTimeout=MAXDWORD;
    to.WriteTotalTimeoutMultiplier=5;
    to.WriteTotalTimeoutConstant=50;
    SetCommTimeouts(hCom,&to);
     //清除缓冲区
    PurgeComm(hCom,PURGE_RXCLEAR);
    PurgeComm(hCom,PURGE_TXCLEAR);
   }
  return(hCom);
 }

/* 从指定串行口接收一字符 */
int Rxd(HANDLE hCom)
 {
  char szBuf[10];
  DWORD nByte;
  int ch;

  ReadFile(hCom,szBuf,1,&nByte,NULL);
  if(nByte<=0) ch=-1;
  else ch = szBuf[0]&0x0ff;

  return(ch);
 }

/* 从指定串行口发送一字符*/
void Txd(HANDLE hCom,int ch)
 {
  char szBuf[10];
  DWORD nByte;

  szBuf[0] = (char)ch; szBuf[1]=0;
  WriteFile(hCom,szBuf,1,&nByte,NULL);
 }

/* 从指定串行口接收一字符串 */
void Rxds(HANDLE hCom,char *pszBuf)
 {
  int ch,flag;

  flag =0;
  while(1){
    ch = Rxd(hCom);
    if(ch<0)ProcessMessage();
    else {
      if(flag==0){
        if(ch==ASC_STX)flag=1;
          Txd(hCom,ASC_STX);
         }
      else if(ch == ASC_ETX){
            Txd(hCom,ASC_ETX);
            break;
           }
      else *(pszBuf++) = (char)ch;
     }
   }
  *pszBuf = 0;
 }

void Txds(HANDLE hCom, char *str)
{
  Txd(hCom,ASC_STX);
  for(;*str != 0;str++)
    Txd(hCom,*str);
  Txd(hCom,ASC_ETX);
}

/* 通过串行口发送一个浮点数 */
void Txdf(HANDLE hCom, float dt)
 {
  char *p;
  int i;

  p = (char *)&dt;
  for(i=0;i<4;i++) Txd(hCom,p[i]);
 }

/* 从指定串行口接收一浮点数 */
float Rxdf(HANDLE hCom)
 {
  float dt1;
  char *p;
  int i,ch;

  p = (char *)&dt1;
  for(i=0;i<4;){
    ch = Rxd(hCom);
    if(ch<0)ProcessMessage();
    else  p[i++]=(char)ch;
   }
  return (dt1);
 }
   /* 向RS232-485转换器发送数据 */
int Write485Comm(HANDLE hCom,char *pszBuf,int nSize)
 {
  DWORD nByte;

  EscapeCommFunction(hCom,SETRTS);  /*发数据前置转换器高电平*/
  return(WriteFile(hCom,pszBuf,nSize,&nByte,NULL));
 }

/*函数过程中发送消息*/
void ProcessMessage(void)
 {
  MSG	msg;
  while(PeekMessage(&msg,0,0,0,PM_REMOVE)){
    TranslateMessage(&msg);
    DispatchMessage(&msg);
   }
 }
/*函数过程中处理特定发送消息*/
void ProcessTypeMessage(UINT nMsg)
 {
  MSG	msg;
  while(PeekMessage(&msg,0,nMsg,nMsg,PM_REMOVE)){
    TranslateMessage(&msg);
    DispatchMessage(&msg);
   }
 }
//将毫秒转化为计算机时间中断次数
unsigned int GetDelay(unsigned int nTime) {
  return(nTime/55);  /* 1000/18.21=54.925 */
 }

void *malloc1(unsigned int nSize)
 {
  void *p;
  if((p=malloc(nSize))==NULL)
     MessageBox(NULL,"内存溢出","内存操作",
                 MB_OK|MB_ICONHAND|MB_APPLMODAL);
  return(p);
 }

void DrawBitmap(HDC hDC,int x, int y,HBITMAP hBmp)
 {
  BITMAP bm;
  HDC hMemDC;

  hMemDC = CreateCompatibleDC(hDC);
  GetObject(hBmp,sizeof(BITMAP),(LPSTR)&bm);
  hBmp=SelectObject(hMemDC,hBmp);
  SetMapMode(hMemDC,GetMapMode(hDC));
  BitBlt(hDC,x,y,bm.bmWidth,bm.bmHeight,hMemDC,0,0,SRCCOPY);
  SelectObject(hMemDC,hBmp);
  DeleteDC(hMemDC);
 }

int wchz(HDC hDC,const char *p,int x,int y,int TextColor,int BkColor)
 {
  int nLen;
  int nMode;
  HGDIOBJ hFont;
  COLORREF nText,nBack;

  nLen=strlen(p);
  if(nLen == 0)return(0);

  hFont = SelectObject(hDC,GetStockObject(SYSTEM_FIXED_FONT));
  nText = SetTextColor(hDC,PALETTEINDEX(TextColor));
  if(BkColor!=-1){
    nBack =SetBkColor(hDC,PALETTEINDEX(BkColor));
    nMode = SetBkMode(hDC,OPAQUE);
   }
  else nMode = SetBkMode(hDC,TRANSPARENT);

  TextOut(hDC,x,y,p,nLen);

  SelectObject(hDC,hFont);
  SetTextColor(hDC,nText);
  if(BkColor!=-1)
    SetBkColor(hDC,nBack);
  SetBkMode(hDC,nMode);

  return(nLen);
 }

int DrawTextInRect(HDC hDC,const char *p,const RECT *rt,
  int TextColor,int BkColor,int LTColor,int RBColor)
 {
  int nLen;
  int nMode;
  HBRUSH hBrush;
  HPEN hPen;
  HGDIOBJ hFont;
  COLORREF nText;

  if(rt == NULL)return(0);

  hBrush = SelectObject(hDC,CreateSolidBrush(PALETTEINDEX(BkColor)));
  hPen = SelectObject(hDC,GetStockObject(NULL_PEN));
  Rectangle(hDC,rt->left,rt->top,rt->right,rt->bottom);

  SelectObject(hDC, CreatePen(PS_SOLID,1,PALETTEINDEX(LTColor)));
  Line(hDC,rt->left,rt->top,rt->right,rt->top);
  Line(hDC,rt->left,rt->top,rt->left,rt->bottom);

  DeleteObject(SelectObject(hDC,
	  CreatePen(PS_SOLID,1,PALETTEINDEX(RBColor))));
  Line(hDC,rt->left,rt->bottom,rt->right,rt->bottom);
  Line(hDC,rt->right,rt->top,rt->right,rt->bottom);

  DeleteObject(SelectObject(hDC,hBrush));
  DeleteObject(SelectObject(hDC,hPen));

  nLen=strlen(p);
  if(nLen>0){
    hFont = SelectObject(hDC,GetStockObject(SYSTEM_FIXED_FONT));
    nMode = SetBkMode(hDC,TRANSPARENT);
    nText = SetTextColor(hDC,PALETTEINDEX(TextColor));
    DrawText(hDC,(LPCTSTR)p,nLen,(LPRECT)rt,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
    SetBkMode(hDC,nMode);
    SetTextColor(hDC,nText);
    SelectObject(hDC,hFont);
   }
  return(nLen);
 }

void Line(HDC hDC,int x1,int y1,int x2,int y2)
 {
  MoveToEx(hDC,x1,y1,NULL);
  LineTo(hDC,x2,y2);
 }

void DrawRect(HDC hDC,int x1,int y1,int x2,int y2)
 {
  POINT point[5];
   point[0].x = x1; point[0].y = y1;
   point[1].x = x2; point[1].y = y1;
   point[2].x = x2; point[2].y = y2;
   point[3].x = x1; point[3].y = y2;
   point[4].x = x1; point[4].y = y1;
  Polyline(hDC,(POINT FAR *)point,5);
 }
#pragma argsused

⌨️ 快捷键说明

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