生成一个点击鼠标右键出现子对话框,点击鼠标左键没反应的m语言程序
时间: 2024-02-12 10:05:15 浏览: 153
可以使用以下 MFC 代码实现:
```cpp
// 右键菜单对话框
class CRightMenuDlg : public CDialogEx
{
public:
CRightMenuDlg(CWnd* pParent = nullptr);
enum { IDD = IDD_RIGHTMENU_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
CRightMenuDlg::CRightMenuDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_RIGHTMENU_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRightMenuDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CRightMenuDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()
void CRightMenuDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 绘制设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 中心图标
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;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
HCURSOR CRightMenuDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CRightMenuDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
CMenu menu;
menu.CreatePopupMenu();
menu.AppendMenu(MF_STRING, 1, _T("子对话框"));
menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
CDialogEx::OnRButtonDown(nFlags, point);
}
// 主程序
class CRightMenuApp : public CWinApp
{
public:
CRightMenuApp();
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
CRightMenuApp::CRightMenuApp()
{
}
CRightMenuApp theApp;
BOOL CRightMenuApp::InitInstance()
{
CWinApp::InitInstance();
AfxEnableControlContainer();
CRightMenuDlg dlg;
m_pMainWnd = &dlg;
dlg.DoModal();
return FALSE;
}
```
这个程序创建了一个 MFC 对话框窗口,当用户在窗口上点击鼠标右键时,会弹出一个子对话框。而当用户点击鼠标左键时,没有任何反应。
阅读全文