The MFC dialog implements trapezoidal paging

  • 2020-09-28 09:03:17
  • OfStack

This article example Shared MFC dialog box for you to achieve trapezoidal page specific code, for your reference, the specific content is as follows


// MFCDlg.h:  The header file 
//

#pragma once
#include "CMemoCtrl.h"


// CMFCDlg  dialog 
class CMFCDlg : public CDialogEx
{
//  structure 

 CMemoCtrl m_Memo;
public:
 CMFCDlg(CWnd* pParent = nullptr); //  Standard constructor 

//  Dialog data 
#ifdef AFX_DESIGN_TIME
 enum { IDD = IDD_MFC_DIALOG };
#endif

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

//  implementation 
protected:
 HICON m_hIcon;

 //  The generated message mapping function 
 virtual BOOL OnInitDialog();
 afx_msg void OnPaint();
 afx_msg HCURSOR OnQueryDragIcon();
 DECLARE_MESSAGE_MAP()

};

// MFCDlg.cpp:  Implementation file 
//

#include "pch.h"
#include "framework.h"
#include "MFC.h"
#include "MFCDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCDlg  dialog 

CMFCDlg::CMFCDlg(CWnd* pParent /*=nullptr*/)
 : CDialogEx(IDD_MFC_DIALOG, pParent)
{
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

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

BEGIN_MESSAGE_MAP(CMFCDlg, CDialogEx)
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
END_MESSAGE_MAP()


// CMFCDlg  Message handler 

BOOL CMFCDlg::OnInitDialog()
{
 CDialogEx::OnInitDialog();
 CRect rect;
 GetClientRect(rect);
 rect.top = rect.bottom - 18;
 m_Memo.Create(5, rect, this, 8088);
 m_Memo.SetFont(GetFont());
 m_Memo.InsertItem(0,_T(" The breakpoint "));
 m_Memo.InsertItem(1,_T(" Abnormal Settings "));
 m_Memo.InsertItem(2,_T(" The output "));
 m_Memo.InsertItem(3,_T(" To find the "));
 //  Sets the icon for this dialog box.   When the main window of the application is not a dialog box, the framework will automatically 
 //  Perform this operation 
 SetIcon(m_hIcon, TRUE); //  Set the big icon 
 SetIcon(m_hIcon, FALSE); //  Set the little icon 

 return TRUE; //  Returns unless focus is set to the control  TRUE
}


void CMFCDlg::OnPaint()
{
 CPaintDC dc(this);

 CDialogEx::OnPaint();
}

// The system calls this function to get the cursor when the user drags the minimize window 
// Display. 
HCURSOR CMFCDlg::OnQueryDragIcon()
{
 return static_cast<HCURSOR>(m_hIcon);
}

//CMemoCtrl.h

#pragma once
#include <afxwin.h>

class CMemoCtrl :
 public CWnd
{
 int m_nLeft;
 int m_nIndex;
 HCURSOR m_hCursor;
 CArray<CString> m_aItems;
 CArray<CRect> m_rc;
 CFont m_font;
public:
 CMemoCtrl();
 BOOL Create(int nIndex, const RECT& rect, CWnd* pParentWnd, UINT nID);
 DECLARE_MESSAGE_MAP()
 afx_msg void OnPaint();
 afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
public:
 void InsertItem(int nIndex,CString szItemName);
 void SetFont(CFont* pFont);
 afx_msg void OnMouseMove(UINT nFlags, CPoint point);
};

//CMemoCtrl.cpp

#include "pch.h"
#include "CMemoCtrl.h"

CMemoCtrl::CMemoCtrl() 
{
 m_nLeft = 0;
 m_nIndex = 0;
 m_hCursor = ::LoadCursor(NULL,IDC_HAND);
}

BOOL CMemoCtrl::Create(int nCount, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
 return CWnd::Create(NULL, NULL, WS_CHILD|WS_VISIBLE, rect, pParentWnd, nID);
}
BEGIN_MESSAGE_MAP(CMemoCtrl, CWnd)
 ON_WM_PAINT()
 ON_WM_LBUTTONDOWN()
 ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()


void CMemoCtrl::OnPaint()
{
 CPaintDC dc(this);
 CRect rect, rt, rc;
 GetClientRect(rect);
 rt = rect;
 int i = 0;
 CSize size;
 CPoint p[5];
 m_nLeft = 8;
 m_rc.SetSize(m_aItems.GetSize());
 CBrush br(GetSysColor(COLOR_3DFACE));
 dc.FillRect(rect, &br);
 CFont* pOldFont = dc.SelectObject(&m_font);
 while (i<m_aItems.GetSize())
 {
 size = dc.GetTextExtent(m_aItems[i]);
 rt.left = m_nLeft;
 p[1].SetPoint(m_nLeft - 8, rt.top);
 p[2].SetPoint(m_nLeft + 4, rt.bottom);
 rt.right = m_nLeft+ size.cx + rect.Height();
 m_rc.SetAt(i, rt);
 m_nLeft = rt.right;
 p[3].SetPoint(m_nLeft - 4, rt.bottom);
 p[4].SetPoint(m_nLeft, rt.bottom -6);
 p[0].SetPoint(m_nLeft - 8, rt.top);

 //dc.SetBkColor(RGB(255, 255, 255));
 dc.SetBkMode(TRANSPARENT);
 
 if (i == m_aItems.GetSize() - 1)
 {
 // The last 1 A set 
 p[0].SetPoint(rect.right, rt.top);
 p[4].SetPoint(m_nLeft + 8, rt.top);
 dc.MoveTo(p[4]);
 dc.LineTo(p[0]);
 }
 if (i != m_nIndex)
 {
 dc.SelectObject(&m_font);
 dc.MoveTo(p[0]);
 dc.Polyline(p, 5);
 dc.DrawText(m_aItems.GetAt(i), rt, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
 }
 else
 {
 rc = rt;
 }
 //TRACE("%d,(%d,%d),(%d,%d),(%d,%d)\n",m_nLeft, p[0].x, p[0].y,p[2].x,p[2].y, p[4].x, p[4].y);
 i++;
 }
 p[0].SetPoint(rc.left -8,rc.top);
 p[1].SetPoint(rc.left + 4, rc.bottom);
 p[2].SetPoint(rc.right - 4, rc.bottom);
 p[3].SetPoint(rc.right + 8, rc.top);
 CBrush brc(RGB(255, 255, 255));
 dc.SelectObject(brc);
 dc.Polygon(p, 4);
 CPen pen(PS_SOLID,1,RGB(255, 255, 255));
 dc.SelectObject(pen);
 p[0].Offset(1, 0);
 dc.MoveTo(p[0]);
 dc.LineTo(p[3]);
 dc.SelectObject(pOldFont);
 dc.DrawText(m_aItems.GetAt(m_nIndex), rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}

void CMemoCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
 for (int i = 0; i < m_rc.GetSize(); i++)
 {
 if (m_rc.GetAt(i).PtInRect(point))
 {
 m_nIndex = i;
 Invalidate();
 }
 }

 CWnd::OnLButtonDown(nFlags, point);
}

void CMemoCtrl::InsertItem(int nIndex, CString szItemName)
{
 m_aItems.InsertAt(nIndex, szItemName);
}


void CMemoCtrl::SetFont(CFont* pFont)
{
 if (!pFont)
 return;
 LOGFONT lf;
 pFont->GetLogFont(&lf);
 if(m_font.GetSafeHandle())
 m_font.DeleteObject();
 m_font.CreateFontIndirect(&lf);
}


void CMemoCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
 for (int i = 0; i < m_rc.GetSize(); i++)
 {
 if (m_rc.GetAt(i).PtInRect(point))
 {
 SetCursor(m_hCursor);
 }
 }

 CWnd::OnMouseMove(nFlags, point);
}

Related articles: