⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gridctrl.h

📁 一种简单的股票软件源代码,编译后可以实时显示证券行情
💻 H
字号:
#if !defined(AFX_GRIDCTRL_H__768A7058_3D28_11D8_B617_A69FB0BE0671__INCLUDED_)
#define AFX_GRIDCTRL_H__768A7058_3D28_11D8_B617_A69FB0BE0671__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// GridCtrl.h : header file
//

// Grid line/scrollbar selection
#define GVL_NONE                0L      // Neither
#define GVL_HORZ                1L      // Horizontal line or scrollbar
#define GVL_VERT                2L      // Vertical line or scrollbar
#define GVL_BOTH                3L      // Both

#include "GridDefaultCell.h"
#include "CellRange.h"
//#include "GridCell.h"
#include <afxtempl.h>//为了使用数组和Map

class CMyGridFrame;


// storage typedef for each row in the grid
typedef CTypedPtrArray<CObArray, CGridCellBase*> GRID_ROW;

/////////////////////////////////////////////////////////////////////////////
// CGridCtrl window


class CGridCtrl : public CWnd
{
	DECLARE_DYNCREATE(CGridCtrl)

// Construction
public:
	CGridCtrl(int nRows = 0, int nCols = 0, int nFixedRows = 0, int nFixedCols = 0);


// Attributes
public:
	BOOL SetRowCount(int nRows = 10);//设置总行数,缺省为10
	BOOL SetColumnCount(int nCols = 10);//设置总列数
    BOOL SetFixedRowCount(int nFixedRows = 1);//设置固定行数
    BOOL SetFixedColumnCount(int nFixedCols = 1);//固定列数

	int  GetRowCount() const                    { return m_nRows; }//返回...
    int  GetColumnCount() const                 { return m_nCols; }
    int  GetFixedRowCount() const               { return m_nFixedRows; }
    int  GetFixedColumnCount() const            { return m_nFixedCols; }
	void SetHeaderSort(BOOL bSortOnClick = TRUE){ m_bSortOnClick = bSortOnClick;    }
	BOOL GetHeaderSort() const                  { return m_bSortOnClick; }

	int  GetRowHeight(int nRow) const;//返回行高
    BOOL SetRowHeight(int row, int height);//设置行高
    
	int  GetColumnWidth(int nCol) const;//列宽
    BOOL SetColumnWidth(int col, int width);//..
	
	int  GetFixedRowHeight() const;
    int  GetFixedColumnWidth() const;
	long GetVirtualWidth() const;
    long GetVirtualHeight() const;

	//关于操作
	BOOL IsValid(int nRow, int nCol) const;//合法性检验
    BOOL IsValid(const CCellID& cell) const;
    BOOL IsValid(const CCellRange& range) const;

	BOOL IsCellVisible(int nRow, int nCol);
    BOOL IsCellVisible(CCellID cell);

	BOOL GetCellOrigin(int nRow, int nCol, LPPOINT p);
    BOOL GetCellOrigin(const CCellID& cell, LPPOINT p);
	BOOL GetCellRect(int nRow, int nCol, LPRECT pRect);
    BOOL GetCellRect(const CCellID& cell, LPRECT pRect);

    BOOL MouseOverColumnResizeArea(CPoint& point);

	void ClearCells();


	CCellID GetCellFromPt(CPoint point, BOOL bAllowFixedCellCheck = TRUE);//从鼠标点得到单元格ID

	CGridCellBase* GetCell(int nRow, int nCol) const;   // Get the actual cell!

	void Refresh();

	//排序:
	virtual void  OnFixedRowClick(CCellID& cell);
	BOOL SortItems(int nCol, BOOL bAscending, LPARAM data = 0);
    BOOL SortTextItems(int nCol, BOOL bAscending, LPARAM data = 0);
    BOOL SortItems(PFNLVCOMPARE pfnCompare, int nCol, BOOL bAscending, LPARAM data = 0);
    BOOL SortItems(PFNLVCOMPARE pfnCompare, int nCol, BOOL bAscending, LPARAM data,
                   int low, int high);

