📄 w95trace1.cpp
字号:
/*
Implementation of Win95 tracing facility to mimic that of NT
*/
#include "stdafx.h"
#include "windows.h"
#include <stdio.h>
#include <stdarg.h>
#include <process.h>
//#include "w95trace.h"
#include <afxpriv.h> // for MFC WM_ messages
#ifdef _DEBUG
int CTraceFn::nIndent=-1; // current indent level
#define _countof(array) (sizeof(array)/sizeof(array[0]))
#include <time.h>
void ClearDebugWindow(char *str/*=0*/, BOOL bClean/*=TRUE*/)
{
char timestr[80];
time_t ltime;
time(<ime);
wsprintf(timestr, "********** %s **********", ctime(<ime));
if ((bClean == FALSE) && (str == 0)) return;
else
{
HWND hDbgWnd, hEdit;
hDbgWnd = (HWND) FindWindow("DBWin32", "Debug Messages (WIN32)");
if (hDbgWnd != NULL)
{
char *fullStr = 0;
if (str == 0)
{
fullStr = new char[60];
strcpy(fullStr, "\r\n");
strcat(fullStr, timestr);
strcat(fullStr, "\r\n\r\n");
}
else
{
fullStr = new char[strlen(str)+1+120];
strcpy(fullStr, "\r\n");
strcat(fullStr, timestr);
strcat(fullStr, "\r\n");
strcat(fullStr, str);
strcat(fullStr, "\r\n\r\n");
}
hEdit = (HWND) FindWindowEx(hDbgWnd, NULL, "Edit", "");
int len;
if ((bClean == FALSE) && (str != 0))
{
len = SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
SendMessage(hEdit, EM_SETSEL, len+1, len+1);
SendMessage(hEdit, EM_REPLACESEL, FALSE, (long) fullStr);
}
else // (bClean == TRUE)
SendMessage(hEdit, WM_SETTEXT, 0, (long) fullStr);
len = SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
SendMessage(hEdit, EM_SETSEL, len+1, len+1);
delete[] fullStr;
}
}
}
void OutputDebugStringW95( LPCTSTR lpOutputString, ...)
{
HANDLE heventDBWIN; /* DBWIN32 synchronization object */
HANDLE heventData; /* data passing synch object */
HANDLE hSharedFile; /* memory mapped file shared data */
LPSTR lpszSharedMem;
int iWC;
char achBuffer1[256];
char achBuffer[256];
/* create the output buffer */
va_list args;
va_start(args, lpOutputString);
iWC = vsprintf(achBuffer1, lpOutputString, args);
if (iWC >= 256)
vsprintf(achBuffer1, "%s", "Insufficient memory allocation in OutputDebugStrW95()\n");
va_end(args);
/* insert spaces for TRACEFN */
static BOOL bStartNewLine = TRUE;
char* nextline;
iWC = 0;
for (char* start=achBuffer1; *start; start=nextline+1)
{
if (bStartNewLine)
{
if ((afxTraceFlags & traceMultiApp) && (AfxGetApp() != NULL))
iWC+=sprintf(&achBuffer[iWC], "%s: ", AfxGetApp()->m_pszExeName);
for (int i=0; i<CTraceFn::nIndent; i++)
achBuffer[iWC++] = ' ';
if (CTraceFn::nIndent>0)
achBuffer[iWC] = '\0';
bStartNewLine = FALSE;
}
nextline = strchr(start, '\n');
if (nextline) {
*nextline = 0; // terminate string at newline
bStartNewLine = TRUE;
}
iWC+=sprintf(&achBuffer[iWC], start);
if (!nextline)
break;
iWC+=sprintf(&achBuffer[iWC], "\n");
}
/*
Do a regular OutputDebugString so that the output is
still seen in the debugger window if it exists.
This ifdef is necessary to avoid infinite recursion
from the inclusion of W95TRACE.H
*/
#ifdef _UNICODE
::OutputDebugStringW(achBuffer);
#else
::OutputDebugStringA(achBuffer);
#endif
/* bail if it's not Win95 */
{
OSVERSIONINFO VerInfo;
VerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&VerInfo);
if ( VerInfo.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS )
return;
}
/* make sure DBWIN is open and waiting */
heventDBWIN = OpenEvent(EVENT_MODIFY_STATE, FALSE, "DBWIN_BUFFER_READY");
if ( !heventDBWIN )
{
//MessageBox(NULL, "DBWIN_BUFFER_READY nonexistent", NULL, MB_OK);
return;
}
/* get a handle to the data synch object */
heventData = OpenEvent(EVENT_MODIFY_STATE, FALSE, "DBWIN_DATA_READY");
if ( !heventData )
{
// MessageBox(NULL, "DBWIN_DATA_READY nonexistent", NULL, MB_OK);
CloseHandle(heventDBWIN);
return;
}
hSharedFile = CreateFileMapping((HANDLE)-1, NULL, PAGE_READWRITE, 0, 4096, "DBWIN_BUFFER");
if (!hSharedFile)
{
//MessageBox(NULL, "DebugTrace: Unable to create file mapping object DBWIN_BUFFER", "Error", MB_OK);
CloseHandle(heventDBWIN);
CloseHandle(heventData);
return;
}
lpszSharedMem = (LPSTR)MapViewOfFile(hSharedFile, FILE_MAP_WRITE, 0, 0, 512);
if (!lpszSharedMem)
{
//MessageBox(NULL, "DebugTrace: Unable to map shared memory", "Error", MB_OK);
CloseHandle(heventDBWIN);
CloseHandle(heventData);
return;
}
/* wait for buffer event */
WaitForSingleObject(heventDBWIN, INFINITE);
/* write it to the shared memory */
*((LPDWORD)lpszSharedMem) = _getpid();
wsprintf(lpszSharedMem + sizeof(DWORD), "%s", achBuffer);
/* signal data ready event */
SetEvent(heventData);
/* clean up handles */
CloseHandle(hSharedFile);
CloseHandle(heventData);
CloseHandle(heventDBWIN);
return;
}
//////////////////
// Get window name in the form classname[HWND,title]
// Searches all the parents for a window with a title.
//
CString sDbgName(CWnd* pWnd)
{
CString sTitle;
HWND hwnd = pWnd->GetSafeHwnd();
if (hwnd==NULL)
sTitle = "NULL";
else if (!::IsWindow(hwnd))
sTitle = "[bad window]";
else {
sTitle = "[no title]";
for (CWnd* pw = pWnd; pw; pw = pw->GetParent()) {
if (pw->GetWindowTextLength() > 0) {
pw->GetWindowText(sTitle);
break;
}
}
}
CString s;
s.Format("%s[0x%04x,\"%s\"]",
pWnd ? pWnd->GetRuntimeClass()->m_lpszClassName : "NULL",
hwnd, (LPCTSTR)sTitle);
return s;
}
struct {
UINT msg;
LPCTSTR name;
} MsgData[] = {
{ WM_CREATE,_T("WM_CREATE") },
{ WM_DESTROY,_T("WM_DESTROY") },
{ WM_MOVE,_T("WM_MOVE") },
{ WM_SIZE,_T("WM_SIZE") },
{ WM_ACTIVATE,_T("WM_ACTIVATE") },
{ WM_SETFOCUS,_T("WM_SETFOCUS") },
{ WM_KILLFOCUS,_T("WM_KILLFOCUS") },
{ WM_ENABLE,_T("WM_ENABLE") },
{ WM_SETREDRAW,_T("WM_SETREDRAW") },
{ WM_SETTEXT,_T("WM_SETTEXT") },
{ WM_GETTEXT,_T("WM_GETTEXT") },
{ WM_GETTEXTLENGTH,_T("WM_GETTEXTLENGTH") },
{ WM_PAINT,_T("WM_PAINT") },
{ WM_CLOSE,_T("WM_CLOSE") },
{ WM_QUERYENDSESSION,_T("WM_QUERYENDSESSION") },
{ WM_QUIT,_T("WM_QUIT") },
{ WM_QUERYOPEN,_T("WM_QUERYOPEN") },
{ WM_ERASEBKGND,_T("WM_ERASEBKGND") },
{ WM_SYSCOLORCHANGE,_T("WM_SYSCOLORCHANGE") },
{ WM_ENDSESSION,_T("WM_ENDSESSION") },
{ WM_SHOWWINDOW,_T("WM_SHOWWINDOW") },
{ WM_WININICHANGE,_T("WM_WININICHANGE") },
{ WM_SETTINGCHANGE,_T("WM_SETTINGCHANGE") },
{ WM_DEVMODECHANGE,_T("WM_DEVMODECHANGE") },
{ WM_ACTIVATEAPP,_T("WM_ACTIVATEAPP") },
{ WM_FONTCHANGE,_T("WM_FONTCHANGE") },
{ WM_TIMECHANGE,_T("WM_TIMECHANGE") },
{ WM_CANCELMODE,_T("WM_CANCELMODE") },
{ WM_SETCURSOR,_T("WM_SETCURSOR") },
{ WM_MOUSEACTIVATE,_T("WM_MOUSEACTIVATE") },
{ WM_CHILDACTIVATE,_T("WM_CHILDACTIVATE") },
{ WM_QUEUESYNC,_T("WM_QUEUESYNC") },
{ WM_GETMINMAXINFO,_T("WM_GETMINMAXINFO") },
{ WM_PAINTICON,_T("WM_PAINTICON") },
{ WM_ICONERASEBKGND,_T("WM_ICONERASEBKGND") },
{ WM_NEXTDLGCTL,_T("WM_NEXTDLGCTL") },
{ WM_SPOOLERSTATUS,_T("WM_SPOOLERSTATUS") },
{ WM_DRAWITEM,_T("WM_DRAWITEM") },
{ WM_MEASUREITEM,_T("WM_MEASUREITEM") },
{ WM_DELETEITEM,_T("WM_DELETEITEM") },
{ WM_VKEYTOITEM,_T("WM_VKEYTOITEM") },
{ WM_CHARTOITEM,_T("WM_CHARTOITEM") },
{ WM_SETFONT,_T("WM_SETFONT") },
{ WM_GETFONT,_T("WM_GETFONT") },
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -