封装的grid控件
Posted yang131
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了封装的grid控件相关的知识,希望对你有一定的参考价值。
class CGridCtrl : public CWnd { DECLARE_DYNAMIC(CGridCtrl) public: void Create(CWnd* pParent, DWORD dwStyle, RECT rect, UINT nId); void InsertColumn(LPCTSTR lpstColName); void InsertRow(); void Set(int Row, int Col, CString data) { /* while (vctRows[Row].size() < Col) { vctRows[Row].push_back(_T("")); } */ vctRows[Row].push_back(data); } afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar); void HScroll(UINT, UINT); LPCTSTR GetClassName() { return _T("GridCtrl"); } LPCTSTR GetWindowName() { return _T("GridCtrl"); } afx_msg void OnPaint(); afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); DECLARE_MESSAGE_MAP() private: bool RegisterCtrl(); //void(int nRow,int nCol); void InsertCol(LPCTSTR lpctRow); std::vector<LPCTSTR> GetRows(); static bool m_bRegisted; static LRESULT GridCtrlProc(HWND, UINT, WPARAM, LPARAM); typedef std::vector<CString> Cells; std::vector<Cells> vctRows; int m_nCurRow; CScrollBar scHorizon; CScrollBar scVertical; double dbHScroll; CPoint mouseP; COLORREF clrBK,clrSelect,clrForeground; };
.cpp
#include "pch.h" #include "CGridCtrl.h" bool CGridCtrl::m_bRegisted = false; IMPLEMENT_DYNAMIC(CGridCtrl, CWnd) BEGIN_MESSAGE_MAP(CGridCtrl, CWnd) ON_WM_PAINT() ON_WM_HSCROLL() ON_WM_HSCROLL_REFLECT() ON_WM_MBUTTONDOWN() ON_WM_LBUTTONDOWN() ON_WM_LBUTTONDBLCLK() ON_WM_LBUTTONUP() END_MESSAGE_MAP() void CGridCtrl::Create(CWnd* pParent, DWORD dwStyle, RECT rect, UINT nID) { WNDCLASS windowclass; HINSTANCE hInst = AfxGetInstanceHandle(); if (!::GetClassInfo(hInst, GetClassName(), &windowclass)) { RegisterCtrl(); } CWnd::Create(GetClassName(), GetWindowName(), dwStyle | SS_NOTIFY /*| WS_HSCROLL | WS_VSCROLL*/, rect, pParent, nID); scHorizon.Create(SBS_HORZ, rect, this, nID + 3); scHorizon.EnableScrollBar(ESB_DISABLE_RIGHT); scVertical.Create(SBS_VERT, rect, this, nID + 32); scVertical.EnableScrollBar(ESB_ENABLE_BOTH); scHorizon.SetScrollRange(0, 200); clrBK = RGB(23, 100, 100); clrSelect = RGB(223, 100, 233); } void CGridCtrl::InsertColumn(LPCTSTR lpstColName) { if (vctRows.size() >= 1) (vctRows.end() - 1)->push_back(lpstColName); } void CGridCtrl::InsertRow() { Cells cells; vctRows.push_back(cells); } void CGridCtrl::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { //scHorizon.SetScrollPos(nPos); int TempPos = pScrollBar->GetScrollPos(); //SCROLLINFO si; //GetScrollInfo(SB_HORZ, &si, SIF_ALL); //m_ImgVScrollPos = si.nPos; //dbHScroll = si.nPos; dbHScroll = nPos; Invalidate(TRUE); scHorizon.SetScrollPos(nPos); switch (nSBCode) { case SB_THUMBPOSITION://拖动滑块 pScrollBar->SetScrollPos(nPos); break; case SB_LINELEFT://点击左边的箭头 if (TempPos > 1) { TempPos--; } pScrollBar->SetScrollPos(TempPos); break; case SB_LINERIGHT://点击右边的箭头 if (TempPos < 100) { TempPos++; } pScrollBar->SetScrollPos(TempPos); break; } } void CGridCtrl::HScroll(UINT nSBCode, UINT nPos) { scHorizon.SetScrollPos(nPos); } void CGridCtrl::OnPaint() { CWnd::OnPaint(); CClientDC dc(this); CRect cr; CRect rectClient; GetClientRect(&rectClient); //dc.Draw3dRect(0, 0, cr.Width(), cr.Height(), RGB(233, 0, 0), RGB(0, 233, 0)); int nRowHeight = 30; int nColWidth = 150; double dbHorScrollRange = 0; double dbVerScrollRange = 0; cr.SetRect(0, 0, nColWidth, nRowHeight); bool bShowHorizen = false; bool bShowVertical = false; for (int nRow = 0; nRow < vctRows.size(); nRow++) { for (int nCol = 0; nCol < vctRows[nRow].size(); nCol++) { int nCoordX = nCol * nColWidth - dbHScroll; int nCoordY = nRow * nRowHeight; cr.MoveToXY(nCoordX, nCoordY); //cr.OffsetRect(nCoordX, nCoordY); if (nColWidth + nCoordX > rectClient.Width()) { dbHorScrollRange = nColWidth + nCoordX; bShowHorizen = true; } if (nRowHeight + nCoordY > rectClient.Height()) { //dbVerScrollRange = nCo + nCoordX; bShowVertical = true; } CString ss = vctRows[nRow][nCol]; if (cr.PtInRect(mouseP)) { dc.FillSolidRect(&cr, clrSelect); //dc.Draw3dRect(&cr, RGB(255, 0, 0), RGB(13, 255, 312)); // dc.SetBkColor(RGB(200, 200, 200)); } else { dc.FillSolidRect(&cr, clrBK); } // dc.MoveTo(nCoordX, nCoordY); dc.LineTo(nCoordX, nCoordY + nRowHeight); dc.MoveTo(nCoordX, nCoordY); dc.LineTo(nCoordX + nColWidth, nCoordY); dc.DrawText(vctRows[nRow][nCol], &cr, DT_CENTER | DT_SINGLELINE | DT_VCENTER); } } /*CRect ell; ell.SetRect(-10, -10, 10, 10); ell.MoveToXY(mouseP.x - 10, mouseP.y - 10); dc.Ellipse(ell);*/ if (bShowHorizen) { CRect cr; cr.SetRect(0, rectClient.Height() - 20, rectClient.Width(), rectClient.Height()); scHorizon.MoveWindow(cr); scHorizon.ShowScrollBar(); scHorizon.SetScrollRange(0, dbHorScrollRange); //scHorizon.SetScrollPos(3); //this->EnableScrollBar(WS_VSCROLL); } /*if (bShowVertical) { CRect cr; cr.SetRect(rectClient.Width() - 40, 0, rectClient.Width(), rectClient.Height()); scVertical.MoveWindow(cr); scVertical.ShowScrollBar(); }*/ } void CGridCtrl::OnLButtonDown(UINT nFlags, CPoint point) { CWnd::OnMButtonDown(nFlags, point); mouseP = point; Invalidate(); } void CGridCtrl::OnLButtonDblClk(UINT nFlags, CPoint point) { CWnd::OnLButtonDblClk(nFlags, point); mouseP = point; Invalidate(); } void CGridCtrl::OnLButtonUp(UINT nFlags, CPoint point) { } void CGridCtrl::InsertCol(LPCTSTR lpctRow) { m_nCurRow = vctRows.size(); } bool CGridCtrl::RegisterCtrl() { WNDCLASS w; memset(&w, 0, sizeof(WNDCLASS)); // start with NULL defaults w.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW; w.lpszClassName = GetClassName(); w.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW); w.hIcon = NULL;//GetIcon(FALSE); w.hInstance = AfxGetInstanceHandle(); w.lpszMenuName = NULL;//_T("Menu"); w.cbClsExtra = w.cbWndExtra = 0; w.hbrBackground = (HBRUSH)(COLOR_WINDOWFRAME + 1);//::GetSysColor(COLOR_WINDOW); w.lpfnWndProc = (WNDPROC)AfxWndProc;//::DefWindowProc;//(WNDPROC)CGridCtrl::GridCtrlProc; if (!AfxRegisterClass(&w)) { AfxThrowResourceException(); return false; } return true; } LRESULT CGridCtrl::GridCtrlProc(HWND hWnd, UINT msgType, WPARAM wParam, LPARAM lParam) { return AfxWndProc(hWnd, msgType, wParam, lParam); }
以上是关于封装的grid控件的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段14——Vue的axios网络请求封装