	// in-built sort functions 内联排序函数
	static int CALLBACK pfnCellTextCompare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
	static int CALLBACK pfnCellNumericCompare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);

	void SetSortColumn(int nCol);//设置排序的列
	int  GetSortColumn() const                    { return m_nSortColumn;             }
	BOOL GetSortAscending() const                 { return m_bAscending;              }
	void SetSortAscending(BOOL bAscending)        { m_bAscending = bAscending;        }



	// ***************************************************************************** //
    // These have been deprecated. Use GetDefaultCell and then set the colors
    void     SetTextColor(COLORREF clr)      { m_cellDefault.SetTextClr(clr);        }
    COLORREF GetTextColor()                  { return m_cellDefault.GetTextClr();    }
    void     SetTextBkColor(COLORREF clr)    { m_cellDefault.SetBackClr(clr);        }
    COLORREF GetTextBkColor()                { return m_cellDefault.GetBackClr();    }
    void     SetFixedTextColor(COLORREF clr) { m_cellFixedRowDef.SetTextClr(clr); 
                                               m_cellFixedColDef.SetTextClr(clr); 
                                               m_cellFixedRowColDef.SetTextClr(clr); }
    COLORREF GetFixedTextColor() const       { return m_cellFixedRowDef.GetTextClr(); }
    void     SetFixedBkColor(COLORREF clr)   { m_cellFixedRowDef.SetBackClr(clr); 
                                               m_cellFixedColDef.SetBackClr(clr); 
                                               m_cellFixedRowColDef.SetBackClr(clr); }
	void     SetGridBkColor(COLORREF clr)         { m_crGridBkColour = clr;           }
    COLORREF GetGridBkColor() const               { return m_crGridBkColour;          }


//    BOOL GetFixedColumnSelection()                { return m_bFixedColumnSelection;   }
	BOOL   SetItemText(int nRow, int nCol, LPCTSTR str);


	BOOL   SetItemState(int nRow, int nCol, UINT state);
    UINT   GetItemState(int nRow, int nCol) const;
	BOOL   SetItemBkColor(int nRow, int nCol, COLORREF cr = CLR_DEFAULT);
    COLORREF GetItemBkColor(int nRow, int nCol) const;
    BOOL   SetItemFgColor(int nRow, int nCol, COLORREF cr = CLR_DEFAULT);
    COLORREF GetItemFgColor(int nRow, int nCol) const;


// Operations
public:


// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CGridCtrl)
	public:
	virtual CScrollBar* GetScrollBarCtrl(int nBar) const;
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CGridCtrl();
	CUIntArray  m_arRowHeights, m_arColWidths;

public:
	void EraseBkgnd(CDC* pDC);
	void OnDraw(CDC* pDC);

	BOOL RedrawCell(int nRow, int nCol, CDC* pDC = NULL);
    BOOL RedrawCell(const CCellID& cell, CDC* pDC = NULL);
	BOOL RedrawRow(int row);

    CGridCellBase* GetDefaultCell(BOOL bFixedRow, BOOL bFixedCol) const;
	// Generated message map functions
protected:
	//{{AFX_MSG(CGridCtrl)
	afx_msg void OnPaint();
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
	afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
	afx_msg void OnTimer(UINT nIDEvent);
	afx_msg void OnDestroy();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()

protected:

	CRect m_FillRect;//高亮显示按钮位置

	CString m_shstr,m_szstr,m_bkstr,m_zxstr,m_qtstr,m_tjstr;

	// General attributes
    COLORREF    m_crFixedTextColour, m_crFixedBkColour;
    COLORREF    m_crGridBkColour, m_crGridLineColour;
    COLORREF    m_crWindowText, m_crWindowColour, m_cr3DFace,     // System colours
                m_crShadow;

	BOOL        m_bDoubleBuffer;
	BOOL        m_bSortOnClick;

	// Cell size details
    int  m_nRows, m_nFixedRows, m_nCols, m_nFixedCols;//总行数/固定行数/总列数/固定列数

	int         m_nVScrollMax, m_nHScrollMax;//滚动条最大值
	int         m_nBarState;//滚动状态
	

	int  m_nGridLines;//表格线

	BOOL        m_bAllowColHide, m_bAllowRowHide;

	//Mouse operations such as cell selection
	CCellID     m_LeftClickDownCell, m_SelectionStartCell,m_SelectedCell;
    CCellID     m_idCurrentCell, m_idTopLeftCell;//当前单元格ID,左上角单元格ID


	// Fonts and images
    CRuntimeClass*   m_pRtcDefault; // determines kind of Grid Cell created by default
    CGridDefaultCell m_cellDefault;  // "default" cell. Contains default colours, font etc.
    CGridDefaultCell m_cellFixedColDef, m_cellFixedRowDef, m_cellFixedRowColDef;

	// Cell data
    CTypedPtrArray<CObArray, GRID_ROW*> m_RowData;

	// Mouse operations such as cell selection
