📄 datashowview.cpp
字号:
//显示辅助菜单
CMenu menu;
if(menu.CreatePopupMenu())
{
if(!m_bfrozen)
menu.AppendMenu(MF_ENABLED|MF_STRING,1001,"冻结屏幕");
else
menu.AppendMenu(MF_ENABLED|MF_STRING,1001,"取消冻结");
menu.AppendMenu(MF_SEPARATOR,0,"");
menu.AppendMenu(MF_ENABLED|MF_STRING,1006,"清除窗口");
if(m_bfrozen)
{
menu.AppendMenu(MF_SEPARATOR,0,"");
if(m_selfrom!=-1)
{
menu.AppendMenu(MF_ENABLED|MF_STRING,1002,"拷贝HEX码");
menu.AppendMenu(MF_ENABLED|MF_STRING,1003,"拷贝ASCII码");
menu.AppendMenu(MF_SEPARATOR,0,"");
}
menu.AppendMenu(MF_ENABLED|MF_STRING,1004,"全部选中");
menu.AppendMenu(MF_SEPARATOR,0,"");
menu.AppendMenu(MF_ENABLED|MF_STRING,1005,"选择特殊字符");
}
POINT pt;
GetCursorPos(&pt);
menu.TrackPopupMenu(TPM_RIGHTBUTTON,pt.x,pt.y,this);
}
CView ::OnRButtonUp(nFlags, point);
}
////////////////////////////////////////////////////////////
//函数:OnMouseMove;功能,处理鼠标移动的操作
////////////////////////////////////////////////////////////
void CDataShowView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bpointing)
{
//如果正在选择特殊字符,则在鼠标移动的同时,指出当前指向的字符
//方法是给这个字符加一个边框!
RECT rect1;
GetClientRect(&rect1);
int curline=(rect1.bottom-point.y)/15;//(point.y+10)/15;
curline=GetScrollPos(SB_VERT)-curline;
if(curline<0)
curline=0;
int curchar=point.x>=m_bytesperline*24?m_bytesperline-1:point.x/24;
curchar+=curline*m_bytesperline;
if(curchar>=m_bytes.GetSize())
curchar=m_bytes.GetSize()-1;
if(curchar!=m_oldcharpos)
{
RECT rect;
CDC *pdc=GetDC();
CPen *oldpen;
if(m_oldcharpos!=-1)
{
CPen pen;
if(m_selfrom!=-1 && m_oldcharpos >=m_selfrom &&
m_oldcharpos<=m_selto)
pen.CreatePen(PS_SOLID,1,m_color_selectback);
else
pen.CreatePen(PS_SOLID,1,m_color_normalback);
oldpen=pdc->SelectObject(&pen);
rect.left=(m_oldcharpos%m_bytesperline)*24;
rect.right=rect.left+16;
//rect.top=m_oldcharpos/m_bytesperline+m_linesshown-GetScrollPos(SB_VERT)-1;
rect.top=rect1.bottom-(GetScrollPos(SB_VERT)-m_oldcharpos/m_bytesperline+1)*15;
//rect.top*=15;
rect.bottom=rect.top+15;
//rect.left--;
//rect.top--;
//rect.bottom++;
//rect.right++;
//InvalidateRect(&rect);
pdc->MoveTo(rect.left,rect.top);
pdc->LineTo(rect.right,rect.top);
pdc->LineTo(rect.right,rect.bottom);
pdc->LineTo(rect.left,rect.bottom);
pdc->LineTo(rect.left,rect.top);
pdc->SelectObject(oldpen);
pen.DeleteObject();
}
m_oldcharpos=curchar;
rect.left=m_oldcharpos%m_bytesperline*24;
rect.right=rect.left+16;
//rect.top=m_oldcharpos/m_bytesperline+m_linesshown-GetScrollPos(SB_VERT)-1;
rect.top=rect1.bottom-(GetScrollPos(SB_VERT)-m_oldcharpos/m_bytesperline+1)*15;
//rect.top*=15;
rect.bottom=rect.top+15;
CPen pen;
pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
oldpen=pdc->SelectObject(&pen);
//pdc->DrawEdge(&rect,BF_RECT,0);
pdc->MoveTo(rect.left,rect.top);
pdc->LineTo(rect.right,rect.top);
pdc->LineTo(rect.right,rect.bottom);
pdc->LineTo(rect.left,rect.bottom);
pdc->LineTo(rect.left,rect.top);
pdc->SelectObject(oldpen);
ReleaseDC(pdc);
pen.DeleteObject();
}
CView ::OnMouseMove(nFlags, point);
return;
}
if(m_bmousedown)
{//现在的情况是字符范围选择过程中的拖动,随着鼠标的拖动,
//选择的范围也不停的变化
static int oldchar=-1;
RECT rect1;
GetClientRect(&rect1);
int curline=(rect1.bottom-point.y)/15;//(point.y+10)/15;
curline=GetScrollPos(SB_VERT)-curline;
if(curline<0)
curline=0;
int curchar=point.x>=m_bytesperline*24?m_bytesperline:point.x/24;
curchar+=curline*m_bytesperline;
int oldfrom=m_selfrom,oldto=m_selto;
if(m_firstsel>=curchar)
{
m_selfrom=curchar;
m_selto=m_firstsel;
}
else
{
m_selfrom=m_firstsel;
m_selto=curchar;
}
if(oldfrom!=m_selfrom && oldto!=m_selto)
{
if(m_selfrom<oldfrom)
oldfrom=m_selfrom;
if(m_selto>oldto)
oldto=m_selto;
}
else if(oldfrom!=m_selfrom)
{
if(oldfrom>m_selfrom)
{
oldto=oldfrom;
oldfrom=m_selfrom;
}
else
oldto=m_selfrom;
}
else
{
if(oldto<m_selto)
{
oldfrom=oldto;
oldto=m_selto;
}
else
oldfrom=m_selto;
}
RECT rect;
GetClientRect(&rect);
//rect.top=oldfrom/m_bytesperline+m_linesshown-GetScrollPos(SB_VERT)-1;
//rect.top*=15;
//rect.bottom=oldto/m_bytesperline+m_linesshown-GetScrollPos(SB_VERT);
//rect.bottom*=15;
rect.top=rect1.bottom-(GetScrollPos(SB_VERT)-oldfrom/m_bytesperline+1)*15;
rect.bottom=rect1.bottom-(GetScrollPos(SB_VERT)-oldto/m_bytesperline)*15;
rect.bottom++;
if(curchar!=oldchar)
{
oldchar=curchar;
InvalidateRect(&rect);
}
}
RECT rect;
GetClientRect(&rect);
if(point.y>rect.bottom+5 && m_btimer!=1)
{
m_btimer=1;
SetTimer(1111,50,NULL);
}
else if(point.y<rect.top-5 && m_btimer!=2)
{
m_btimer=2;
SetTimer(1111,50,NULL);
}
else if(m_btimer && point.y<=rect.bottom+5 && point.y>= rect.top-5)
{
KillTimer(1111);
m_btimer=0;
}
CView ::OnMouseMove(nFlags, point);
}
////////////////////////////////////////////////////////
//函数:OnFreezeScreen,冻结或解冻屏幕
void CDataShowView::OnFreezeScreen()
{
m_bfrozen=!m_bfrozen;
}
//////////////////////////////////////////////////////
//函数:OnCopyData, 将选中的数据源码拷贝到剪贴板上
/////////////////////////////////////////////////////
void CDataShowView::OnCopyData()
{
if(m_selfrom==-1)
return;
if(OpenClipboard())
{
if(EmptyClipboard())
{
HGLOBAL hmem=GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,m_selto-m_selfrom+2);
BYTE *pb=(BYTE *)GlobalLock(hmem);
memset(pb,0,m_selto-m_selfrom+2);
for(int i=0;i<m_selto-m_selfrom+1&& m_selfrom+i<m_bytes.GetSize();i++)
pb[i]=m_bytes[m_selfrom+i];
GlobalUnlock(hmem);
SetClipboardData(CF_TEXT,hmem);
}
CloseClipboard();
}
}
//////////////////////////////////////////////////////
//函数:OnCopyDisp, 将选中的数据显示码拷贝到剪贴板上
/////////////////////////////////////////////////////
void CDataShowView::OnCopyDisp()
{
if(m_selfrom==-1)
return;
if(OpenClipboard())
{
if(EmptyClipboard())
{
HGLOBAL hmem=GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,(m_selto-m_selfrom+2)*3);
char *pb=(char *)GlobalLock(hmem);
memset(pb,0,(m_selto-m_selfrom+2)*3);
char buf[4];
for(int i=0;i<m_selto-m_selfrom+1 && m_selfrom+i<m_bytes.GetSize();i++)
{
wsprintf(buf,"%02X ",m_bytes[m_selfrom+i]);
strcat(pb,buf);
}
GlobalUnlock(hmem);
SetClipboardData(CF_TEXT,hmem);
}
CloseClipboard();
}
}
//////////////////////////////////////////////////////
//函数:OnSelectAll,选中所有的字符
/////////////////////////////////////////////////////
void CDataShowView::OnSelectAll()
{
if(m_bytes.GetSize())
{
m_selfrom=0;
m_selto=m_bytes.GetSize()-1;
Invalidate();
}
}
//////////////////////////////////////////////////////
//函数:OnSetSpByte,开始设定特殊显示的字符
/////////////////////////////////////////////////////
void CDataShowView::OnSetSpByte()
{
if(m_bytes.GetSize()==0)
return;
m_bpointing=TRUE;
//SetCapture();
//SetCursor(::LoadCursor(NULL,IDC_ARROW));
SetClassLong(m_hWnd,GCL_HCURSOR,(long)(::LoadCursor(NULL,IDC_ARROW)));
}
//////////////////////////////////////////////////////
//函数:OnClearWindow,清除窗口内容
/////////////////////////////////////////////////////
void CDataShowView::OnClearWindow()
{
EnterCriticalSection(&cs);
m_bytes.RemoveAll();
m_selfrom=-1;
LeaveCriticalSection(&cs);
SetScrollRange(SB_VERT,0,1);
SetScrollPos(SB_VERT,0);
Invalidate();
}
//////////////////////////////////////////////////////
//函数:OnTimer,自动向上向下卷轴的定时器
/////////////////////////////////////////////////////
void CDataShowView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1111&& m_btimer)
{
int min,max,pos;
pos=GetScrollPos(SB_VERT);
GetScrollRange(SB_VERT,&min,&max);
if((m_btimer==1 && pos>=max)||(m_btimer==2 && pos<=min))
{
KillTimer(1111);
m_btimer=0;
}
else
{
if(m_btimer==1)
{
// m_selto+=m_bytesperline;
// RECT rect;
// GetClientRect(&rect);
// rect.top=rect.bottom-15;
// InvalidateRect(&rect);
OnVScroll(SB_LINEDOWN,0,NULL);
}
else if(m_btimer==2)
{
// m_selfrom-=m_bytesperline;
// if(m_selfrom<0)
// m_selfrom=0;
OnVScroll(SB_LINEUP,0,NULL);
}
CPoint pt;
GetCursorPos(&pt);
RECT rect;
GetWindowRect(&rect);
pt.x-=rect.left;
pt.y-=rect.top;
OnMouseMove(0,pt);
//m_selto+=m_bytesperline;
//RECT rect;
//GetClientRect(&rect);
//rect.top=rect.bottom-15;
//InvalidateRect(&rect);
}
}
CView ::OnTimer(nIDEvent);
}
//////////////////////////////////////////////////////
//函数:SetBufferLen,设置显示缓冲的大小
/////////////////////////////////////////////////////
void CDataShowView::SetBufferLen(int nlen)
{
//Commwin.cpp中已判断 if(nlen>10240) nlen=10240;//10K bytes
EnterCriticalSection(&cs);
m_bufferbytes=nlen;
LeaveCriticalSection(&cs);
}
//////////////////////////////////////////////////////
//函数:OnDraw,画图函数,是用来显示字符的。
/////////////////////////////////////////////////////
void CDataShowView::OnDraw(CDC* pDC)
{
// TODO: Add your specialized code here and/or call the base class
RECT rect;
GetClientRect(&rect);
CBitmap bmp,*ob;
CDC cdc;
cdc.CreateCompatibleDC(pDC);
bmp.CreateCompatibleBitmap(pDC,rect.right,rect.bottom);
ob=cdc.SelectObject(&bmp);
// cdc.SetTextColor(RGB(0,0,0));
// cdc.SetBkColor(RGB(255,255,255));
cdc.FillRect(&rect,CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
COLORREF oldt,oldb;
oldt=cdc.SetTextColor(m_color_normaltext);
oldb=cdc.SetBkColor(m_color_normalback);
// TODO: Add your message handler code here
// SetFont(&m_font);
//采用临界区操作,进入临界区
EnterCriticalSection(&cs);
int total=m_bytes.GetSize();
if(total==0)
{
LeaveCriticalSection(&cs);
return;
}
int pos=GetScrollPos(SB_VERT);
total=(pos+1)*m_bytesperline>total?total:(pos+1)*m_bytesperline;
int lines=pos-m_linesshown;
int from;
if(lines<0)
from=0;
else
from=lines;
from*=m_bytesperline;
if(lines<0)
lines=pos;
else
lines=m_linesshown;
// char buf1[256];
// char buf2[56];
char temp[4];
int line=-1;
CFont *of;
of=cdc.SelectObject(&m_font);
for(;from<total;from+=m_bytesperline)
{
//memset(buf1,0,256);
//memset(buf2,0,56);
for(int i=0;i<m_bytesperline;i++)
{
if(from+i>=total)//字符显示结束,无字符的行剩余部分显示空
{
wsprintf(temp," ");
cdc.SetTextColor(m_color_normaltext);
cdc.SetBkColor(m_color_normalback);
cdc.TextOut(i*24,rect.bottom-15*(lines-line),temp);
/*weiyi */ wsprintf(temp," ");
/*weiyi */ cdc.TextOut(rect.right-(m_bytesperline-i)*8-8,rect.bottom-15*(lines-line),temp);
}
else
{
wsprintf(temp,"%02X ",m_bytes[from+i]);
//strcat(buf1,temp);
//如果属于选中部分,则颜色需改变
if(m_selfrom!= -1 && from+i>=m_selfrom && from+i<=m_selto)
{
cdc.SetTextColor(m_color_selecttext);
cdc.SetBkColor(m_color_selectback);
}
else
{
cdc.SetTextColor(m_color_normaltext);
cdc.SetBkColor(m_color_normalback);
}
//如果要显示的字符为特殊字符,则设定为特殊的颜色
if(m_bytes[from+i]==m_specialbyte)
cdc.SetTextColor(m_color_keywordtext);
cdc.TextOut(i*24,rect.bottom-15*(lines-line),temp);
/*weiyi */ wsprintf(temp,"%c",m_bytes[from+i]<32?'.':m_bytes[from+i]);
/*weiyi */ cdc.TextOut(rect.right-(m_bytesperline-i)*8-8,rect.bottom-15*(lines-line),temp);
}
}
line++;
}
//离开临界区
LeaveCriticalSection(&cs);
pDC->BitBlt(0,0,rect.right,rect.bottom,&cdc,0,0,SRCCOPY);
cdc.SetTextColor(oldt);
cdc.SetBkColor(oldb);
cdc.SelectObject(of);
cdc.SelectObject(ob);
cdc.DeleteDC();
}
char *buf1[]={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
void CDataShowView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
/* if(m_bfrozen)
return;
char chTemp = nChar;
if(isxdigit(chTemp))
{
chTemp = toupper(nChar);
strManual+=chTemp;
if (strManual.GetLength()==2)
{
BYTE aaa = 16 * GetHexValue((char*)(const char*)strManual.Left(1))
+GetHexValue((char*)(const char*)strManual.Right(1));
ShowData(&aaa,1);
strManual.Empty();
}
}
else if (nChar == VK_BACK)
{
int nCountOfByte = m_bytes.GetSize();
if (nCountOfByte > 0)
{
EnterCriticalSection(&cs);
m_bytes.RemoveAt(nCountOfByte-1,1);
LeaveCriticalSection(&cs);
RedrawWindow();
}
else
strManual.Empty();
}
*/ CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
BYTE CDataShowView::GetHexValue(char* chTemp)
{
for(BYTE i=0;i<16;i++)
{
if (*chTemp == *buf1[i])
{
return i;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -