MFC电子词典


// dictDlg.cpp: 实现文件
//MFC 创建MFC项目 -->对话框项目  MFC的ui界面在 视图->资源视图->Dialog里面

#include "pch.h"
#include "framework.h"
#include "dict.h"
#include "dictDlg.h"
#include "afxdialogex.h"
#pragma warning(disable:4996)
//屏蔽警告
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

struct dict
{
 char* key;
 char* content;
 struct dict* next;//指向链表下一个节点的指针
};

//打开字典文件,并读取文件内容
int open_dict(struct dict** p, const char* dict_filename)//open dict.txt,and read dict
{
 FILE* pfile = fopen(dict_filename, "r");//只读方式打开文件
 if (pfile == NULL)
  return 0;//打开文件失败,函数返回

 char buf[2048] = { 0 };
 size_t len = 0;
 int i = 0;//计数器,记录读到到的词条总数

 *p = (struct dict*)malloc(sizeof(struct dict));//分配链表首节点内存
 memset(*p, 0, sizeof(struct dict));

 struct dict* pD = *p;//pD指向链表首地址
 while (!feof(pfile))//循环读取文件,直到文件末尾
 {
  memset(buf, 0, sizeof(buf));
  fgets(buf, sizeof(buf), pfile);//读取文件一行
  len = strlen(buf);//得到读取到字符串长度
  if (len > 0)
  {
   pD->key = (char*)malloc(len);//根据字符串长度分配内存
   memset(pD->key, 0, len);
   strcpy(pD->key, &buf[1]);//将读取到的内容拷贝到key中
  }

  memset(buf, 0, sizeof(buf));
  fgets(buf, sizeof(buf), pfile);
  len = strlen(buf);
  if (len > 0)
  {
   pD->content = (char*)malloc(len);
   memset(pD->content, 0, len);
   strcpy(pD->content, &buf[6]);
  }
  pD->next = (struct dict*)malloc(sizeof(struct dict));//为链表的下一个节点分配内存
  memset(pD->next, 0, sizeof(struct dict));

  pD = pD->next;//将pD指向下一个节点位置
  i++;
 }
 fclose(pfile);//关闭字典文件
 return i;//返回读取到的字典词条数
}

//根据关键字key,在字典中查找内容
int search_dict(const struct dict* p, int size, const char* key, char* content)
{
 const struct dict* pD = p;
 while (pD)//遍历字典
 {
  if ((pD->key) && (pD->content))
  {
   if (strncmp(pD->key, key, strlen(key)) == 0)
   {
    strcpy(content, pD->content);
    return 1;//找到符合条件记录,返回1
   }
  }
  pD = pD->next;//指向链表下一个节点位置
 }
 return 0;//没有找到符合条件记录,返回0
}

//释放链表内存
void free_dict(struct dict* p, int size)
{
 struct dict* pD = p;
 while (pD)
 {
  if (pD->key)//删除链表节点中key成员内存
   free(pD->key);
  if (pD->content)//删除链表节点中content成员内存
   free(pD->content);

扫描二维码关注公众号,回复: 6493843 查看本文章

  struct dict* tmp = pD->next;//保存链表下一个节点的地址
  free(pD);//删除链表当前节点
  pD = tmp;//p指向下一个节点的位置
 }
}

struct dict* p = NULL;
int dict_size = 0;
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
 CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
 enum { IDD = IDD_ABOUTBOX };
#endif

 protected:
 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()


// CdictDlg 对话框

CdictDlg::CdictDlg(CWnd* pParent /*=nullptr*/)
 : CDialogEx(IDD_DICT_DIALOG, pParent)
 , test1(_T(""))
 , labell(_T(""))
{
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
//数据
void CdictDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialogEx::DoDataExchange(pDX);
 DDX_Text(pDX, IDC_EDIT1, test1);
 DDX_Text(pDX, IDC_STATIC1, labell);
}

BEGIN_MESSAGE_MAP(CdictDlg, CDialogEx)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_BN_CLICKED(IDOK, &CdictDlg::OnBnClickedOk)
 ON_BN_CLICKED(IDCANCEL, &CdictDlg::OnBnClickedCancel)
END_MESSAGE_MAP()


// CdictDlg 消息处理程序

BOOL CdictDlg::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);  // 设置小图标

 // TODO: 在此添加额外的初始化代码
 dict_size = open_dict(&p, "dict.txt");
 if (dict_size == 0)
 {
  exit(0);
 }
 return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CdictDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
 {
  CAboutDlg dlgAbout;
  dlgAbout.DoModal();
 }
 else
 {
  CDialogEx::OnSysCommand(nID, lParam);
 }
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CdictDlg::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 CdictDlg::OnQueryDragIcon()
{
 return static_cast<HCURSOR>(m_hIcon);
}

//点击按钮

void CdictDlg::OnBnClickedOk()
{
 // TODO: 在此添加控件通知处理程序代码
 //CDialogEx::OnOK();
 char key[2048];
 char content[2048];
 memset(key, 0, sizeof(key));
 memset(content, 0, sizeof(content));
 UpdateData(TRUE);//将edit控件中的数据更新到变量中
 CStringA key_a(test1);//将宽字码转化为gdk
 strcpy(key, key_a);
 if (search_dict(p, dict_size, key, content))
  labell = content;
 else
  labell = L"没有找到"; //L代表宽字码
 UpdateData(FALSE);


}


void CdictDlg::OnBnClickedCancel()
{
 // TODO: 在此添加控件通知处理程序代码
 free_dict(p, dict_size);
 CDialogEx::OnCancel();
}

猜你喜欢

转载自www.cnblogs.com/countryboy666/p/11027952.html
MFC