{"id":223,"date":"2009-11-12T23:11:39","date_gmt":"2009-11-12T17:41:39","guid":{"rendered":"http:\/\/techtwaddle.net\/?p=223"},"modified":"2011-04-12T23:11:58","modified_gmt":"2011-04-12T17:41:58","slug":"displaying-a-context-menu-in-your-application","status":"publish","type":"post","link":"https:\/\/techtwaddle.co.in\/blog\/2009\/11\/12\/displaying-a-context-menu-in-your-application\/","title":{"rendered":"Displaying a context menu in your application"},"content":{"rendered":"<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">Context menus really add to the user experience. You may decide to display a context popup menu when the user taps-and-holds or double-taps on the touch screen. In this post we will see how a few lines of code enable you to do just that.<\/span><\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">While reading about popup menus I came across the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa931036.aspx\"><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">WM_CONTEXTMENU<\/span><\/a> message. According to the windows mobile documentation that I referred this message is sent to a window when the user right clicks on the window&#8217;s client area. Since I haven&#8217;t yet seen a windows mobile device which supports right click functionality, I decided to leave it at that. And for this demonstration I display my context menu whenever the user double clicks (or double taps) on the screen. Remember that for your window to recieve a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa926302.aspx\"><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">WM_LBUTTONDBLCLK<\/span><\/a> message when the user double clicks, you should set the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms633574(VS.85).aspx\"><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">CS_DBLCLKS<\/span><\/a> style of your <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms633574(VS.85).aspx\"><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">WNDCLASS<\/span><\/a> structure when you do a <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">RegisterClass()<\/span>. Without this your window will <span style=\"text-decoration: underline;\">not<\/span> recieve the double click message.<\/span><\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">If you are comfortable with the gesture api&#8217;s, which Windows Mobile 6.5 supports, then you could use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ee220942.aspx\"><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">GID_HOLD<\/span><\/a> message. This message is sent when the user taps and holds on the screen for a specified amount of time.<\/span><\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">When my main window recieves a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa926302.aspx\"><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">WM_LBUTTONDBLCLK<\/span><\/a> message, I call the following function:<\/span><\/div>\n<p>\n<span style=\"font-family: Verdana; color: rgb(51, 153, 102);\">\/\/error checking omitted on purpose<\/span><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">POPUP_ITEMS pi[] = {<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; {IDM_POPUP_ABOUT, TEXT(&quot;About&quot;)},<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; {IDM_POPUP_EXIT, TEXT(&quot;Exit&quot;)}<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">};<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">int ShowContextMenu(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">{<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; POINT pt;<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; pt.x = LOWORD(lParam);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; pt.y = HIWORD(lParam);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; ClientToScreen(hWnd, &amp;pt);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; HMENU hMenu = CreatePopupMenu();<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; for (int i=0; i&lt; sizeof(pi)\/sizeof(pi[0]); i++)<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; {<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; InsertMenu(hMenu, 0xFFFFFFFF, MF_BYPOSITION | MF_STRING | MF_ENABLED, pi[i].id, pi[i].szText);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; }<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; return TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD, pt.x, pt.y, 0, hWnd, NULL);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">}<\/span><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Comic Sans MS;\">The <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">POPUP_ITEMS<\/span> structure is defined to hold an integer and a string,<\/span><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">typedef struct _popup_items_<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">{<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; int id;<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; TCHAR szText[128];<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">} POPUP_ITEMS;<\/span><\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">In the function, I first get the client co-ordinates of the point where the user double clicked and convert them to Screen co-ordinates. <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa932177.aspx\"><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">TrackPopupMenu()<\/span><\/a> api, which displays the popup menu, expects the co-ordinates to be passed w.r.t the screen.<\/span><\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\"><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa925887.aspx\"><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">CreatePopupMenu()<\/span><\/a> creates an empty menu. Use <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa920776.aspx\"><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">InsertMenu()<\/span><\/a> api to add items into the menu and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa932177.aspx\"><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">TrackPopupMenu()<\/span><\/a> will display the popup menu.<\/span><\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">When you set <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa932177.aspx\"><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">TPM_RETURNCMD<\/span><\/a> flag in the call to <a href=\"msdn.microsoft.com\/en-us\/library\/aa932177.aspx\"><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">TrackPopupMenu()<\/span><\/a>, the function will return with the identifier of the menu item which the user selected. If the user clicks outside, without selecting anything, then the function simply returns zero. These functions are simple to use and the MSDN documentation will suffice. I have provided links to msdn above.<\/span><\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">Finally, here is the video,<\/span><\/div>\n<p>&nbsp;<\/p>\n<p><object width=\"425\" height=\"344\"><param value=\"http:\/\/www.youtube.com\/v\/OZm9v5d5JPk&amp;hl=en_US&amp;fs=1&amp;\" name=\"movie\" \/><param value=\"true\" name=\"allowFullScreen\" \/><param value=\"always\" name=\"allowscriptaccess\" \/><\/object><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Context menus really add to the user experience. You may decide to display a context popup menu when the user taps-and-holds or double-taps on the touch screen. In this post we will see how a few lines of code enable you to do just that. &nbsp; While reading about popup menus I came across the &hellip; <a href=\"https:\/\/techtwaddle.co.in\/blog\/2009\/11\/12\/displaying-a-context-menu-in-your-application\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Displaying a context menu in your application<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ktFF-3B","_links":{"self":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/223"}],"collection":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":1,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":224,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions\/224"}],"wp:attachment":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}