Win32 windows and C + + logs

Source: Internet
Author: User
Tags sprintf

Winshow

 showwin.h: interface for the cshowwin class.///////////////////////////////// #include  <windows.h> #include  <dos.h> #include  <string.h> #include  <stdio.h> #include  <stdlib.h> #define  DISPLEN  900class cshowwin  {public: void setwinname (Lpstr winname); void  Addcontodisp (CHAR&NBSP;*PCON,WORD&NBSP;WLEN,BOOL&NBSP;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&NBSP;HDC);  static lresult callback watchproc ( HWND hWnd, UINT  message,wparam wparam, lparam lparam );  static uint watchthread (LPVOID &NBSP;*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&NBSP;WBYTEPOS,HDC&NBSP;HDC) { WORD wRow, Wcol; wrow=wbytepos/30; wcol=wbytepos%30; if (bcolorbuf[wbytepos]==true)  //Bytes Sent  {   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]); &NBSp 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) &NBSP;&NBSP;&NBsp {    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,&AMP;PTSTR);   for (i=0;i<mywin->displength;i++)   {     mywin->outonebyte (I,HDC);   }  endpaint (HWND,&AMP;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&NBSP;*PCON,&NBSP;WORD&NBSP;WLEN,&NBSP;BOOL&NBSP;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)   {  &nbsP;memmove (Dispbuff,dispbuff+wdelete,displength - wdelete);     //  Memmove (Pcomdev->szcolorbuf,pcomdev->szcolorbuf+wdelete,pcomdev->wdisppos - wdelete);    for (i=wdelete;i<displength;i++)    {    bcolorbuf[i-wdelete]= bcolorbuf[i];   }   memcpy (Dispbuff + displength - wdelete, Pcon,wlen);    for (i=0;i<wlen;i++)    {    bcolorbuf[ Displength-wdelete+i]=brcv;   }  }  else if (wDelete==DispLength)    {   memcpy (Dispbuff,pcon,wlen);      for (i=0;i<wLen;i++)     {    bColorBuf[i]=bRcv;   }  }  else   {   memcpy (Dispbuff,pcon+wdelete-displength,wleft);    for (i=0;i<wLeft;i + +)    {    bColorBuf[i]=bRcv;   }  }  DispLength=wLeft;  &NBSP;HDC&NBSP;HDC=GETDC (m_hwnd);   for (i=0;i<displength;i++)   {     Outonebyte (I,HDC);   }  releasedc (M_HWND,HDC);   invalidaterect (M_hwnd,NULL,TRUE);  } else //Direct Write screen  {  memcpy (&dispbuff[displength],pcon,wlen);//  HDC &NBSP;HDC=GETDC (m_hwnd);  // selectobject (Hdc, pcomdev->hfont);   for (i=0;i<wLen; i++) &NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;BCOLORBUF[DISPLENGTH+I]=BRCV;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;HDC &NBSP;HDC=GETDC (m_hwnd);   for (i=0;i<wlen;i++)   {   outonebyte (DispLength +I,HDC);   }  releasedc (M_HWND,HDC);  displength += wlen; //  InvalidateRect (m_hwnd,null,true);  }}void cshowwin::setwinname (lpstr winname) { windowname  = wInname; ::setwindowtext (m_hwnd, windowname);} 

File Log Interface

#pragma  once#include <string>using namespace std;extern wstring wlogfile; Extern string logfile;bool writelogfile (char * filename, char *pcon,  WORD&NBSP;WLEN,&NBSP;BOOL&NBSP;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 &NBSP;LOGINFO (Message_ Data)   wlog_info (message_data,__file__,__line__) #else                                               #define &NBSP;LOGINFO (Message_data)    log_info (message_data,__file__,__line__)   #endif

CPP:

