📄 sourcemonitor.cpp
字号:
// SourceMonitor.cpp : implementation file
//
#include "stdafx.h"
#include "SourceMonitor.h"
#include "resource.h"
#include "setup.h"
#include "VSInterpreter.h" //为了给查找按钮增加刷新脚本文件的功能
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyToolBarCtrl
CMyToolBarCtrl::CMyToolBarCtrl()
{
}
CMyToolBarCtrl::~CMyToolBarCtrl()
{
}
BEGIN_MESSAGE_MAP(CMyToolBarCtrl, CToolBarCtrl)
//{{AFX_MSG_MAP(CMyToolBarCtrl)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyToolBarCtrl message handlers
int CMyToolBarCtrl::Create2(CWnd *pParentWnd, RECT rectToolBarCtrl, UINT uID)
{
//创建工具条
CToolBarCtrl::Create(WS_BORDER | WS_VISIBLE | WS_CHILD
/*| CCS_TOP*/,
rectToolBarCtrl, pParentWnd, uID);
//创建按钮
int id1 = AddStrings("单步");
int id2 = AddStrings("下一步");
int id3 = AddStrings("断点");
int id4 = AddStrings("查询/刷新");
int id5 = AddStrings("查找");
int id6 = AddStrings("下个通道");
int id7 = AddStrings("上个通道");
TBBUTTON ptbButton[7];
ptbButton[0].iBitmap = NULL;
ptbButton[0].idCommand = ID_TOOLBAR_STEP;
ptbButton[0].fsState = TBSTATE_ENABLED;
ptbButton[0].fsStyle = TBSTYLE_BUTTON | TBSTYLE_CHECK;
ptbButton[0].dwData = 0;
ptbButton[0].iString = id1;
ptbButton[1].iBitmap = NULL;
ptbButton[1].idCommand = ID_TOOLBAR_STEPOVER;
ptbButton[1].fsState = TBSTATE_ENABLED;
ptbButton[1].fsStyle = TBSTYLE_BUTTON;
ptbButton[1].dwData = 0;
ptbButton[1].iString = id2;
ptbButton[2].iBitmap = NULL;
ptbButton[2].idCommand = ID_TOOLBAR_BREAKPOINT;
ptbButton[2].fsState = TBSTATE_ENABLED;
ptbButton[2].fsStyle = TBSTYLE_BUTTON;
ptbButton[2].dwData = 0;
ptbButton[2].iString = id3;
ptbButton[3].iBitmap = NULL;
ptbButton[3].idCommand = ID_TOOLBAR_QUERY;
ptbButton[3].fsState = TBSTATE_ENABLED;
ptbButton[3].fsStyle = TBSTYLE_BUTTON;
ptbButton[3].dwData = 0;
ptbButton[3].iString = id4;
ptbButton[4].iBitmap = NULL;
ptbButton[4].idCommand = ID_TOOLBAR_FIND;
ptbButton[4].fsState = TBSTATE_ENABLED;
ptbButton[4].fsStyle = TBSTYLE_BUTTON;
ptbButton[4].dwData = 0;
ptbButton[4].iString = id5;
ptbButton[5].iBitmap = NULL;
ptbButton[5].idCommand = ID_TOOLBAR_NEXTCHANNEL;
ptbButton[5].fsState = TBSTATE_ENABLED;
ptbButton[5].fsStyle = TBSTYLE_BUTTON;
ptbButton[5].dwData = 0;
ptbButton[5].iString = id6;
ptbButton[6].iBitmap = NULL;
ptbButton[6].idCommand = ID_TOOLBAR_PREVIOUSCHANNEL;
ptbButton[6].fsState = TBSTATE_ENABLED;
ptbButton[6].fsStyle = TBSTYLE_BUTTON;
ptbButton[6].dwData = 0;
ptbButton[6].iString = id7;
AddButtons(sizeof(ptbButton)/sizeof(TBBUTTON), ptbButton);
SetOwner(pParentWnd);
CheckButton(ID_TOOLBAR_STEP, FALSE);
return(0);
}
int CMyToolBarCtrl::Create(CWnd *pParentWnd, RECT rectToolBarCtrl, UINT uID)
{
//创建工具条
CToolBarCtrl::Create(WS_BORDER | WS_VISIBLE | WS_CHILD
/*| CCS_TOP*/,
rectToolBarCtrl, pParentWnd, uID);
//创建按钮
int id1 = AddStrings("单步");
int id2 = AddStrings("下一步");
int id3 = AddStrings("断点");
int id4 = AddStrings("查询/刷新");
int id5 = AddStrings("查找");
int id6 = AddStrings("下个通道");
int id7 = AddStrings("上个通道");
TBBUTTON ptbButton[7];
ptbButton[0].iBitmap = NULL;
ptbButton[0].idCommand = ID_TOOLBAR_STEP;
ptbButton[0].fsState = TBSTATE_ENABLED;
ptbButton[0].fsStyle = TBSTYLE_BUTTON | TBSTYLE_CHECK;
ptbButton[0].dwData = 0;
ptbButton[0].iString = id1;
ptbButton[1].iBitmap = NULL;
ptbButton[1].idCommand = ID_TOOLBAR_STEPOVER;
ptbButton[1].fsState = TBSTATE_ENABLED;
ptbButton[1].fsStyle = TBSTYLE_BUTTON;
ptbButton[1].dwData = 0;
ptbButton[1].iString = id2;
ptbButton[2].iBitmap = NULL;
ptbButton[2].idCommand = ID_TOOLBAR_BREAKPOINT;
ptbButton[2].fsState = TBSTATE_ENABLED;
ptbButton[2].fsStyle = TBSTYLE_BUTTON;
ptbButton[2].dwData = 0;
ptbButton[2].iString = id3;
ptbButton[3].iBitmap = NULL;
ptbButton[3].idCommand = ID_TOOLBAR_QUERY;
ptbButton[3].fsState = TBSTATE_ENABLED;
ptbButton[3].fsStyle = TBSTYLE_BUTTON;
ptbButton[3].dwData = 0;
ptbButton[3].iString = id4;
ptbButton[4].iBitmap = NULL;
ptbButton[4].idCommand = ID_TOOLBAR_FIND;
ptbButton[4].fsState = TBSTATE_ENABLED;
ptbButton[4].fsStyle = TBSTYLE_BUTTON;
ptbButton[4].dwData = 0;
ptbButton[4].iString = id5;
ptbButton[5].iBitmap = NULL;
ptbButton[5].idCommand = ID_TOOLBAR_NEXTCHANNEL;
ptbButton[5].fsState = TBSTATE_ENABLED;
ptbButton[5].fsStyle = TBSTYLE_BUTTON;
ptbButton[5].dwData = 0;
ptbButton[5].iString = id6;
ptbButton[6].iBitmap = NULL;
ptbButton[6].idCommand = ID_TOOLBAR_PREVIOUSCHANNEL;
ptbButton[6].fsState = TBSTATE_ENABLED;
ptbButton[6].fsStyle = TBSTYLE_BUTTON;
ptbButton[6].dwData = 0;
ptbButton[6].iString = id7;
AddButtons(sizeof(ptbButton)/sizeof(TBBUTTON), ptbButton);
SetBitmapSize((0, 0)); //不知为啥,这句话会使工具栏变窄,而且不可调
SetOwner(pParentWnd); //好象对按钮和工具栏之间的消息传递没用
CheckButton(ID_TOOLBAR_STEP, FALSE);
//创建EDITBOX成员
RECT rectEdit = {500, 0, 600, rectToolBarCtrl.bottom - 8};
DWORD dwEditStyle = WS_BORDER | WS_VISIBLE
| WS_CHILD | ES_AUTOHSCROLL/* | WS_THICKFRAME*/;
m_Edit.Create(dwEditStyle, rectEdit, this, ID_TOOLBAR_EDIT);
m_Edit.m_iType = EDITTYPE_QUERY;
m_Edit.SetOwner(pParentWnd); //没用!
m_Edit.ModifyStyleEx(0, WS_EX_CLIENTEDGE | WS_EX_WINDOWEDGE | WS_EX_STATICEDGE, 0);
//创建EDITBOX_FIND成员
RECT rectEdit_Find = {605, 0, 705, rectToolBarCtrl.bottom - 8};
dwEditStyle = WS_BORDER | WS_VISIBLE
| WS_CHILD | ES_AUTOHSCROLL/* | WS_THICKFRAME*/;
m_Edit_Find.Create(dwEditStyle, rectEdit_Find, this, ID_TOOLBAR_EDITFIND);
m_Edit_Find.m_iType = EDITTYPE_FIND;
m_Edit_Find.ModifyStyleEx(0, WS_EX_CLIENTEDGE | WS_EX_WINDOWEDGE | WS_EX_STATICEDGE, 0);
return(0);
}
BOOL CMyToolBarCtrl::OnCommand(WPARAM wParam, LPARAM lParam)
{
if(LOWORD(wParam) == ID_TOOLBAR_EDIT)
{
Sleep(1);
if(HIWORD(wParam) == EN_CHANGE)
Sleep(1);
}
return CToolBarCtrl::OnCommand(wParam, lParam);
}
// end of CMyToolBarCtrl message handlers
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// CSourceMonitor
IMPLEMENT_DYNCREATE(CSourceMonitor, CFrameWnd)
CSourceMonitor::CSourceMonitor()
{
m_szFileBuf = NULL;
}
CSourceMonitor::~CSourceMonitor()
{
//保存断点
int iBreakPointCount = 0;
////清除原先保存在文件中的断点
// WritePrivateProfileString("BreakPoint", NULL, NULL, ".\\SourceMonitor.inf");
while(1)
{
if(m_ptrListBreakPointLineNo.IsEmpty())
break;
else
{
iBreakPointCount ++;
char aszBreakPointCount[10] = "\0";
char aszBreakPointLineNo[10] = "\0";
itoa(iBreakPointCount, aszBreakPointCount, 10);
itoa(*(int *)(m_ptrListBreakPointLineNo.GetHead()), aszBreakPointLineNo, 10);
WritePrivateProfileString("BreakPoint", aszBreakPointCount, aszBreakPointLineNo, ".\\SourceMonitor.inf");
free(m_ptrListBreakPointLineNo.RemoveHead());
}
}
//释放成员
if(m_szFileBuf != NULL)
free(m_szFileBuf);
}
BEGIN_MESSAGE_MAP(CSourceMonitor, CFrameWnd)
//{{AFX_MSG_MAP(CSourceMonitor)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_SIZE()
ON_NOTIFY( NM_CLICK, 113, OnToolBarNotify)
ON_EN_VSCROLL(ID_EDIT_SOURCEWINDOW, OnEditScroll)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSourceMonitor message handlers
int CALLBACK EnumFontFamProc(
ENUMLOGFONT FAR *lpelf, // pointer to logical-font data
NEWTEXTMETRIC FAR *lpntm, // pointer to physical-font data
int FontType, // type of font
LPARAM lParam // pointer to application-defined data
)
{
LPCTSTR lpsz = (char *)lpelf->elfFullName;
return(1);
}
HINSTANCE hResourceDll = NULL;
HICON hCurLineIcon = NULL;
HICON hWindowIcon = NULL;
BOOL CSourceMonitor::Create(void *pvsInterpreter)
{
//初始化一些成员
m_iHeightOfToolBar = 30;
m_iHeightOfStatusBar = 20;
m_iIndicatorAreaWidth = 20;
m_bIsStep = FALSE;
m_bIsBreakPoint = FALSE;
m_iLineHeight = 0;
m_szQueryVariableName[0] = 0;
if((m_hEventStep = CreateEvent(NULL, FALSE, FALSE, "step")) == NULL)
{
return(-1);
}
if((m_hEventQuery = CreateEvent(NULL, FALSE, FALSE, "query")) == NULL)
{
return(-1);
}
//创建主窗口
int iWidth_MainWindow = 750;
int iHeight_MainWindow = 550;
RECT rectMainWindow = {0, 0, iWidth_MainWindow, iHeight_MainWindow};
CFrameWnd::Create(NULL, WINDOW_CAPTION, WS_OVERLAPPEDWINDOW | WS_MINIMIZE, rectMainWindow);
//装入ICON资源
if(hResourceDll == NULL)
{
char s[100];
GetCurrentDirectory(100, s);
hResourceDll = LoadLibrary(".\\ResourceDll.dll");
hCurLineIcon = LoadIcon(hResourceDll, MAKEINTRESOURCE(102));
hWindowIcon = LoadIcon(hResourceDll, MAKEINTRESOURCE(104));
}
::SendMessage(this->m_hWnd, WM_SETICON, ICON_BIG, (LPARAM)hWindowIcon);
//创建工具栏
RECT rectToolBarCtrl = {0, 0, iWidth_MainWindow, m_iHeightOfToolBar};
m_toolBarCtrl.Create(this, rectToolBarCtrl, ID_TOOLBAR);
//创建状态栏
RECT rectStatusBarCtrl = {0, 0, 50, m_iHeightOfStatusBar};
m_statusBarCtrl.Create(WS_CHILD | WS_VISIBLE, rectStatusBarCtrl, this, ID_STATUSBAR);
int *piWidths;
int iPartCount;
iPartCount = 4;
piWidths = (int *)malloc( iPartCount * sizeof(int) );
piWidths[0] = 100;
piWidths[1] = 300;
piWidths[2] = 500;
piWidths[3] = -1;
m_statusBarCtrl.SetParts(iPartCount, piWidths);
m_statusBarCtrl.SetText("变量值=", 0, 0);
m_statusBarCtrl.SetText("本通道话务量: 0", 1, 0);
m_statusBarCtrl.SetText("总呼入话务量: 0", 2, 0);
m_statusBarCtrl.SetText("总呼出话务量: 0", 3, 0);
//创建指示符号
m_hIndexIcon = (HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE);
m_iLineY = 0;
//创建源码框
RECT rectMainWnd, rect;
GetClientRect(&rectMainWnd);
rect.top = rectMainWnd.top + m_iHeightOfToolBar;
rect.bottom = rectMainWnd.bottom - m_iHeightOfStatusBar;
rect.left = rectMainWnd.left + m_iIndicatorAreaWidth;
rect.right = rectMainWnd.right;
DWORD dwEditStyle = ES_MULTILINE | WS_VISIBLE
| WS_CHILD | ES_AUTOHSCROLL
| ES_AUTOVSCROLL | WS_VSCROLL
| WS_HSCROLL //| ES_READONLY
| WS_TABSTOP;
m_Edit.Create(dwEditStyle, rect, this, ID_EDIT_SOURCEWINDOW);
m_Edit.ModifyStyleEx(0, WS_EX_CLIENTEDGE | WS_EX_WINDOWEDGE | WS_EX_STATICEDGE, 0);
m_Edit.SetReadOnly(TRUE);
m_Edit.SetFocus(); //在这里设置没用!要放在OnPaint()中
//改变源码框的字体
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT)); // Clear out structure.
lf.lfHeight = 15; // Request a 20-pixel-high font
strcpy(lf.lfFaceName, "Comic Sans MS"); // with face name "Arial".
m_font.CreateFontIndirect(&lf); // Create the font.
m_Edit.SetFont(&m_font);
m_pvsInterpreter = pvsInterpreter;
ShowWindow(SW_SHOW);
return(TRUE);
}
void CSourceMonitor::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
::SendMessage(this->m_hWnd, WM_SETICON, ICON_BIG, (LPARAM)hWindowIcon);
//画当前行标志
int Y = m_iLineY + m_iEditY + 5; //修正
//计算当前的可见的首行和末行的行号
int iFirstVisibleLine = m_Edit.GetFirstVisibleLine();
int iLastVisibleLine;
RECT rectEdit, rectBreakPoint, rectCurSel;
m_Edit.GetRect(&rectEdit);
iLastVisibleLine = iFirstVisibleLine + (rectEdit.bottom - rectEdit.top)/m_iLineHeight;
//当前行左边绘画区的中点
CPoint pointCenter;
pointCenter.x = m_iIndicatorAreaWidth/2;
pointCenter.y = (m_iCurSelLineNo - iFirstVisibleLine) * m_iLineHeight + m_iEditY + m_iLineHeight/2;
//有当前选中行出现就画选中标志
int iIndicatorCurLineWidth = 14;
if(m_iLineHeight != 0)
{
if(m_iCurSelLineNo > iFirstVisibleLine && m_iCurSelLineNo < iLastVisibleLine)
{//落在EDIT的可见范围内,可画
rectCurSel.left = 15;
rectCurSel.bottom = (m_iCurSelLineNo - iFirstVisibleLine) * m_iLineHeight + m_iEditY + m_iLineHeight/2;
rectCurSel.right = rectCurSel.left + 5;
rectCurSel.top = rectCurSel.bottom + 5;
::DrawIconEx(dc.m_hDC, pointCenter.x - iIndicatorCurLineWidth/2, pointCenter.y - iIndicatorCurLineWidth/2, hCurLineIcon, iIndicatorCurLineWidth, iIndicatorCurLineWidth, 0, NULL, DI_NORMAL);
}
}
//有断点出现就画断点标志
int iIndicatorBreakPointWidth = 10;
if(m_iLineHeight != 0)
{
for(int i = 0; i < m_ptrListBreakPointLineNo.GetCount(); i ++)
{
POSITION position = m_ptrListBreakPointLineNo.FindIndex(i);
int iLineNo = *(int *)m_ptrListBreakPointLineNo.GetAt(position);
if(iLineNo > iFirstVisibleLine && iLineNo < iLastVisibleLine)
{//落在EDIT的可见范围内,可画
CPoint pointCenter_BreakPoint;
pointCenter_BreakPoint.x = pointCenter.x;
pointCenter_BreakPoint.y = (iLineNo - iFirstVisibleLine) * m_iLineHeight + m_iEditY + m_iLineHeight/2;
rectBreakPoint.left = pointCenter_BreakPoint.x - iIndicatorBreakPointWidth/2;
rectBreakPoint.bottom = pointCenter_BreakPoint.y - iIndicatorBreakPointWidth/2;
rectBreakPoint.right = pointCenter_BreakPoint.x + iIndicatorBreakPointWidth/2;
rectBreakPoint.top = pointCenter_BreakPoint.y + iIndicatorBreakPointWidth/2;
CBrush brush;
brush.CreateSolidBrush(0x00ff00ff);
CBrush *pbrushOld = dc.SelectObject(&brush);
dc.Ellipse(&rectBreakPoint);
dc.SelectObject(pbrushOld);
}
}
}
m_Edit.SetFocus();
}
int CSourceMonitor::SetChannelNo(int iChannelNo)
{
return 0;
}
int CSourceMonitor::LoadFile(char *szInstructionFile, int iChannelNo)
{
FILE *file = fopen(szInstructionFile, "rb");
if(file == NULL)
return -1;
strcpy(m_aszFileName, szInstructionFile);
m_iChannelNo = iChannelNo;
char aszCaption[100] = "\0";
sprintf(aszCaption, "%s - %d - %s", WINDOW_CAPTION, iChannelNo, szInstructionFile);
SetWindowText(aszCaption);
OFSTRUCT of;
HFILE hFile = OpenFile(szInstructionFile, &of, OF_READ);
if(hFile == HFILE_ERROR)
{
AfxMessageBox("SourceMonitor OpenFile() error");
return(-1);
}
m_dwFileBufSize = GetFileSize((HANDLE)hFile, NULL);
if(m_dwFileBufSize == -1)
{
AfxMessageBox("SourceMonitor GetFileSize() error");
return(-1);
}
CloseHandle((HANDLE)hFile);
m_szFileBuf = (char *)realloc(m_szFileBuf, m_dwFileBufSize);
fread(m_szFileBuf, 1, m_dwFileBufSize, file);
fclose(file);
m_hwndEdit = m_Edit.m_hWnd;
::SendMessage(m_hwndEdit, EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, MAKELONG(10, 0));
::SendMessage(m_hwndEdit, EM_SETSEL, 0, -1);
::SendMessage(m_hwndEdit, WM_SETTEXT, FALSE, (LPARAM)m_szFileBuf);
//计算Edit在Dlg中的位置
POINT pointDlg, pointEdit;
pointDlg.x = 0; pointDlg.y = 0;
pointEdit.x = 0; pointEdit.y = 0;
::ClientToScreen(this->m_hWnd, &pointDlg);
::ClientToScreen(m_hwndEdit,&pointEdit);
m_iEditY = pointEdit.y - pointDlg.y;
//得到源码框中的行高
int iChar1 = m_Edit.LineIndex(0);
int iChar2 = m_Edit.LineIndex(1);
if(iChar1 != -1 && iChar2 != -1)
{
CPoint point1 = m_Edit.PosFromChar(iChar1);
CPoint point2 = m_Edit.PosFromChar(iChar2);
m_iLineHeight = point2.y - point1.y;
}
//恢复保存的断点
int iLineNo_BreakPoint;
for(int iCount = 1; ; iCount ++)
{
char aszIndex[10] = "\0";
itoa(iCount, aszIndex, 10);
char s[100];
GetCurrentDirectory(100, s);
iLineNo_BreakPoint = GetPrivateProfileInt("BreakPoint", aszIndex, -1, ".\\SourceMonitor.inf");
if(iLineNo_BreakPoint == -1)
break;
else
{
int *piBreakPointLineNo = (int *)malloc(sizeof(int));
*piBreakPointLineNo = iLineNo_BreakPoint;
m_ptrListBreakPointLineNo.AddTail(piBreakPointLineNo);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -