MFC中CString的Format方法使用
%c 单个字符
%d 十进制整数(int)
%ld 十进制整数(long)
%f 十进制浮点数(float)
%lf 十进制浮点数(double)
%o 八进制数
%s 字符串
%u 无符号十进制数
%x 十六进制数
int转换为CString:
CString str;
int number=15;
//str=”15”
str.Format(_T(“%d”),number);
//str=” 15”(前面有两个空格;4表示将占用4位,如果数字超过4位将输出所有数字,不会截断)
str.Format(_T(“%4d”),number);
//str=”0015”(.4表示将占用4位,如果数字超过4位将输出所有数字,不会截断)
str.Format(_T(“%.4d”),number);
long转换为CString的方法与上面相似,只需要把%d改为%ld就可以了。
将十进制数转换为八进制:
CString str;
int num=255;
//str=”377”
str.Format(_T(“%o”),num);
//str=”00000377”
str.Format(_T(“%.8o”),num);
TCHAR转Cstring %s
itoa—integer to ASCII–将整形转换成字符串
atof—ascii to float–字符串转换成浮点型
atol—ascii to long—字符串转换成长整形
gcvt—浮点型转换成字符串(四舍五入)
strtod—字符串转换成浮点型
strtol—字符串转换成长整形
strtoul–字符串转换成无符号长整形
toascii—将整形转换成合法的ASCII码字符
_ttoi—可以将CString转换成整形
_itot_s—将int转换成char*
CWnd * pWnd = (CWnd*)GetDlgItem(IDC_EDIT4) ;
CString strValue ;
pWnd->GetWindowText(strValue) ;
strValue.Replace(_T(“sec”), _T(“”)) ;
int iValue = _ttoi(strValue) ;//将Cstring转换成int
TCHAR buffer[7] ;
_itot_s(iValue, buffer, 3, 10) ;//将int转换成char*
strValue.Format(_T(“%s”)) ;//将char*转换成CString
strValue = strValue + _T(“sec”) ;//CString实现字符串相加
CString转Char* 强转(LPSTR)(LPCTSTR)
CWnd *m_dla=nullptr;
m_dla=AfxGetApp()->m_pMainWnd;//获得主对话框的指针
CXXXDlg pDlg=(CXXXDlg)AfxGetMainWnd();
要包含XXXDlg.h头文件
Cxxxdlg pDlg = (Cxxxdlg*)GetParent();
然后用pDlg即可。
CxxxDlg dlg = (CxxxDlg)theApp.m_pMainWnd;
然後就能用dlg->什麽什麽什麽了
a.由指针获得句柄
CWND* pwnd ;
HWND hwnd ;
hwnd = pwnd-> GetSafeHwnd();
b.由句柄得到指针:
CWND* pwnd = FromeHandle(hmyhandle);
pwnd-> SetWindowText(” hello world!” ) ;
另外cpp访问主cpp的控件
CDM_MFCDlg *dlg = (CDM_MFCDlg*)theApp.m_pMainWnd;
CListCtrl *pList = (CListCtrl *)dlg->GetDlgItem(IDC_LIST1);
int i= pList->GetItemCount();
CString str;
str.Format("%d", i);
AfxMessageBox(str);
CVc6Dlg dlg;
/dlg.dm_ver = dm.Ver();/
m_pMainWnd = &dlg;
结构体变量
typedef struct Cstr//标识符
{
int thread_handle; // 线程句柄
int * is_run; // 线程结束标志
}ScriptInfo;//变量类型
ScriptInfo script_info = (ScriptInfo )malloc(sizeof(ScriptInfo));//申请结构体内存
script_info->is_run = (int *)malloc(sizeof(int));//申请变量内存
*script_info->is_run = 1;//给变量赋值
script_info->thread_handle=(int)malloc(sizeof(int));
script_info->thread_handle = 789652314;
CString i;
int p;
p = script_info->thread_handle;
i.Format(“%d”, p);
SetDlgItemText(IDC_EDIT1, i);
结构体和容器
include
using namespace std;
typedef struct//标识符
{
int thread_handle; // 线程句柄
int * is_run; // 线程结束标志
}ScriptInfo;//变量类型
获取文件路径
{
CString filter;
filter = "Launcher(*.exe)|*.exe|";
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, filter);
if (dlg.DoModal() == IDOK)
{
SetDlgItemText(IDC_EDIT1, dlg.GetPathName());
UpdateData(FALSE);
}
}