本文实例为大家分享了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(m_hIcon); }
//CMemoCtrl.h #pragma once #includeclass CMemoCtrl : public CWnd { int m_nLeft; int m_nIndex; HCURSOR m_hCursor; CArray m_aItems; CArray 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 (iGetLogFont(&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); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。