windows api help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2007
Posts: 56
Reputation: curt22 is an unknown quantity at this point 
Solved Threads: 0
curt22 curt22 is offline Offline
Junior Poster in Training

windows api help

 
0
  #1
Sep 17th, 2007
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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: windows api help

 
0
  #2
Sep 17th, 2007
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>)

Originally Posted by curt22 View 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.
  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. }
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: windows api help

 
0
  #3
Sep 17th, 2007
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: windows api help

 
0
  #4
Sep 17th, 2007
line 5 should be delete[] Entry
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 56
Reputation: curt22 is an unknown quantity at this point 
Solved Threads: 0
curt22 curt22 is offline Offline
Junior Poster in Training

Re: windows api help

 
0
  #5
Sep 18th, 2007
Thanks I got it working using this code:
  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;
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC