Click here to Skip to main content
Licence 
First Posted 25 Oct 2005
Views 31,586
Bookmarked 14 times

How to use popup-menu to simulate menu assigned to window?

By | 25 Oct 2005 | Article
for the window skined with no-regular region, the menu assigned to window is difficult to be placed on it's normal location, so it does not work. I will introduce to you a way that use popup-menu to simulate menu assigned to window.

 

for the window skined with no-regular region, the menu assigned to window is difficult to be placed on it's normal location, so it does not work. I will introduce to you a way that use popup-menu to simulate menu assigned to window.

 

          first, draw a menu’ effect in any region on the window. the region we call it menu-region, we will save every menu-item's rect.

       

CRect m_rcMenu[3];

 

           we handle tree three messages mostly: WM_LBUTTONDOWN, WM_MOUSEMOVE, WM_ENTERIDLE.

 

       BEGIN_MESSAGE_MAP(CMyDlg, CDialog)

              //{{AFX_MSG_MAP(CMyDlg)

                    ON_WM_LBUTTONDOWN( )

                    ON_WM_MOUSEMOVE()

                    ON_WM_ENTERIDLE()

              //}}AFX_MSG_MAP

       END_MESSAGE_MAP()

 

         when the mouse left button is pushed in the menu-region, we start to use popup-menu to         simulate menu assigned to window.

 

        void CMyDlg::OnLButtonDown(UINT nFlags, CPoint ptStart)

        {

              for (i=0 ;i<3; i++)

              {

                     if (PtInRect(m_rcMenu[i], point))

                     {

        

                           if (!PtInRect(m_rcMenu[m_iMenuIndex], point))

                           {

                                  InvalidateRect(&m_rcMenu[m_iMenuIndex], TRUE);

                                  UpdateWindow();

                           }

 

                           //this is the popup menu index be shown.

                           m_iMenuIndex = i;

                           m_bShowMenu = TRUE;

 

           show the menu, if m_bMenuContinue is TRUE, show new popup menu repeated, and older is cleared on WM_ENTERIDLE, because when menu assigned to window is shown and the mouse move in menu-region(no left button down), the menu item changes.

 

                         do

                         {

 

                           //draw the effect that menu is pushed.

                                   CDC* pDC = GetDC();

                                pDC->Draw3dRect(&m_rcMenu[m_iMenuIndex], RGB(100, 100, 100), RGB(160, 160, 160));

                                   

ReleaseDC(pDC);

                                    m_bMenuContinue = FALSE;

                                    ShowMenu();

                          } while(m_bMenuContinue);  

                          InvalidateRect(&m_rcMenu[m_iMenuIndex], TRUE);

                          UpdateWindow();

                           m_bShowMenu = FALSE;

                           CWnd::OnLButtonDown(nFlags, point);

                           return;

                    }

               }

               if ( i== 3)

                      m_iMenuIndex = -1;

               CDialog::OnLButtonDown(nFlags, point);

 

           }

 

         then change the menu index in OnMouseMove

 

           void CMyDlg::OnMouseMove(UINT nFlags, CPoint point)

           {

                   // TODO: Add your message handler code here and/or call default

 

                   //undo the effect that menu be pushed.

                   if (m_iMenuIndex != -1)

                   {

                             if (!PtInRect(m_rcMenu[m_iMenuIndex], point))

                                       InvalidateRect(&m_rcMenu[m_iMenuIndex], TRUE);

 

                   }

 

                  UpdateWindow();

 

                   m_iMenuIndex = -1;

                   for (i=0 ;i<3; i++)

                   {

                            if (PtInRect(m_rcMenu[i], point))

                            {

                                  //change the index

                                  m_iMenuIndex = i;

                                  //draw the effect that mouse stop on it without left-button down.

                                  CDC* pDC = GetDC();

                                  pDC->Draw3dRect(&m_rcMenu[i], RGB(160, 160, 160), RGB(100, 100, 100));

                                  ReleaseDC(pDC);

                                  break;

                              }

                    }

 

                    CDialog::OnMouseMove(nFlags, point);

            }

 

          cancel the popup menu in OnEnterIdle and re-show it in OnLButtonDown's do-while loop.

    

          void CMyDlg::OnEnterIdle(UINT nWhy, CWnd* pWho)

          {

                  CWnd::OnEnterIdle(nWhy, pWho);

 

                  // TODO: Add your message handler code here

                  if (nWhy == MSGF_MENU)

                  {

 

                        if (!m_bShowMenu)

                                return;

                         CPoint point;

                         GetCursorPos(&point);

                         ScreenToClient(&point);

 

                         for (int i=0 ;i<3; i++)

                         {

                                   if (PtInRect(m_rcMenu[i], point))

                                   {

                                             if (m_iMenuIndex != i)

                                             {

                                                      if (m_iMenuIndex != -1)

                                                      {

                                                                if (!PtInRect(m_rcMenu[m_iMenuIndex], point))

                                                                {

                                                                       InvalidateRect(&m_rcMenu[m_iMenuIndex], TRUE);

                                                                       UpdateWindow();

                                                                }

                                                      }

                                                      m_iMenuIndex = i;

                                                      m_bMenuContinue = TRUE;

                                                      SendMessage(WM_CANCELMODE);

                                               }

                                               break;

                                     }

                           }

 

                 }

         }

 

       last, this is the code for show menu.

        

void CMyDlg::ShowMenu()

         {

               if (m_PopupMenu.GetSafeHmenu())

                         m_PopupMenu.DestroyMenu();

               m_PopupMenu.LoadMenu(IDR_POPUP);

               CMenu* pSub = m_PopupMenu.GetSubMenu(m_iMenuIndex);

               CPoint pt(m_rcMenu[m_iMenuIndex].left, m_rcMenu[m_iMenuIndex].bottom+2);

               ClientToScreen(&pt);

               TrackPopupMenu(pSub->GetSafeHmenu(), TPM_LEFTALIGN |

                                    TPM_LEFTBUTTON, pt.x, pt.y, NULL, m_hWnd, NULL);

         }

 

      so we can use popup menu to simulate menu assigned to window with this code. you can place your menu on any location in the window skined with no-regular region.

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

goodboyws



China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralHuh ? I don't understand ... PinmemberMaximilien7:04 26 Oct '05  
GeneralRe: Huh ? I don't understand ... Pinmembergoodboyws15:09 26 Oct '05  
GeneralRe: Huh ? I don't understand ... PinmemberRandomMonkey15:29 26 Oct '05  
GeneralRe: Huh ? I don't understand ... PinmemberJim Tomasko13:38 25 Jan '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 25 Oct 2005
Article Copyright 2005 by goodboyws
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid