码迷,mamicode.com
首页 > 编程语言 > 详细

MFC 完全自定义控件

时间:2020-06-23 19:01:05      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:static   client   move   定义   with   wm_paint   span   text   Once   

头文件

#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 完全自定义控件

标签:static   client   move   定义   with   wm_paint   span   text   Once   

原文地址:https://www.cnblogs.com/yang131/p/13183643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!