您好,登錄后才能下訂單哦!
這篇文章主要講解了MFC對話框實現梯形分頁的代碼解析,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
本文實例為大家分享了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); }
看完上述內容,是不是對MFC對話框實現梯形分頁的代碼解析有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。