前端之家收集整理的这篇文章主要介绍了
敏感词提示及管理,增删改,并保存至xml,使用tinyxml,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
// SenStivWrdDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SenStivWrd.h"
#include "SenStivWrdDlg.h"
#include "afxdialogex.h"
#include "ModifyDlg.h"
#include "AddDlg.h"
#include "tinystr.h"
#include "tinyxml.h"
#include <xstring>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBox };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg,CDialogEx)
END_MESSAGE_MAP()
// CSenStivWrdDlg dialog
CSenStivWrdDlg::CSenStivWrdDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CSenStivWrdDlg::IDD,pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSenStivWrdDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX,IDC_RICHEDIT21,m_RichEditCtrl);
DDX_Control(pDX,IDC_LIST1,m_ListCtrl);
}
BEGIN_MESSAGE_MAP(CSenStivWrdDlg,CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDSEND,&CSenStivWrdDlg::OnBnClickedSend)
ON_BN_CLICKED(IDC_ADD,&CSenStivWrdDlg::OnBnClickedAdd)
ON_BN_CLICKED(IDC_MODIF,&CSenStivWrdDlg::OnBnClickedModif)
ON_BN_CLICKED(IDC_DEL,&CSenStivWrdDlg::OnBnClickedDel)
ON_BN_CLICKED(IDC_SAVE,&CSenStivWrdDlg::OnBnClickedSave)
END_MESSAGE_MAP()
// CSenStivWrdDlg message handlers
BOOL CSenStivWrdDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
flag = 0;
// Add "About..." menu item to system menu.
// IDM_ABOUTBox must be in the system command range.
ASSERT((IDM_ABOUTBox & 0xFFF0) == IDM_ABOUTBox);
ASSERT(IDM_ABOUTBox < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
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);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon,TRUE); // Set big icon
SetIcon(m_hIcon,FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CSenStivWrdDlg::OnSysCommand(UINT nID,LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBox)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID,lParam);
}
}
// If you add a minimize button to your dialog,you will need the code below
// to draw the icon. For MFC applications using the document/view model,// this is automatically done for you by the framework.
void CSenStivWrdDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND,reinterpret_cast<WPARAM>(dc.GetSafeHdc()),0);
// Center icon in client rectangle
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;
// Draw the icon
dc.DrawIcon(x,y,m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSenStivWrdDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CSenStivWrdDlg::OnBnClickedSend()
{
// TODO: Add your control notification handler code here
int nCont = m_ListCtrl.GetItemCount();
int index = 0;
long n = 0;
for(index = 0; index < nCont; index++)
{
CString str = m_ListCtrl.GetItemText(index,0);
FINDTEXTEX ft;
ft.chrg.cpMin = 0;
ft.chrg.cpMax = -1;
ft.lpstrText = str;
n = m_RichEditCtrl.FindText(FR_DOWN,&ft); // FR_DOWN从前往后、匹配大小写、整词|FR_MATCHCASE|FR_WHOLEWORD
if (n != -1)
{
MessageBox(_T("您发送内容中含有关健敏感字!"));
break;
}
}
}
// 增加敏感词
void CSenStivWrdDlg::OnBnClickedAdd()
{
// TODO: Add your control notification handler code here
CAddDlg AddDlg;
AddDlg.DoModal();
int Item = m_ListCtrl.InsertItem(flag,AddDlg.m_AddWrd);
//MessageBox(m_ListCtrl.GetItemText(Item,0));
//int index = m_ListCtrl.GetItemCount();
//m_ListCtrl.InsertItem(index,_T("File Name"));
flag++;
}
// 更改敏感词
void CSenStivWrdDlg::OnBnClickedModif()
{
// TODO: Add your control notification handler code here
int nIdx = m_ListCtrl.GetNextItem(-1,LVNI_SELECTED); // -1表示匹配的第一个项目
if (nIdx != -1)
{
CModifyDlg Dlg; // 弹出修改敏感词对话框
Dlg.DoModal();
m_ListCtrl.SetItemText(nIdx,Dlg.m_Modifywrd); // 将item中的text改为修改敏感词对话框中的输入内容
}
}
// 删除敏感词
void CSenStivWrdDlg::OnBnClickedDel()
{
// TODO: Add your control notification handler code here
int nIdx = m_ListCtrl.GetNextItem(-1,LVNI_SELECTED); // -1表示匹配的第一个项目
if (nIdx != -1)
{
m_ListCtrl.DeleteItem(nIdx);
}
}
// 保存关键词到xml文件
void CSenStivWrdDlg::OnBnClickedSave()
{
// TODO: Add your control notification handler code here
TiXmlDocument* pDoc = new TiXmlDocument; //定义一个TiXmlDocument类指针
TiXmlElement *pRootEle = new TiXmlElement("SensitiveWord"); // 生成根节点指针做参数
pDoc->LinkEndChild(pRootEle);
int nCont = m_ListCtrl.GetItemCount();
int index = 0;
long n = 0;
for(index = 0; index < nCont; index++)
{
//TiXmlElement *pEle = new TiXmlElement("StvWrd");
CString str = m_ListCtrl.GetItemText(index,0); // 得到listctrl上的敏感词
// CString转换为const char*
int nTextLen = WideCharToMultiByte (CP_UTF8,str,-1,NULL,NULL);
char* pElementText = new char[nTextLen + 1];
memset((void*)pElementText,sizeof(char)*(nTextLen + 1));
WideCharToMultiByte( CP_UTF8,pElementText,nTextLen,NULL ); // 宽字节转换为多字节
TiXmlElement *pEle = new TiXmlElement(pElementText); // 将敏感词作为各个节点名称保存到xml文件
//pEle->SetAttribute("Value",pElementText); // 敏感词作为节点Value属性值
pRootEle->LinkEndChild(pEle);
}
pDoc->SaveFile("1.xml");
}