📄 myeditview.cpp
字号:
VERIFY(SetScrollInfo(SB_VERT, &si, TRUE));
Invalidate(FALSE);
}
void CMyEditView::SetHorzScrollBar()
{
if((420 - m_LeftChar + 2) < m_PageCols) m_LeftChar = 0; //80
SCROLLINFO si;
ZeroMemory(&si,sizeof(si));
si.cbSize = sizeof(si);
si.fMask = SIF_DISABLENOSCROLL | SIF_PAGE | SIF_POS | SIF_RANGE;
si.nMin = 0;
si.nMax = 420; //80
si.nPage = m_PageCols;
si.nPos = m_LeftChar;
VERIFY(SetScrollInfo(SB_HORZ, &si, TRUE));
Invalidate(FALSE);
}
//处理鼠标按键及拖动
void CMyEditView::OnLButtonDown(UINT nFlags, CPoint point)
{
CView::OnLButtonDown(nFlags, point);
m_StartDrag=TRUE;
m_bFirst=TRUE;
m_SelStart=m_SelEnd=Point2Pos(point);
Invalidate(FALSE);
}
void CMyEditView::OnLButtonUp(UINT nFlags, CPoint point)
{
CView::OnLButtonUp(nFlags, point);
if(m_StartDrag)
{
m_SelEnd=Point2Pos(point);
Invalidate(FALSE);
m_StartDrag=FALSE;
}
}
void CMyEditView::OnMouseMove(UINT nFlags, CPoint point)
{
CView::OnMouseMove(nFlags, point);
if(m_StartDrag)
{
m_SelEnd=Point2Pos(point);
Invalidate(FALSE);
}
}
//根据鼠标位置计算对应的字符在缓冲区的位置
UINT CMyEditView::Point2Pos(CPoint point)
{
if(m_text == NULL)
return 0;
UINT line = m_TopLine + point.y / m_LineHeight - 1;
UINT charset;
if(point.x < m_HexStart)
charset = 0;
else if(point.x < m_HexStart + m_CharWidth * 300) //100*3->16*3
charset = (point.x - m_HexStart)/(m_CharWidth*3);
else if(point.x < m_TextStart + 100 * m_CharWidth) //100->16
charset = (point.x - m_TextStart)/m_CharWidth;
else
charset = 99; //99->15
return ((line * 100 + charset)>m_CharCount?m_CharCount-1:(line * 100 + charset));//16
}
//处理鼠标滚轮
BOOL CMyEditView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
if(zDelta>0)
m_TopLine= --m_TopLine<1?1:m_TopLine;
else
if((m_TopLine + m_PageRows -2) < m_Rows)
m_TopLine+=1;
SetVertScrollBar();
SetHorzScrollBar();
return CView::OnMouseWheel(nFlags, zDelta, pt);
}
//下面是对滚动条的处理
void CMyEditView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
CView::OnHScroll(nSBCode, nPos, pScrollBar);
SCROLLINFO si;
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
VERIFY(GetScrollInfo(SB_HORZ, &si));
switch (nSBCode)
{
case SB_LEFT:
m_LeftChar = 0;
break;
case SB_BOTTOM:
m_LeftChar = 420 - m_PageCols + 1; //80
break;
case SB_LINEUP:
m_LeftChar = m_LeftChar - 1;
break;
case SB_LINEDOWN:
m_LeftChar = m_LeftChar + 1;
break;
case SB_PAGEUP:
m_LeftChar = m_LeftChar - si.nPage + 1;
break;
case SB_PAGEDOWN:
m_LeftChar = m_LeftChar + si.nPage - 1;
break;
case SB_THUMBPOSITION:
case SB_THUMBTRACK:
m_LeftChar = si.nTrackPos;
break;
default:
return;
}
if((m_LeftChar + m_PageCols - 2) > 420) m_LeftChar = 420 - m_PageCols +2; //420->80
if(m_LeftChar < 0) m_LeftChar = 0;
SetHorzScrollBar();
}
void CMyEditView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
CView::OnVScroll(nSBCode, nPos, pScrollBar);
SCROLLINFO si;
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
VERIFY(GetScrollInfo(SB_VERT, &si));
switch (nSBCode)
{
case SB_TOP:
m_TopLine = 0;
break;
case SB_BOTTOM:
m_TopLine = m_Rows - m_PageRows + 1;
break;
case SB_LINEUP:
m_TopLine = m_TopLine - 1;
break;
case SB_LINEDOWN:
m_TopLine = m_TopLine + 1;
break;
case SB_PAGEUP:
m_TopLine = m_TopLine - si.nPage + 1;
break;
case SB_PAGEDOWN:
m_TopLine = m_TopLine + si.nPage - 1;
break;
case SB_THUMBPOSITION:
case SB_THUMBTRACK:
m_TopLine = si.nTrackPos+1;
break;
default:
return;
}
if((m_TopLine + m_PageRows -2) > m_Rows) m_TopLine = m_Rows - m_PageRows + 2;
if(m_TopLine < 1) m_TopLine = 1;
SetVertScrollBar();
}
//0-9 a-f A-F键的处理
void CMyEditView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CView::OnChar(nChar, nRepCnt, nFlags);
UINT input;
UCHAR cHex = m_text[m_SelEnd];
if(nChar > 47 && nChar < 58)
{
input = nChar - 48;
}
else if (nChar > 64 && nChar < 71)
{
input = nChar - 64 + 9;
}
else if (nChar > 96 && nChar < 103)
{
input = nChar - 96 + 9;
}
else
{
return;
}
//用输入的字符替换对应位置的数据
if(m_bFirst)
{
input = input << 4;
cHex = cHex & 0x0f;
cHex = cHex | input;
m_bFirst = FALSE;
m_text[m_SelEnd] = cHex;
}
else
{
cHex = cHex & 0xf0;
cHex = cHex | input;
m_bFirst = TRUE;
m_text[m_SelEnd] = cHex;
m_SelEnd ++;
}
if(m_SelEnd == m_CharCount)
{
m_CharCount = m_SelEnd + 1;
m_Rows = m_CharCount/100; //16
}
m_SelStart = m_SelEnd;
ChangeTop();
ChangeLeft();
Invalidate(FALSE);
}
BOOL CMyEditView::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN)
{
switch (LOWORD(pMsg->wParam))
{
case VK_LEFT:
OnCharLeft();
break;
case VK_RIGHT:
OnCharRight();
break;
case VK_UP:
OnLineUp();
break;
case VK_DOWN:
OnLineDown();
break;
default:
break;
}
}
return CView::PreTranslateMessage(pMsg);
}
//下面四个是对上下左右键的处理
void CMyEditView::OnCharLeft()
{
if(m_bFirst && m_SelEnd > 0)
{
m_SelEnd --;
m_bFirst = FALSE;
}
else
{
m_bFirst = TRUE;
}
ChangeTop();
ChangeLeft();
m_SelStart = m_SelEnd;
Invalidate(FALSE);
}
void CMyEditView::OnCharRight()
{
if(m_bFirst)
{
m_bFirst = FALSE;
}
else if( m_SelEnd < m_CharCount-1)
{
m_bFirst = TRUE;
m_SelEnd++;
}
ChangeTop();
ChangeLeft();
m_SelStart = m_SelEnd;
Invalidate(FALSE);
}
void CMyEditView::OnLineUp()
{
if(m_SelEnd > 99) //15
m_SelEnd = m_SelEnd - 100; //16
ChangeTop();
ChangeLeft();
m_SelStart = m_SelEnd;
Invalidate(FALSE);
}
void CMyEditView::OnLineDown()
{
if(m_SelEnd + 100 < m_CharCount) //16
m_SelEnd = m_SelEnd + 100; //16
else
m_SelEnd = m_CharCount-1;
ChangeTop();
ChangeLeft();
m_SelStart = m_SelEnd;
Invalidate(FALSE);
}
//计算显示的最上边字符位置
void CMyEditView::ChangeTop()
{
int newtop = m_TopLine;
if((m_SelEnd/100 + 1) < m_TopLine) //16
{
newtop = m_SelEnd/100 + 1; //16
}
if((m_SelEnd/100 + 2) > m_TopLine+m_PageRows)//16
{
newtop = m_SelEnd/100 + 2 - m_PageRows; //16
}
if(newtop != m_TopLine)
{
m_TopLine = newtop;
SetVertScrollBar();
}
}
//计算显示的最左边字符位置
void CMyEditView::ChangeLeft()
{
int currentcol = m_SelEnd%100; //16
if(currentcol*3 + 14 < m_LeftChar) //14为地址占的位置长度
{
m_LeftChar = currentcol*3 + 14; //14
SetHorzScrollBar();
}
if(currentcol*3 + 14 - m_LeftChar+1 > m_PageCols)//14
{
m_LeftChar = currentcol*3 + 14 - m_PageCols +2;//14
SetHorzScrollBar();
}
}
void CMyEditView::OnSetFocus(CWnd* pOldWnd)
{
CView::OnSetFocus(pOldWnd);
m_bFocused=TRUE;
}
void CMyEditView::OnKillFocus(CWnd* pNewWnd)
{
CView::OnKillFocus(pNewWnd);
m_bFocused=FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -