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

📄 gridsvw5.cpp

📁 mfc internals 源码 mfc internals 源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// gridsvw5.cpp : implementation of the CGridSample5View class
//

// This is a part of the Objective Grid C++ Library.
// Copyright (C) 1995 ClassWorks, Stefan Hoenig.
// All rights reserved.
//
// This source code is only intended as a supplement to
// the Objective Grid Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding
// the Objective Grid product.
//

#include "stdafx.h"
#include "gridapp.h"

#include "gridsdoc.h"
#include "gridsvw5.h"
#include "dlguser.h"
#include "mainfrm.h"
#include "gridfrms.h"

#include "gxtwnd.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

//
// CGridSample5View can be used as standalone, splitter or worksheet gridview
// as you have already seen with CGridSampleView
//
// CGridSample5View illustrates applying controls to cells or deriving
// and register韓g your own controls. It also illustrates how to
// use comboboxes in column headers.
//
// CSimpleButton provides a button control you can use a prototype
// for creating your own controls.
//
// CBitmapBtnEdit provides a sample how to use small buttons in a control
//
// CArrowRowHeader is a control you can apply to ro headers. It will
// display an arrow if the row owns the current cell.
//

IMPLEMENT_DYNCREATE(CGridSample5View, CMyGridView)
IMPLEMENT_CONTROL(CSimpleButton, CGXStatic)
IMPLEMENT_CONTROL(CBitmapBtnEdit, CGXEditControl)
IMPLEMENT_CONTROL(CArrowRowHeader, CGXControl)
IMPLEMENT_CONTROL(COwnerDrawnComboBox, CGXComboBoxWnd)
IMPLEMENT_CONTROL(CMyComboBox, CGXComboBox)

static char BASED_CODE szInstruct[] =
		"This view shows standard and user defined controls and special headers. "
		"You can apply controls to cells by calling Format->Cells->Control "
		"or by changing the Control setting of a base style (Format->Styles).";

#define new DEBUG_NEW


/////////////////////////////////////////////////////////////////////////////
// CSimpleButton control

CSimpleButton::CSimpleButton(CGXGridCore* pGrid)
	: CGXStatic(pGrid)
{
	m_bPressed = FALSE;
}

CSimpleButton::~CSimpleButton()
{
}

void CSimpleButton::Draw(CDC* pDC, CRect rect, ROWCOL nRow, ROWCOL nCol, const CGXStyle& style, const CGXStyle* pStandardStyle)
{
	WORD w = 0;

	// Select font
	CFont* pOldFont = LoadFont(pDC, style, pStandardStyle);

	BOOL bActive = (m_nRow == nRow && m_nCol == nCol && !Grid()->IsPrinting()
				&& Grid()->IsActiveFrame());

	if (bActive)
	{
		w |= GX_BTNFOCUS;

		if (m_bPressed)
			w |= GX_BTNPRESSED;
	}


	// start of drawing buttons technique 1 ->

	CRect rectText = GetCellRect(nRow, nCol, rect);

	// Background
	DrawBackground(pDC, rect, style);

	// Draw pressed or button look
	if (style.GetIncludeDraw3dFrame() || style.GetDraw3dFrame() == gxFrameNormal)
	{
		CRect r = CGXControl::GetCellRect(nRow, nCol, rect);

		if (bActive && m_bPressed)
		{
			// Inset
			GXDraw3dFrame(pDC, r.left, r.top, r.right-1, r.bottom-1, 1,
				  RGB(0,0,0), RGB(255,255,255));

			// text will be moved to the bottom-right corner a bit
			rectText += CPoint(1,1);
		}
		else
			// Raised
			GXDraw3dFrame(pDC, r.left, r.top, r.right-1, r.bottom-1, 1,
				  RGB(255,255,255), RGB(0,0,0));
	}

	// Draw static text
	pDC->SetBkMode(TRANSPARENT);

	DWORD dtAlign = style.GetHorizontalAlignment() | style.GetVerticalAlignment();

	if (style.GetWrapText())
		dtAlign |= DT_NOPREFIX | DT_WORDBREAK;
	else
		dtAlign |= DT_NOPREFIX | DT_SINGLELINE;

	pDC->SetTextColor(style.GetTextColor());

	if (style.GetIncludeValue())
		GXDrawTextLikeMultiLineEdit(pDC,
			style.GetValueRef(), strlen(style.GetValueRef()), rectText, (UINT) dtAlign | DT_NOPREFIX | DT_WORDBREAK);

	// <- end of technique 1

/*
	// start of drawing buttons technique 2 ->

	DrawBackground(pDC, rect, style);

	GXDrawPushButton(pDC,
		rect.left, rect.top,
		rect.Width(), rect.Height(),
		w,
		style.GetValueRef());

	// <- end of technique 2
*/

	if (pOldFont)
		pDC->SelectObject(pOldFont);
}

BOOL CSimpleButton::LButtonDown(UINT nFlags, CPoint pt, UINT nHitState)
{
	m_bMouseDown = TRUE;
	m_bPressed = TRUE;
	Refresh();

	// unreferenced:
	nFlags, pt, nHitState;

	return TRUE;
}

BOOL CSimpleButton::RButtonDown(UINT nFlags, CPoint pt, UINT nHitState)
{
	m_bMouseDown = TRUE;
	m_bPressed = TRUE;
	Refresh();

	// unreferenced:
	nFlags, pt, nHitState;

	return TRUE;
}

BOOL CSimpleButton::LButtonUp(UINT nFlags, CPoint pt, UINT nHitState)
{
	nFlags, pt;

	CRect rect = CGXControl::GetCellRect(m_nRow, m_nCol);

	if (m_bMouseDown && m_bPressed && rect.PtInRect(pt))
		OnClickedButton(NULL);

	m_bPressed = FALSE;
	m_bMouseDown = FALSE;
	Refresh();

	// unreferenced:
	nFlags, pt, nHitState;

	return TRUE;
}

BOOL CSimpleButton::RButtonUp(UINT nFlags, CPoint pt, UINT nHitState)
{
	nFlags, pt;

	CRect rect = CGXControl::GetCellRect(m_nRow, m_nCol);

	if (m_bMouseDown && m_bPressed && rect.PtInRect(pt))
		AfxMessageBox("Right button clicked!\n");

	m_bPressed = FALSE;
	m_bMouseDown = FALSE;
	Refresh();

	// unreferenced:
	nFlags, pt, nHitState;

	return TRUE;
}

BOOL CSimpleButton::MouseMove(UINT nFlags, CPoint pt, UINT nHitState)
{
	nFlags, pt;

	CRect rect = CGXControl::GetCellRect(m_nRow, m_nCol);

	BOOL bState = rect.PtInRect(pt);

	if (m_bMouseDown && bState != m_bPressed)
	{
		m_bPressed = bState;
		Refresh();
	}

	// unreferenced:
	nFlags, pt, nHitState;

	return TRUE;
}

BOOL CSimpleButton::KeyPressed(UINT nMessage, UINT nChar, UINT nRepCnt, UINT flags)
{
	// unused:
	nMessage, nRepCnt, flags;

	if (nChar == 32 && nMessage == WM_KEYDOWN)
	{
		// Draw pressed
		m_bPressed = TRUE;
		Refresh();
	}
	else if (nChar == 32 && nMessage == WM_KEYUP && m_bPressed)
	{
		// trigger event
		OnClickedButton(NULL);

		// Draw normal
		m_bPressed = FALSE;
		Refresh();
	}
	return TRUE;
}

void CSimpleButton::InvertBorders(CDC* pDC, const CRect& r)
{
	// I don't want an inverted frame

	// unreferenced:
	pDC, r;
}

void CSimpleButton::OnClickedButton(CGXChild* pChild)
{
	// Unreferenced parameters:
	pChild;

	// display a message box with the text specified in
	// the user attribut "MessageText" (see the style-sheet)

	AfxMessageBox(Grid()->LookupStyleRowCol(m_nRow, m_nCol)
		.GetUserAttribute(IDS_ATTR_MSGTEXT));
}

/////////////////////////////////////////////////////////////////////////////
// CBitmapBtnEdit control

CBitmapBtnEdit::CBitmapBtnEdit(CGXGridCore* pGrid, UINT nID)
	: CGXEditControl(pGrid, nID)
{
	AddChild(m_pButton = new CGXBitmapButtonChild(this));
	VERIFY(m_pButton->LoadBitmaps(IDB_BITMAP1));

	m_sizeBtn = CSize(14,14);
}

BEGIN_MESSAGE_MAP(CBitmapBtnEdit, CGXEditControl)
	//{{AFX_MSG_MAP(CBitmapBtnEdit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CRect CBitmapBtnEdit::GetCellRect(ROWCOL nRow, ROWCOL nCol, LPRECT rectItem /* = NULL */, const CGXStyle* pStyle /*= NULL*/)
{
	// compute the interior rectangle for the text
	// without buttons and borders

	CRect rect = CGXEditControl::GetCellRect(nRow, nCol, rectItem, pStyle);

	rect.left += m_sizeBtn.cx+3;

	return rect;
}

void CBitmapBtnEdit::OnInitChilds(ROWCOL nRow, ROWCOL nCol, const CRect& rect)
{
	nRow, nCol;

	int nTop = (max(0, rect.Height() - m_sizeBtn.cy)) / 2;

	// init BitmapBtn button
	CRect rectBtn;
	rectBtn.IntersectRect(rect,
				CRect(rect.left+3,
					rect.top + nTop,
					rect.left+3+m_sizeBtn.cx,
					rect.top+ nTop + m_sizeBtn.cy)
				);

	m_pButton->SetRect(rectBtn);
}

void CBitmapBtnEdit::OnClickedButton(CGXChild* pChild)
{
	pChild;
	AfxMessageBox("You pressed the bitmap");
};

