MSDN中是这样描述CWnd::GetDlgItemInt方法的:

UINT GetDlgItemInt( int nID, BOOL* lpTrans = NULL, BOOL bSigned = TRUE
) const;

Parameters
--------------------------------------------------------------------------------

nID 
Specifies the integer identifier of the dialog-box control to be translated.

lpTrans 
Points to the Boolean variable that is to receive the translated flag.

bSigned 
Specifies whether the value to be retrieved is signed.

Return Value
--------------------------------------------------------------------------------

Specifies the translated value of the dialog-box item text. Since 0 is a valid return value, lpTrans must be used to detect errors. If a signed return value is desired, cast it as an int type.

The function returns 0 if the translated number is greater than INT_MAX (for signed numbers) or UINT_MAX (for unsigned).

When errors occur, such as encountering nonnumeric characters and exceeding the above maximum, GetDlgItemInt copies 0 to the location pointed to by lpTrans. If there are no errors, lpTrans receives a nonzero value. If lpTrans is NULL, GetDlgItemInt does not warn about errors.

Remarks
--------------------------------------------------------------------------------

It translates the text of the specified control in the given dialog box into an integer value by stripping any extra spaces at the beginning of the text and converting decimal digits. It stops the translation when it reaches the end of the text or encounters any nonnumeric character.

If bSigned is TRUE, GetDlgItemInt checks for a minus sign (–) at the beginning of the text and translates the text into a signed number. Otherwise, it creates an unsigned value.

It sends a WM_GETTEXT message to the control.

显然该方法可以将文本控件中的文本直接转换为数字。看到Return Value的第一句,知道函数的返回值就是转换出来的数据。但是数据类型竟然只有UINT,而函数参数列表中明显声明了用bSigned参数区分返回值是有符号数还是无符号数。这岂不是与返回值为UINT矛盾了?

这时候看到Return Value中的第三句话:If a signed return value is desired, cast it as an int type.(如果需要返回的值是有符号数,那么直接将返回值看做int类型即可。) 于是一切便明朗了,因为一个数是否有符号仅仅在内存中是看不出来的,所以MS在描述此函数原型的时候似乎偷了个懒,无论返回值是否有符号,一律按照无符号数来返回。至于这个数是否有符号则需要程序员来进行一次转换。直接上代码:

int i = GetDlgItemInt(IDC_EDIT_TEMPERATURE, NULL, 1);  //若看做有符号数,则bSigned为1,返回值直接以int类型去接收UINT j = GetDlgItemInt(IDC_EDIT_TEMPERATURE, NULL, 0);  //若看做无符号数,则bSigned为0,返回值按照UINT类型来接收

MFC中GetDlgItemInt()方法的疑惑与使用总结相关推荐

  1. C++ 中transform方法的疑惑和自己的感受

    C++中的transform方法实在头文件<algorithm>中定义的,这个函数的作用就是将源区间的元素复制到目标区间,还能将两个区间的元素合并,并将结果写入目标区间.函数原型为: te ...

  2. vs2019 MFC 中 cannot open include file 'afxres.h' 问题解决方法

    vs2019 MFC 中 cannot open include file 'afxres.h' 问题解决方法 使用Everything查找afxres.h文件,如果没有everything的,可以到 ...

  3. VC++/MFC中调用CHM帮助文档的方法--ShellExecute

    (1)用Word编辑好帮助文档,并保存为网页格式,如mhtml格式. (2)用EasyCHM软件生成chm文档.生成方法很简单的,相信你能很快搞定的!当然用其它方法制作CHM文档也可以了. (3)在M ...

  4. MFC中动态创建控件以及事件响应实现方法

    本文实例讲述了MFC中动态创建控件以及事件响应实现方法,分享给大家供大家参考.具体实现方法如下: 动态控件是指在需要时由Create()创建的控件,这与预先在对话框中放置的控件是不同的. 一.创建动态 ...

  5. 【MFC】MFC中调用系统软键盘的几种方法

    1.直接运行微软系统自带的虚拟键盘程序"osk.exe" 在普通MFC项目中可以调用ShellExecute或者WinExec方法来直接运行微软系统自带的虚拟键盘程序"o ...

  6. MFC中使用SDL播放音频没有声音的解决方法

    2019独角兽企业重金招聘Python工程师标准>>> 本文所说的音频是指的纯音频,不包含视频的那种. 在控制台中使用SDL播放音频,一般情况下不会有问题. 但是在MFC中使用SDL ...

  7. MFC中获取App,MainFrame,Doc和View类等指针的方法

    From: http://hi.baidu.com/wxnxs/item/156a68f5b3b4ed18e3e3bd03 MFC中获取App,MainFrame,Doc和View类等指针的方法 1  ...

  8. MFC中的CString类使用方法指南

    MFC中的CString类使用方法指南 原文出处:codeproject:CString Management [禾路:这是一篇比较老的资料了,但是对于MFC的程序设计很有帮助.我们在MFC中使用字符 ...

  9. MFC中CImage类显示的半透明PNG存在的问题以及处理方法

    在MFC中自己也做过很多关于如何实现图片半透明的方法,包括抠图,图像数据计算等,但是使用MFC中CImage的时候有时候是透明的,有时候透明部分为白色!让人难以置信,最后在不经意间发现了这边文章才恍然 ...

最新文章

  1. xcode8控制台输出大量不用的log的问题解决NSLog失效的解决
  2. AVG Anti-Spyware 7.5 .0.50(原EWIDO)汉化 破解 注册 序列号
  3. 大型网站架构学习笔记
  4. linux编程之pthread_create函数
  5. 学校计算机教学演示,案例演示在计算机基础教学中的运用
  6. 重要更新|《Python程序设计开发宝典》例12-7代码有bug
  7. 在Python程序中设置函数最大递归深度
  8. Cabin, 手机端的Kubernetes管理app
  9. 中国甲腈行业市场供需与战略研究报告
  10. 类和对象编程(七):this指针
  11. UNIX环境高级编程之第9章:进程关系
  12. 19.Virtual Type
  13. 2020计算机一级考试wps分数,计算机一级WPS辅导:在WPS下实现用域真正分数输入技巧...
  14. bss是什么_BSS的完整形式是什么?
  15. 【技术文档】centernet(姿态估计)
  16. win10内网穿透实现远程桌面连接
  17. moviepy第2天|对视频添加圆圈渐变大小的结尾及文字
  18. Vmware vSphere Web Client部署ovf模板报错:传输失败 OVF 描述符不可用
  19. 用c语言编写打猎小游戏,使用c语言编写简单小游戏.docx
  20. 计算机设计大赛感言,计算机编程比赛获奖感言.doc

热门文章

  1. 2021-03-29 PE条件(自适应、参数辨识、数据驱动常常涉及)
  2. Java爬取校内论坛新帖
  3. Prim算法的3个版本
  4. 第一百二十九天 how can I坚持
  5. 《那些年啊,那些事——一个程序员的奋斗史》——111
  6. LeetCode35.搜索插入位置
  7. Mininet 系列实验(一)
  8. 229. Majority Element II
  9. html5中高德、腾讯、百度 地图api调起手机app
  10. Vue.js指令实例