zoukankan      html  css  js  c++  java
  • C++学习总结

    关于获取本地磁盘盘符
    有四个基本的函数:GetLogicalDrives, GetLogicalDriveStrings, GetDriveType 和 GetVolumeInformation。第五个是 SetVolumeLabel,如果你愿意,可以用它设置卷标。
    第一个函数,GetLogicalDrives,返回一个DWORD的位掩码(bitmask)值,以告知驱动盘符。“0”表示是驱动器A,“1”表示驱动器B,依次类推。
    10110 10001 11000 00000 00000 00000 00
    这条信息表示在我的电脑里有驱动器:A、C、D、F、J、K、和L。
    第二个函数GetLogicalDriveStrings,它返回一个代表所有驱动器字母的重要字符串。每一个驱动器字母拥有D:\(尾随一个‘\’)的形式,这里 D 表示驱动器盘符,每个字符串有一空(null)终结符,结尾处有两个null。
    GetDriveType返回一个代码,如2代表软驱, 3代表硬盘,5代表CD-ROM驱动器。

    1. GetLogicalDriveStrings方法
    TCHAR buf[100];
    DWORD len = GetLogicalDriveStrings(sizeof(buf)/sizeof(TCHAR),buf);
    从名字上就知道GetLogicalDriveStrings是获得盘符的字符形式
    这个函数将所有盘符都存在buf中,形式是这样:
    "C:\<NULL>D:\<NULL>E:\<NULL><NULL>"
    即盘符之间用NULL隔开,最后一个盘符后面跟两个NULL
    所以可以这样从buf中提取单个盘符:
    for (CString strDisks,TCHAR* s=buf; *s; s+=_tcslen(s)+1)
    {
    LPCTSTR sDrivePath = s; //单个盘符
    strDisks += sDrivePath;
    strDisks += " ";
    }
    开始不知道为什么要用TCHAR,怕用错,查MSDN,得到这么一句
    #define char TCHAR; 汗啊~~~

    2. GetLogicalDrives方法
    DWORD dwDrives;
    char a;
    CString DriveName;
    dwDrives = GetLogicalDrives();
    a = 'A';
    while (dwDrives > 0)
    {
    if (dwDrives % 2 == 1)
    {
    DriveName.Format("%c", a);
    GetTreeCtrl().InsertItem(DriveName, m_nImageClose,
    m_nImageOpen, TVI_ROOT, TVI_LAST);
    }
    a++;
    dwDrives /= 2;
    }
    GetLogicalDrives函数返回一个DWORD值,4个字节32bit,每个bit代表一个盘符,比如bit0代表A盘,但是因为英文字母只有26个,所以最多可以表示26个盘符,不过,一般来说是够用了



    关于时间处理
    time_t date;
    time(&date);   //得到以1970-1-1 8:00:00开始算的秒数
    struct tm when;
    when=*localtime(&date);//得到1900-1-1 00:00:00开始算的时间结构
    asctime(&when);  //得到Tue Apr 03 10:32:27 2007格式


    关于ini文件操作
    WritePrivateProfileString("Job",parame[i],parameValue[i],szFileName);


    后台执行控制台命令
    WinExec("COMMAND.COM /C C:\\Inocmd32.exe > c:\\aaa.txt",SW_HIDE);


    写二进制文件
     CFile  l_f;
     if(!l_f.Open(szFileName,CFile::modeReadWrite)) //若不存在则创建该文件    
       {    
       l_f.Open(szFileName,CFile::modeCreate|CFile::modeReadWrite);    
       }
     l_f.Seek(0,CFile::begin); 
     //l_f.Read(l_buf,sizeof(l_buf)); 
     l_f.Write(&sjob,sizeof(JobScan::S_JobScan)); 
     l_f.Close();

    弹出一个文件选择框
     打开一个文件
      CFileDialog cfile(true);
      cfile.DoModal();
      m_edit1 = cfile.GetPathName();
      GetDlgItem(IDC_EDIT1)->SetWindowText(m_edit1);

    弹出一个目录选择框

     //打开一个目录
     CString FolderName;
      if(OpenDir("请选择一个目录:",FolderName))//若目录不存在则创建
     {
       if(CreateDir(FolderName))
      {
       this->SetDlgItemText(IDC_EDIT1,FolderName);
       //iniOp.patchSavePath=FolderName;
      }
      else MessageBox("指定路径非法!");  
     }
    bool Ctest::OpenDir(LPCTSTR initFolerName , CString &selectFolderName)
    {
     const int nMaxByte=200;
     char Mycom[nMaxByte];
     BROWSEINFO Myfold;
     Myfold.hwndOwner=NULL;
     Myfold.pidlRoot=NULL;
     Myfold.pszDisplayName=Mycom;
     Myfold.lpszTitle=initFolerName;
     Myfold.ulFlags=0;
     Myfold.lpfn=NULL;
     Myfold.lParam=NULL;
     Myfold.iImage=NULL;
     Mycom[0]='\0';
     if(SHGetPathFromIDList(SHBrowseForFolder(&Myfold),Mycom))
     {
      selectFolderName=Mycom;
      return TRUE;
     }
     else
     {
      return FALSE;
     } 
    }

    bool Ctest::CreateDir(LPCTSTR dirName)
    {
     // 为 -1 则说明不存在,为其它则说明存在
     if(_access(dirName, 0) == -1)
     {
      SECURITY_ATTRIBUTES  attrDir;
      SECURITY_DESCRIPTOR  SecurityDescriptor;
      InitializeSecurityDescriptor(&SecurityDescriptor,SECURITY_DESCRIPTOR_REVISION);
      attrDir.nLength = sizeof(SECURITY_ATTRIBUTES);
      attrDir.bInheritHandle = TRUE;
      attrDir.lpSecurityDescriptor = &SecurityDescriptor;
      //如果目录不存在则创建一个新的目录
      if( CreateDirectory(dirName,&attrDir) == 0 )
       return FALSE;
     }
     return TRUE;
    }

    CListCtrl使用技巧
    http://www.x5dj.com/userforum/00100239/00331235.shtml

  • 相关阅读:
    A. Maze
    A. Ice Skating (联通块)
    A. DZY Loves Chessboard (找到一个涂一个)
    C. Kefa and Park
    A. Party
    N皇后问题
    八皇后问题
    A. DZY Loves Sequences
    A. Reorder the Array (二分变形)
    BZOJ1681 [Usaco2005 Mar]Checking an Alibi 不在场的证明
  • 原文地址:https://www.cnblogs.com/bingxuefly/p/697893.html
Copyright © 2011-2022 走看看