📄 listctrlstyled.cpp
字号:
void CListCtrlStyled::SetRowSelectedFont(int nRow,CFont * pFont,bool redraw)
{ // We must retrieve the Style info structure of this item
//
LVITEM pItem;
InitLVITEM(nRow,0,&pItem);
LS_item * lpLS_item = NULL;
lpLS_item = (LS_item*) pItem.lParam;
// Verify if a style for this Row already exist or not
//
LS_item * lpLS_row = NULL;
lpLS_row = lpLS_item->row_style;
if(lpLS_row == NULL)
{ // We must create one
//
lpLS_row = new LS_item;
this->Init_LS_item(lpLS_row,false);
// attach to the item
//
lpLS_item->row_style = lpLS_row;
}
// Take the selected style structure
//
if(lpLS_row->selected_style == NULL)
{ // Create a structure style
//
lpLS_row->selected_style = new LS_item;
this->Init_LS_item( lpLS_row->selected_style );
}
lpLS_row = lpLS_row->selected_style;
// if any internal font exist for this item then delete it
//
this->Free_LS_font(lpLS_row);
// no we can update the style
//
lpLS_row->cfont = pFont;
lpLS_row->ifont = false;
lpLS_row->in_use = true;
// Redraw it
if(redraw) CListCtrl::Update(nRow);
}
// **********************************
// ** New STYLE Methods on Columns **
// **********************************
void CListCtrlStyled::SetColStyle(int nCol,DWORD Style,bool redraw)
{
// Verify if a style for this Row already exist or not
//
LS_item * lpLS_col = NULL;
lpLS_col = this->columns[nCol];
if(lpLS_col == NULL)
{ // We must create one
//
lpLS_col = new LS_item;
this->Init_LS_item(lpLS_col,false);
// attach to Array
//
this->columns.SetAt(nCol, lpLS_col);
}
// no we can update the style
//
lpLS_col->StyleFlag = Style;
DWORD mask = LIS_BOLD | LIS_ITALIC | LIS_UNDERLINE| LIS_STROKE ;
lpLS_col->in_use = (Style & mask) > 0;
// if any font exist for this item then delete it
//
this->Free_LS_font(lpLS_col);
// Redraw it
if(redraw)
{ this->RedrawItems(0, this->GetItemCount() );
this->UpdateWindow();
}
}
void CListCtrlStyled::SetColTxtColor(int nCol,COLORREF txtColor,bool redraw)
{
// Verify if a style for this Row already exist or not
//
LS_item * lpLS_col = NULL;
lpLS_col = this->columns[nCol];
if(lpLS_col == NULL)
{ // We must create one
//
lpLS_col = new LS_item;
this->Init_LS_item(lpLS_col,false);
// attach to Array
//
this->columns.SetAt(nCol, lpLS_col);
}
// no we can update the style
//
lpLS_col->txtColor = txtColor;
// Redraw it
if(redraw)
{ this->RedrawItems(0, this->GetItemCount() );
this->UpdateWindow();
}
}
void CListCtrlStyled::SetColBgColor(int nCol,COLORREF txtBgColor,bool redraw)
{
// Verify if a style for this Row already exist or not
//
LS_item * lpLS_col = NULL;
lpLS_col = this->columns[nCol];
if(lpLS_col == NULL)
{ // We must create one
//
lpLS_col = new LS_item;
this->Init_LS_item(lpLS_col,false);
// attach to Array
//
this->columns.SetAt(nCol, lpLS_col);
}
// no we can update the style
//
lpLS_col->bgColor = txtBgColor;
// Redraw it
if(redraw)
{ this->RedrawItems(0, this->GetItemCount() );
this->UpdateWindow();
}
}
void CListCtrlStyled::SetColFont(int nCol,CFont * pFont,bool redraw)
{
// Verify if a style for this Row already exist or not
//
LS_item * lpLS_col = NULL;
lpLS_col = this->columns[nCol];
if(lpLS_col == NULL)
{ // We must create one
//
lpLS_col = new LS_item;
this->Init_LS_item(lpLS_col,false);
// attach to Array
//
this->columns.SetAt(nCol, lpLS_col);
}
// if any internal font exist for this item then delete it
//
this->Free_LS_font(lpLS_col);
// no we can update the style
//
lpLS_col->cfont = pFont;
lpLS_col->ifont = false;
lpLS_col->in_use = true;
// Redraw it
if(redraw)
{ this->RedrawItems(0, this->GetItemCount() );
this->UpdateWindow();
}
}
// *******************************************
// ** New STYLE Methods on Columns SELECTED **
// *******************************************
void CListCtrlStyled::SetColSelectedStyle(int nCol,DWORD Style,bool redraw)
{
// Verify if a style for this Col already exist or not
//
LS_item * lpLS_col = NULL;
lpLS_col = this->columns[nCol];
if(lpLS_col == NULL)
{ // We must create one
//
lpLS_col = new LS_item;
this->Init_LS_item(lpLS_col,false);
// attach to Array
//
this->columns.SetAt(nCol, lpLS_col);
}
// Take the selected style structure
//
if(lpLS_col->selected_style == NULL)
{ // Create a structure style
//
lpLS_col->selected_style = new LS_item;
this->Init_LS_item( lpLS_col->selected_style );
}
lpLS_col = lpLS_col->selected_style;
// no we can update the style
//
lpLS_col->StyleFlag = Style;
DWORD mask = LIS_BOLD | LIS_ITALIC | LIS_UNDERLINE| LIS_STROKE ;
lpLS_col->in_use = (Style & mask) > 0;
// if any font exist for this item then delete it
//
this->Free_LS_font(lpLS_col);
// Redraw it
if(redraw)
{ this->RedrawItems(0, this->GetItemCount() );
this->UpdateWindow();
}
}
void CListCtrlStyled::SetColSelectedTxtColor(int nCol,COLORREF txtColor,bool redraw)
{
// Verify if a style for this Row already exist or not
//
LS_item * lpLS_col = NULL;
lpLS_col = this->columns[nCol];
if(lpLS_col == NULL)
{ // We must create one
//
lpLS_col = new LS_item;
this->Init_LS_item(lpLS_col,false);
// attach to Array
//
this->columns.SetAt(nCol, lpLS_col);
}
// Take the selected style structure
//
if(lpLS_col->selected_style == NULL)
{ // Create a structure style
//
lpLS_col->selected_style = new LS_item;
this->Init_LS_item( lpLS_col->selected_style );
}
lpLS_col = lpLS_col->selected_style;
// no we can update the style
//
lpLS_col->txtColor = txtColor;
// Redraw it
if(redraw)
{ this->RedrawItems(0, this->GetItemCount() );
this->UpdateWindow();
}
}
void CListCtrlStyled::SetColSelectedBgColor(int nCol,COLORREF txtBgColor,bool redraw)
{
// Verify if a style for this Row already exist or not
//
LS_item * lpLS_col = NULL;
lpLS_col = this->columns[nCol];
if(lpLS_col == NULL)
{ // We must create one
//
lpLS_col = new LS_item;
this->Init_LS_item(lpLS_col,false);
// attach to Array
//
this->columns.SetAt(nCol, lpLS_col);
}
// Take the selected style structure
//
if(lpLS_col->selected_style == NULL)
{ // Create a structure style
//
lpLS_col->selected_style = new LS_item;
this->Init_LS_item( lpLS_col->selected_style );
}
lpLS_col = lpLS_col->selected_style;
// no we can update the style
//
lpLS_col->bgColor = txtBgColor;
// Redraw it
if(redraw)
{ this->RedrawItems(0, this->GetItemCount() );
this->UpdateWindow();
}
}
void CListCtrlStyled::SetColSelectedFont(int nCol,CFont * pFont,bool redraw)
{
// Verify if a style for this Row already exist or not
//
LS_item * lpLS_col = NULL;
lpLS_col = this->columns[nCol];
if(lpLS_col == NULL)
{ // We must create one
//
lpLS_col = new LS_item;
this->Init_LS_item(lpLS_col,false);
// attach to Array
//
this->columns.SetAt(nCol, lpLS_col);
}
// Take the selected style structure
//
if(lpLS_col->selected_style == NULL)
{ // Create a structure style
//
lpLS_col->selected_style = new LS_item;
this->Init_LS_item( lpLS_col->selected_style );
}
lpLS_col = lpLS_col->selected_style;
// if any internal font exist for this item then delete it
//
this->Free_LS_font(lpLS_col);
// no we can update the style
//
lpLS_col->cfont = pFont;
lpLS_col->ifont = false;
lpLS_col->in_use = true;
// Redraw it
if(redraw)
{ this->RedrawItems(0, this->GetItemCount() );
this->UpdateWindow();
}
}
// *********************************
// ** New HIGHLIGHT COLOR Methods **
// *********************************
void CListCtrlStyled::SetHighlightTextColor(COLORREF Color)
{ this->m_highlighttext = Color; }
void CListCtrlStyled::SetHighlightColor(COLORREF Color)
{ this->m_highlight = Color; }
// ******************************************
// ** Internal Methods for Drawing Process **
// ******************************************
void CListCtrlStyled::OnCustomdraw(NMHDR* pNMHDR, LRESULT* pResult)
{
// first, lets extract data from
// the message for ease of use later
NMLVCUSTOMDRAW* pNMLVCUSTOMDRAW = (NMLVCUSTOMDRAW*)pNMHDR;
// we'll copy the device context into hdc
// but wont convert it to a pDC* until (and if)
// we need it as this requires a bit of work
// internally for MFC to create temporary CDC
// objects
HDC hdc = pNMLVCUSTOMDRAW->nmcd.hdc;
CDC* pDC = NULL;
// here is the item info
// note that we don't get the subitem
// number here, as this may not be
// valid data except when we are
// handling a sub item notification
// so we'll do that separately in
// the appropriate case statements
// below.
int nItem = (int)pNMLVCUSTOMDRAW->nmcd.dwItemSpec;
UINT nState = pNMLVCUSTOMDRAW->nmcd.uItemState;
LPARAM lParam = pNMLVCUSTOMDRAW->nmcd.lItemlParam;
// next we set up flags that will control
// the return value for *pResult
bool bNotifyPostPaint = false;
bool bNotifyItemDraw = false;
bool bNotifySubItemDraw = false;
bool bSkipDefault = false;
bool bNewFont = false;
// what we do next depends on the
// drawing stage we are processing
switch (pNMLVCUSTOMDRAW->nmcd.dwDrawStage) {
case CDDS_PREPAINT:
{
// PrePaint
m_pOldItemFont = NULL;
m_pOldSubItemFont = NULL;
bNotifyPostPaint = false;
bNotifyItemDraw = true;
}
break;
case CDDS_ITEMPREPAINT:
{
// Item PrePaint
//
// set up a different font to use, if any
if (! pDC) pDC = CDC::FromHandle(hdc);
m_pOldItemFont = NULL;
bNotifyPostPaint = false;
bNotifySubItemDraw = true;
// Store the first time, information about default font
//
if(m_Default_pCFont == NULL)
{ m_Default_pCFont = pDC->GetCurrentFont();
m_Default_pCFont->GetLogFont(&m_Default_LOGFONT);
}
m_item_selected = false;
m_item_selected = this->GetItemState(nItem, LVIS_SELECTED) != 0;
if(m_item_selected)
{ this->SetItemState(nItem,NULL,LVIS_SELECTED);
bNotifyPostPaint = true;
}
CFont* pNewFont = FontForItem(nItem,0,lParam,pNMLVCUSTOMDRAW);
if (pNewFont) {
m_pOldItemFont = pDC->SelectObject(pNewFont);
bNotifyPostPaint = true; // need to restore old font
bNewFont = true;
}
}
break;
case CDDS_ITEMPREPAINT|CDDS_SUBITEM:
{ // Sub Item PrePaint
// set sub item number (data will be valid now)
int nSubItem = pNMLVCUSTOMDRAW->iSubItem;
m_pOldSubItemFont = NULL;
bNotifyPostPaint = false;
// set up a different font to use, if any
if (! pDC) pDC = CDC::FromHandle(hdc);
CFont* pNewFont = FontForItem(nItem,nSubItem,lParam,pNMLVCUSTOMDRAW);
if (pNewFont) {
m_pOldSubItemFont = pDC->SelectObject(pNewFont);
bNotifyPostPaint = true; // need to restore old font
bNewFont = true;
}
else if(m_pOldItemFont)
{ m_pOldSubItemFont = pDC->SelectObject(m_pOldItemFont);
bNotifyPostPaint = true; // need to restore old font
bNewFont = true;
}
}
break;
case CDDS_ITEMPOSTPAINT|CDDS_SUBITEM:
{
// Sub Item PostPaint
// set sub item number (data will be valid now)
int nSubItem = pNMLVCUSTOMDRAW->iSubItem;
// restore old font if any
if (! pDC) pDC = CDC::FromHandle(hdc);
if (m_pOldSubItemFont) {
pDC->SelectObject(m_pOldSubItemFont);
m_pOldSubItemFont = NULL;
}
}
break;
case CDDS_ITEMPOSTPAINT:
{
// Item PostPaint
// restore old font if any
if (! pDC) pDC = CDC::FromHandle(hdc);
if (m_pOldItemFont) {
pDC->SelectObject(m_pOldItemFont);
m_pOldItemFont = NULL;
}
if(m_item_selected)
{ m_item_selected = false;
this->SetItemState(nItem,LVIS_SELECTED,LVIS_SELECTED);
}
}
break;
}
ASSERT(CDRF_DODEFAULT==0);
*pResult = 0;
if (bNotifyPostPaint) {
*pResult |= CDRF_NOTIFYPOSTPAINT;
}
if (bNotifyItemDraw) {
*pResult |= CDRF_NOTIFYITEMDRAW;
}
if (bNotifySubItemDraw) {
*pResult |= CDRF_NOTIFYSUBITEMDRAW;
}
if (bNewFont) {
*pResult |= CDRF_NEWFONT;
}
if (bSkipDefault) {
*pResult |= CDRF_SKIPDEFAULT;
}
if (*pResult == 0) {
// redundant as CDRF_DODEFAULT==0 anyway
// but shouldn't depend on this in our code
*pResult = CDRF_DODEFAULT;
}
}
CFont * CListCtrlStyled::FontForItem(int nItem,int nSubItem,LPARAM lParam,NMLVCUSTOMDRAW* pNMLVCUSTOMDRAW)
{
// Allow to create the correct font for an item of the CListCtrl
//
// Cast the lParam into an LS_item object
LS_item * lpLS_root = NULL;
lpLS_root = (LS_item*)lParam;
LS_item * lpLS_item = NULL;
LS_item * lpLS_row = NULL;
LS_item * lpLS_col = NULL;
// Set a mask value
//
DWORD mask = LIS_BOLD | LIS_ITALIC | LIS_UNDERLINE| LIS_STROKE ;
// Take Corects Structure style that we need
//
lpLS_item = lpLS_root;
lpLS_row = lpLS_root->row_style;
lpLS_col = this->columns[nSubItem];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -