MFC 完全自定义控件

头文件

#pragma once
#include "pch.h"
class CGridCtrl : public CWnd
{
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);
    }

    LPCTSTR GetClassName() {
        return _T("GridCtrl");
    }
    LPCTSTR GetWindowName() {
        return _T("GridCtrl");
    }
    afx_msg void OnPaint();
    DECLARE_MESSAGE_MAP()
private:
    void RegisterCtrl();
    //void(int nRow,int nCol);
    void InsertCol(LPCTSTR lpctRow);
    std::vector  GetRows();
    static bool m_bRegisted;
    static LRESULT GridCtrlProc(HWND, UINT, WPARAM, LPARAM);
    typedef  std::vector Cells;
    std::vector vctRows;
    int m_nCurRow;

};

cpp

#include "pch.h"
#include "CGridCtrl.h"

bool CGridCtrl::m_bRegisted = false;

BEGIN_MESSAGE_MAP(CGridCtrl, CWnd)
    ON_WM_PAINT()
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, rect, pParent, nID);
}

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::OnPaint()
{
    CWnd::OnPaint();
    CClientDC dc(this);
    CRect cr;
    GetClientRect(&cr);
    dc.Draw3dRect(0, 0, cr.Width(), cr.Height(), RGB(233, 0, 0), RGB(0, 233, 0));
    int nRowHeight = 30;
    int nColWidth = 150;
    cr.SetRect(0, 0, nColWidth, nRowHeight);
    for (int nRow = 0; nRow < vctRows.size(); nRow++)
    {
        for (int nCol = 0; nCol < vctRows[nRow].size(); nCol++)
        {
            int nCoordX = nCol * nColWidth;
            int nCoordY = nRow * nRowHeight;
            cr.MoveToXY(nCoordX, nCoordY);
            //dc.Draw3dRect(nCoordX, nCoordY, nColWidth, nRowHeight, RGB(12, 0, 3), RGB(32, 22, 12));
            CString ss;
            ss = vctRows[nRow][nCol];
            dc.DrawText(vctRows[nRow][nCol], cr, DT_CENTER);
        }
    }
}

void CGridCtrl::InsertCol(LPCTSTR lpctRow)
{
    m_nCurRow = vctRows.size();


}



void 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_WINDOW + 1);//::GetSysColor(COLOR_WINDOW);
    w.lpfnWndProc = (WNDPROC)AfxWndProc;//::DefWindowProc;//(WNDPROC)CGridCtrl::GridCtrlProc;
    if (!AfxRegisterClass(&w))
    {
        AfxThrowResourceException();
        return;
    }
}



LRESULT CGridCtrl::GridCtrlProc(HWND hWnd, UINT msgType, WPARAM wParam, LPARAM lParam)
{
    return AfxWndProc(hWnd, msgType, wParam, lParam);
}

 

你可能感兴趣的:(MFC 完全自定义控件)