zoukankan      html  css  js  c++  java
  • 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);
    }
  • 相关阅读:
    JS原型链与instanceof底层原理
    流程关系图制作---ProcessOn从入门到精通
    VBA比较两个Excel数据的异同
    C# 通过 Quartz .NET 实现 schedule job 的处理
    C# 通过 Quartz .NET 实现Timer Job并将其注册成为Windows Service
    在.Net Framework中调用Python的脚本方法 (以VB和C#为例)
    用Python建立连接直接读取与更改Rockwell Control Logix Controller的tag值
    C#通过第三方组件生成二维码(QR Code)和条形码(Bar Code)
    如何根据条件来确定某个字段是否应该被序列化
    在Asp.Net MVC 中如何用JS访问Web.Config中appSettings的值
  • 原文地址:https://www.cnblogs.com/yang131/p/13183643.html
Copyright © 2011-2022 走看看