943,892 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1212
  • C++ RSS
Sep 17th, 2007
0

windows api help

Expand Post »
How can I make this print the text in the edit box when you push the button instead of "You pushed a button." I have looked all over but I can't figure it out.
C++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2. #include "resource.h"
  3. HWND hWndButton;
  4. HWND hWndEditBox;
  5. HFONT hFont;
  6.  
  7. /* Declare Windows procedure */
  8. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  9.  
  10. /* Make the class name into a global variable */
  11. char szClassName[ ] = "WindowsApp";
  12.  
  13. int WINAPI WinMain (HINSTANCE hThisInstance,
  14. HINSTANCE hPrevInstance,
  15. LPSTR lpszArgument,
  16. int nFunsterStil)
  17.  
  18. {
  19. HWND hWnd; /* This is the handle for our window */
  20. MSG messages; /* Here messages to the application are saved */
  21. WNDCLASSEX wincl; /* Data structure for the windowclass */
  22.  
  23. /* The Window structure */
  24. wincl.hInstance = hThisInstance;
  25. wincl.lpszClassName = szClassName;
  26. wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
  27. wincl.style = CS_DBLCLKS; /* Catch double-clicks */
  28. wincl.cbSize = sizeof (WNDCLASSEX);
  29.  
  30. /* Use default icon and mouse-pointer */
  31. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  32. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  33. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  34. wincl.lpszMenuName = NULL; /* No menu */
  35. wincl.cbClsExtra = 0; /* No extra bytes after the window class */
  36. wincl.cbWndExtra = 0; /* structure or the window instance */
  37. /* Use Windows's default color as the background of the window */
  38. wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  39.  
  40. /* Register the window class, and if it fails quit the program */
  41. if (!RegisterClassEx (&wincl))
  42. return 0;
  43.  
  44. /* The class is registered, let's create the program*/
  45. hWnd = CreateWindowEx (
  46. 0, /* Extended possibilites for variation */
  47. szClassName, /* Classname */
  48. "Windows App", /* Title Text */
  49. WS_SYSMENU | WS_MINIMIZEBOX , /* default window */
  50. CW_USEDEFAULT, /* Windows decides the position */
  51. CW_USEDEFAULT, /* where the window ends up on the screen */
  52. 330, /* The programs width */
  53. 210, /* and height in pixels */
  54. HWND_DESKTOP, /* The window is a child-window to desktop */
  55. NULL, /* No menu */
  56. hThisInstance, /* Program Instance handler */
  57. NULL /* No Window Creation data */
  58. );
  59.  
  60. /* Make the window visible on the screen */
  61. ShowWindow (hWnd, nFunsterStil);
  62.  
  63. /* Run the message loop. It will run until GetMessage() returns 0 */
  64. while (GetMessage (&messages, NULL, 0, 0))
  65. {
  66. /* Translate virtual-key messages into character messages */
  67. TranslateMessage(&messages);
  68. /* Send message to WindowProcedure */
  69. DispatchMessage(&messages);
  70. }
  71.  
  72. /* The program return-value is 0 - The value that PostQuitMessage() gave */
  73. return messages.wParam;
  74. }
  75.  
  76.  
  77. /* This function is called by the Windows function DispatchMessage() */
  78.  
  79. LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  80. {
  81. switch (message) /* handle the messages */
  82. {
  83. case WM_CREATE:
  84. {
  85. HINSTANCE hInstance = (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE) ;
  86. hWndButton = CreateWindowEx(
  87. 0,
  88. "BUTTON",
  89. "ADD",
  90. WS_VISIBLE | WS_CHILD,
  91. 250,
  92. 0,
  93. 80,
  94. 20,
  95. hWnd,
  96. (HMENU) IDB_BUTTON,
  97. hInstance,
  98. NULL);
  99. hFont = CreateFont(0, 0, 0, 0, 0, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_MODERN, "Courier New");
  100. hWndEditBox = CreateWindow(
  101. "EDIT",
  102. "",
  103. WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL,
  104. 0,
  105. 0,
  106. 250,
  107. 20,
  108. hWnd,
  109. (HMENU)IDC_EDITBOX_TEXT,
  110. (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
  111. NULL);
  112. SendMessage(hWndEditBox, WM_SETFONT, (WPARAM)hFont, TRUE);
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. }
  120.  
  121. break;
  122. case WM_DESTROY:
  123.  
  124. PostQuitMessage (0); /* send a WM_QUIT to the message queue */
  125. break;
  126. case WM_COMMAND:
  127. {
  128. switch (LOWORD(wParam))
  129. {
  130. case IDB_BUTTON:
  131. {
  132. switch (HIWORD(wParam))
  133. {
  134. case BN_CLICKED:
  135.  
  136.  
  137. MessageBox(NULL, "You pushed a button.", "", MB_OK | MB_ICONINFORMATION);
  138. break;
  139. }
  140. }
  141. break;
  142. }
  143. return 0;
  144. }
  145. break;
  146. default: /* for messages that we don't deal with */
  147. return DefWindowProc (hWnd, message, wParam, lParam);
  148.  
  149. }
  150.  
  151. return 0;
  152. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
curt22 is offline Offline
56 posts
since Sep 2007
Sep 17th, 2007
0

Re: windows api help

If you look at line 137 it calles the MessageBox function which takes the arguments
MessageBox(<message to show>, <title>, <buttons to show>, <icon to show>)

Click to Expand / Collapse  Quote originally posted by curt22 ...
How can I make this print the text in the edit box when you push the button instead of "You pushed a button." I have looked all over but I can't figure it out.
c++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2. #include "resource.h"
  3. HWND hWndButton;
  4. HWND hWndEditBox;
  5. HFONT hFont;
  6.  
  7. /* Declare Windows procedure */
  8. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  9.  
  10. /* Make the class name into a global variable */
  11. char szClassName[ ] = "WindowsApp";
  12.  
  13. int WINAPI WinMain (HINSTANCE hThisInstance,
  14. HINSTANCE hPrevInstance,
  15. LPSTR lpszArgument,
  16. int nFunsterStil)
  17.  
  18. {
  19. HWND hWnd; /* This is the handle for our window */
  20. MSG messages; /* Here messages to the application are saved */
  21. WNDCLASSEX wincl; /* Data structure for the windowclass */
  22.  
  23. /* The Window structure */
  24. wincl.hInstance = hThisInstance;
  25. wincl.lpszClassName = szClassName;
  26. wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
  27. wincl.style = CS_DBLCLKS; /* Catch double-clicks */
  28. wincl.cbSize = sizeof (WNDCLASSEX);
  29.  
  30. /* Use default icon and mouse-pointer */
  31. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  32. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  33. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  34. wincl.lpszMenuName = NULL; /* No menu */
  35. wincl.cbClsExtra = 0; /* No extra bytes after the window class */
  36. wincl.cbWndExtra = 0; /* structure or the window instance */
  37. /* Use Windows's default color as the background of the window */
  38. wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  39.  
  40. /* Register the window class, and if it fails quit the program */
  41. if (!RegisterClassEx (&wincl))
  42. return 0;
  43.  
  44. /* The class is registered, let's create the program*/
  45. hWnd = CreateWindowEx (
  46. 0, /* Extended possibilites for variation */
  47. szClassName, /* Classname */
  48. "Windows App", /* Title Text */
  49. WS_SYSMENU | WS_MINIMIZEBOX , /* default window */
  50. CW_USEDEFAULT, /* Windows decides the position */
  51. CW_USEDEFAULT, /* where the window ends up on the screen */
  52. 330, /* The programs width */
  53. 210, /* and height in pixels */
  54. HWND_DESKTOP, /* The window is a child-window to desktop */
  55. NULL, /* No menu */
  56. hThisInstance, /* Program Instance handler */
  57. NULL /* No Window Creation data */
  58. );
  59.  
  60. /* Make the window visible on the screen */
  61. ShowWindow (hWnd, nFunsterStil);
  62.  
  63. /* Run the message loop. It will run until GetMessage() returns 0 */
  64. while (GetMessage (&messages, NULL, 0, 0))
  65. {
  66. /* Translate virtual-key messages into character messages */
  67. TranslateMessage(&messages);
  68. /* Send message to WindowProcedure */
  69. DispatchMessage(&messages);
  70. }
  71.  
  72. /* The program return-value is 0 - The value that PostQuitMessage() gave */
  73. return messages.wParam;
  74. }
  75.  
  76.  
  77. /* This function is called by the Windows function DispatchMessage() */
  78.  
  79. LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  80. {
  81. switch (message) /* handle the messages */
  82. {
  83. case WM_CREATE:
  84. {
  85. HINSTANCE hInstance = (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE) ;
  86. hWndButton = CreateWindowEx(
  87. 0,
  88. "BUTTON",
  89. "ADD",
  90. WS_VISIBLE | WS_CHILD,
  91. 250,
  92. 0,
  93. 80,
  94. 20,
  95. hWnd,
  96. (HMENU) IDB_BUTTON,
  97. hInstance,
  98. NULL);
  99. hFont = CreateFont(0, 0, 0, 0, 0, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_MODERN, "Courier New");
  100. hWndEditBox = CreateWindow(
  101. "EDIT",
  102. "",
  103. WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL,
  104. 0,
  105. 0,
  106. 250,
  107. 20,
  108. hWnd,
  109. (HMENU)IDC_EDITBOX_TEXT,
  110. (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
  111. NULL);
  112. SendMessage(hWndEditBox, WM_SETFONT, (WPARAM)hFont, TRUE);
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. }
  120.  
  121. break;
  122. case WM_DESTROY:
  123.  
  124. PostQuitMessage (0); /* send a WM_QUIT to the message queue */
  125. break;
  126. case WM_COMMAND:
  127. {
  128. switch (LOWORD(wParam))
  129. {
  130. case IDB_BUTTON:
  131. {
  132. switch (HIWORD(wParam))
  133. {
  134. case BN_CLICKED:
  135.  
  136.  
  137. MessageBox(NULL, "You pushed a button.", "", MB_OK | MB_ICONINFORMATION);
  138. break;
  139. }
  140. }
  141. break;
  142. }
  143. return 0;
  144. }
  145. break;
  146. default: /* for messages that we don't deal with */
  147. return DefWindowProc (hWnd, message, wParam, lParam);
  148.  
  149. }
  150.  
  151. return 0;
  152. }
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Sep 17th, 2007
0

Re: windows api help

C++ Syntax (Toggle Plain Text)
  1. int TxtSize = GetWindowTextLenth (hwndEditBox) + 1;
  2. char *Entry = new char [TxtSize];
  3. GetWindowText (hwndEditBox, Entry, TxtSize);
  4. MessageBox (NULL, Entry, "", MB_OK | MB_ICONINFORMATION)
  5. delete Entry [];
I'm pretty rusty at C++, so note useage of NEW & DELETE
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005
Sep 17th, 2007
0

Re: windows api help

line 5 should be delete[] Entry
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Sep 18th, 2007
0

Re: windows api help

Thanks I got it working using this code:
C++ Syntax (Toggle Plain Text)
  1. int TxtSize = GetWindowTextLength (hWndEditBox) + 1;
  2. char *Entry = new char [TxtSize];
  3. GetWindowText (hWndEditBox, Entry, TxtSize);
  4. MessageBox (NULL, Entry, "", MB_OK | MB_ICONINFORMATION);
  5. delete[] Entry;
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
curt22 is offline Offline
56 posts
since Sep 2007

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: need help
Next Thread in C++ Forum Timeline: Help with code.





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


Follow us on Twitter


© 2011 DaniWeb® LLC