鼠标单击从上下文菜单创建的对话框

时间:2017-07-13 15:43:18

标签: visual-c++ mfc modal-dialog contextmenu

我有一个基于MFC对话框的应用程序,带有上下文菜单。上下文菜单中的条目会弹出一个模态对话框。但是在第二个对话框中的任何地方单击鼠标都不会做任何事情。通过主菜单打开相同的对话框效果很好。就像上下文菜单以某种方式窃取鼠标点击一样。调出上下文菜单的代码是:

void Cxxx::OnContextMenu(CWnd* pWnd, CPoint ptScreen)
{
    CMenu menu;

    VERIFY(menu.LoadMenu(IDR_MAIN_WINDOW_RIGHT_MOUSE));
    CMenu* pPopup = menu.GetSubMenu(0);
    ASSERT(pPopup != NULL);
    pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, ptScreen.x, ptScreen.y, this);
}

上下文菜单用于调出对话框的代码是:

void Cxxx::OnPaste()
{
    CPasteDlg Dlg;
    ...
    if (Dlg.DoModal() != IDOK){
        ...
    }
}

OnPaste代码与主菜单使用的api完全相同,并且可以从主菜单中正常工作。

有没有人知道我做错了什么阻止鼠标在通过上下文菜单调出时在粘贴对话框内工作?

如果重要,我在Win7-64位上使用Visual Studio 11。

提前感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:0)

你有没有记得做正确的事情?我刚刚创建了一个测试应用程序来显示一个弹出菜单,并根据随后的鼠标点击显示一个对话框:

// TestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Test.h"
#include "TestDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx
{
public:
    CAboutDlg();

// Dialog Data
#ifdef AFX_DESIGN_TIME
    enum { IDD = IDD_ABOUTBOX };
#endif

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CTestDlg dialog



CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(IDD_TEST_DIALOG, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_WM_CONTEXTMENU()
    ON_COMMAND(ID_TEST_SHOWDIALOG, &CTestDlg::OnTestShowdialog)
END_MESSAGE_MAP()


// CTestDlg message handlers

BOOL CTestDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        BOOL bNameValid;
        CString strAboutMenu;
        bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
        ASSERT(bNameValid);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);         // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon

    // TODO: Add extra initialization here

    return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialogEx::OnSysCommand(nID, lParam);
    }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTestDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialogEx::OnPaint();
    }
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTestDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}



void CTestDlg::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
{
    CMenu menu;

    VERIFY(menu.LoadMenu(IDR_MENU1));
    CMenu* pPopup = menu.GetSubMenu(0);
    ASSERT(pPopup != nullptr);
    pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
}


void CTestDlg::OnTestShowdialog()
{
    CAboutDlg dlgAbout;

    dlgAbout.DoModal();
}

这有帮助吗?我们没有看到您的其余代码或对话框资源。

答案 1 :(得分:-1)

使用NM_RCLICK代替OnConTextMenu()

BEGIN_MESSAGE_MAP(CXXX, CVIEW2)
    ON_NOTIFY_REFLECT(NM_RCLICK, &CXXX::OnNMRClick)
END_MESSAGE_MAP()

VOID CXXX::OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult)
{
    CPoint pt1, pt2;
    GetCursorPost(&pt1);
    pt2 = pt1;

    //TODO: Add your code with pt2, pt1 here.
    //TODO: Add your event code here.

    CMenu menu;
    if(menu.LoadMenu(IDR_MAIN_MENU))
    {
        CMenu *pMenu_sub = menu.GetSubMenu(2);
        pMenu_sub->TrackPopupMenu(TPM_LEFTALIGN,pt2.x, pt2.y, this);
    }
    return;
}