时间:2021-06-19 08:19:53 | 栏目:C代码 | 点击:次
本文实例为大家分享了MFC对话框实现梯形分页的具体代码,供大家参考,具体内容如下
// MFCDlg.h: 头文件 // #pragma once #include "CMemoCtrl.h" // CMFCDlg 对话框 class CMFCDlg : public CDialogEx { // 构造 CMemoCtrl m_Memo; public: CMFCDlg(CWnd* pParent = nullptr); // 标准构造函数 // 对话框数据 #ifdef AFX_DESIGN_TIME enum { IDD = IDD_MFC_DIALOG }; #endif protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: HICON m_hIcon; // 生成的消息映射函数 virtual BOOL OnInitDialog(); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); DECLARE_MESSAGE_MAP() };
// MFCDlg.cpp: 实现文件 // #include "pch.h" #include "framework.h" #include "MFC.h" #include "MFCDlg.h" #include "afxdialogex.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CMFCDlg 对话框 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 消息处理程序 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("断点")); m_Memo.InsertItem(1,_T("异常设置")); m_Memo.InsertItem(2,_T("输出")); m_Memo.InsertItem(3,_T("查找")); // 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 return TRUE; // 除非将焦点设置到控件,否则返回 TRUE } void CMFCDlg::OnPaint() { CPaintDC dc(this); CDialogEx::OnPaint(); } //当用户拖动最小化窗口时系统调用此函数取得光标 //显示。 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) { //最后一个设置 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); }