#include   "log.h" #include  <time.h> #include  <windows.h>wstring wlogfile;string   logfile;bool writelogfile (char * filename, char *pcon, word  WLEN,&NBSP;BOOL&NBSP;BRCV) { systemtime currenttime; getlocaltime (&currenttime);  char  spacechar =  ' \ n '; char spacechar2 =  '   ';  char rx[4] =  {' R ', ' X ', ': ', '}; char tx[4] = {' T ', ' X ', ': ', ' '}; 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,&NBSP;0&NBSP;,100);   for (i=0; i<wlen; i++)   {    sprintf (Timearray, "%02x", (BYTE) pcon[i]);    fwrite (Timearray,sizeof (char), 2,FP);    fwrite (&spacechar2,sizeof (char), 1,FP);   }  fwrite (&spacechar,sizeof (char), 1, FP);   fclose (FP);  return true; } else {  return false;  }}int stringtowstring (CONST&NBSP;STD::STRING&NBSP;&AMP;STR,STD::WSTRING&NBSP;&AMP;WSTR) {      int nLen =  (int) str.length ();      wstr.resize ( Nlen,l '   ');  int nresult = multibytetowidechar (cp_acp,0, (LPCSTR) str.c_str (), NLen, ( LPWSTR) Wstr.c_str (), nlen);  if  (nresult == 0)  {  return -1; } return 0;} Int log_info (Wstring messagedata,string csourcefile,int line,wstring vlogfile) {  if (Vlogfile.empty ())   wlogfile = vlogfile; wlogfile = vlogfile; file  * fp  = _wfopen (Wlogfile.c_str (), L "a");  time_t t = time (0);   wchar_t tmp[64];  wcsftime ( tmp, sizeof (TMP),  L "%Y-%m-%d %X  %A  ", LocalTime (&t)  );   fputws (TMP,FP);  fputws (L" at file  ", FP);  setlocale (Lc_all, ". 936");  wstring csourcefil; stringtowstring (Csourcefile,csourcefil);  fputws (Csourcefil.c_str (), FP),  fputws (L " -", FP);  fputws (Messagedata.c_str (), FP);   fputws (L "\ n", FP);  fclose (FP);  return 0;} Int log_info (STRING&NBSP;MESSAGEDATA,STRING&NBSP;CSOURCEFILE,INT&NBSP;LINE,STRING&NBSP;VLOGFILe) { if (Vlogfile.empty ())   logfile = vlogfile; logfile = vlogfile;  file * fp  = fopen (Logfile.c_str (), "a");  time_t t = time (0) ;   char tmp[64];  strftime ( tmp, sizeof (TMP),  "%Y-%m-%d %X % a  ", LocalTime (&t)  );   fputs (TMP,FP);  fputs (" at file  ", FP);  SetLocale (Lc_all, ". 936");  fputs (Csourcefile.c_str (), FP);  fputs (" -", FP);  fputs ( Messagedata.c_str (), FP);   fputs ("\ n", FP);  fclose (FP);  return 0;}  void wrpackettofile (Byte *ch, word wlen) { char buf[4096]; systemtime  localtime; getlocaltime (&localtime);  char filename[100]; sprintf (filename, "E:\\ Icandat\\%d%d at%d (. txt ", localtime.wday,localtime.whour,localtime.wminute);  file *hcomfile=fopen ( FileName, "a");  if (Hcomfile!=null)  {  foR (word i=0;i<wlen;i++)   {   sprintf (&buf[i*3], "%02X ", * (Ch+i));   }  fputs (Buf,hcomfile);  } fclose (hcomfile);} Void wrstringtofile (CHAR&NBSP;*STR) { systemtime localtime; getlocaltime (&LocalTime);  char filename[100]; sprintf (filename, "%d e:\\icandat\\%d days%d", Localtime.wday, Localtime.whour,localtime.wminute);  file *hcomfile=fopen (filename, "a");  fputs (Str,hComFile);  fclose (hcomfile);}

Win32 windows and C + + logs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.