Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2565732
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: Windows平台

2013-08-18 00:06:31

    关键代码:

点击(此处)折叠或打开

  1. /////////////////////////////////////////////////////////////////////////////
  2. // CDfwefwfeView message handlers

  3. void CDfwefwfeView::OnRead()
  4. {
  5.     // TODO: Add your command handler code here
  6. #if 0 ///< C语言方式
  7.     FILE * pFile = fopen("t.txt", "r");
  8.     char buf[1024] = {0};
  9.     size_t sCount;
  10.     
  11.     if (NULL == pFile)
  12.     {
  13.         MessageBox("打开文件失败");
  14.         return;
  15.     }
  16.     if ((sCount = fread(buf, 1, 1024, pFile) <= 0))
  17.     {
  18.         MessageBox("读取文件失败");
  19.     }
  20.     fclose(pFile);
  21.     
  22.     MessageBox(buf);
  23. #endif
  24.     
  25.     CFileDialog cDlg(TRUE);
  26.     cDlg.m_ofn.lpstrTitle = "打开文件";
  27.     cDlg.m_ofn.lpstrFilter = "所有文件(*.*)\0*.*\0文本文件(*.txt)\0*.txt\0\0";
  28.     if (IDOK == cDlg.DoModal())
  29.     {
  30.         int offset = 0;
  31.         UINT rCount = 0;
  32.         char * buffer = NULL;
  33.         char buf[1024] = {0};

  34.         CFile cFile(cDlg.GetPathName(), CFile::modeRead);
  35.         buffer = new char[cFile.GetLength() + 1];
  36.         while ((rCount = cFile.Read(buf, 1024)) > 0)    ///< 一次读取太大的话,慢,至少我试了是这样的!
  37.         {
  38.             memcpy(buffer + offset, buf, rCount);
  39.             offset += rCount;
  40.         }
  41.         buffer[offset] = '\0';
  42.             
  43.         ///< 方法一,非模态对话框调用  【Show_Text是添加的一个继承Dialog的类,添加了一个显示接口->GetDlgItem(IDC_EDIT1)->SetWindowText(text);】
  44.         Show_Text * pSh = new Show_Text();
  45.         pSh->Create(IDD_DIALOG1, this);
  46.         pSh->ShowWindow(SW_SHOWNORMAL);

  47.         ///< 方法二,模态对话框调用
  48.         //pSh->DoModal();  ///< 无法采用这种方式,因为需要调用后面显示接口,文章最后我做了个使用这种模式的显示方式
  49.         
  50.         ///< 显示读取的文本内容
  51.         pSh->showText(buffer);
  52.         
  53.         //delete pSh;    ///< 不能立刻释放,否则一闪而过,看不到了...泄漏就泄漏吧,测试而已
  54.         
  55.         cFile.Close();
  56.     }
  57. }

  58. void CDfwefwfeView::OnWrite()
  59. {
  60.     // TODO: Add your command handler code here
  61. #if 0    ///< C语言方式
  62.     FILE * pFile = fopen("t.txt", "w+");
  63.     size_t sCount;
  64.     
  65.     if (NULL == pFile)
  66.     {
  67.         MessageBox("打开文件失败");
  68.         return;
  69.     }
  70.     if ((sCount = fwrite("http://lego.blog.chinaunix.net", 1,
  71.         strlen("http://lego.blog.chinaunix.net"), pFile) <= 0))
  72.     {
  73.         MessageBox("写入文件失败");
  74.         return;
  75.     }
  76.     
  77.     fseek(pFile, 0, SEEK_SET);
  78.     if ((sCount = fwrite(" ftp:", 1, strlen(" ftp:"), pFile) <= 0))
  79.     {
  80.         MessageBox("写入文件失败");
  81.         return;
  82.     }
  83.     
  84.     fclose(pFile);
  85. #endif
  86.     
  87.     CFileDialog cDlg(FALSE);
  88.     cDlg.m_ofn.lpstrTitle = "保存文件";
  89.     cDlg.m_ofn.lpstrFilter = "所有文件(*.*)\0*.*\0文本文件(*.txt)\0*.txt\0\0";
  90.     cDlg.m_ofn.lpstrDefExt = "txt";
  91.     if (IDOK == cDlg.DoModal())
  92.     {
  93.         CFile cFile(cDlg.GetPathName(),
  94.             CFile::modeCreate | CFile::modeWrite);
  95.         cFile.Write("这里从编辑器获得内容", strlen("这里从编辑器获得内容"));
  96.         cFile.Close();
  97.     }
  98.     return;
  99. }

  100. void CDfwefwfeView::OnReg()
  101. {
  102.     // TODO: Add your command handler code here
  103.     HKEY hk = NULL, nhk = NULL;
  104.     ULONG ret = 0;
  105.     LPCTSTR key = _T("Shabti");
  106.     
  107.     ///< 打开一个指定的注册表键、获得句柄hk
  108.     ret = ::RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software"), 0, KEY_READ, &hk );
  109.     if ( ret || !hk ) {
  110.         MessageBox("Open failed");
  111.         return;
  112.     }

  113.     ///< 创建或打开注册表项nhk。默认为创建,当注册表中有此项时。为打开。
  114.     ret = ::RegCreateKey(hk, key, &nhk);
  115.     if ( ret || !hk ) {
  116.         MessageBox("New key create failed");
  117.         return;
  118.     }
  119.     ///< 设置默认注册表键值
  120.     ::RegSetValue(nhk, NULL, REG_SZ, "欢迎使用导航犬", strlen("欢迎使用导航犬"));
  121.     ///< 基于nhk添加新的项
  122.     ::RegSetValue(nhk, "嗯", REG_SZ, "想家了", strlen("想家了"));
  123.     ///< 未nhk添加新的键值
  124.     DWORD dwAge=30;
  125.     ::RegSetValueEx(nhk,"age", 0, REG_DWORD, (CONST BYTE*)&dwAge, 4);
  126.     ::RegSetValueEx(nhk,"name", 0, REG_SZ, (CONST BYTE*)"小明", strlen("小明"));
  127.     
  128.     ///< 删除注册表项
  129.     if ( nhk )
  130.         ::RegCloseKey(nhk);
  131.     ret = ::RegDeleteKey(hk, key);    ///< 删除失败了,是我添加太多了????
  132.     if ( ret ) {
  133.         MessageBox("New key delete failed");
  134.         return;
  135.     }
  136.     MessageBox("Succeeded in creating and deleting a registry key on a KEY_READ key!");
  137.     
  138.     return;
  139. }
        

        


    文件读取部分,最后对显示对话框做了下改动(添加了一个按钮,定义了一个const char * text的指针保存buffer,然后按钮CLICKED事件显示):

    关键代码:

点击(此处)折叠或打开

  1. Show_Text pSh; ///< 采用DoModal后,可以不用new
  2.    //pSh->Create(IDD_DIALOG1, this);
  3.     //pSh->ShowWindow(SW_SHOWNORMAL);
  4.     pSh.text = buffer;

  5.     pSh.DoModal(); ///< 该窗口执行退出了才执行其他


    
阅读(3235) | 评论(0) | 转发(0) |
0

上一篇:MFC_逃跑按钮

下一篇:3D图形算法基本概念

给主人留下些什么吧!~~