vs中的类和函数

来源:互联网 发布:mac 移动硬盘绝对路径 编辑:程序博客网 时间:2024/05/18 00:30

1.GetDlgItem

在一个对话框或窗口中得到一个指向特定控件或子窗口的指针。

函数原型有两种:

virtual CWnd* GetDlgItem(   int nID ) const;virtual void GetDlgItem(   int nID,   HWND* phWnd ) const;
nID:想要得到的控件的ID
phWnd:指向控件父窗口句柄的指针
2.CComboBox类(下拉框)
int GetCount( ) const;//获得下拉框中选项的个数int GetCurSel( ) const;//获取选中项的索引(索引从0开始)int SetCurSel(   int nSelect );//将某项设为选中项int AddString(   LPCTSTR lpszString );//添加选项

3.CEdit类(文本框)
int GetCount( ) const;//获得下拉框中选项的个数int GetCurSel( ) const;//获取选中项的索引(索引从0开始)int SetCurSel(   int nSelect );//将某项设为选中项int AddString(   LPCTSTR lpszString );//添加选项void SetWindowText(   LPCTSTR lpszString );//设置文本框中的内容int GetWindowText(   LPTSTR lpszStringBuf,   int nMaxCount ) const;void GetWindowText(   CString& rString ) const;//获取文本框中的内容

4.atof

double atof(const char *nptr);

把字符串转换成浮点数


5.字符串分割(以“,”为分割符,可以在下面的代码中设定)

void CDialog::SplitString(CString str,CStringArray * sa){while(1){int pos=str.Find(L",");if (pos>=0){sa->Add(str.Left(pos));str=str.Mid(pos+1);}else{sa->Add(str);break;}}}

6.获取系统背景色(重绘时可以用此时得到的颜色擦除之前的图形)
//获取系统背景色COLORREF origionCol=GetSysColor(COLOR_3DFACE);

7.判断文件是否存在的函数
PathFileExists

8.退出整个程序
在CMainFrame的OnClose函数中添加下面的代码:

void CMainFrame::OnClose(){PostQuitMessage(0);// TODO: 在此添加消息处理程序代码和/或调用默认值CFrameWndEx::OnClose();}

9.弹出是否保存的对话框
int saveFlag=MessageBox(TEXT("是否要保存当前场景?"),TEXT("新建场景"),MB_YESNO|MB_ICONQUESTION);if (IDYES==saveFlag){//do something if you want to save the scene} else{//do other thing if you do not want to save the scene}

10.字符串分割
// AfxExtractSubString 从一个字符(chSep)分隔的字符串(lpszFullString)中取出第iSubString个子串,输出到rStringBOOL AFXAPI AfxExtractSubString (       CString& rString,                 // 用于输出子串    LPCTSTR lpszFullString,     // 被分隔的字符串    int iSubString,                      // zero-based substring index    TCHAR chSep = '\n'           // 分隔符    ) // eg:CString sDesc= "张三|男|28|医生";CString sOccupation;if(AfxExtractSubString ( sOccupation, sDesc, 3, '|'))    cout << "职业:" << sOccupation << endl;

11.读取文件并实现字符串分割
字符串格式
ID name path
1 | bird | bird.jpg
2 | car  |  car.png
...
//读取应该加入下拉列表中的实体名称CStdioFile dataFile;CString path("SHIP TABLE.txt");dataFile.Open(path,CStdioFile::modeRead);CString lineString,record;CString num,modeType,modePath;// 保存每一条数据记录的数组std::vector<CString> recordArray;// 保存每一个分隔符的位置std::vector<int> posArray;setlocale(LC_CTYPE,"chs");while (dataFile.ReadString(lineString)){recordArray.push_back(lineString);}// 遍历数据记录,生成模型数据结构体for (int i=1;i<(int)recordArray.size();i++){// 查找一条数据记录中分隔符的位置posArray.clear();record=recordArray.at(i);int pos=0;while(TRUE){if (pos<0){break;}pos=record.Find('|',pos+1);posArray.push_back(pos);}// 将每两个分隔符之间的属性读取出来num=record.Mid(0,posArray.at(0));modeType=record.Mid(posArray.at(0)+1,posArray.at(1)-posArray.at(0)-1);modePath=record.Mid(posArray.at(1)+1,record.GetLength()-posArray.at(1)-1);num.Trim();modeType.Trim();modePath.Trim();}

12.头文件包含
在MFC中创建的一个对话框类中使用如下代码时:
CMainFrame *pMain=(CMainFrame   *)AfxGetApp()->m_pMainWnd;CXXXMFCView* pView = (CXXXView*)(pMain->GetActiveView());

需要添加MainFrm.h和XXXMFCView.h的头文件,但是在对话框的源文件中添加上这两个头文件后编译器一直提示在ZoneVisionMFCView.h中有错误,解决的方法很奇怪,就是在包含XXXMFCView.h的地方同时也包含XXXMFCDoc.h


13.字符串分解

CString cstr(123.5,145.5,478.56double v1,v2,v3;swscanf(cstr,L"%lf,%lf,%lf",&v1,&v2,&v3);
如果不是Unicode编码,不需要上面的L

经过上面的处理后v1为123.5,v2为145.5,v3为478.56。

14.如果std::string中含有中文,那么用格式输出时"%s"时会数不出内容,这是就需要先做下面的转化
/************************************************************************//* 将字符串转化成宽字符串                                                                     *//************************************************************************/static LPWSTR ConvertCharToLPWSTR(const char * szString){int dwLen = strlen(szString) + 1;int nwLen = MultiByteToWideChar(CP_ACP, 0, szString, dwLen, NULL, 0);//算出合适的长度LPWSTR lpszPath = new WCHAR[dwLen];MultiByteToWideChar(CP_ACP, 0, szString, dwLen, lpszPath, nwLen);return lpszPath;}


原创粉丝点击