📄 hotlistctrl.cpp
字号:
// HotListCtrl.cpp : implementation file
//
#include "stdafx.h"
//#include "Hots.h"
#include "HotListCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define DEFAULT_ROW_HEIGHT 40
#define DEFAULT_FONT_SIZE 10
/////////////////////////////////////////////////////////////////////////////
// CHotListCtrl
//int CHotListCtrl::m_nDefaultScrollHeight = 0;//11;
//int CHotListCtrl::m_nRowHeight = 0; // set this based on font size
CHotListCtrl::CHotListCtrl()
{
m_nFirstVisibleItem = 0;
m_nTopScrollHeight = 0;
m_nBottomScrollHeight = 0;
m_nSelectedItem = 0;
m_nHotItem = -1;
m_nPressedItem = -1;
m_hBitmapUp = NULL;
m_hBitmapDown = NULL;
m_hBitmapMask = NULL;
m_HasBackGround = FALSE;
m_pbmpOldBk = NULL;
m_nRows = 0;
}
CHotListCtrl::~CHotListCtrl()
{
//Caller will delete these two objects
/*
if(m_hBitmapUp)
::DeleteObject(m_hBitmapUp);
if(m_hBitmapDown)
::DeleteObject(m_hBitmapDown);
*/
if(m_hBitmapMask)
::DeleteObject(m_hBitmapMask);
m_StringArray.RemoveAll();
//m_ToolTipArray.RemoveAll();
m_DataArray.RemoveAll();
if(m_dcBk.m_hDC && m_pbmpOldBk){
m_dcBk.SelectObject(m_pbmpOldBk);
}
CBitmap::DeleteTempMap();
}
BEGIN_MESSAGE_MAP(CHotListCtrl, CWnd)
//{{AFX_MSG_MAP(CHotListCtrl)
ON_WM_DESTROY()
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_SIZE()
//ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHotListCtrl message handlers
void CHotListCtrl::PreSubclassWindow()
{
CWnd::PreSubclassWindow();
}
BOOL CHotListCtrl::Create(DWORD dwStyle, CRect rect, CWnd* pWndParent, UINT nID)
{
// if no style parameters, then just use default settings
if (dwStyle == 0)
{
dwStyle = WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
}
ASSERT (pWndParent && IsWindow(pWndParent->m_hWnd));
ASSERT(pWndParent->GetDlgItem(nID) == NULL);
// register a class name
LPCTSTR szClassName = AfxRegisterWndClass(0,
AfxGetApp()->LoadStandardCursor(IDC_ARROW), 0, 0);
// create the window
BOOL bResult = CWnd::Create(szClassName, _T(""),dwStyle, rect, pWndParent, nID);
//Initialize();
return bResult;
}
void CHotListCtrl::Init(int fontSize,int RowHeight)
{
ASSERT(m_Font.m_hObject == NULL); // No need to call initialize when created as a window
if (m_Font.m_hObject != NULL) return;
// create the control font
m_Font.CreateFont(fontSize, 0, 0, 0,
FW_BOLD, FALSE, FALSE, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, _T("System"));
CRect rc;
GetWindowRect(&rc);
m_nRowHeight = RowHeight;
m_nRows = (rc.bottom - rc.top)/m_nRowHeight;
return;
}
void CHotListCtrl::OnDestroy()
{
CWnd::OnDestroy();
// clean up the custom font
m_Font.Detach();
m_Font.DeleteObject();
}
void CHotListCtrl::OnPaint()
{
ASSERT(m_Font.m_hObject); // Did initialize get called?
CPaintDC dc(this); // device context for painting
////////////////////////////
CRect Rect;
GetClientRect(&Rect);
int Width = Rect.Width();
int Height = Rect.Height();
CDC MemDC;
MemDC.CreateCompatibleDC(&dc);
CBitmap MemBmp;
MemBmp.CreateCompatibleBitmap(&dc,Width,Height);
CBitmap *pOldMemBmp = MemDC.SelectObject(&MemBmp);
//Draw items
Draw(&MemDC);
dc.BitBlt(0,0,Width,Height,&MemDC,0,0,SRCCOPY);
MemDC.SelectObject(pOldMemBmp);
//////////////////////////////////////
// Do not call CWnd::OnPaint() for painting messages
}
// erase the background of the control to the App Workspace color
BOOL CHotListCtrl::OnEraseBkgnd(CDC* pDC)
{
/*
if (!m_HasBackGround){
CWnd *pParent = GetParent();
if (pParent){
CRect Rect;
GetClientRect(&Rect);
ClientToScreen(&Rect);
pParent->ScreenToClient(&Rect);
CDC *pDC = pParent->GetDC();
CDC memdc;
memdc.CreateCompatibleDC(pDC);
if(NULL == m_Bmp.m_hObject){
m_Bmp.CreateCompatibleBitmap(pDC,Rect.Width(),Rect.Height());
}
CBitmap *oldbmp = memdc.SelectObject(&m_Bmp);
memdc.BitBlt(0,0,Rect.Width(),Rect.Height(),pDC,Rect.left,Rect.top,SRCCOPY);
memdc.SelectObject(oldbmp);
m_HasBackGround = TRUE;
pParent->ReleaseDC(pDC);
}
}
*/
return TRUE;
}
BOOL CHotListCtrl::PreTranslateMessage(MSG* pMsg)
{
return CWnd::PreTranslateMessage(pMsg);
}
void CHotListCtrl::OnMove(int x, int y)
{
CWnd::OnMove(x, y);
ShowWindow(SW_HIDE);
m_HasBackGround = FALSE;
Invalidate();
UpdateWindow();
ShowWindow(SW_SHOW);
}
// funtion to get the drawable rect for this control
void CHotListCtrl::GetWindowRect(LPRECT lpRect)
{
GetClientRect(lpRect);
//lpRect->right = lpRect->right;//- 1;
//lpRect->bottom = lpRect->top + m_nRowHeight*3;
}
// function to get the rect for an item
// returns FALSE if there is insufficient room to draw the item
BOOL CHotListCtrl::GetItemRect(LPRECT lpRect, int nItem)
{
GetWindowRect(lpRect);
/*
// reduce the sides
lpRect->left = lpRect->left;
lpRect->right = lpRect->right;
// adjust for the borders
lpRect->top = lpRect->top;
lpRect->bottom = lpRect->bottom;
*/
lpRect->top = lpRect->top + (nItem-m_nFirstVisibleItem)*m_nRowHeight;
if (lpRect->top + m_nRowHeight > lpRect->bottom) return FALSE; // not enough room to draw the item
lpRect->bottom = lpRect->top + m_nRowHeight;
return TRUE;
}
///////////////////////////////////////////////////////////////////
// CHotListCtrl Item Management Functions
void CHotListCtrl::AddItem(UINT nIDText, UINT nToolTipID, DWORD dwData)
{
CString s;
s.LoadString(nIDText);
AddItem(s, nToolTipID, dwData);
}
void CHotListCtrl::AddItem(CString strText, UINT nToolTipID, DWORD dwData)
{
m_StringArray.Add(strText);
//m_ToolTipArray.Add(nToolTipID);
m_DataArray.Add(dwData);
}
int CHotListCtrl::GetCurSel()
{
return max(m_nSelectedItem, -1);
}
DWORD CHotListCtrl::GetItemData(int nItem)
{
ASSERT(nItem < m_DataArray.GetSize());
if (nItem >= m_DataArray.GetSize()) return NULL;
return (m_DataArray.GetAt(nItem));
}
int CHotListCtrl::GetItemCount()
{
return m_StringArray.GetSize();
}
CString CHotListCtrl::GetItemText(int nItem)
{
ASSERT(nItem < m_StringArray.GetSize());
if (nItem >= m_StringArray.GetSize()) return _T("");
return (m_StringArray.GetAt(nItem));
}
///////////////////////////////////////////////////////////////////
// CHotListCtrl Drawing Functions
void CHotListCtrl::Draw(CDC* pDeviceDC)
{
// for better drawing, draw to a compatible bitmap
// and BitBlt the bitmap to the DC
CDC dc;
CBitmap bitmap;
CRect rcDraw;
GetClientRect(&rcDraw);
if (rcDraw.IsRectNull()) return;
if (!dc.CreateCompatibleDC(pDeviceDC)) return;
if (!bitmap.CreateCompatibleBitmap(pDeviceDC, rcDraw.Width(), rcDraw.Height())) return;
dc.SelectObject(&bitmap);
int n;
int remainder;
int size = m_StringArray.GetSize();
remainder = size%m_nRows;
if(0 != remainder)
size += (m_nRows-remainder);
for (n = m_nFirstVisibleItem; n < size; ++n)
{
if (!DrawItem(&dc, n, FALSE, (n == m_nSelectedItem))) break;
}
VERIFY(pDeviceDC->BitBlt(rcDraw.left,
rcDraw.top,
rcDraw.Width(),
rcDraw.Height(),
&dc,
0,
0,
SRCCOPY));
bitmap.DeleteObject();
}
void CHotListCtrl::DrawBorder(CDC* pDC)
{
CRect rc;
GetWindowRect(&rc);
DrawHighlightRect(pDC, &rc, GetSysColor(COLOR_BTNSHADOW),
GetSysColor(COLOR_HIGHLIGHT));
rc.InflateRect(-1, -1, -1, -1);
DrawHighlightRect(pDC, &rc, RGB(0, 0, 0),
GetSysColor(COLOR_BTNFACE));
}
// function to draw highlight and shadow rects
void CHotListCtrl::DrawHighlightRect(CDC* pDC, CRect* rc,
COLORREF rgbTopLeft, COLORREF rgbBottomRight)
{
CPen Pen;
CPen* pOldPen;
Pen.CreatePen(PS_SOLID, 1, rgbBottomRight);
pOldPen = pDC->SelectObject(&Pen);
pDC->MoveTo(rc->left, rc->bottom);
pDC->LineTo(rc->right, rc->bottom);
pDC->LineTo(rc->right, rc->top-1);
pDC->SelectObject(pOldPen);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -