MFC 完全自定义控件

Posted yang131

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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<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;

};

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 完全自定义控件的主要内容,如果未能解决你的问题,请参考以下文章

圆规的 MFC C++ 自定义控件

MFC如何添加自定义控件

自定义 MFC 控件包含另一个控件 - 消息未通过

MFC GUI自定义控件:如何绘制光标更新以响应鼠标移动?

MFC——table控件的使用

如何在 MFC 自定义控件类中挂钩控件关闭