View Full Version: Another way to get text?

C++ Learning Community > win32 api C++ programming > Another way to get text?


Title: Another way to get text?


Dragon - July 1, 2003 04:31 PM (GMT)
I know of GetWindowText to get text from an edit control, but are there other ways? I need to know how to get a string from the edit and then store the string in a variable for use later.

dr voodoo - July 1, 2003 05:38 PM (GMT)
This seems to work:
CODE
char text[GetWindowTextLength(EDIT_hwnd)+1];
char* text_pointer=text;
GetWindowText(EDIT_hwnd,text_pointer,GetWindowTextLength(EDIT_hwnd)+1);
string text_string=text;

GetWindowText needs a pointer towards a char and as far I know there is no other function for doing this. So first we declar a char array which is as long as the text in our window. Then we declare a pointer towards it and call GetWindowText. Now our char containes the text and converting a char array to a string is no problem.

Althought I don't think using strings in Win32 is a good idea. Most functions use char (for example MessageBox()) and not string so before using it we need to convert it back to char which isn't that easy and char is a more efficent way of storing data then string as far as I know.

Dragon - July 1, 2003 05:50 PM (GMT)
Yeah, I've been thinking about that. Just to make sure (although I am pretty sure), char is a single character, right? So if I want to store multiple characters I can set a variable like: char var[5], so var can hold up to 5 characters? Is that how you do it? Also, if you know, is there a maximum value that can be used, such as var[8000]. Would that be too big?

dr voodoo - July 2, 2003 02:43 PM (GMT)
QUOTE
char var[5], so var can hold up to 5 characters? Is that how you do it?

Absolutly correct. Note that if you use char you don't need to include string.h
And when you declare a char array you can write this:
CODE
char text[ ]="Hello"

In this case the array will be as long as the initial value.

CODE
Also, if you know, is there a maximum value that can be used, such as var[8000]. Would that be too big?

I'm not sure about this but I think an array of basic data types (char,int,float,bool,double) can have an unlimited size. For classes (HWND,string,...) it depends on each class.




* Hosted for free by InvisionFree