943,519 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 682
  • C++ RSS
Jul 1st, 2009
0

Having problems with my word editor pprogram in API

Expand Post »
I was going through a tutorial and had some trouble with an example program. Here's the coding I have and I can't fix it.

Here is the main cpp file.

C++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2.  
  3. #include "resource.h"
  4.  
  5. BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
  6. {
  7. switch(Message)
  8. {
  9. case WM_INITDIALOG:
  10. // This is where we set up the dialog box, and initialise any default values
  11.  
  12. SetDlgItemText(hwnd, IDC_TEXT, "This is a string");
  13. SetDlgItemInt(hwnd, IDC_NUMBER, 5, FALSE);
  14. break;
  15. case WM_COMMAND:
  16. switch(LOWORD(wParam))
  17. {
  18. case IDC_ADD:
  19. {
  20. // When somebody clicks the Add button, first we get the number of
  21. // they entered
  22.  
  23. BOOL bSuccess;
  24. int nTimes = GetDlgItemInt(hwnd, IDC_NUMBER, &bSuccess, FALSE);
  25. if(bSuccess)
  26. {
  27. // Then we get the string they entered
  28. // First we need to find out how long it is so that we can
  29. // allocate some memory
  30.  
  31. int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
  32. if(len > 0)
  33. {
  34. // Now we allocate, and get the string into our buffer
  35.  
  36. int i;
  37. char* buf;
  38.  
  39. buf = (char*)GlobalAlloc(GPTR, len + 1);
  40. GetDlgItemText(hwnd, IDC_TEXT, buf, len + 1);
  41.  
  42. // Now we add the string to the list box however many times
  43. // the user asked us to.
  44.  
  45. for(i = 0;i < nTimes; i++)
  46. {
  47. int index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);
  48.  
  49. // Here we are associating the value nTimes with the item
  50. // just for the heck of it, we'll use it to display later.
  51. // Normally you would put some more useful data here, such
  52. // as a pointer.
  53. SendDlgItemMessage(hwnd, IDC_LIST, LB_SETITEMDATA, (WPARAM)index, (LPARAM)nTimes);
  54. }
  55.  
  56. // Dont' forget to free the memory!
  57. GlobalFree((HANDLE)buf);
  58. }
  59. else
  60. {
  61. MessageBox(hwnd, "You didn't enter anything!", "Warning", MB_OK);
  62. }
  63. }
  64. else
  65. {
  66. MessageBox(hwnd, "Couldn't translate that number :(", "Warning", MB_OK);
  67. }
  68.  
  69. }
  70. break;
  71. case IDC_REMOVE:
  72. {
  73. // When the user clicks the Remove button, we first get the number
  74. // of selected items
  75.  
  76. HWND hList = GetDlgItem(hwnd, IDC_LIST);
  77. int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
  78. if(count != LB_ERR)
  79. {
  80. if(count != 0)
  81. {
  82. // And then allocate room to store the list of selected items.
  83.  
  84. int i;
  85. int *buf = GlobalAlloc(GPTR, sizeof(int) * count);
  86. SendMessage(hList, LB_GETSELITEMS, (WPARAM)count, (LPARAM)buf);
  87.  
  88. // Now we loop through the list and remove each item that was
  89. // selected.
  90.  
  91. // WARNING!!!
  92. // We loop backwards, because if we removed items
  93. // from top to bottom, it would change the indexes of the other
  94. // items!!!
  95.  
  96. for(i = count - 1; i >= 0; i--)
  97. {
  98. SendMessage(hList, LB_DELETESTRING, (WPARAM)buf[i], 0);
  99. }
  100.  
  101. GlobalFree(buf);
  102. }
  103. else
  104. {
  105. MessageBox(hwnd, "No items selected.", "Warning", MB_OK);
  106. }
  107. }
  108. else
  109. {
  110. MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK);
  111. }
  112. }
  113. break;
  114. case IDC_CLEAR:
  115. SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
  116. break;
  117. case IDC_LIST:
  118. switch(HIWORD(wParam))
  119. {
  120. case LBN_SELCHANGE:
  121. {
  122. // Get the number of items selected.
  123.  
  124. HWND hList = GetDlgItem(hwnd, IDC_LIST);
  125. int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
  126. if(count != LB_ERR)
  127. {
  128. // We only want to continue if one and only one item is
  129. // selected.
  130.  
  131. if(count == 1)
  132. {
  133. // Since we know ahead of time we're only getting one
  134. // index, there's no need to allocate an array.
  135.  
  136. int index;
  137. int err = SendMessage(hList, LB_GETSELITEMS, (WPARAM)1, (LPARAM)&index);
  138. if(err != LB_ERR)
  139. {
  140. // Get the data we associated with the item above
  141. // (the number of times it was added)
  142.  
  143. int data = SendMessage(hList, LB_GETITEMDATA, (WPARAM)index, 0);
  144.  
  145. SetDlgItemInt(hwnd, IDC_SHOWCOUNT, data, FALSE);
  146. }
  147. else
  148. {
  149. MessageBox(hwnd, "Error getting selected item :(", "Warning", MB_OK);
  150. }
  151. }
  152. else
  153. {
  154. // No items selected, or more than one
  155. // Either way, we aren't going to process this.
  156. SetDlgItemText(hwnd, IDC_SHOWCOUNT, "-");
  157. }
  158. }
  159. else
  160. {
  161. MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK);
  162. }
  163. }
  164. break;
  165. }
  166. break;
  167. }
  168. break;
  169. case WM_CLOSE:
  170. EndDialog(hwnd, 0);
  171. break;
  172. default:
  173. return FALSE;
  174. }
  175. return TRUE;
  176. }
  177.  
  178. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  179. LPSTR lpCmdLine, int nCmdShow)
  180. {
  181. return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
  182. }