//    int         m_MouseMode;
    BOOL        m_bLMouseButtonDown, m_bRMouseButtonDown;
    CPoint      m_LeftClickDownPoint, m_LastMousePoint;

    int         m_nTimerID;
    int         m_nTimerInterval;
    int         m_nResizeCaptureRange;
    BOOL        m_bAllowRowResize, m_bAllowColumnResize;
    int         m_nRowsPerWheelNotch;

	BOOL        m_bColSizing;//改变列大小
	BOOL        m_bAllowDraw;
//	BOOL        m_bFixedRowSelection, m_bFixedColumnSelection;

	// sorting
    int         m_bAscending;
	int         m_nSortColumn;
	PFNLVCOMPARE m_pfnCompare;//排序函数的指针

protected:

	void SetupDefaultCells();

	//关于滚动条:
	void ResetScrollBars();//重值滚动条
	void EnableScrollBars(int nBar, BOOL bEnable = TRUE);//允许有滚动条
    BOOL IsVisibleVScroll() { return ( (m_nBarState & GVL_VERT) > 0); } //??
    BOOL IsVisibleHScroll() { return ( (m_nBarState & GVL_HORZ) > 0); } //??
	int  GetScrollPos32(int nBar, BOOL bGetTrackPos = FALSE);//得到滚动指针
    BOOL SetScrollPos32(int nBar, int nPos, BOOL bRedraw = TRUE);//设置滚动指针

	// CGridCellBase Creation and Cleanup
    virtual CGridCellBase* CreateCell(int nRow, int nCol);
	virtual void DestroyCell(int nRow, int nCol);

	BOOL SetCell(int nRow, int nCol, CGridCellBase* pCell);
	CCellID GetTopleftNonFixedCell(BOOL bForceRecalculation = FALSE);//得到左上角非固定单元格
	CCellRange GetVisibleNonFixedCellRange(LPRECT pRect = NULL, BOOL bForceRecalculation = FALSE);//可见部分单元格范围


};

// Returns the default cell implementation for the given grid region
inline CGridCellBase* CGridCtrl::GetDefaultCell(BOOL bFixedRow, BOOL bFixedCol) const
{ 
    //三个返回对象都是CGridDefaultCell的变量,只是初始化时对颜色设了不同值.
	//
	if (bFixedRow && bFixedCol) return (CGridCellBase*) &m_cellFixedRowColDef;
    if (bFixedRow)              return (CGridCellBase*) &m_cellFixedRowDef;
    if (bFixedCol)              return (CGridCellBase*) &m_cellFixedColDef;
    return (CGridCellBase*) &m_cellDefault;
}

inline CGridCellBase* CGridCtrl::GetCell(int nRow, int nCol) const
{
//这个函数返回Grid中一个Cell的指针:

	    
	if (nRow < 0 || nRow >= m_nRows || nCol < 0 || nCol >= m_nCols)//如果传进来的行列号非法
		return NULL;                                               //就返回空

    GRID_ROW* pRow = m_RowData[nRow];
    if (!pRow) return NULL;
    return pRow->GetAt(nCol);//传回数组中的一个元素
}

inline BOOL CGridCtrl::SetCell(int nRow, int nCol, CGridCellBase* pCell)
{
//    if (GetVirtualMode())
//        return FALSE;

    if (nRow < 0 || nRow >= m_nRows || nCol < 0 || nCol >= m_nCols) 
        return FALSE;

    GRID_ROW* pRow = m_RowData[nRow];
    if (!pRow) return FALSE;

//    pCell->SetCoords( nRow, nCol); 
    pRow->SetAt(nCol, pCell);

    return TRUE;
}

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_GRIDCTRL_H__768A7058_3D28_11D8_B617_A69FB0BE0671__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -