Page 81 - DCAP408_WEB_PROGRAMMING
P. 81

Unit 4: Windows Controls




          4.9 Edit Controls                                                                     Notes

          One of the most generally used controls in the windows environment, the EDIT control, is used
          to permit the user to enter, modify, copy, etc... text. Windows Notepad is little more than a plain
          old window with a big edit control inside it.


                 Example: Here is the code used to interface with the edit control in this example:
                  SetDlgItemText(hwnd,  IDC_TEXT,  “This  is  a  string”);


          That’s all it takes to modify the text included in the control (this can be used for pretty much any
          control that has a text value connected with it, STATICs, BUTTONs and so on).
          Retrieving the text from the control is simple as well, though a little more work than setting it...
                  int  len  =  GetWindowTextLength(GetDlgItem(hwnd,  IDC_TEXT));
              if(len > 0)
              {
                  int i;
                  char* buf;


                          buf  =  (char*)GlobalAlloc(GPTR,  len  +  1);
                          GetDlgItemText(hwnd,  IDC_TEXT,  buf,  len  +  1);


                     //... do  stuff with  text ...


                          GlobalFree((HANDLE)buf);
              }
          Initially, we need to assign some memory to store the string in, it won’t just return us a pointer
          to  the string  previously  in memory. So as to do this, we first require to know how  much
          memory to assign. There isn’t a GetDlgItemTextLength(), but there is a GetWindowTextLength(),
          so all we require to do is obtain the handle to the control by means of GetDlgItem().
          Now that we have the length, we can assign some memory. Here I’ve added a verify to observe
          if there is any text to start with, as most likely you don’t want to be working with an empty
          string... at times you might, but that’s up to you. Presuming that there  is something there to
          function with, we call GlobalAlloc() to assign some memory. GlobalAlloc() as I’ve used it here
          is equivalent to calloc(), if you’re used to DOS/UNIX coding. It assigns some memory, initializes
          it’s contents to 0 and returns a pointer to that memory. There are dissimilar flags you can pass as
          the  first parameter to make it behave differently for different purposes, but this is the  only
          manner we will be using it.





             Notes  Observe that we added 1 to the length in two positions, what’s up with that? Well,
             GetWindowTextLength() returns  the number of characters  of text the control  includes
             NOT INCLUDING the null terminator. This signifies that if we were to assign a string
                                                                                 Contd...




                                           LOVELY PROFESSIONAL UNIVERSITY                                   75
   76   77   78   79   80   81   82   83   84   85   86