Page 82 - DCAP408_WEB_PROGRAMMING
P. 82
Windows Programming
Notes without adding 1, the text would fit, but the null terminator would overflow the memory
block, perhaps corrupting other data, causing an access violation, or any number of other
bad things. You must be careful when dealing with string sizes in windows, some APIs
and messages expect text lengths to comprise the null and others don’t, always read the
docs comprehensively.
Lastly we can call GetDlgItemText() to retrieve the contents of the control into the memory
buffer that we’ve just assigned. This call expects the size of the buffer INCLUDING the null
terminator. The return value, which we unnoticed here, is the number of characters copied, NOT
involving the null terminator.... fun eh? :)
After we’re all done by means of the text (which we’ll get to in a moment), we are required to
free up the memory that we assigned so that it doesn’t leak out and drip down onto the CPU and
short circuit your computer. To achieve this, we simply call GlobalFree() and pass in our pointer.
You may be or become conscious of a second set of APIs named LocalAlloc(), LocalFree(), etc...
which are legacy APIs from 16-bit windows. In Win32, the Local* and Global* memory functions
are identical.
4.9.1 Edits with Numbers
Entering text is all well and okay, but what if you would like the user to enter in a number? This
is a pretty general task, and luckily there is an API to make this easier, which takes care of all the
memory allocation, in addition to converting the string to an integer value.
BOOL bSuccess;
int nTimes = GetDlgItemInt(hwnd, IDC_NUMBER, &bSuccess, FALSE);
GetDlgItemInt() functions much like GetDlgItemText(), except that instead of copying the string
to a buffer, it transforms it internally into an integer and returns the value to you. The third
parameter is optional, and takes a pointer to a BOOL. As the function returns 0 on failure, there
is no way to tell just from that whether or not the function failed or the user just entered 0. If you
are fine with a value of 0 in the event of an error, then feel free to disregard this parameter.
Another functional trait is the ES_NUMBER style for edit controls, which permits only the
characters 0 through 9 to be entered. This is very handy if you only want positive integers,
otherwise it’s not much good, as you can’t enter any other characters, involving - (minus) .
(decimal) or , (comma).
Self Assessment
Fill in the blanks:
14. The ........................... control is used to permit the user to enter, modify, copy, etc... text.
15. Initially, we need to assign some memory to store the ........................... in, it won’t just
return us a pointer to the string previously in memory.
4.10 Summary
Window controls are considered as predefined window classes i.e. you are not required to
call the RegisterClass() function to generate a window class before the control.
A text static control is like an edit control, but it does not obtain typed input from the user.
76 LOVELY PROFESSIONAL UNIVERSITY