【问题标题】:Mouse clicks in a dialog box created from a context menu在从上下文菜单创建的对话框中单击鼠标
【发布时间】:2017-07-13 15:43:18
【问题描述】:

我有一个带有上下文菜单的基于 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。

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

【问题讨论】:

  • 只是猜测,给dlg构造函数正确的父窗口,这可能是个问题,当你有其他弹出窗口时......
  • 你试过CPasteDlg dlg(this);吗?

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


【解决方案1】:

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

// 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();
}

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

【讨论】:

    【解决方案2】:

    使用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;
    }
    

    【讨论】:

    • 这是一个糟糕的建议。上下文菜单键呢?
    猜你喜欢
    • 2011-08-27
    • 2017-05-13
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    相关资源
    最近更新 更多