📄 formitem.cpp
字号:
// FormItem.cpp: implementation of the CFormItem class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FormItem.h"
#include "FormListCtrl.h"
#include "DrawTextEx.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
using namespace DRA;
//---------------------------------------------------------------------------
//
// CFormItem
//
//---------------------------------------------------------------------------
CFormItem::CFormItem()
: m_dwFlags (FIF_NORMAL)
{
}
CFormItem::~CFormItem()
{
}
//---------------------------------------------------------------------------
//
// CFormItem operations
//
//---------------------------------------------------------------------------
// CFormItem::Show
//
// Shows or hides an item
//
void CFormItem::Show(BOOL bShow)
{
if(bShow)
m_dwFlags |= FIF_VISIBLE;
else
m_dwFlags &= ~FIF_VISIBLE;
}
// CFormItem::Enable
//
// Enables or disables an item
//
void CFormItem::Enable(BOOL bEnable)
{
if(bEnable)
m_dwFlags |= FIF_ENABLED;
else
m_dwFlags &= ~FIF_ENABLED;
}
// CFormItem::DrawOptions
//
// Enables or disables the drawing of active option's button
//
void CFormItem::DrawOptions(BOOL bEnable)
{
if(bEnable)
m_dwFlags |= FIF_DRAWOPTIONS;
else
m_dwFlags &= ~FIF_DRAWOPTIONS;
}
// CFormItem::DrawFocus
//
// Draws the focus rectangle
//
void CFormItem::DrawFocus(HDC hDC, RECT* prc)
{
if(IsSelected())
{
HPEN hNewPen,
hOldPen;
HBRUSH hNewBrx,
hOldBrx;
RECT rc;
CopyRect(&rc, prc);
rc.left += SCALEX(4);
rc.bottom -= SCALEY(1);
hNewPen = ::CreatePen(PS_DASH, 1, RGB(0, 0, 192));
hNewBrx = (HBRUSH)::GetStockObject(NULL_BRUSH);
hOldPen = (HPEN)::SelectObject(hDC, hNewPen);
hOldBrx = (HBRUSH)::SelectObject(hDC, hNewBrx);
DRA::Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
::SelectObject(hDC, hOldBrx);
::SelectObject(hDC, hOldPen);
}
}
// CFormItem::DrawBottomLine
//
// Draws the underline
//
void CFormItem::DrawBottomLine(NMLVCUSTOMDRAW* pLVCD)
{
HPEN hPenNew,
hPenOld;
POINT pt[2];
HDC hDC = pLVCD->nmcd.hdc;
RECT rc = pLVCD->nmcd.rc;
hPenNew = ::CreatePen(PS_SOLID, 1, RGB(192, 192, 192));
hPenOld = (HPEN) ::SelectObject(hDC, hPenNew);
pt[0].x = rc.left + SCALEX(4);
pt[1].x = rc.right;
pt[0].y = pt[1].y = rc.bottom - 1;
::Polyline(hDC, pt, 2);
::SelectObject(hDC, hPenOld);
::DeleteObject(hPenNew);
}
// CFormItem::DrawArrow
//
// Draws a combobox-like arrow
//
void CFormItem::DrawArrow(HDC hDC, const CRect &rc, BOOL bSelected)
{
HBRUSH hBrushBack,
hBrushNew,
hBrushOld;
HPEN hPenNew,
hPenOld;
int x = rc.Width() / 2 - SCALEX(4) + rc.left,
y = rc.Height() / 2 + rc.top - SCALEY(2);
POINT pt[2*4] ={{x,y}, {x+SCALEX(6),y}, {x+SCALEX(3),y+SCALEY(3)}};
//
// Paint the background
//
hBrushBack = ::CreateSolidBrush(bSelected ? RGB(0,0,0) : RGB(255,255,255));
::FillRect(hDC, &rc, hBrushBack);
//
// Create the painting pen
//
hPenNew = ::CreatePen(PS_SOLID, 1, bSelected ? RGB(255,255,255) : RGB(0,0,0));
hPenOld = (HPEN)::SelectObject(hDC, hPenNew);
//
// Create the brush
//
hBrushNew = ::CreateSolidBrush(bSelected ? RGB(255,255,255) : RGB(0,0,0));
hBrushOld = (HBRUSH)::SelectObject(hDC, hBrushNew);
::Polygon(hDC, pt, 3);
//
// Cleanup
//
::SelectObject(hDC, hPenOld);
::SelectObject(hDC, hBrushOld);
::DeleteObject(hPenNew);
::DeleteObject(hBrushNew);
::DeleteObject(hBrushBack);
}
//---------------------------------------------------------------------------
//
// CFormItem virtual methods
//
//---------------------------------------------------------------------------
// CFormItem::ValidateData
//
// By default, say data is ok
//
BOOL CFormItem::ValidateData()
{
return TRUE;
}
// CFormItem::InsertOn
//
// Inserts the item on the form list
//
int CFormItem::InsertOn(CFormListCtrl* pForm, int iItem)
{
int iIndex;
LVITEM lvi;
ASSERT(pForm);
if(iItem == -1)
iItem = pForm->GetItemCount();
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iItem = iItem;
lvi.iSubItem = 0;
lvi.pszText = LPSTR_TEXTCALLBACK;
lvi.lParam = (LPARAM)this;
iIndex = pForm->InsertItem(&lvi);
return iIndex;
}
// CFormItem::GetControlData
//
// Get data from the control
//
void CFormItem::GetControlData()
{
}
// CFormItem::RenderData
//
// Default implementation for data rendering
//
LPCTSTR CFormItem::RenderData(LV_DISPINFO *pDispInfo)
{
LPCTSTR pszData = _T("");
int iSubItem;
if(!pDispInfo) // If pointer is NULL
iSubItem = 1; // Assume rendering data
else
iSubItem = pDispInfo->item.iSubItem;
//
// Assume only the label data. Field data will be rendered
// by the derived class.
//
if(iSubItem == 0)
pszData = m_strCaption;
return pszData;
}
// CFormItem::DrawCaption
//
// Draws the caption part of the item
//
void CFormItem::DrawCaption(CFormListCtrl *pForm, NMLVCUSTOMDRAW *pLVCD)
{
HDC hDC = pLVCD->nmcd.hdc; // Display context
RECT rc = pLVCD->nmcd.rc; // Item rect
DWORD dwItemSpec = pLVCD->nmcd.dwItemSpec;
COLORREF rgbOldText;
HFONT hFontOld;
//
// Check if we need to draw the options arrow
//
if(m_dwFlags & FIF_HASOPTIONS)
{
CRect rcArrow(rc);
BOOL bEnable;
rc.left += SCALEX(10);
rcArrow.left = 0;
rcArrow.right = SCALEX(12);
bEnable = (int)dwItemSpec == pForm->GetEditIndex() &&
(m_dwFlags & FIF_DRAWOPTIONS);
DrawArrow(hDC, rcArrow, bEnable);
}
//
// Check if the caption is highlighted
//
if((int)dwItemSpec == pForm->GetEditIndex())
{
HBRUSH hBrushNew = NULL,
hBrushOld = NULL;
HPEN hPenNew = NULL,
hPenOld = NULL;
hBrushNew = ::CreateSolidBrush(RGB(200, 200, 255));
hPenNew = ::CreatePen(PS_SOLID, 1, RGB(200, 200, 255));
if(hBrushNew && hPenNew)
{
::SetBkColor(hDC, RGB(200, 200, 255));
hBrushOld = (HBRUSH) ::SelectObject(hDC, hBrushNew);
hPenOld = (HPEN) ::SelectObject(hDC, hPenNew );
::RoundRect(hDC, rc.left, rc.top, rc.right, rc.bottom - 1,
SCALEX(8), SCALEY(8));
if(hPenOld)
::SelectObject(hDC, hPenOld);
if(hBrushOld)
::SelectObject(hDC, hBrushOld);
}
if(hBrushNew)
::DeleteObject(hBrushNew);
if(hPenNew)
::DeleteObject(hPenNew);
}
//
// Write the caption string
//
rc.top++;
rc.right -= SCALEX(4);
if(IsMandatory())
rgbOldText = ::SetTextColor(hDC, RGB(255,0,0));
if(IsUnderlined())
hFontOld = (HFONT)::SelectObject(hDC, *pForm->GetUnderlineFont());
::DrawText(hDC, m_strCaption, -1, &rc, DT_RIGHT | DT_SINGLELINE | DT_TOP);
::SetBkColor(hDC, RGB(255, 255, 255));
if(hFontOld)
::SelectObject(hDC, hFontOld);
if(IsMandatory())
::SetTextColor(hDC, rgbOldText);
}
// CFormItem::DrawContent
//
// Draw the content part of the item
//
void CFormItem::DrawContent(CFormListCtrl *pForm, NMLVCUSTOMDRAW *pLVCD)
{
CString strText = RenderData(NULL);
HFONT hFontOld = NULL;
CFont* pFont = pForm->GetContentFont();
COLORREF rgbBack = pLVCD->clrTextBk,
rgbOldBack;
HBRUSH hBrushBack;
HDC hDC = pLVCD->nmcd.hdc; // Display context
RECT rc = pLVCD->nmcd.rc; // Item rect
//
// If the data is invalid, use a yellow background
//
if(!ValidateData())
rgbBack = RGB(255, 255, 127);
rgbOldBack = ::SetBkColor(hDC, rgbBack);
//
// Paint a solid background
//
rc.left += SCALEX(4);
hBrushBack = ::CreateSolidBrush(rgbBack);
::FillRect(hDC, &rc, hBrushBack);
::DeleteObject(hBrushBack);
rc.left += SCALEX(2);
rc.top++;
if(pFont)
hFontOld = (HFONT) ::SelectObject(hDC, *pFont);
::DrawTextEx(hDC, strText, -1, &rc,
DT_LEFT | DT_SINGLELINE | DT_TOP | DT_END_ELLIPSIS | DT_MODIFYSTRING);
if(hFontOld)
::SelectObject(hDC, hFontOld);
if(m_dwFlags & FIF_LINE)
DrawBottomLine(pLVCD);
::SetBkColor(hDC, rgbOldBack);
DrawFocus(hDC, &pLVCD->nmcd.rc);
}
// CFormItem::CustomDraw
//
// Default behaviour for custom draw
//
LRESULT CFormItem::CustomDraw(CFormListCtrl* pForm, NMLVCUSTOMDRAW *pLVCD)
{
DWORD dwDrawStage = pLVCD->nmcd.dwDrawStage;
LRESULT lRet = CDRF_DODEFAULT;
if(dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))
{
HDC hDC = pLVCD->nmcd.hdc; // Display context
RECT rc = pLVCD->nmcd.rc; // Item rect
COLORREF rgbText, // Text RGB
rgbOldText; // Old text RGB
HFONT hFontOld = NULL;
HPEN hPenWhite = ::CreatePen(PS_SOLID, 1, RGB(255,255,255)),
hPenOld;
HBRUSH hBrushWhite = ::CreateSolidBrush(RGB(255,255,255)),
hBrushOld;
//
// Start by clearing the rect
//
hPenOld = (HPEN) ::SelectObject(hDC, hPenWhite);
hBrushOld = (HBRUSH) ::SelectObject(hDC, hBrushWhite);
DRA::Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
::SelectObject(hDC, hBrushOld);
::SelectObject(hDC, hPenOld);
::DeleteObject(hPenWhite);
//
// Prepare the gray text RGB for disabled items
//
if(IsEnabled())
rgbText = pLVCD->clrText;
else
rgbText = RGB(128, 128, 128); // Grey
rgbOldText = ::SetTextColor(hDC, rgbText);
if(pLVCD->iSubItem == 1)
{
DrawContent(pForm, pLVCD);
}
else
{
DrawCaption(pForm, pLVCD);
}
::SetTextColor(hDC, rgbOldText);
lRet = CDRF_SKIPDEFAULT;
}
return lRet;
}
// CFormItem::ShowEditor
//
// Shows or hides the specialized editor
//
BOOL CFormItem::ShowEditor(CFormListCtrl *pForm, BOOL bShow, int iItem, int iSubItem)
{
return FALSE;
}
// CFormEditor::Select
//
// Selects or deselects an item
//
BOOL CFormItem::Select(CFormListCtrl* pForm, BOOL bSelect, int iItem)
{
if(bSelect)
m_dwFlags |= FIF_SELECTED;
else
m_dwFlags &= ~FIF_SELECTED;
return TRUE;
}
// CFormItem::MoveEditor
//
// Moves the editor to the new position
//
void CFormItem::MoveEditor(const RECT &rcItem)
{
}
// CFormItem::OnCtrlNotify
//
// Handles control notification messages
//
BOOL CFormItem::OnCtrlNotify(CFormListCtrl* pForm, int iItem, WORD wNotify)
{
return FALSE;
}
BOOL CFormItem::OnCmdMsg(UINT nID, int nCode, void *pExtra, AFX_CMDHANDLERINFO *pHandlerInfo)
{
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -