📄 listboxex.cpp
字号:
// ListBoxEx.cpp : implementation file
//
#include "stdafx.h"
#include "huffman.h"
#include "ListBoxEx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CListBoxEx
CListBoxEx::CListBoxEx()
{
width = 0;
m_bkBrush.CreateSolidBrush(RGB(128,128,128));
}
CListBoxEx::~CListBoxEx()
{
}
BEGIN_MESSAGE_MAP(CListBoxEx, CListBox)
//{{AFX_MSG_MAP(CListBoxEx)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_VSCROLL()
ON_WM_HSCROLL()
ON_WM_DRAWITEM_REFLECT()
ON_CONTROL_REFLECT(LBN_SELCHANGE, OnSelchange)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CListBoxEx message handlers
void CListBoxEx::updateWidth(LPCTSTR s)
{
CClientDC dc(this);
CFont * f = CListBox::GetFont();
dc.SelectObject(f);
CSize sz = dc.GetTextExtent(s, _tcslen(s));
sz.cx += 3 * ::GetSystemMetrics(SM_CXBORDER);
if(sz.cx > width)
{ /* extend */
width = sz.cx;
CListBox::SetHorizontalExtent(width);
} /* extend */
}
int CListBoxEx::AddString(LPCTSTR s)
{
int result = CListBox::AddString(s);
if(result < 0)
return result;
updateWidth(s);
return result;
}
int CListBoxEx::InsertString(int i, LPCTSTR s)
{
int result = CListBox::InsertString(i, s);
if(result < 0)
return result;
updateWidth(s);
return result;
}
void CListBoxEx::ResetContent()
{
CListBox::ResetContent();
width = 0;
}
int CListBoxEx::DeleteString(int n)
{
int result = CListBox::DeleteString(n);
if(result < 0)
return result;
CClientDC dc(this);
CFont * f = CListBox::GetFont();
dc.SelectObject(f);
width = 0;
for(int i = 0; i < CListBox::GetCount(); i++)
{ /* scan strings */
CString s;
CListBox::GetText(i, s);
CSize sz = dc.GetTextExtent(s);
sz.cx += 3 * ::GetSystemMetrics(SM_CXBORDER);
if(sz.cx > width)
width = sz.cx;
} /* scan strings */
CListBox::SetHorizontalExtent(width);
return result;
}
/*void CListBoxEx::OnPaint()
{
CPaintDC dc(this); // device context for painting
/* CDC memDC;
CRect rect;
GetClientRect(&rect);
memDC.CreateCompatibleDC(&dc);
CBitmap bmp;
bmp.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
memDC.SelectObject(&bmp);
CRgn rgn;
rgn.CreateRectRgnIndirect(rect);
memDC.FillRect(rect,&CBrush(RGB(0,0,255)));
dc.BitBlt(0,0,rect.Width(),rect.Height(),&memDC,0,0,SRCCOPY);//
// Do not call CListBox::OnPaint() for painting messages
}*/
HBRUSH CListBoxEx::CtlColor(CDC* pDC, UINT nCtlColor)
{
// pDC->SetTextColor(RGB(205,205,0));
// pDC->SetBkMode(TRANSPARENT);
// CRect rect;
// GetClientRect(rect);
// pDC->FillRect(rect, &m_bkBrush);
// return (HBRUSH) m_bkBrush;
// TODO: Return a non-NULL brush if the parent's handler should not be called
return NULL;
}
/*void CListBoxEx::OnPaint()
{
CPaintDC dc(this); // device context for painting
CDC memDC;
CRect rect;
GetClientRect(&rect);
// rect.right=rect.left+GetHorizontalExtent();
memDC.CreateCompatibleDC(&dc);
CBitmap bmp;
bmp.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
memDC.SelectObject(&bmp);
CRgn rgn;
rgn.CreateRectRgnIndirect(rect);
memDC.SelectClipRgn(&rgn);
rgn.DeleteObject();
CWnd::DefWindowProc(WM_PAINT,(WPARAM)memDC.m_hDC,0);
CDC maskDC;
maskDC.CreateCompatibleDC(&dc);
CBitmap maskBmp;
maskBmp.CreateBitmap(rect.Width(),rect.Height(),1,1,NULL);
maskDC.SelectObject(&maskBmp);
memDC.SetBkColor(::GetSysColor(COLOR_WINDOW));
maskDC.BitBlt(0,0,rect.Width(),rect.Height(),&memDC,rect.left
,rect.top,SRCCOPY);
memDC.SetBkColor(RGB(49,70,96));
memDC.SetTextColor(RGB(255,255,255));
memDC.BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),
&maskDC,rect.left,rect.top,SRCAND);
dc.BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),
&memDC,rect.left,rect.top,SRCCOPY);
memDC.DeleteDC();
maskDC.DeleteDC();
bmp.DeleteObject();
maskBmp.DeleteObject();
}*/
void CListBoxEx::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
InvalidateRect(NULL);
CListBox::OnVScroll(nSBCode, nPos, pScrollBar);
}
void CListBoxEx::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// updateWidth();
Invalidate();
CListBox::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CListBoxEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
}
void CListBoxEx::OnSelchange()
{
Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -