1. 改变光标形状
例如,改变光标为等待状态
加载别的形状的光标:HCURSOR hCursor = SetCursor(LoadCursor(NULL,IDC_WAIT));
注意,要保存SetCursor函数的返回值,用于恢复光标,使用SetCursor(hCursor)恢复原来的光标
2.1.判断某个文件是否存在
BOOL PathFileExists( LPCTSTR pszPath
);
Returns TRUE if the file exists, or FALSE otherwise. Call GetLastError for extended error information.
#include <windows.h> #include <iostream.h> #include "Shlwapi.h" void main( void ) { // Valid file path name (file is there). char buffer_1[ ] = "C://TEST//file.txt"; char *lpStr1; lpStr1 = buffer_1; // Invalid file path name (file is not there). char buffer_2[ ] = "C://TEST//file.doc"; char *lpStr2; lpStr2 = buffer_2; // Return value from "PathFileExists". int retval; // Search for the presence of a file with a true result. retval = PathFileExists(lpStr1); if(retval == 1) { cout << "Search for the file path of : " << lpStr1 << endl; cout << "The file requested /"" << lpStr1 << "/" is a valid file" << endl; cout << "The return from function is : " << retval << endl; } else{ cout << "/nThe file requested " << lpStr1 << " is not a valid file" << endl; cout << "The return from function is : " << retval << endl; } // Search for the presence of a file with a false result. retval = PathFileExists(lpStr2); if(retval == 1) { cout << "/nThe file requested " << lpStr2 << "is a valid file" << endl; cout << "Search for the file path of : " << lpStr2 << endl; cout << "The return from function is : " << retval << endl; } else{ cout << "/nThe file requested /"" << lpStr2 << "/" is not a valid file" << endl; cout << "The return from function is : " << retval << endl; } } OUTPUT ============== Search for the file path of : C:/TEST/file.txt The file requested "C:/TEST/file.txt" is a valid file The return from function is : 1 The file requested "C:/TEST/file.doc" is not a valid file The return from function is : 0
3. Rich Edit控件的使用
1) 在Dialog中放一个rich edit控件,然后用classwizard做一个变量对应这个控件,例如为m_RichEdit;
2) 在initdialog函数中,初始化这个控件
m_RichEdit.GetDefaultCharFormat(m_cf);
m_cf.cbSize=sizeof(CHARFORMAT);
m_cf.dwMask=CFM_COLOR|CFM_CHARSET|CFM_FACE;
m_cf.crTextColor=COLOR_NORMAL;
m_cf.bCharSet=GB2312_CHARSET;
memcpy(m_cf.szFaceName,"宋体",4);
m_RichEdit.SetDefaultCharFormat(m_cf);
m_RichEdit.SetBackgroundColor(0,RGB(200,200,200));
m_Font.CreatePointFont(120,"宋体");
m_RichEdit.SetFont(&m_Font);
3) 制作几个功能函数:
void XXXX::ClearMsg() { m_RichEdit.SetWindowText(""); } void XXXX::ShowMsg(CString str,COLORREF dw,BOOL bBold) { CHARFORMAT cf; m_RichEdit.GetDefaultCharFormat(cf); cf.dwMask=CFM_COLOR|CFM_PROTECTED; cf.dwEffects=CFE_PROTECTED; if(bBold) { cf.dwMask|=CFM_BOLD; cf.dwEffects|=CFE_BOLD; } cf.crTextColor=dw; //------------ m_RichEdit.HideSelection(1,0); m_RichEdit.SetSel(0,-1); m_RichEdit.SetSel(-1,-1); m_RichEdit.SetSelectionCharFormat(cf); m_RichEdit.ReplaceSel(str); }
void XXXX::OnRicheditCopy() { m_RichEdit.Copy(); } void XXXX::OnRicheditSaveas() { LPCTSTR lpszFilter="文本文件(*.txt)|*.txt|所有文件|*.*|"; CFileDialog fd(0,"txt",NULL,0,lpszFilter); fd.m_ofn.lpstrTitle="保存文件..."; if(fd.DoModal()==IDOK) { CString str; m_RichEdit.GetWindowText(str); CFile file; if(!file.Open(fd.GetPathName(),CFile::modeCreate|CFile::modeWrite)) return; file.WriteHuge(str,str.GetLength()); file.Close(); } }
void XXXX::OnRicheditSelectall() { m_RichEdit.SetSel(0,-1); }
3. 创建浏览目录窗口
#include <Shlobj.h>
UpdateData(TRUE); char szDir[MAX_PATH]; BROWSEINFO bi; ITEMIDLIST *pidl; bi.hwndOwner = this->m_hWnd; bi.pidlRoot = NULL; bi.pszDisplayName = szDir; bi.lpszTitle = "请选择目录"; bi.ulFlags = BIF_STATUSTEXT | BIF_RETURNONLYFSDIRS; bi.lpfn = NULL; bi.lParam = 0; bi.iImage = 0; pidl = SHBrowseForFolder(&bi); if(pidl == NULL) return; if(!SHGetPathFromIDList(pidl, szDir)) { return; } else { strPath = szDir; }
4. 判断文件夹是否存在以及创建文件夹
Header: Declared in Shlwapi.h. Import Library: Shlwapi.lib.
CString strDirPath = m_strSavePath; strDirPath += "\\"; strDirPath += m_strDeviceName; if(!PathIsDirectory(strDirPath)){ if(!CreateDirectory(strDirPath,NULL)){ MessageBox("创建目录失败!"); } }
5. CFileDialog 构造函数参数说明
CFileDialog::CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL );参数意义如下: bOpenFileDialog 为TRUE则显示打开对话框,为FALSE则显示保存对话文件对话框。 lpszDefExt 指定默认的文件扩展名。 lpszFileName 指定默认的文件名。 dwFlags 指明一些特定风格。 lpszFilter 是最重要的一个参数,它指明可供选择的文件类型和相应的扩展名。参数格式如: "Chart Files (*.xlc)|*.xlc|Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||";
文件类型说明和扩展名间用 | 分隔,同种类型文件的扩展名间可以用 ; 分割,每种文件类型间用 | 分隔,末尾用 || 指明。
6. 创建多级子目录函数
BOOL CGlobal::CreateMultipleDirectory(const CString& strPath) { CString strDir(strPath);//存放要创建的目录字符串 //确保以'\'结尾以创建最后一个目录 if (strDir.GetAt(strDir.GetLength()-1)!=_T('\\')){ strDir += (_T('\\')); } CStringArray strArray; CString strTemp;//一个临时变量,存放目录字符串 strTemp.Empty(); BOOL bSuccess = TRUE;//成功标志 //遍历要创建的字符串 for (int i=0;i<strDir.GetLength();++i){ if (strDir.GetAt(i) != _T('\\')) {//如果当前字符不是'\\' strTemp += (strDir.GetAt(i)); } else {//如果当前字符是'\\' strArray.Add(strTemp); //将当前层的字符串添加到数组中 strTemp += _T('\\'); } } //遍历存放目录的数组,创建每层目录 for (i = 0 ; i < strArray.GetSize() ; i++){ CString strDirPath = strArray.GetAt(i); if(!PathIsDirectory(strDirPath)){ // 判断目录是否存在 bSuccess = CreateDirectory(strDirPath, NULL) ? TRUE:FALSE; // 不存在,创建目录 if(!bSuccess){ break; } } } return bSuccess; }
7.十六进制字符串转换为十进制数
int H2D(CString str) { char** g=NULL; return (int)strtol(str,g,16); }
8. 如何判断某个对象是哪个类的实例
IsKindOf(RUNTIME_CLASS(CXXXX)) , ASSERT_KINDOF(类名, 类指针);
9.16进制的字符串转化为16进制数
使用sscanf;例如:
CString strText,
DWORD dwTT;
sscanf(strText,"%x",&dwTT);
10. 从文档指针中得到视图指针
POSITION pos = pDoc->GetFirstViewPosition(); CView* pView = pDoc->GetNextView(pos);
并且可以从视图指针得到Frame指针
CFrameWnd *pFrame = pView->GetParentFrame();
11. 动态添加动态库指令:
#pragma comment(lib, "XXX.lib")
12.用CString作为Key使用CMap
CMap在用CString做key类型时,ARG_KEY要选LPCTSTR
CMap<CString, LPCTSTR, int, int> typeMap; typeMap.SetAt(_T("ONE"),1); typeMap.SetAt(_T("TWO"),2); int nValue = 0; BOOL ret = typeMap.Lookup(_T("ONE"), nValue); ret = typeMap.Lookup(_T("THREE"), nValue); ret = typeMap.Lookup(_T("TWO"), nValue);
关键是ARG_KEY要选LPCTSTR
13.Windows精确定时
LARGE_INTEGER litmp; LONGLONG QPart1,QPart2; double dfMinus, dfFreq, dfTim; QueryPerformanceFrequency(&litmp); dfFreq = (double)litmp.QuadPart;// 获得计数器的时钟频率 QueryPerformanceCounter(&litmp); QPart1 = litmp.QuadPart;// 获得初始值 Sleep(100); QueryPerformanceCounter(&litmp); QPart2 = litmp.QuadPart;//获得中止值 dfMinus = (double)(QPart2-QPart1); dfTim = dfMinus / dfFreq;// 获得对应的时间值,单位为秒14. 关于float转DWORD和DWORD转float DWORD dw = 0; float f1 = 0.25; cout<<"f1 = "<<f1<<endl; dw = *((DWORD*)&f1); cout<<"dw = *((DWORD*)&f1): "<<dw<<endl; f1 = 0.0; cout<<"f1 = 0.0: "<<f1<<endl; f1 = *((float*)&dw); cout<<"f1 = *((float*)&dw): "<<f1<<endl; 运行结果如下 f1 = 0.25 dw = *((DWORD*)&f1): 1048576000 f1 = 0.0: 0 f1 = *((float*)&dw): 0.25 其实这就是利用了编译器转型的手段,这里float和DWORD都是4个字节长度,通过以上手段转型,不会丢失二进制的数据,所以也就不会丢失原始数据。 *((DWORD*)&f1),首先获取一个float指针,然后将其强制转换为DWORD类型的指针,最后解引用,获取的是一个DWORD类型的值,*((float*)&dw)则是同理15 .字符串转化为10进制数/10进制数转化为字符串字符串转化为10进制数: strtol( const char *nptr, char **endptr, int base );10进制数转化为字符串:char *_itoa( int value, char *string, int radix );
16.判断两个矩形是否相交:
BOOL Intersects(const CRect& rect1,const CRect& rect2) { rect1.NormalizeRect(); rect2.NormalizeRect(); return !(rect1 & rect2).IsRectEmpty(); }
17.批处理直接删除注册表命了:
reg delete HKEY_CURRENT_USER\Software\XXX\XXXX /f
直接用/fc参数就可以了,不需要其他的参数