BOOL CBitmapBtnEdit::OnValidate()
{
	CString s;
	GetWindowText(s);
	if (atoi(s) > 100)
	{
		Grid()->SetWarningText("Values greater 100 are invalid!");
		return FALSE;
	}
	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CArrowRowHeader

CArrowRowHeader::CArrowRowHeader(CGXGridCore* pGrid)
	: CGXControl(pGrid)
{
}

void CArrowRowHeader::Draw(CDC* pDC, CRect rect, ROWCOL nRow, ROWCOL nCol, const CGXStyle& style, const CGXStyle* pStandardStyle)
{
	// no arrow needed when printing
	if (Grid()->IsPrinting())
		return;

	// Cell-Color
	COLORREF rgbText = style.GetTextColor();
	COLORREF rgbCell = style.GetInteriorRef().GetColor();

	BOOL bArrow = nRow > 0 && Grid()->IsCurrentCell(nRow);

	GXDrawButton(pDC, rect.left, rect.top,
		rect.Width(), rect.Height(), FALSE, rgbCell);

	const int nSize = 9;

	if (bArrow && rect.Width() > nSize && rect.Height() > nSize)
	{
		int x = rect.left + (rect.Width() - nSize) / 2;
		int y = rect.top + (rect.Height() - nSize) / 2;

		GXPatB(pDC, x++, y++, 1, 9, rgbText);
		GXPatB(pDC, x++, y++, 1, 7, rgbText);
		GXPatB(pDC, x++, y++, 1, 5, rgbText);
		GXPatB(pDC, x++, y++, 1, 3, rgbText);
		GXPatB(pDC, x++, y++, 1, 1, rgbText);
	}

	CGXControl::Draw(pDC, rect, nRow, nCol, style, pStandardStyle);
}

/////////////////////////////////////////////////////////////////////////////
// CMyComboBox control

CMyComboBox::CMyComboBox(CGXGridCore* pGrid, UINT nEditID, UINT nListBoxID, UINT nFlags)
	: CGXComboBox(pGrid, nEditID, nListBoxID, nFlags)
{
}

void CMyComboBox::Init(ROWCOL nRow, ROWCOL nCol)
{
	CGXComboBox::Init(nRow, nCol);

	// If you don't want to let the user change the text in the edit control
	// you should call
	//
	// SetReadOnly(TRUE);
}

BOOL CMyComboBox::OnCommand(WPARAM wParam, LPARAM lParam)
{
#if _MFC_VER < 0x0300
	UINT nNotification = HIWORD(lParam);
	HWND hCtl = (HWND) LOWORD(lParam);
#else
	UINT nNotification = HIWORD(wParam);
	HWND hCtl = (HWND) lParam;
#endif

	if (hCtl == m_hWnd)
	{
		// Edit Control changed
	}
	else if (GetDroppedState())
	{
		// Listbox changed
		if (nNotification == LBN_SELCHANGE)
			TRACE("Listbox changed\n" );
		else
			TRACE("Listbox notification %d\n", nNotification );
	}

	return CGXComboBox::OnCommand(wParam, lParam);
}




/////////////////////////////////////////////////////////////////////////////
// COwnerDrawnComboBox

static COLORREF VGAColorsArray[20] = {
		RGB(0,0,0),         // Black
		RGB(0,0,255),       // Bright blue
		RGB(0,255,0),       // Bright green
		RGB(0,255,255),     // Cyan
		RGB(255,0,0),       // Bright red
		RGB(255,0,255),     // Magenta
		RGB(255,255,0),     // Bright yellow
		RGB(255,255,255),   // White
		RGB(0,0,128),       // Dark blue
		RGB(0,128,0),       // Dark green
		RGB(0,128,128),     // Blue-green
		RGB(128,0,0),       // Brown
		RGB(128,0,128),     // Dark purple
		RGB(128,128,0),     // Olive
		RGB(128,128,128),   // Dark gray
		RGB(192,192,192),   // Light gray
		RGB(192,220,192),   // Pale green
		RGB(166,202,240),   // Light blue
		RGB(255,251,240),   // Off-white
		RGB(160,160,164),   // Medium gray
	};

COwnerDrawnComboBox::COwnerDrawnComboBox(CGXGridCore* pGrid)
	: CGXComboBoxWnd(pGrid)
{
	m_bFillWithChoiceList = FALSE;  // must be FALSE for ownderdrawn combobox
	m_bWantArrowKeys = FALSE;       // pass arrow keys to Grid
	m_nIndexValue = 0;              // 0 = Item 0, 1 = Item 1, ... n = Item N

	// set m_nIndexValue = 1 if you want to have 1 = Item 0, 2 = Item 1, ... n = Item N+1
}

COwnerDrawnComboBox::~COwnerDrawnComboBox()
{
}

void COwnerDrawnComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CRect rc = lpDrawItemStruct->rcItem;
	COLORREF rgbColor = lpDrawItemStruct->itemData;
	CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

	GXPatB(pDC, rc, rgbColor);

	if (lpDrawItemStruct->itemState & ODS_SELECTED)
	{
		CBrush br;
		br.CreateStockObject(BLACK_BRUSH);
		pDC->FrameRect(CRect(rc.left, rc.top, rc.right, rc.bottom), &br);
	}
}

⌨️ 快捷键说明

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