📄 nlistbox.cpp
字号:
// NListBox.cpp: implementation of the CNListBox class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "NListBox.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CNListBox::CNListBox()
{
SetMemWindow(FALSE);
}
CNListBox::~CNListBox()
{
}
BOOL CNListBox::Create(LPCTSTR lpszCaption, DWORD dwStyle,
const RECT& rect, CNWnd* pParentWnd, UINT nID)
{
CNWnd* pWnd = this;
return pWnd->Create(_T("LISTBOX"), lpszCaption, dwStyle, rect, pParentWnd, nID);
}
void CNListBox::OnDrawItem(int /*nIDCtl*/, LPDRAWITEMSTRUCT lpdis)
{
const int MAX_TEXT_LENGTH = 255;
TCHAR tchBuffer[MAX_TEXT_LENGTH] = {0,};
int y;
TEXTMETRIC tm;
// Display the text associated with the item.
if(lpdis->itemID != -1) // to be sure that it's not an empty item.
GetItemText(lpdis->itemID, tchBuffer, MAX_TEXT_LENGTH);
GetTextMetrics(lpdis->hDC, &tm);
y = (lpdis->rcItem.bottom + lpdis->rcItem.top -
tm.tmHeight) / 2;
int nOldMode;
COLORREF crOldTextColor, crOldBkColor;
if (lpdis->itemState & ODS_SELECTED)
{
nOldMode = ::SetBkMode(lpdis->hDC,TRANSPARENT);
crOldTextColor = ::SetTextColor(lpdis->hDC, LSITBOX_SELECTED_TXEXTCOLOR);
crOldBkColor =::SetBkColor(lpdis->hDC, LSITBOX_SELECTED_BKCOLOR);
} else
{
crOldBkColor = ::SetBkColor(lpdis->hDC, LISTBOX_BKCOLOR);
}
ExtTextOut(lpdis->hDC,
3,
y,
ETO_OPAQUE,
&lpdis->rcItem,
tchBuffer,
_tcslen(tchBuffer),
NULL);
if (lpdis->itemState & ODS_SELECTED)
{
SetBkMode(lpdis->hDC,nOldMode);
SetTextColor(lpdis->hDC, crOldTextColor);
}
SetBkColor(lpdis->hDC, crOldBkColor);
}
BOOL CNListBox::OnEraseBkgnd(HDC hdc)
{
RECT rect;
// it's not mem window, don't call member function GetClientRect()
::GetClientRect(GetSafeHwnd(), &rect);
FillSolidRect(hdc, &rect, LISTBOX_BKCOLOR);
return TRUE;
}
void CNListBox::PreSubclassWindow()
{
CNCtrl::PreSubclassWindow();
DWORD dwStyle = ::GetWindowLong(GetSafeHwnd(), GWL_STYLE);
// dwStyle = dwStyle | LBS_OWNERDRAWFIXED;
SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle);
}
int CNListBox::GetItemText( int nItem, LPTSTR lpszText, int nLen ) const
{
TCHAR *pBuffer;
LONG nTextLength;
nTextLength = SendMessage(LB_GETTEXTLEN, (WPARAM)nItem, 0);
ASSERT(nTextLength != LB_ERR);
pBuffer = (TCHAR*)malloc((nTextLength+1)*sizeof(TCHAR));
SendMessage(LB_GETTEXT, (WPARAM)nItem, (LPARAM)pBuffer);
_tcsncpy(lpszText, pBuffer, nLen);
lpszText[nLen - 1] = (TCHAR)0; // null terminate
free(pBuffer);
return nTextLength;
}
void CNListBox::AddItem(LPCTSTR lpstr, DWORD uItemData)
{
int nItem;
nItem = SendMessage(LB_ADDSTRING, 0, (LPARAM)lpstr);
//SendMessage(LB_SETITEMDATA, (WPARAM)nItem, (LPARAM)uItemData);
SetItemData(nItem, uItemData);
}
LRESULT CNListBox::WndProc(HWND hWnd,
UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static int i = 0;
/* if( uMsg!=0x180 && uMsg!=0x189 && uMsg!= 0x18a && uMsg!= 0x200 && uMsg!=0x84
&& uMsg!=0x20 && uMsg!=0x2b)
TRACE(_T("%d uMsg = %x\n"), i++, uMsg);
*/
return CNCtrl::WndProc(hWnd, uMsg, wParam, lParam);
}
int CNListBox::SetCurSel(int nSelect) const
{
int nItem;
nItem = SendMessage(LB_SETCURSEL, (WPARAM)nSelect, (LPARAM)0);
return nItem;
}
int CNListBox::GetCurSel( ) const
{
return SendMessage(LB_GETCURSEL, (WPARAM)0, (LPARAM)0);
}
int CNListBox::GetCount( ) const
{
return SendMessage(LB_GETCOUNT, (WPARAM)0, (LPARAM)0);
}
// return LB_ERR if an error occurs.
int CNListBox::SetItemData ( int nIndex, DWORD dwItemData )
{
return SendMessage(LB_SETITEMDATA, (WPARAM)nIndex, (LPARAM)dwItemData);
}
// The return value is the value associated with the item,
// or LB_ERR if an error occurs.
DWORD CNListBox::GetItemData( int nIndex ) const
{
return SendMessage(LB_GETITEMDATA, (WPARAM)nIndex, (LPARAM)0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -