Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Menu updating - (http://www.programmingforums.org/showthread.php?t=14989)

pcbrainbuster Jan 20th, 2008 8:38 PM

Menu updating -
 
Sup guys,

As some of you guys may know I recently had little trouble with EnumWindows, well that problem is gone now :) The new problem is that I have a menu that needs to be updated somehow. I'm just going to give you a task because it is easier this way(then to explain it). First create a dropdown menu called "Windows", now every seconds or two this menu should have a string that is updated with the total number of windows and that's it. I know this sounds possibly simple and/or stupid but its part of a bigger picture.

Thanks.

Ancient Dragon Jan 20th, 2008 9:03 PM

Re: Menu updating -
 
Search MSDN for GetMenu function. Now look on the left panel and you will see a list of menu functions that can manipulate the menu. Search those functions to see if you can find one that will solve your problem.

Near the bottom of the window you will also find some See Also links that give more information about menus

pcbrainbuster Jan 20th, 2008 11:33 PM

Re: Menu updating -
 
Well, Ancient Dragon, I've already tried that but I kept getting unexpected results which is why I asked for help here :(

Sorry for keep on asking you guys for so much help :( but could you please try and do the task listed above? As this will answer(most likely) my question.

Thank you :).

Sane Jan 20th, 2008 11:43 PM

Re: Menu updating -
 
People aren't here to "do tasks for you". Post your code, and maybe someone can point out what you've done wrong. If you don't have any code to show for, then don't expect anyone to help someone who's shown no effort. If you expect that from us, then post this in the job offers where your "tasks" might be more welcome. The reason I say this, is because your request is not so simple. It takes time to do, and that's time you're most likely not going to get out of anyone here for free.

pcbrainbuster Jan 21st, 2008 12:35 AM

Re: Menu updating -
 
That's alfirmative sarge! ;)

:

  1. #include <windows.h>
  2. #include <iostream.h>
  3.  
  4. const char *ClsName = "BasicApp";
  5. const char *WndName = "Test -";
  6.  
  7. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  8. BOOL CALLBACK EnumProc(HWND hWnd2, LPARAM lParam);
  9.  
  10. HINSTANCE hInstance;
  11. HWND hWnd;
  12. char String[3];
  13. int NumWnds;
  14.  
  15. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  16. LPSTR lpCmdLine, int nCmdShow)
  17. {
  18. MSG Msg;
  19. HWND hWnd;
  20. WNDCLASSEX WndClsEx;
  21.  
  22. // Create the application window
  23. WndClsEx.cbSize = sizeof(WNDCLASSEX);
  24. WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
  25. WndClsEx.lpfnWndProc = WndProc;
  26. WndClsEx.cbClsExtra = 0;
  27. WndClsEx.cbWndExtra = 0;
  28. WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  29. WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
  30. WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  31. WndClsEx.lpszMenuName = NULL;
  32. WndClsEx.lpszClassName = ClsName;
  33. WndClsEx.hInstance = hInstance;
  34. WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  35.  
  36. // Register the application
  37. RegisterClassEx(&WndClsEx);
  38.  
  39. // Create the window object
  40. hWnd = CreateWindow(ClsName,
  41. WndName,
  42. WS_OVERLAPPEDWINDOW,
  43. CW_USEDEFAULT,
  44. CW_USEDEFAULT,
  45. CW_USEDEFAULT,
  46. CW_USEDEFAULT,
  47. NULL,
  48. NULL,
  49. hInstance,
  50. NULL);
  51.  
  52. // Find out if the window was created
  53. if( !hWnd ) // If the window was not created,
  54. return 0; // stop the application
  55.  
  56. // Display the window to the user
  57. ShowWindow(hWnd, SW_SHOWNORMAL);
  58. UpdateWindow(hWnd);
  59.  
  60. // Decode and treat the messages
  61. // as long as the application is running
  62. while( GetMessage(&Msg, NULL, 0, 0) )
  63. {
  64. TranslateMessage(&Msg);
  65. DispatchMessage(&Msg);
  66. }
  67.  
  68. return Msg.wParam;
  69. }
  70.  
  71. LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  72. {
  73.  
  74. static HMENU MainMenu = CreateMenu(), TotalWnds = CreatePopupMenu();
  75.  
  76. switch(Msg)
  77. {
  78.  
  79.   case WM_DESTROY :
  80.  
  81.   KillTimer(hWnd, 201);
  82.   PostQuitMessage(WM_QUIT);
  83.  
  84.   break;
  85.  
  86.   case WM_CLOSE :
  87.  
  88.   DestroyWindow(hWnd);
  89.  
  90.   break;
  91.  
  92.   case WM_CREATE :
  93.  
  94.   AppendMenu(MainMenu, MF_POPUP, (UINT)TotalWnds, "Total Windows");
  95.   SetMenu(hWnd, MainMenu);
  96.   SetTimer(hWnd, 201, 1000, NULL);
  97.  
  98.   break;
  99.  
  100.   case WM_TIMER :
  101.  
  102.   switch(wParam)
  103.   {
  104.  
  105.     case 201 :
  106.  
  107.     EnumWindows((WNDENUMPROC)&EnumProc, NULL);
  108.     sprintf(String, "%d", NumWnds);
  109.     AppendMenu(TotalWnds, MF_STRING, NULL, String);
  110.  
  111.     NumWnds = 0;
  112.  
  113.     break;
  114.  
  115.   }
  116.  
  117.   break;
  118.  
  119. }
  120.  
  121. return DefWindowProc(hWnd, Msg, wParam, lParam);
  122.  
  123. }
  124.  
  125. BOOL CALLBACK EnumProc(HWND hWnd2, LPARAM lParam)
  126. {
  127.  
  128. ++NumWnds;
  129.  
  130. return true;
  131.  
  132. }


Please help :).

Thanks again!

Ancient Dragon Jan 21st, 2008 5:38 AM

Re: Menu updating -
 
I compiled and ran your program. Works (sortof) on my computer :) Every second or so it adds a new menu item with a number on it. Isn't that what you want ? If you want to just update an existing menu item instead of adding a new one (which makes more sense to me) than call SetMenuItemInfo() instead of AppendMenu() inside that timer event handler function.

pcbrainbuster Jan 21st, 2008 7:18 AM

Re: Menu updating -
 
Quote by Ancient Dragon: ":)"
Finally, a post with a smile(just kidding :)) ;).

Well getting back to topic, the menu is updated every second which is great and all, but as you may have noticed the menu gows bigger and bigger which is not needed, so is there a way to get rid of the old value and get in the new one?

Thanks.

Ancient Dragon Jan 21st, 2008 9:46 AM

Re: Menu updating -
 
re-read my post because I already told you how to do it -- see the last sentence. I'll leave it up to you to figure out how to use that function.

"It works on my computer" got the smily because that is a famous comment by programmers whose program doesn't work when tested by someone else.

pcbrainbuster Jan 21st, 2008 1:41 PM

Re: Menu updating -
 
Oh sorry for that, my eye didn't seem to catch that :) Anyway, I don't really have experience with the function SetMenuItemInfo(...), actually I tried once and failed... Although I WILL try, could you(if you want), just edit the code above to show me how to do it?

Thank you.

pcbrainbuster Jan 21st, 2008 3:32 PM

Re: Menu updating -
 
RESOLVED - I found another method which works just fine.


All times are GMT -5. The time now is 12:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC