如何在MFC中将CString转换为UINT [英] How to convert CString to UINT in MFC

查看:880
本文介绍了如何在MFC中将CString转换为UINT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MFC的新手,我在这里将CString转换为UINT,我试过了

 UINT iEditId = _ttoi((LPCSTR)sTempString); 



sTempString值为IDC_EDIT5,我的iEditId = 0。

我无法将其转换为正确的值。

解决方案

谢谢你们的建议。经过大量搜索,我找到了解决方案。实际上我想要将一组唯一ID分配给一组动态生成的CEdit对象,所以我打算从CString转换为UINT。如果我使用以下行,其中iEditId值为1005(来自resource.h)



unsigned int iEditId = IDC_EDIT5;



所以下次我可以通过增加变量iEditId来增加ID值。



For(iIndex = 0; iIndex< = 5; iIndex ++ )

{

pEdit [iIndex] - >创建(WS_CHILD | WS_VISIBLE | WS_DLGFRAME | ES_AUTOHSCROLL | WS_TABSTOP,CRect(110,(30 + iSize * 30),260 ,(50 + iSize * 30)),this,iEditId + iIndex);

}


此代码正常工作并返回12:

 CString abc = _T(  12/12/2014< /跨度>); 
int date = _ttoi(abc.Left( 2 ));





您肯定不希望整数值为IDC_EDIT5,而是CEdit的内容的整数值,不要你呢?


你正在以错误的方式使用标签: IDC_EDIT5 已在你的一个头文件中定义,喜欢:

  #define IDC_EDIT5 1005  



你只是按原样使用该标签来获取它的值,可能得到如下项目:

 item = GetDlgItem(hDlg,IDC_EDIT5); 


I am new to MFC and here i am converting CString to UINT, I have tried

UINT iEditId = _ttoi((LPCSTR)sTempString); 


sTempString value is "IDC_EDIT5" and I am getting iEditId=0.
I can't convert it into proper value.

解决方案

Thank you guys for your suggestions. I found the solution after searching a lot. Actually I wanted set of unique IDs to be assigned to a set of dynamically generated CEdit objects, so i was planning to convert from CString to UINT. If i use the following line where iEditId value is 1005(From resource.h)

unsigned int iEditId = IDC_EDIT5;

so next time i can simply increase the ID value by just increment the variable iEditId.

For(iIndex=0;iIndex<=5;iIndex++)
{
pEdit[iIndex]->Create(WS_CHILD | WS_VISIBLE | WS_DLGFRAME | ES_AUTOHSCROLL | WS_TABSTOP, CRect(110, (30 + iSize * 30), 260, (50 + iSize * 30)), this, iEditId + iIndex);
}


This code works fine and returns "12":

CString abc = _T("12/12/2014");   
int date = _ttoi(abc.Left(2));



You surely don't want the integer value of "IDC_EDIT5" but the integer value of the content of the CEdit, don't you?


You are using the tag in the wrong way: IDC_EDIT5 is already defined in one of your header files, something like:

#define IDC_EDIT5 1005


You just use that label as is to get its value, possibly to get an item like:

item = GetDlgItem(hDlg, IDC_EDIT5);


这篇关于如何在MFC中将CString转换为UINT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