免责声明:内容仅供学习参考,请合法利用知识,禁止进行违法犯罪活动!
如果看不懂、不知道现在做的什么,那就跟着做完看效果
内容参考于:易道云信息技术研究院VIP课
上一个内容:29.数据推测功能的算法实现
码云地址(master 分支):https://gitee.com/dye_your_fingers/titan
码云版本号:5d3fa4236f208ffa47cc970274ef917447888f8a
代码下载地址,在 titan 目录下,文件名为:titan-数据搜索功能.zip
链接:https://pan.baidu.com/s/1W-JpUcGOWbSJmMdmtMzYZg
提取码:q9n5
--来自百度网盘超级会员V4的分享
HOOK引擎,文件名为:黑兔sdk升级版.zip
链接:https://pan.baidu.com/s/1IB-Zs6hi3yU8LC2f-8hIEw
提取码:78h8
--来自百度网盘超级会员V4的分享
以 29.数据推测功能的算法实现 它的代码为基础进行修改
当前程序不要点击,下图红框里的按钮,它会卡死
拦截的数据虽然是明文数据但它还是是数字,如下图
搜索功能就是通过写一个字符串去看看,这个字符串在哪一段数字里,比如搜索一个文字是 阿民我爱你 这几个字,搜索的时候就要把 阿民我爱你 这几个字转换成它对应的数字(十六进制编码,到内存里就自动转换成数字了)然后去列表中匹配,匹配到以后就把它选中,选中之后就分析
然后提示框它自己不会关闭,所以给窗口添加一个鼠标左键点击事件,通过左键点击让提示框关闭
然后添加一个Dialog
它的属性
它的控件
下拉菜单要点击下图红框位置,才能往下拖,不往下拖大了,会看不到下拉框里的数据
下拉框的属性
然后给它添加一个类
类名
然后给下拉框添加一个变量
然后给复选框添加一个变量
输入框添加变量
然后双击下图红框按钮给它添加点击事件
效果图:
然后就搜索到了字符串
DataAnlyDlg.cpp文件的修改:修改了OnCopyData函数,新加 Search函数、TxtBuff函数、OnBnClickedButton3函数(搜索按钮点击事件处理函数)
// DataAnlyDlg.cpp: 实现文件
//#include "pch.h"
#include "framework.h"
#include "DataAnly.h"
#include "DataAnlyDlg.h"
#include "afxdialogex.h"
#include "extern_all.h"
#include <fstream> // 引入io流,用于写文件#ifdef _DEBUG
#define new DEBUG_NEW
#endif// 用于应用程序“关于”菜单项的 CAboutDlg 对话框class CAboutDlg : public CDialogEx
{
public:CAboutDlg();// 对话框数据
#ifdef AFX_DESIGN_TIMEenum { IDD = IDD_ABOUTBOX };
#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持// 实现
protected:DECLARE_MESSAGE_MAP()
};CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);
}BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()// CDataAnlyDlg 对话框CDataAnlyDlg::CDataAnlyDlg(CWnd* pParent /*=nullptr*/): CDialogEx(IDD_DATAANLY_DIALOG, pParent), Pause(FALSE)
{m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CDataAnlyDlg::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);DDX_Control(pDX, IDC_LIST1, lstView);DDX_Check(pDX, IDC_CHECK1, Pause);
}BEGIN_MESSAGE_MAP(CDataAnlyDlg, CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDOK, &CDataAnlyDlg::OnBnClickedOk)ON_WM_COPYDATA()ON_BN_CLICKED(IDC_CHECK1, &CDataAnlyDlg::OnBnClickedCheck1)ON_BN_CLICKED(IDC_BUTTON1, &CDataAnlyDlg::OnBnClickedButton1)ON_BN_CLICKED(IDC_BUTTON4, &CDataAnlyDlg::OnBnClickedButton4)ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, &CDataAnlyDlg::OnNMCustomdrawList1)ON_BN_CLICKED(IDC_BUTTON5, &CDataAnlyDlg::OnBnClickedButton5)ON_NOTIFY(NM_CLICK, IDC_LIST1, &CDataAnlyDlg::OnNMClickList1)ON_WM_MOVE()ON_BN_CLICKED(IDC_BUTTON3, &CDataAnlyDlg::OnBnClickedButton3)
END_MESSAGE_MAP()// CDataAnlyDlg 消息处理程序BOOL CDataAnlyDlg::OnInitDialog()
{CDialogEx::OnInitDialog();// 将“关于...”菜单项添加到系统菜单中。// IDM_ABOUTBOX 必须在系统命令范围内。ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != nullptr){BOOL bNameValid;CString strAboutMenu;bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);ASSERT(bNameValid);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动// 执行此操作SetIcon(m_hIcon, TRUE); // 设置大图标SetIcon(m_hIcon, FALSE); // 设置小图标// ShowWindow(SW_MAXIMIZE); // 设置最大号启动// TODO: 在此添加额外的初始化代码SetListView(&lstView);wchar_t buff[0xFF];/*获取当前程序名,这个名字是全路径,不只有程序名字(就是.exe文件的名字)它还可以获取别人的名字详细看微软的MSDN里的说明(百度搜索 “GetModuleFileName msdn” 关键字,就可以搜索出微软MSDN的说明)*/ GetModuleFileName(0, buff, sizeof(buff));int i;for (i = 0xFF - 1; buff[i] != L'\\'; i--);buff[++i] = 0;AppPath.Format(L"%s", buff);// AfxMessageBox(AppPath);ConfigPath = AppPath + L"Config\\";DataPath = AppPath + L"Datas\\";DefConfigIni = ConfigPath + L"titan.ini";SetIni = AppPath + L"Set.ini";/*读取一个Set.ini文件Set.ini文件目前存放了,当程序启动时默认加载什么配置文件的数据*/GetPrivateProfileString(L"main", L"defPro", L"", buff, 0xFF, SetIni);// 获取配置文件,也就是现在的titan.ini这个文件DefConfigIni = AppPath + L"Config\\" + buff + L".ini";LoadConfig(DefConfigIni);/*lstView.InsertColumn(0, L"消息类型", 0, 70);lstView.InsertColumn(1, L"数据类型", 0, 70);lstView.InsertColumn(2, L"内容", 0, 700);lstView.InsertColumn(3, L"长度", 0, 50);lstView.InsertColumn(4, L"时间", 0, 130);这几行代码意思是设置表头*/lstView.InsertColumn(0, L"消息类型", 0, 70);lstView.InsertColumn(1, L"数据类型", 0, 70);lstView.InsertColumn(2, L"内容", 0, 700);lstView.InsertColumn(3, L"长度", 0, 50);lstView.InsertColumn(4, L"时间", 0, 130);struct tm newtiem {};time_t t;time(&t);localtime_s(&newtiem, &t); // 获取时间LogName.Format(L"%.4d-%.2d-%.2d %.2d-%.2d-%.2d.log", newtiem.tm_year + 1900, newtiem.tm_mon+1, newtiem.tm_mday, newtiem.tm_hour, newtiem.tm_min, newtiem.tm_sec);wndSet.Create(IDD_SET);wndData.Create(IDD_DIALOG1);// wndSearch.Create(IDD_SEARCH); // wndSearch要成模态窗口所以不要创建return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}void CDataAnlyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialogEx::OnSysCommand(nID, lParam);}
}// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。void CDataAnlyDlg::OnPaint()
{if (IsIconic()){CPaintDC dc(this); // 用于绘制的设备上下文SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);// 使图标在工作区矩形中居中int cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;// 绘制图标dc.DrawIcon(x, y, m_hIcon);}else{CDialogEx::OnPaint();}
}//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CDataAnlyDlg::OnQueryDragIcon()
{return static_cast<HCURSOR>(m_hIcon);
}void CDataAnlyDlg::OnBnClickedOk()
{wchar_t buff[]{ L"我爱阿民" };COPYDATASTRUCT copydata{};/*这里的0表示字符串0具体是什么要看 WM_COPYDATA消息处理函数怎样写的*/copydata.dwData = 0;copydata.cbData = sizeof(buff);copydata.lpData = buff;SendMessage(WM_COPYDATA, 0, (LPARAM)©data);
}BOOL CDataAnlyDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* p)
{UpdateData(true);if (Pause)return CDialogEx::OnCopyData(pWnd, p);PCOPYDATA pdata = (PCOPYDATA)p->lpData;struct tm newtiem {};localtime_s(&newtiem, &pdata->ltime); // 获取时间int type = p->dwData;if (type < MAX_MSG) {CString _result;CString _time;_time.Format(L"%.2d-%.2d-%.2d %.2d_%.2d_%.2d", newtiem.tm_year + 1900, newtiem.tm_mon + 1, newtiem.tm_mday, newtiem.tm_hour, newtiem.tm_min, newtiem.tm_sec);// 0数据包 1 char 2 wchar_tif (_MsgPtr[type].Type == 0) { // 数据包// 转换为buffunsigned char* buff = (unsigned char*)pdata->buff;CString _tmp;for (int i = 0; i < pdata->len; i++) {_tmp.Format(L"%.2X ", buff[i]);_result = _result + _tmp;}}CString wlen;PDATA _data = _MsgPtr[type].GetData(pdata->MsgType);bool Show = true;CString txtDetails;if (_data){Show = _data->IsUi;txtDetails.Format(L"%s(%X)", _data->Name, pdata->MsgType);}else {txtDetails.Format(L"%X", pdata->MsgType);}if (Show) {lstView.InsertItem(0, _MsgPtr[type].txtName);lstView.SetItemData(0, (DWORD_PTR)_data);lstView.SetItemText(0, 1, txtDetails);lstView.SetItemText(0, 2, _result);lstView.SetItemText(0, 4, _time);wlen.Format(L"%d", pdata->len);lstView.SetItemText(0, 3, wlen);}CString _log;CString _logfile;_log.Format(L"[%s][%s][%d][%s][%s]", _MsgPtr[type].txtName, _time.GetBuffer(), pdata->len, txtDetails, _result);_logfile = DataPath + productName + L"\\" + LogName;if(_MsgPtr[type].IsTxt) Savelog(_log.GetBuffer(), _logfile.GetBuffer()); // 写到文件if ((_data)&&(_data->IsTxt)) { // 所有这里存放所有类型的数据包_logfile.Format(L"%s%s\\%s.log", DataPath, productName, _MsgPtr[type].txtDefine);Savelog(_log.GetBuffer(), _logfile.GetBuffer());}if ((_data) && (_data->IsTxtAlone)) { // 单独输出,也就是相同类型的数据包放到一个文件中_logfile.Format(L"%s%s\\%s\\%X.log", DataPath, productName, _MsgPtr[type].txtDefine, _data->Id);Savelog(_log.GetBuffer(), _logfile.GetBuffer());}}else {AfxMessageBox(L"接收到无法识别的数据,需要重新根据配置生成头文件!");} return CDialogEx::OnCopyData(pWnd, p);
}void CDataAnlyDlg::OnBnClickedCheck1()
{
}void CDataAnlyDlg::OnBnClickedButton1()
{lstView.DeleteAllItems();
}void CDataAnlyDlg::OnBnClickedButton4()
{wndSet.ShowWindow(TRUE);wndSet.LoadConfig(DefConfigIni, productName);
}void CDataAnlyDlg::LoadConfig(CString inifile) {/*从配置文件中读取一个数据,意思是从main下的count读取一个数字int类型的数字GetPrivateProfileInt用来获取.ini文件里的内容,最后的Int表示读取的数据类型Int就表示读取int类型,String表示读取String类型,也就是一个字符串GetPrivateProfileString(L"main", L"title", L"", buff, 0xFF, inifile);里的L""是默认值GetPrivateProfileInt(L"main", L"count", 0, inifile);里的0是默认值默认值的意思,如果读取不到数据就把默认值返回给我们*/int icount = GetPrivateProfileInt(L"main", L"count", 0, inifile);if (_MsgPtr) delete[] _MsgPtr;if (icount < 1) return;MAX_MSG = icount;_MsgPtr = new MSG_DEC[icount];lstView.DeleteAllItems();wchar_t buff[0xFF];GetPrivateProfileString(L"main", L"title", L"", buff, 0xFF, inifile);this->SetWindowText(buff);GetPrivateProfileString(L"main", L"name", L"", buff, 0xFF, inifile);productName = buff;WritePrivateProfileString(L"main", L"defPro", productName, SetIni);/*CString App;CString AppData;CString _Name;它是用来初始化字符串的,利用 Foramt 函数把从.ini文件里获取的数据全部转成字符串*/CString App;CString AppData;CString _Name;for (int i = 0; i < icount; i++){App.Format(L"Message_%d", i);GetPrivateProfileString(App, L"name", L"", _MsgPtr[i].txtName, 0xFF, inifile);_Name.Format(L"%s", _MsgPtr[i].txtName);GetPrivateProfileString(App, L"define", L"", _MsgPtr[i].txtDefine, 0xFF, inifile);GetPrivateProfileString(App, L"type", L"", buff, 0xFF, inifile);_MsgPtr[i].Type = TxtToType(buff);GetPrivateProfileString(App, L"istxt", L"", buff, 0xFF, inifile);_MsgPtr[i].IsTxt = TxtToBool(buff);;int lcount = GetPrivateProfileInt(App, L"count", 0, inifile);_MsgPtr[i].Datas = new DATA_DEC[lcount];_MsgPtr[i].DataCount = lcount;for (int j = 0; j < lcount; j++){AppData.Format(L"%s_%d", _Name.GetBuffer(), j);PDATA dataDec = &(_MsgPtr[i].Datas[j]);dataDec->Msg = &(_MsgPtr[i]);GetPrivateProfileString(AppData, L"id", L"0", buff, 0xFF, inifile);dataDec->Id = _wtoi(buff);GetPrivateProfileString(AppData, L"name", L"0", dataDec->Name, 0xFF, inifile);GetPrivateProfileString(AppData, L"backcolor", L"0", buff, 0xFF, inifile);dataDec->BackColor = TxtToColor(buff);GetPrivateProfileString(AppData, L"fontcolor", L"0", buff, 0xFF, inifile);dataDec->FontColor = TxtToColor(buff);GetPrivateProfileString(AppData, L"isui", L"0", buff, 0xFF, inifile);dataDec->IsUi = TxtToBool(buff);GetPrivateProfileString(AppData, L"istxt", L"0", buff, 0xFF, inifile);dataDec->IsTxt = TxtToBool(buff);GetPrivateProfileString(AppData, L"istxtalone", L"0", buff, 0xFF, inifile);dataDec->IsTxtAlone = TxtToBool(buff);}}/*要做一个数据包保存到硬盘功能mainlog = DataPath + productName; 是用来获取保存到什么文件的也就是一个文件的绝对路径,这个文件用来保存数据包*/mainlog = DataPath + productName;
}void CDataAnlyDlg::OnNMCustomdrawList1(NMHDR* pNMHDR, LRESULT* pResult)
{LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);// TODO: 在此添加控件通知处理程序代码*pResult = 0;NMLVCUSTOMDRAW* lp = (NMLVCUSTOMDRAW*)pNMHDR;NMCUSTOMDRAW& ncmd = lp->nmcd;switch (ncmd.dwDrawStage){case CDDS_PREPAINT: {*pResult = CDRF_NOTIFYITEMDRAW;break;}case CDDS_ITEMPREPAINT: {PDATA _data = (PDATA)lstView.GetItemData(ncmd.dwItemSpec);if (_data) {lp->clrTextBk = _data->BackColor;lp->clrText = _data->FontColor;*pResult = CDRF_DODEFAULT;}break;}}}void CDataAnlyDlg::Savelog(const wchar_t* logs, const wchar_t* file)
{// 设置追加写入文件std::wofstream ofs(file, std::ios::app);// 设置语言集,防止写中文出错ofs.imbue(std::locale("chs")); if (ofs.bad())return;ofs << logs << std::endl;ofs.close();
}// 加载按钮
void CDataAnlyDlg::OnBnClickedButton5()
{LoadConfig(DefConfigIni);
}void CDataAnlyDlg::OnNMClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);// TODO: 在此添加控件通知处理程序代码*pResult = 0;NM_LISTVIEW* p = (NM_LISTVIEW*)pNMHDR;if (p->iItem != -1) {wndData.ShowWindow(TRUE);wndData.m_Edit.SetWindowTextW(lstView.GetItemText(p->iItem,2));}
}void CDataAnlyDlg::OnMove(int x, int y)
{CDialogEx::OnMove(x, y);CRect rect_main, rect_data;GetWindowRect(&rect_main);wndData.GetWindowRect(&rect_data);/*这个计算的原因是,GetWindowRect函数获取的left、right、top、bottom是坐标不是大小,rect_data.right - rect_data.left;这样一减就能得到窗口的宽度(这里的宽度是差值,left到right的差值)然后由于它们都是坐标,所以计算的结果也是坐标,就是宽度所在的坐标然后再由left坐标到right坐标绘制窗口,就能保证窗口宽度不会变化了*/unsigned w = rect_data.right - rect_data.left;rect_data.left = rect_main.right-15;rect_data.right = rect_data.left + w;rect_data.top = rect_main.top;rect_data.bottom = rect_main.bottom;wndData.MoveWindow(&rect_data, TRUE);
}void CDataAnlyDlg::OnBnClickedButton3()
{if (wndSearch.DoModal() == IDOK) {Search(wndSearch.txtSearch, wndSearch.selected, wndSearch.BHEX);}
}// 搜索
void CDataAnlyDlg::Search(CString& txt, SEARCH_TYPE type, BOOL Hex)
{CStringA tmpA;CString tmp;int len{};char* buff{};wchar_t* wbuff{};int val;float fval;double dbval;long long llval;short stval;char cval;int ilen{};_locale_t lcl = _create_locale(LC_ALL, "chs"); // 设置时区为中国switch (type){case SEARCH_TYPE::ASCII_STRING:tmpA = txt;buff = tmpA.GetBuffer();for (;buff[ilen]; ilen++);//当前是推断数据为ASCII类型,所以要使用单字节的形式转换为CString类型tmp = TxtBuff((unsigned char*)buff, ilen);break;case SEARCH_TYPE::UTF16_STRING:wbuff = txt.GetBuffer();for (; wbuff[ilen]; ilen++);tmp = TxtBuff((unsigned char*)wbuff, ilen);// 双字节数据转,CStringbreak;case SEARCH_TYPE::DOUBLE:dbval = _wtof_l (txt, lcl);tmp = TxtBuff((unsigned char*)&dbval, 8);break;case SEARCH_TYPE::INT64:if(Hex)llval = _wcstoll_l(txt, NULL , 16, lcl);else llval = _wtoll(txt);tmp = TxtBuff((unsigned char*)&llval, 8);break;case SEARCH_TYPE::INT32:if (Hex)val = wcstol(txt, NULL, 16);elseval = _wtoi(txt);tmp = TxtBuff((unsigned char*)&val, 4);break;case SEARCH_TYPE::FLOAT:if (Hex)fval = wcstol(txt, NULL, 16);elsefval = _wtoi(txt);tmp = TxtBuff((unsigned char*)&fval, 4);break;case SEARCH_TYPE::SHORT:if (Hex)stval = wcstol(txt, NULL, 16);elsestval = _wtoi(txt);tmp = TxtBuff((unsigned char*)&stval, 2);break;case SEARCH_TYPE::CHAR:if (Hex)cval = wcstol(txt, NULL, 16);elsecval = _wtoi(txt);tmp = TxtBuff((unsigned char*)&cval, 1);break;case SEARCH_TYPE::TXT:tmp = txt;break;default:break;}int nsel = lstView.GetSelectionMark();CString txtRead;if (nsel < 0) nsel = 0;else nsel++;for (int i = nsel; i< lstView.GetItemCount(); i++){// lstView.GetItemText(i, 2); 获取 lstView列表第i行第3列txtRead = lstView.GetItemText(i, 2);if (txtRead.Find(tmp) > -1) { // 查找字符串// 更改列表控件中的项的状态lstView.SetItemState(i, LVNI_FOCUSED | LVNI_SELECTED, LVNI_FOCUSED | LVNI_SELECTED);// 确保列表视图项至少部分可见,可以定位到选中的位置lstView.EnsureVisible(i, FALSE);// 设置焦点,不设置焦点无法选中行(高亮行)lstView.SetFocus();// return; // 放开return一次只能搜索出一个数据,注释return一次可以搜索出所有满足条件的数据}}
}CString CDataAnlyDlg::TxtBuff(unsigned char* buff, unsigned len)
{CString _result;CString _tmp;for (int i = 0; i < len; i++){_tmp.Format(L"%.2X ", buff[i]);_result = _result + _tmp;}return _result;
}
DataAnlyDlg.h文件的修改:新加 Search函数、TxtBuff函数、OnBnClickedButton3函数、wndSearch变量
// DataAnlyDlg.h: 头文件
//#pragma once
#include "CSet.h"
#include "MSG_DEC.h"
#include "CWndData.h"
#include "CWndSearch.h"typedef struct COPY_DATA {time_t ltime; // 时间unsigned MsgType; // 数据类型unsigned len; // 数据长度char buff[0x2000]; // 数据
}*PCOPYDATA;// CDataAnlyDlg 对话框
class CDataAnlyDlg : public CDialogEx
{
// 构造
public:CDataAnlyDlg(CWnd* pParent = nullptr); // 标准构造函数// 对话框数据
#ifdef AFX_DESIGN_TIMEenum { IDD = IDD_DATAANLY_DIALOG };
#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持// 实现
protected:HICON m_hIcon;// 生成的消息映射函数virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();DECLARE_MESSAGE_MAP()
public:CListCtrl lstView;afx_msg void OnBnClickedOk();afx_msg BOOL OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct);BOOL Pause;afx_msg void OnBnClickedCheck1();afx_msg void OnBnClickedButton1();CSet wndSet;CWndData wndData;CWndSearch wndSearch;afx_msg void OnBnClickedButton4();void CDataAnlyDlg::LoadConfig(CString inifile);afx_msg void OnNMCustomdrawList1(NMHDR* pNMHDR, LRESULT* pResult);CString LogName; // 数据包写到硬盘上的文件名CString mainlog; // 数据包保存目录CString productName; // 当前titlevoid Savelog(const wchar_t* logs, const wchar_t* file);afx_msg void OnBnClickedButton5();afx_msg void OnNMClickList1(NMHDR* pNMHDR, LRESULT* pResult);afx_msg void OnMove(int x, int y);afx_msg void OnBnClickedButton3();void Search(CString& txt, SEARCH_TYPE type, BOOL Hex);CString TxtBuff(unsigned char* buff, unsigned len);
};
CWndData.cpp文件的修改:新加 OnLButtonUp函数(窗口鼠标左键单机事件处理函数)
// CWndData.cpp: 实现文件
//#include "pch.h"
#include "DataAnly.h"
#include "CWndData.h"
#include "afxdialogex.h"// CWndData 对话框IMPLEMENT_DYNAMIC(CWndData, CDialogEx)CWndData::CWndData(CWnd* pParent /*=nullptr*/): CDialogEx(IDD_DIALOG1, pParent)
{}CWndData::~CWndData()
{
}void CWndData::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);DDX_Control(pDX, IDC_EDIT1, m_Edit);
}BOOL CWndData::OnInitDialog()
{/*为了处理数据方便,我要给定时器一个成员函数然后成员函数它不满足 TIMERPROC 这个类型,编译器也不让我们强制转换所以要用一个联合体封装一下,联合体里的变量都共用一个内存内存大小由联合体里最大的变量决定*/union {TIMERPROC _address;void (CWndData::* _classProc)(HWND, UINT, CWndData*, DWORD);}v;v._classProc = &CWndData::loops;CDialogEx::OnInitDialog();//hTips = 0;/*定时器执行的函数stdcall,让它调用的函数是类的成员函数定时器调用的时候可能不会有this(也就是ecx的值不是类对象地址)所以这里要手动的把this传递过去这样可以方便在函数里对数据进行操作*/ ::SetTimer(m_hWnd, (UINT_PTR)this, 100, v._address);return TRUE;
}void CWndData::loops(HWND, UINT, CWndData* _this, DWORD)
{int nstart = 0;// 选中的内容起始下标int nend = 0; // 选中的内容结束下标_this->m_Edit.GetSel(nstart, nend); // 获取选中的文字int ncount = nend - nstart;if (ncount > 1) {CString txt;CString tmp;_this->m_Edit.GetWindowText(txt); // 获取编辑框里的内容tmp = txt.Mid(nstart, ncount);tmp.Replace(L" ", L""); // 把空格替换成空字符if (_this->lastTxt != tmp) {int lenth = tmp.GetLength();if (lenth % 2 == 0) { // 必须是2的倍数,如果不是就说明没有选择全_this->lastTxt = tmp;_this->ShowTips();}}}
}TOOLINFO tips; // 提示框结构体void CWndData::ShowTips()
{tips.cbSize = sizeof(tips);// 固定写法,也就是必须这样写,必须有这一句unsigned char ExDataBuff[0x1000]{};int ilenth = lastTxt.GetLength() / 2; // 字符串是用2字节显示一个内容if (ilenth<=0) {}CString tmp;for (int i = 0; i < ilenth; i++) {tmp = lastTxt.Mid(i*2, 2);// 这里的Mid函数的意思是从i*2下标位置往后取2个字符// wcstol函数降字符串转成long类型,这里将字符转成16进制的long类型ExDataBuff[i] = (unsigned char)wcstol(tmp, NULL, 16);}wchar_t* wBuff = (wchar_t*)ExDataBuff;CStringA Buff = (char*)ExDataBuff;time_t* _time;long long* llRead;double* dbRead;int* ntRead;float* fRead;short* stRead;CString txtUnicode, txtAscii,txttm, txtll, txtdb, txtnt, txtfloat, txtst,tmpR;txtUnicode.Format(L"%s", wBuff);txtAscii = Buff;// tmp.Format(L"\r\nUTF16:%s\r\nAscII:%s", wBuff, aBuff.GetBuffer());for (INT i = 0; i < ilenth; i++){// 推测8字节数据if ((ilenth - i > 7)&&(i%8==0)) {llRead = (long long*)&ExDataBuff[i];dbRead = (double*)&ExDataBuff[i];_time = (time_t*)&ExDataBuff[i];tmpR.Format(L"[%i64d]", llRead[0]);txtll += tmpR;tmpR.Format(L"[%1f]", dbRead[0]);txtdb += tmpR;tmpR.Format(L"[%s]", TimeToTxt(_time));txttm += tmpR;}// 推测4字节数据if ((ilenth - i > 3) && (i % 4 == 0)) {fRead = (float*)&ExDataBuff[i];ntRead = (int*)&ExDataBuff[i];tmpR.Format(L"[%d]", ntRead[0]);txtnt += tmpR;tmpR.Format(L"[%f]", fRead[0]);txtfloat += tmpR;}// 推测2字节数据if ((ilenth - i > 1) && (i % 2 == 0)) {stRead = (short*)&ExDataBuff[i];tmpR.Format(L"[%d]", stRead[0]);txtst += tmpR;}}if (txttm != "")tmp = tmp + L"\r\n[time]" + txttm;if (txtll != "")tmp = tmp + L"\r\n[int64]" + txtll;if (txtdb != "")tmp = tmp + L"\r\n[double]" + txtdb;if (txtfloat != "")tmp = tmp + L"\r\n[float]" + txtfloat;if (txtnt != "")tmp = tmp + L"\r\n[int]" + txtnt;if (txtst != "")tmp = tmp + L"\r\n[short]" + txtst;tmp = L"";int imax = txtUnicode.GetLength();if (imax > 0) {tmp = tmp + L"\r\n[Utf16]:" + txtUnicode;imax += 8;}int icount = txtAscii.GetLength();if (icount > 0) {tmp = tmp + L"\r\n[Ascii]:" + txtAscii;icount += 8;if (icount > imax) {imax = icount;}}icount = txttm.GetLength();if (icount > 0) {tmp = tmp + L"\r\n[time]:" + txttm;icount += 7;if (icount > imax) {imax = icount;}}icount = txtll.GetLength();if (icount > 0) {tmp = tmp + L"\r\n[int64]:" + txtll;icount += 8;if (icount > imax) {imax = icount;}}icount = txtdb.GetLength();if (icount > 0) {tmp = tmp + L"\r\n[double]:" + txtdb;icount += 9;if (icount > imax) {imax = icount;}}icount = txtfloat.GetLength();if (icount > 0) {tmp = tmp + L"\r\n[float]:" + txtfloat;icount += 8;if (icount > imax) {imax = icount;}}icount = txtnt.GetLength();if (icount > 0) {tmp = tmp + L"\r\n[int]:" + txtnt;icount += 6;if (icount > imax) {imax = icount;}}icount = txtst.GetLength();if (icount > 0) {tmp = tmp + L"\r\n[short]:" + txtst;icount += 8;if (icount > imax) {imax = icount;}}CString _head('=', imax);_head = _head + tmp;tips.lpszText = _head.GetBuffer();// 设置提示框的内容DWORD lPoint = GetMessagePos();// 获取鼠标位置,GetMessagePos函数返回值是一个DWORD类型,高位是x坐标,低位是y坐标if (!hTips) {/*CreateWindow函数的参数说明:第一个参数是窗口注册的类名,是一个字符串,现在写的 TOOLTIPS_CLASS 是一个提示框的类名,由Windows提供的公共控件由Windows对它们进行 RegisterClass 或 RegisterClassEx操作,所以这里可以直接执行CreateWindow操作第二个参数是窗口名称(就是窗口左上角的文字),由于是提示框用来显示描述的提示框,所以写的NULL第三个参数是正在创建的窗口的样式,详情看MSDN(MSDN是微软文档)(去MSDN里搜索 CreateWindowW或者CreateWindowA)第四个参数是窗口初始水平位置,也就是x坐标第五个参数是窗口垂直位置,也就是y坐标第六个参数是窗口的宽度第七个参数是窗口的高度第八个参数是所创建的窗口的父窗口或所有者窗口的句柄,也就是用来给它指定父窗口第九个参数菜单的句柄,没有菜单所以写0第十个参数是要与窗口关联的模块实例的句柄,这里传递的是AfxGetInstanceHandle函数,它会返回当前程序的句柄第十一个参数是给窗口传递的数据,是一个结构体,详情去MSDN看返回值是创建好的窗口句柄*/hTips = CreateWindow(TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,0,0,0,0,m_Edit.m_hWnd,0,AfxGetInstanceHandle(), 0);if (hTips) {// 修改窗口,修改的目的是为了防止提示框被其它窗口遮盖,要确保这个提示框要在最顶层::SetWindowPos(hTips, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);/*给提示框发送 TTM_ADDTOOL 消息展示TOOLINFO里的lpszText它的内容详细去MSDN搜索 TTM_ADDTOOL 查看详细介绍SendMessage的第三个参数和第四个参数可以看做成是 TTM_ADDTOOL 宏的第一个参数和第二个参数也就是把 TTM_ADDTOOL它当作函数看待,忽略SendMessage这样的思路去看MSDNTTM_ADDTOOL消息处理Windows已经做好了,只需要按照MSDN文档写的说明去传参就好效果就是设置第二行-第N行的数据*/::SendMessage(hTips, TTM_ADDTOOL, (WPARAM)1, (LPARAM)&tips);/*发送 TTM_SETTITLE 消息,提示框会设置图标和标题,第四个参数是图标,详情去MSDN搜索 TTM_SETTITLEMSDN里面介绍了发送 TTM_SETTITLE 消息时,SendMessage函数第三个参数与第四个参数是什么SendMessage的第三个参数和第四个参数可以看做成是 TTM_SETTITLE 宏的第一个参数和第二个参数TTM_SETTITLE消息处理Windows已经做好了,只需要按照MSDN文档写的说明去传参就好效果就是设置第一行的内容*/::SendMessage(hTips, TTM_SETTITLE, 0, (LPARAM)L"可能的内容");}}if (hTips) {// GetWindowRect(&tips.rect);// 设置提示框提示的文本(或者说是修改文本)::SendMessage(hTips, TTM_UPDATETIPTEXT, (WPARAM)FALSE, (LPARAM)&tips);/*设置提示框的坐标,详细信息去MSDN搜索 TTM_TRACKPOSITION,扩展:MAKELONG宏可以设置高位数据与低位数据使用例子:MAKELONG(500, 50) 500就是高位数据,50就是低位数据MAKELONG宏返回一个DWORD类型(4字节的数字),它的第一个参数是这个4字节数字高位2字节的数据第二个参数是低位2字节的数据效果就是设置显示位置*/::SendMessage(hTips, TTM_TRACKPOSITION, (WPARAM)FALSE, lPoint);/*显示窗口,详细说明还是去MSDN,MSDN操作方式还是搜索 TTM_TRACKACTIVATE说明的看法与上面三个一样(TTM_SETTITLE、TTM_ADDTOOL、HWND_TOPMOST)*/::SendMessage(hTips, TTM_TRACKACTIVATE, (WPARAM)TRUE, (LPARAM)&tips);}
}CString CWndData::TimeToTxt(time_t* _tm)
{CString rt;struct tm newtiem {};localtime_s(&newtiem, _tm); // 获取时间rt.Format(L"%.4d-%.2d-%.2d %.2d:%.2d:%.2d", newtiem.tm_year + 1900, newtiem.tm_mon + 1, newtiem.tm_mday, newtiem.tm_hour, newtiem.tm_min, newtiem.tm_sec);return rt;
}BEGIN_MESSAGE_MAP(CWndData, CDialogEx)ON_WM_LBUTTONUP()
END_MESSAGE_MAP()// CWndData 消息处理程序void CWndData::OnLButtonUp(UINT nFlags, CPoint point)
{if (hTips) {// 停用提示框(隐藏提示框)::SendMessage(hTips, TTM_TRACKACTIVATE, (WPARAM)FALSE, (LPARAM)&tips);}CDialogEx::OnLButtonUp(nFlags, point);
}
CWndData.h文件的修改:新加 OnLButtonUp函数(窗口鼠标左键单机事件处理函数)
#pragma once// CWndData 对话框class CWndData : public CDialogEx
{DECLARE_DYNAMIC(CWndData)public:CWndData(CWnd* pParent = nullptr); // 标准构造函数virtual ~CWndData();// 对话框数据
#ifdef AFX_DESIGN_TIMEenum { IDD = IDD_DIALOG1 };
#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持DECLARE_MESSAGE_MAP()
public:CEdit m_Edit;HWND hTips{};virtual BOOL OnInitDialog();void loops(HWND, UINT, CWndData* _this, DWORD);void ShowTips();CString lastTxt;CString TimeToTxt(time_t* _tm);// 事件类型转字符串afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
};
新加CWndSearch.h文件
#pragma once// CWndSearch 对话框
enum class SEARCH_TYPE :int {ASCII_STRING,UTF16_STRING,DOUBLE,INT64,INT32,FLOAT,SHORT,CHAR,TXT,MAX // 这个是用来确定它有多少内容
};class CWndSearch : public CDialogEx
{DECLARE_DYNAMIC(CWndSearch)public:wchar_t SearchTypeName[(int)SEARCH_TYPE::MAX][0xFF]{L"ASCII 字符串",L"UTF16 字符串",L"double",L"int64",L"int",L"short",L"float",L"char",L"内容"};CWndSearch(CWnd* pParent = nullptr); // 标准构造函数virtual ~CWndSearch();// 对话框数据
#ifdef AFX_DESIGN_TIMEenum { IDD = IDD_SEARCH };
#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持DECLARE_MESSAGE_MAP()
public:CComboBox cmbType;BOOL BHEX;CString txtSearch;afx_msg void OnBnClickedOk();virtual BOOL OnInitDialog();SEARCH_TYPE selected;
};
新加CWndSearch.cpp文件
// CWndSearch.cpp: 实现文件
//#include "pch.h"
#include "DataAnly.h"
#include "CWndSearch.h"
#include "afxdialogex.h"// CWndSearch 对话框IMPLEMENT_DYNAMIC(CWndSearch, CDialogEx)CWndSearch::CWndSearch(CWnd* pParent /*=nullptr*/): CDialogEx(IDD_SEARCH, pParent), BHEX(FALSE), txtSearch(_T(""))
{}CWndSearch::~CWndSearch()
{
}void CWndSearch::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);DDX_Control(pDX, IDC_COMBO3, cmbType);DDX_Check(pDX, IDC_CHECK1, BHEX);DDX_Text(pDX, IDC_EDIT1, txtSearch);
}BEGIN_MESSAGE_MAP(CWndSearch, CDialogEx)ON_BN_CLICKED(IDOK, &CWndSearch::OnBnClickedOk)
END_MESSAGE_MAP()// CWndSearch 消息处理程序void CWndSearch::OnBnClickedOk()
{UpdateData(TRUE);int nsel = cmbType.GetCurSel(); // 获取下拉框选中的索引if (nsel < 0) return;if (txtSearch.IsEmpty())return;selected = (SEARCH_TYPE)nsel;CDialogEx::OnOK();
}BOOL CWndSearch::OnInitDialog()
{CDialogEx::OnInitDialog();for (int i = 0; i < (int)SEARCH_TYPE::MAX; i++){cmbType.AddString(SearchTypeName[i]); // 下拉框添加数据}return TRUE;
}