Here is my header file.

C++ Syntax (Toggle Plain Text)
  1. //{{NO_DEPENDENCIES}}
  2. // Microsoft Developer Studio generated include file.
  3. // Used by ctl_one.rc
  4. //
  5. #define IDD_MAIN 101
  6. #define IDC_TEXT 1000
  7. #define IDC_NUMBER 1001
  8. #define IDC_LIST 1002
  9. #define IDC_ADD 1003
  10. #define IDC_CLEAR 1004
  11. #define IDC_REMOVE 1005
  12. #define IDC_SHOWCOUNT 1006
  13.  
  14. // Next default values for new objects
  15. //
  16. #ifdef APSTUDIO_INVOKED
  17. #ifndef APSTUDIO_READONLY_SYMBOLS
  18. #define _APS_NEXT_RESOURCE_VALUE 102
  19. #define _APS_NEXT_COMMAND_VALUE 40001
  20. #define _APS_NEXT_CONTROL_VALUE 1007
  21. #define _APS_NEXT_SYMED_VALUE 101
  22. #endif
  23. #endif

And here is the resource file.

C++ Syntax (Toggle Plain Text)
  1. //Microsoft Developer Studio generated resource script.
  2. //
  3. #include "resource.h"
  4.  
  5. #define APSTUDIO_READONLY_SYMBOLS
  6. /////////////////////////////////////////////////////////////////////////////
  7. //
  8. // Generated from the TEXTINCLUDE 2 resource.
  9. //
  10. #ifndef __BORLANDC__
  11. #include "winres.h"
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. #undef APSTUDIO_READONLY_SYMBOLS
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // English (U.S.) resources
  19.  
  20. #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
  21. #ifdef _WIN32
  22. LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
  23. #pragma code_page(1252)
  24. #endif //_WIN32
  25.  
  26. #ifdef APSTUDIO_INVOKED
  27. /////////////////////////////////////////////////////////////////////////////
  28. //
  29. // TEXTINCLUDE
  30. //
  31.  
  32. 1 TEXTINCLUDE DISCARDABLE
  33. BEGIN
  34. "resource.h\0"
  35. END
  36.  
  37. 2 TEXTINCLUDE DISCARDABLE
  38. BEGIN
  39. "#ifndef __BORLANDC__\r\n"
  40. "#include ""winres.h""\r\n"
  41. "#endif\r\n"
  42. "\0"
  43. END
  44.  
  45. 3 TEXTINCLUDE DISCARDABLE
  46. BEGIN
  47. "\0"
  48. END
  49.  
  50. #endif // APSTUDIO_INVOKED
  51.  
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. //
  55. // Dialog
  56. //
  57.  
  58. IDD_MAIN DIALOG DISCARDABLE 0, 0, 207, 156
  59. STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
  60. CAPTION "Controls One"
  61. FONT 8, "MS Sans Serif"
  62. BEGIN
  63. LTEXT "Add",IDC_STATIC,7,10,14,8
  64. EDITTEXT IDC_TEXT,25,7,120,14,ES_AUTOHSCROLL
  65. EDITTEXT IDC_NUMBER,150,7,21,14,ES_NUMBER
  66. LTEXT "times.",IDC_STATIC,177,10,23,8
  67. LISTBOX IDC_LIST,7,25,138,106,LBS_NOINTEGRALHEIGHT |
  68. LBS_EXTENDEDSEL | WS_VSCROLL | WS_TABSTOP
  69. PUSHBUTTON "&Add",IDC_ADD,150,30,50,14
  70. PUSHBUTTON "&Remove",IDC_REMOVE,150,47,50,14
  71. PUSHBUTTON "&Clear",IDC_CLEAR,150,63,50,14
  72. LTEXT "This item was added",IDC_STATIC,7,141,66,8
  73. CTEXT "-",IDC_SHOWCOUNT,77,141,32,8
  74. LTEXT "times",IDC_STATIC,114,141,17,8
  75. END
  76.  
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. //
  80. // DESIGNINFO
  81. //
  82.  
  83. #ifdef APSTUDIO_INVOKED
  84. GUIDELINES DESIGNINFO DISCARDABLE
  85. BEGIN
  86. IDD_MAIN, DIALOG
  87. BEGIN
  88. LEFTMARGIN, 7
  89. RIGHTMARGIN, 200
  90. VERTGUIDE, 145
  91. VERTGUIDE, 150
  92. TOPMARGIN, 7
  93. BOTTOMMARGIN, 149
  94. END
  95. END
  96. #endif // APSTUDIO_INVOKED
  97.  
  98. #endif // English (U.S.) resources
  99. /////////////////////////////////////////////////////////////////////////////

