MFC单文档按钮两两组合按下后不同功能的实现。有若干按钮,按下按钮1和按钮2实现功能一,按下按钮1和按钮3实现功能二,按下按钮1和按钮3实现功能三,按下按钮2和3实现功能四。同时先按下按钮1后按下按钮2和先按下按钮2后按下按钮1实现的功能也不同。*麻烦代码详细一些,初学者理解力较差。
这个不要用按钮,而是应该用CheckBox控件实现。
// Q764102View.cpp : implementation of the CQ764102View class
//
#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "Q764102.h"
#endif
#include "Q764102Doc.h"
#include "Q764102View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CQ764102View
IMPLEMENT_DYNCREATE(CQ764102View, CFormView)
BEGIN_MESSAGE_MAP(CQ764102View, CFormView)
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, &CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CQ764102View::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_BN_CLICKED(IDC_CHECK1, &CQ764102View::OnClickedCheck1)
ON_BN_CLICKED(IDC_CHECK2, &CQ764102View::OnClickedCheck2)
ON_BN_CLICKED(IDC_CHECK3, &CQ764102View::OnClickedCheck3)
ON_BN_CLICKED(IDC_CHECK4, &CQ764102View::OnClickedCheck4)
END_MESSAGE_MAP()
// CQ764102View construction/destruction
CQ764102View::CQ764102View() noexcept
: CFormView(IDD_Q764102_FORM)
{
// TODO: add construction code here
}
CQ764102View::~CQ764102View()
{
}
void CQ764102View::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
}
BOOL CQ764102View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CQ764102View::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
// CQ764102View printing
void CQ764102View::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
}
BOOL CQ764102View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CQ764102View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CQ764102View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CQ764102View::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
void CQ764102View::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void CQ764102View::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// CQ764102View diagnostics
#ifdef _DEBUG
void CQ764102View::AssertValid() const
{
CFormView::AssertValid();
}
void CQ764102View::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CQ764102Doc* CQ764102View::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CQ764102Doc)));
return (CQ764102Doc*)m_pDocument;
}
#endif //_DEBUG
// CQ764102View message handlers
void CQ764102View::DoFunc()
{
CButton* btn1 = (CButton*)GetDlgItem(IDC_CHECK1);
int i1 = btn1->GetCheck();
CButton* btn2 = (CButton*)GetDlgItem(IDC_CHECK2);
int i2 = btn2->GetCheck();
CButton* btn3 = (CButton*)GetDlgItem(IDC_CHECK3);
int i3 = btn3->GetCheck();
CButton* btn4 = (CButton*)GetDlgItem(IDC_CHECK4);
int i4 = btn4->GetCheck();
if (i1 + i2 + i3 + i4 == 2)
{
btn1->SetCheck(0);
btn2->SetCheck(0);
btn3->SetCheck(0);
btn4->SetCheck(0);
if (i1 + i2 == 2)
{
MessageBox(_T("功能1"));
}
else if (i1 + i3 == 2)
{
MessageBox(_T("功能2"));
}
else if (i1 + i4 == 2)
{
MessageBox(_T("功能3"));
}
else if (i2 + i3 == 2)
{
MessageBox(_T("功能4"));
}
else
{
MessageBox(_T("无此功能"));
}
}
}
void CQ764102View::OnClickedCheck1()
{
// TODO: Add your control notification handler code here
DoFunc();
}
void CQ764102View::OnClickedCheck2()
{
// TODO: Add your control notification handler code here
DoFunc();
}
void CQ764102View::OnClickedCheck3()
{
// TODO: Add your control notification handler code here
DoFunc();
}
void CQ764102View::OnClickedCheck4()
{
// TODO: Add your control notification handler code here
DoFunc();
}