winshow

// ShowWin.h: interface for the CShowWin class.////#include 
#include 
#include 
#include 
#include 
#define DISPLEN 900class CShowWin  {public: void SetWinName(LPSTR winname); void AddContoDisp(char *pCon,WORD wLen,BOOL bRcv); void create(LPSTR ClassName,LPSTR WinName,HINSTANCE HInst); HWND m_hwnd; int DispLength; BOOL bStop; BYTE DispBuff[1024]; BOOL bColorBuf[1024]; void OutOneByte(WORD wBytePos,HDC hDC); static LRESULT CALLBACK WatchProc( HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam ); static UINT WatchThread(LPVOID *h); HWND CreateWin(); CShowWin(); HINSTANCE hInst; HMENU hMenu,hMenu1; virtual ~CShowWin(); LPSTR classname,windowname; };

 

CPP

// ShowWin.cpp: implementation of the CShowWin class.//////#include "c103yx.h"#include "ShowWin.h"#include "winuser.h"//// Construction/Destruction////extern CShowWin myWin;CShowWin::CShowWin(){ DispLength=0;}CShowWin::~CShowWin(){}void CShowWin::OutOneByte(WORD wBytePos,HDC hDC){ WORD wRow,wCol; wRow=wBytePos/30; wCol=wBytePos%30; if(bColorBuf[wBytePos]==TRUE) //发送的字节 {  SetBkColor(hDC,RGB(192,192,192));  SetTextColor(hDC,RGB(0,0,0)); } else {  SetBkColor(hDC,RGB(0,0,255));  SetTextColor(hDC,RGB(255,255,0));    }  char DispText[8]; sprintf(DispText,"%02X",DispBuff[wBytePos]); TextOut(hDC,wCol*24,wRow*18,DispText,2);}LRESULT CALLBACK CShowWin::WatchProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ WORD  i; HDC   hDC; PAINTSTRUCT PtStr; WORD wNotifyCode; WORD        wID ; CREATESTRUCT *p; //=(CShowWin *)lParam;// LPSTR name=mywin->classname; static CShowWin* mywin; switch(message) { case WM_CREATE://  SetTimer(hWnd,1,1000,NULL);  p=(CREATESTRUCT *)lParam;  mywin=(CShowWin*)p->lpCreateParams;  break;    case WM_TIMER:   break; case WM_COMMAND:   wNotifyCode = HIWORD(wParam);   wID = LOWORD(wParam);/*  if(wNotifyCode==0)  {   if(wID==0x345)   {    AfxMessageBox("hao");   }   if(wID==0x346)   {    AfxMessageBox("bu");   }   if(wID==0x347)   {    AfxMessageBox("ma");   }  }  //hwndCtl = (HWND) lParam;*/  break; case WM_CHAR:  //CTRL_C  if(wParam == 3)  {   if(mywin->bStop)    mywin->bStop = FALSE;   else    mywin->bStop = TRUE;  }    break; case WM_PAINT: // mywin=(CShowWin *)lParam;  hDC=BeginPaint(hWnd,&PtStr);  for(i=0;i
DispLength;i++)  {    mywin->OutOneByte(i,hDC);  }  EndPaint(hWnd,&PtStr);  break; case WM_DESTROY:  DestroyWindow(mywin->m_hwnd);  mywin->m_hwnd=NULL;  UnregisterClass(mywin->classname,mywin->hInst);   PostQuitMessage ( 0 );   case WM_CLOSE:  DestroyWindow(mywin->m_hwnd);  break; default:     return DefWindowProc(hWnd,message,wParam,lParam); } return (0L);}HWND CShowWin::CreateWin(){  static  WNDCLASS   wndclass;  HWND    hWindow;     wndclass.style = CS_HREDRAW|CS_VREDRAW|CS_PARENTDC|CS_CLASSDC;  wndclass.lpfnWndProc = WatchProc;  wndclass.cbClsExtra = 0;  wndclass.cbWndExtra = 0;     wndclass.hInstance  = (HINSTANCE)hInst;  wndclass.hIcon = NULL;     wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);  wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);  wndclass.lpszClassName = (LPSTR)classname;  if(RegisterClass(&wndclass))  {   //hMenu=CreateMenu();  //hMenu1=CreateMenu();  hWindow=CreateWindow((LPSTR)classname,(LPSTR)windowname,WS_OVERLAPPED|WS_MINIMIZEBOX|WS_THICKFRAME,100,100,600,400,NULL,NULL,(HINSTANCE)hInst,this);        EnableMenuItem (GetSystemMenu(hWindow, FALSE), SC_CLOSE,MF_ENABLED);  /*int bv=InsertMenu(hMenu, 0xFFFFFFFF, MF_BYCOMMAND |MF_BYPOSITION |MF_STRING|MF_POPUP ,    (unsigned int)hMenu1,  "wweee");   bv=InsertMenu(hMenu1, 0xFFFFFFFF, MF_BYCOMMAND |MF_BYPOSITION |MF_STRING ,    0x345,  "wweee");   bv=InsertMenu(hMenu1, 0xFFFFFFFF, MF_BYCOMMAND |MF_BYPOSITION |MF_STRING ,    0x346,  "wweee1");  bv=InsertMenu(hMenu, 0xFFFFFFFF, MF_BYCOMMAND |MF_BYPOSITION |MF_STRING,    0x347,  "wweee1");  DrawMenuBar(hWindow);*/  ShowWindow(hWindow,SW_SHOW);       return(hWindow);  }     return NULL;}UINT CShowWin::WatchThread(LPVOID *h){ MSG    msg; CShowWin* mywin=(CShowWin *)h; if(mywin->m_hwnd!=NULL)  return 1; mywin->m_hwnd=mywin->CreateWin(); // mywin->classname while (GetMessage(&msg, NULL, 0, 0))     {    TranslateMessage((LPMSG)&msg);       DispatchMessage((LPMSG)&msg);    } return 0;} void CShowWin::create(LPSTR ClassName, LPSTR WinName, HINSTANCE HInst){ HANDLE hThread;  DWORD        dwThreadID ; classname=ClassName; windowname=WinName; hInst=HInst; hThread=CreateThread(                    NULL,                       0,                      (LPTHREAD_START_ROUTINE)WatchThread,                     this,                      CREATE_SUSPENDED,                     &dwThreadID); if(hThread!=NULL)     ResumeThread(hThread);  //AfxMessageBox("sucess");  if(hThread==NULL)   return ;}void CShowWin::AddContoDisp(char *pCon, WORD wLen, BOOL bRcv){ if(m_hwnd==NULL) return; if(bStop) return; int i; if((wLen + DispLength)>DISPLEN) {  WORD wTotal = wLen + DispLength;  WORD wDelete = 30;  while(1)  {   if((wTotal-wDelete)<=DISPLEN) break;   wDelete += 30;  }  WORD wLeft=wTotal-wDelete;  if(wDelete < DispLength)  {   memmove(DispBuff,DispBuff+wDelete,DispLength - wDelete);     // memmove(pComDev->szColorBuf,pComDev->szColorBuf+wDelete,pComDev->wDispPos - wDelete);   for(i=wDelete;i
hFont);  for(i=0;i

文件日志接口

#pragma once#include 
using namespace std;extern wstring wlogfile;extern string logfile;bool WriteLogFile(char * filename, char *pCon, WORD wLen, BOOL bRcv);int wLog_info(wstring messagedata,string csourcefile,int line,wstring vlogfile = wlogfile);int Log_info(string messagedata,string csourcefile,int line,string vlogfile = logfile);#define wlogFile(message_data,logfilename)({ wLog_info(message_data,__FILE__,__LINE__,logfilename)})#ifdef UNICODE                                 #define loginfo(message_data)  wLog_info(message_data,__FILE__,__LINE__)#else                                           #define loginfo(message_data)   Log_info(message_data,__FILE__,__LINE__) #endif

CPP:

#include "log.h"#include 
#include 
wstring wlogfile;string  logfile;bool WriteLogFile(char * filename, char *pCon, WORD wLen, BOOL bRcv){ SYSTEMTIME currenttime; GetLocalTime(&currenttime); char spacechar = '\n'; char spacechar2 = ' '; char RX[4] = {'R','X',':','\0'}; char TX[4] = {'T','X',':','\0'}; char timearray[100]; memset(timearray, 0 ,100); int i=0; FILE *fp; fp = fopen(filename,"a+"); if(fp!=NULL) {  sprintf(timearray,"%04d-%02d-%02d %02d:%02d:%02d",currenttime.wYear,currenttime.wMonth,currenttime.wDay,currenttime.wHour,currenttime.wMinute,currenttime.wSecond);  fwrite(timearray,sizeof(char),strlen(timearray),fp);  fwrite(&spacechar2,sizeof(char),1,fp);  if(bRcv)  {   fwrite(TX,sizeof(char),strlen(TX),fp);   }  else  {   fwrite(RX,sizeof(char),strlen(RX),fp);  }  memset(timearray, 0 ,100);  for(i=0; i