I keep getting this error:
1>c:\users\chris\desktop\word editor.cpp(85) : error C2440: 'initializing' : cannot convert from 'HGLOBAL' to 'int *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
Similar Threads
Reputation Points: 8
Solved Threads: 1
Junior Poster
goody11 is offline Offline
117 posts
since Jun 2009
Jul 1st, 2009
0

Re: Having problems with my word editor pprogram in API

Have a look at this point..
c++ Syntax (Toggle Plain Text)
  1. //...
  2. int *buf = (int*)GlobalAlloc(GPTR,Len);
  3. //...
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Jul 1st, 2009
0

Re: Having problems with my word editor pprogram in API

What's Len?
Reputation Points: 8
Solved Threads: 1
Junior Poster
goody11 is offline Offline
117 posts
since Jun 2009
Jul 1st, 2009
0

Re: Having problems with my word editor pprogram in API

Click to Expand / Collapse  Quote originally posted by goody11 ...
What's Len?
thats not the important thing..Just have a look at your GlobalAlloc..All you need to do is casting..
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Jul 1st, 2009
0

Re: Having problems with my word editor pprogram in API

Thank you.I copied and pasted your code and it said that LEN was undeclared but I understand now.
Reputation Points: 8
Solved Threads: 1
Junior Poster
goody11 is offline Offline
117 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with compiler error for self-teacher
Next Thread in C++ Forum Timeline: Class problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC