Windows Popup menu position problem

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

Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

Windows Popup menu position problem

 
0
  #1
Nov 11th, 2008
I don't knof if that's the right place, since it's not c++ specific but a windows development doubt...
(and sorry my english)

My problem is kinda stupid, but i don't know whats happening, im tryng to show a popup menu when user press the righ mouse buttom, in the cursor position, the problem is it shows up far away from the cursor position, but ever in the same position from the mouse..

Im in a windows 2000 SP4, using Visual Studio 2005 pro, win 32 application

My code is a mess..im just praticing windows programin for the first time..

  1. void DisplayPopupMenu(long x, long y){
  2. GetSystemMetrics(SM_MENUDROPALIGNMENT);//determine the correct horizontal alignment flag (TPM_LEFTALIGN or TPM_RIGHTALIGN)
  3. HMENU temp= GetSubMenu(PopupMenu,0);
  4. TrackPopupMenu(temp,TPM_LEFTALIGN|TPM_RIGHTBUTTON,x,y,0,Window,NULL);
  5. }

  1. LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
  2. switch(msg){
  3. case WM_DESTROY: PostQuitMessage(0);break;//when 'X' is clicked: quit application(sends a WM_QUIT)
  4. case WM_COMMAND: WMCommand(hWnd, msg, wParam, lParam);break;//function to buttons(listed ^up)
  5. case WM_RBUTTONUP: DisplayPopupMenu(LOWORD(lParam), HIWORD(lParam)); break;
  6. //WM_RBUTTONUP : actived when mouse right button is released( it uses iParam to
  7. //obtain x an y of the mouse, to get those we need the macro LOWORD(to x) andHIWORD(to y))
  8.  
  9. }
  10. return(DefWindowProc(hWnd, msg, wParam, lParam));
  11. }

Thats the part of the code what do the popup menu stuff, but I don't know if you need more stuff to see, so I will post the entire code:

  1. #include <windows.h>
  2. //#include <winbase.h>
  3. #include "resource.h"
  4.  
  5.  
  6.  
  7. HMENU PopupMenu;
  8. HMENU Menu;
  9. HWND Window;
  10. HWND b_CreateWall;//just as normal windows interface, buttons are handles of a window
  11.  
  12. const int width=640;//screen width and height
  13. const int height=480;
  14. const int b_width=100;//button width and height
  15. const int b_height=20;
  16.  
  17.  
  18. //its a good idea create a function for WM_COMMAND, cause the msg handler(WndProc) will clutter
  19. //if i make it all with 'case:'
  20. //same parameters of the handler
  21. //converts b_CreateWall to LPARAM e compare with lParam, if its the same, a buttom is clicked
  22. void WMCommand(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
  23. if(lParam==(LPARAM)b_CreateWall) MessageBoxA(Window,"apertou 'b_CreateWall'","botao",MB_OK);
  24.  
  25. else if(wParam==ID_FILE_EXIT40003)PostQuitMessage(0);//if clicked exit on file menu
  26.  
  27. else if(wParam==ID_DRAWING_WIREFRAME){//if clicked it
  28. CheckMenuItem(Menu,ID_DRAWING_WIREFRAME,MF_CHECKED);
  29. CheckMenuItem(Menu,ID_DRAWING_SOLID,MF_UNCHECKED);
  30. }
  31. else if(wParam==ID_DRAWING_SOLID){
  32. CheckMenuItem(Menu,ID_DRAWING_SOLID,MF_CHECKED);
  33. CheckMenuItem(Menu,ID_DRAWING_WIREFRAME,MF_UNCHECKED);
  34. }
  35.  
  36. //popup menu:
  37. else if(wParam==ID_POPUP_MOVE) MessageBoxA(Window,"apertou 'Move'","clicked",MB_OK);
  38. else if(wParam==ID_POPUP_DUPLICATE) MessageBoxA(Window,"apertou 'Duplicate'","clicked",MB_OK);
  39. else if(wParam==ID_POPUP_DELETE) MessageBoxA(Window,"apertou 'Delete'","clicked",MB_OK);
  40. else if(wParam==ID_POPUP_TEXTURE) MessageBoxA(Window,"apertou 'Texture'","clicked",MB_OK);
  41. }
  42.  
  43. void DisplayPopupMenu(long x, long y){
  44. GetSystemMetrics(SM_MENUDROPALIGNMENT);//determine the correct horizontal alignment flag (TPM_LEFTALIGN or TPM_RIGHTALIGN)
  45. HMENU temp= GetSubMenu(PopupMenu,0);
  46. TrackPopupMenu(temp,TPM_LEFTALIGN|TPM_RIGHTBUTTON,x,y,0,Window,NULL);
  47. }
  48.  
  49. // the message handler
  50. //(handle to the current window, type of message being sent,additional info about message being sent,
  51. // additional info about message being sent)
  52. LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
  53. switch(msg){
  54. case WM_DESTROY: PostQuitMessage(0);break;//when 'X' is clicked: quit application(sends a WM_QUIT)
  55. case WM_COMMAND: WMCommand(hWnd, msg, wParam, lParam);break;//function to buttons(listed ^up)
  56. case WM_RBUTTONUP: DisplayPopupMenu(LOWORD(lParam), HIWORD(lParam)); break;
  57. //WM_RBUTTONUP : actived when mouse right button is released( it uses iParam to
  58. //obtain x an y of the mouse, to get those we need the macro LOWORD(to x) andHIWORD(to y))
  59.  
  60. }
  61. return(DefWindowProc(hWnd, msg, wParam, lParam));
  62. }
  63. //The DefWindowProc function will ensure that all messages are processed
  64. //either through their respective message handler or through a default message
  65. //same parameters from WndProc callback
  66.  
  67.  
  68. // main XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  69. //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  70.  
  71. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR IpCmdLine,int nShowCmd){
  72.  
  73. WNDCLASS wc;
  74. MSG msg;//message
  75.  
  76. //register a window class
  77. wc.cbClsExtra=0; //extra bytes of memory for the wndclass size
  78. wc.cbWndExtra=0; //extra bytes for the windows instance
  79. wc.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH);//sets the background color
  80. wc.hCursor=LoadCursor(NULL, IDC_ARROW); //sets the cursor for the window(current instance,ID of cursor)
  81. wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);//sets the icon for the application(// , // of the icon)
  82. //We set this first parameter as NULL to indicate we want to use the standard
  83. //Windows icons/cursors rather than specifying custom resources
  84. wc.hInstance=hInstance; //requires the handle of the instance( witch is hInstance from WinMain)
  85. wc.lpfnWndProc=WndProc; //pointer to the window procedure, our message handler
  86. wc.lpszClassName=L"TESTANDO"; //the class name
  87. wc.lpszMenuName=NULL; //controls the menu that is to be loaded for the window class
  88. wc.style= CS_OWNDC|CS_HREDRAW|CS_VREDRAW; //CS_HREDRAW and CS_VREDRAW,will redraw the screen when
  89. //the width or height of the client window has been changed,and CS_OWNDC,
  90. //which gives each registered class a unique device context
  91.  
  92. //now i have to register the class
  93. //if succefull a diferent of 0 number will return
  94. //if error, it will return 0
  95. if(!RegisterClass(&wc)){
  96. MessageBoxA(NULL,"DEU PAU!!! Nao registro a classe", "TAQUIUPARIU!!!", MB_OK);
  97. return 0;
  98. }
  99.  
  100. //now create the window
  101. //WS_OVERLAPPEDWINDOW: creates a window with max and min buttons
  102. //WS_VISIBLE: allow windows to be visible uppon creation
  103. //x and y coordinates
  104. //hWndParent:NULL cause this is the parent, used just when a child must be created
  105. //hMenu: load menu, but at now i dont need
  106. //current instance
  107. //multiple-document interface (MDI)-?complex stuff
  108. Window=CreateWindowA("TESTANDO","Teste WinApi",WS_OVERLAPPEDWINDOW|WS_VISIBLE,100,100,640,480,
  109. NULL,NULL,hInstance,NULL);
  110.  
  111. //create button
  112. //(BUTTON to it know its a button,name,that will be a child not an independent window
  113. //any window only parameter here will be diregarded,x,y,w,h,the parent(created just before)
  114. b_CreateWall=CreateWindowA("BUTTON","Criar Parede",WS_CHILD|WS_VISIBLE,0,100,b_width,b_height,
  115. Window,NULL,hInstance,NULL);
  116. Menu =LoadMenu(hInstance,MAKEINTRESOURCEW(IDR_MENU1));
  117. SetMenu(Window,Menu);
  118.  
  119. PopupMenu=LoadMenu(hInstance,MAKEINTRESOURCEW(IDR_MENU2));
  120. //test
  121. if(Window==NULL){
  122. MessageBoxA(NULL,"IHH,NAO DEU PRA CRIA WINDOW","Pau na janela",MB_OK);
  123. return 0;
  124. }
  125.  
  126. //now the main loop
  127.  
  128.  
  129. //peek message
  130. //message address,handle of the window we are getting messages from,range of the messages
  131. //we can use,range of the messages we can use(0 default),PM_NOREMOVE, which does not remove
  132. //the message from the message queue, and PM_REMOVE,
  133. //if theres no data on the qeue, it returns 0
  134. //If the message is not WM_QUIT, then we must call the TranslateMessage and Dispatch-
  135. //Message functions, which will translate a message from virtual keypresses to
  136. //character messages and dispatch the new message to the window procedure.
  137. while(1){
  138. if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
  139. if(msg.message==WM_QUIT) break;
  140. TranslateMessage(&msg);
  141. DispatchMessage(&msg);
  142. }
  143. }
  144.  
  145.  
  146.  
  147. return 1;
  148. }

By the way, the popup menu is working, the problem is just the position..
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,411
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: Windows Popup menu position problem

 
0
  #2
Nov 11th, 2008
Your problem is very small The coordinate in which you are trying to display the menu is not based on the client location. Change your WM_RBUTTONUP message like this:
  1. case WM_RBUTTONUP:
  2. POINT p;
  3. p.x = LOWORD(lParam);
  4. p.y = HIWORD(lParam);
  5. ClientToScreen(hWnd, &p);
  6. DisplayPopupMenu(p.x, p.y);
  7. break;
Hope this helps.
Last edited by William Hemsworth; Nov 11th, 2008 at 12:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

Re: Windows Popup menu position problem

 
0
  #3
Nov 11th, 2008
Yeah, it works perfectly! o.o
Tanx a lot man!!

But, do you know what was that my wrong code was returning as x and y before? Im curious.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC