MFC中GetDlgItemInt()方法的疑惑与使用总结
生活随笔
收集整理的這篇文章主要介紹了
MFC中GetDlgItemInt()方法的疑惑与使用总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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()方法的疑惑与使用总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++实现软件自动更新功能
- 下一篇: 安卓手机小说阅读器_乐小说阅读器下载手机