首页 > 代码库 > Win32窗口以及C++日志
Win32窗口以及C++日志
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 900 class 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<mywin->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<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; HDC hDC=GetDC(m_hwnd); for(i=0;i<DispLength;i++) { OutOneByte(i,hDC); } ReleaseDC(m_hwnd,hDC); InvalidateRect(m_hwnd,NULL,TRUE); } else //直接写屏 { memcpy(&DispBuff[DispLength],pCon,wLen); // HDC hDC=GetDC(m_hwnd); // SelectObject(hDC, pComDev->hFont); for(i=0;i<wLen;i++) { bColorBuf[DispLength+i]=bRcv; } HDC 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); }
文件日志接口
#pragma once #include <string> 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 <time.h> #include <Windows.h> wstring wlogfile; string logfile; bool WriteLogFile(char * filename, char *pCon, WORD wLen, BOOL bRcv) { SYSTEMTIME currenttime; GetLocalTime(¤ttime); 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<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 std::string &str,std::wstring &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 messagedata,string csourcefile,int line,string 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时%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 *str) { SYSTEMTIME LocalTime; GetLocalTime(&LocalTime); char filename[100]; sprintf(filename,"E:\\icandat\\%d日%d时%d分.txt",LocalTime.wDay,LocalTime.wHour,LocalTime.wMinute); FILE *hComFile=fopen(filename,"a"); fputs(str,hComFile); fclose(hComFile); }
Win32窗口以及C++日志
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。