Displaying text in a window

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

Join Date: Jan 2009
Posts: 8
Reputation: hahanottelling is an unknown quantity at this point 
Solved Threads: 0
hahanottelling hahanottelling is offline Offline
Newbie Poster

Displaying text in a window

 
0
  #1
Jan 30th, 2009
Can anyone help me with displaying text in a C++ window? I've read the chapter 9 at winprog but it makes no sense to me. here's the code I'm using:

  1. #include <windows.h>
  2. #include "resource.h"
  3.  
  4. /* Declare Windows procedure */
  5. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  6.  
  7. /* Make the class name into a global variable */
  8. char szClassName[ ] = "WindowsApp";
  9.  
  10. int WINAPI WinMain (HINSTANCE hThisInstance,
  11. HINSTANCE hPrevInstance,
  12. LPSTR lpszArgument,
  13. int nFunsterStil)
  14.  
  15. {
  16. HWND hwnd; /* This is the handle for our window */
  17. MSG messages; /* Here messages to the application are saved */
  18. WNDCLASSEX wincl; /* Data structure for the windowclass */
  19. HMENU hMenu, hSubMenu;
  20. HICON hIcon, hIconSm;
  21. hMenu = CreateMenu();
  22.  
  23. /* The Window structure */
  24. wincl.hInstance = hThisInstance;
  25. wincl.lpszClassName = szClassName;
  26. wincl.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
  27. wincl.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  28. wincl.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
  29. wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
  30. wincl.style = CS_DBLCLKS; /* Catch double-clicks */
  31. wincl.cbSize = sizeof (WNDCLASSEX);
  32.  
  33. /* Use default icon and mouse-pointer */
  34. wincl.hIcon = LoadIcon (NULL, IDI_WARNING);
  35. wincl.hIconSm = LoadIcon (NULL, IDI_WARNING);
  36. wincl.hCursor = LoadCursor (NULL, IDC_CROSS);
  37. wincl.lpszMenuName = NULL; /* No menu */
  38. wincl.cbClsExtra = 0; /* No extra bytes after the window class */
  39. wincl.cbWndExtra = 0; /* structure or the window instance */
  40. /* Use Windows's default color as the background of the window */
  41. wincl.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
  42.  
  43. /* Register the window class, and if it fails quit the program */
  44. if (!RegisterClassEx (&wincl))
  45. return 0;
  46.  
  47. /* The class is registered, let's create the program*/
  48. hwnd = CreateWindowEx (
  49. 0, /* Extended possibilites for variation */
  50. szClassName, /* Classname */
  51. "Windows App", /* Title Text */
  52. WS_VISIBLE, /* default window */
  53. CW_USEDEFAULT, /* Windows decides the position */
  54. CW_USEDEFAULT, /* where the window ends up on the screen */
  55. 544, /* The programs width */
  56. 375, /* and height in pixels */
  57. NULL, /* The window is a child-window to desktop */
  58. NULL, /* No menu */
  59. hThisInstance, /* Program Instance handler */
  60. NULL /* No Window Creation data */
  61. );
  62.  
  63. /* Make the window visible on the screen */
  64. ShowWindow (hwnd, nFunsterStil);
  65.  
  66. /* Run the message loop. It will run until GetMessage() returns 0 */
  67. while (GetMessage (&messages, NULL, 0, 0))
  68. {
  69. /* Translate virtual-key messages into character messages */
  70. TranslateMessage(&messages);
  71. /* Send message to WindowProcedure */
  72. DispatchMessage(&messages);
  73. }
  74.  
  75. /* The program return-value is 0 - The value that PostQuitMessage() gave */
  76. return messages.wParam;
  77. }
  78.  
  79.  
  80. /* This function is called by the Windows function DispatchMessage() */
  81.  
  82. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  83. {
  84. switch (message) /* handle the messages */
  85. {
  86. case WM_CREATE:
  87. {
  88. HMENU hMenu, hSubMenu;
  89. HICON hIcon, hIconSm;
  90. hMenu = CreateMenu();
  91.  
  92. hSubMenu = CreatePopupMenu();
  93. AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
  94. AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
  95.  
  96. hSubMenu = CreatePopupMenu();
  97. AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "&Go");
  98. AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Stuff");
  99.  
  100. SetMenu(hwnd, hMenu);
  101. }
  102. break;
  103.  
  104. case WM_COMMAND:
  105. switch(LOWORD(wParam))
  106. {
  107. case ID_FILE_EXIT:
  108. return 0;
  109. break;
  110. case ID_STUFF_GO:
  111.  
  112. break;
  113. }
  114. break;
  115.  
  116.  
  117. case WM_DESTROY:
  118. PostQuitMessage (0); /* send a WM_QUIT to the message queue */
  119. break;
  120. default: /* for messages that we don't deal with */
  121. return DefWindowProc (hwnd, message, wParam, lParam);
  122. }
  123.  
  124. return 0;
  125. }

I just want to have text in the space under the menu... Is there any simple way to add it?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Displaying text in a window

 
0
  #2
Jan 30th, 2009
What do you mean by 'Text'? As in an editable text control? or just writing painted onto the windows?

there is a big difference.

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 8
Reputation: hahanottelling is an unknown quantity at this point 
Solved Threads: 0
hahanottelling hahanottelling is offline Offline
Newbie Poster

Re: Displaying text in a window

 
0
  #3
Jan 30th, 2009
I mean just text like this, that shows up on the screen. Not editable or anything, just simple text. Thx for replying.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,405
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Displaying text in a window

 
0
  #4
Jan 30th, 2009
Add this message handler to your windows proc:
  1. case WM_PAINT:
  2. {
  3. PAINTSTRUCT ps;
  4. HDC hdc = BeginPaint( hwnd, &ps );
  5. TextOut( hdc, 0 /* X */, 0 /* Y */, "Blah de blah de blah", 20 /* Number of chars */);
  6. EndPaint( hwnd, &ps );
  7. }
  8. break;
Should be enough to get you started - though google would have been even better.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 8
Reputation: hahanottelling is an unknown quantity at this point 
Solved Threads: 0
hahanottelling hahanottelling is offline Offline
Newbie Poster

Re: Displaying text in a window

 
0
  #5
Jan 30th, 2009
Thank you so much, that's exactly what I needed. I tried google many times, but could not find anything!
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