📄 selectctrl.cpp
字号:
// SelectCtrl.cpp: implementation of the CSelectCtrl class.
//
//////////////////////////////////////////////////////////////////////
#include "SelectCtrl.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSelectCtrl::CSelectCtrl()
{
m_bCreated = FALSE;
m_bInited = TRUE;
// m_bIsFrame = FALSE;
m_bFrameStyle = 0;
m_bIsMiddle = FALSE;
m_nTotalItem = 0;
m_nCurrent = 0;
m_nWordFrame = 0;
m_Setrect.left = __LEFT_DISTANCE__;
m_Setrect.right = __RIGHT_DISTANCE__;
m_Setrect.top = __TOP_DISTANCE__;
m_Setrect.bottom = __SPACE_DISTANCE__;
m_nWordSqu = __WORD_SQU_DISTANCE__;
}
CSelectCtrl::~CSelectCtrl()
{
for(int i=0;i<m_nTotalItem;i++)
{
delete []m_pstrItemText[i];
}
}
int CSelectCtrl::WinProc(UMSG *pMsg)
{
if(m_bInited == FALSE) return -1;
switch(pMsg->nMessage){
case UM_PAINT:
OnPaint();
break;
case UM_KEYPRESS:
OnKey(pMsg);
break;
}
return 0;
}
int CSelectCtrl::Create(CUWin * pWndParent,
const char *szName,
int nStyle,
RECT *pRect,
int nParam )
{
nParam = nParam;
pRect = pRect;
szName = szName;
if(m_bInited == FALSE) return -1;
if(m_bCreated == TRUE) return 0; //already created
SetParent(pWndParent);
SetContainer(pWndParent);
if(nStyle & __SELECT_REALFRAME__)
{
m_bFrameStyle = 1;
}
if(nStyle & __SELECT_DASHFRAME__)
{
m_bFrameStyle = 2;
}
if(nStyle & __SELECT_MIDDLE__)
{
m_bIsMiddle = TRUE;
}
if(nStyle & __SELECT_WORD_LINEFRAME__)
{
m_nWordFrame = 1;
}
if(nStyle & __SELECT_WORD_DASHFRAME__)
{
m_nWordFrame = 2;
}
if(nStyle & __SELECT_WORD_BLACKFRAME__)
{
m_nWordFrame = 3;
}
int npWndStyle = SendMessage(pWndParent, UM_QUERY_STYLE);
npWndStyle &= 0x0000FFFF;
memcpy(&m_rect, pRect, sizeof(RECT));
GetDC()->SetViewPort(&m_rect); //set dc viewport
if(pWndParent != NULL)
PostMessage(pWndParent, UM_NOTIFY, (int)this, NOTIFY_CREATE);
m_bCreated = TRUE;
return 0;
}
void CSelectCtrl::Show()
{
SendMessage(this,UM_PAINT);
}
void CSelectCtrl::OnPaint(void)
{
CUDC * pDC = GetDC();
pDC->BeginBatchPaint();
pDC->ClearDC();
if(m_bFrameStyle == 2)
{
//虚线画边框
pDC->DashedLine(0,0,0,m_rect.bottom-m_rect.top);
pDC->DashedLine(0,0,m_rect.right-m_rect.left,0);
pDC->DashedLine(0,m_rect.bottom-m_rect.top,m_rect.right-m_rect.left,m_rect.bottom-m_rect.top);
pDC->DashedLine(m_rect.right-m_rect.left,0,m_rect.right-m_rect.left,m_rect.bottom-m_rect.top);
}
else if(m_bFrameStyle == 1)
{
//实线画边框
pDC->Line(0,0,0,m_rect.bottom-m_rect.top);
pDC->Line(0,0,m_rect.right-m_rect.left,0);
pDC->Line(0,m_rect.bottom-m_rect.top,m_rect.right-m_rect.left,m_rect.bottom-m_rect.top);
pDC->Line(m_rect.right-m_rect.left,0,m_rect.right-m_rect.left,m_rect.bottom-m_rect.top);
}
GetDistanceHeight(pDC);
//如果有添加项就画出
if(m_nTotalItem > 0)
{
int i;
int nCount;
for(nCount = 0;nCount<m_nTotalItem;nCount++)
{
for(i=0;i<2;i++)
{
//上横线
pDC->Line(m_Setrect.left,(nCount+1)*m_Setrect.top + nCount*select_item_height + i ,
m_Setrect.left + select_item_height,(nCount+1)*m_Setrect.top + nCount*select_item_height + i);
//下横线
pDC->Line(m_Setrect.left,(nCount+1)*m_Setrect.top + nCount*select_item_height + select_item_height - i,
m_Setrect.left +select_item_height,(nCount+1)*m_Setrect.top + nCount*select_item_height + select_item_height- i);
//左竖线
pDC->Line(m_Setrect.left + i,(nCount+1)*m_Setrect.top + nCount*select_item_height ,
m_Setrect.left + i,(nCount+1)*m_Setrect.top + nCount*select_item_height + select_item_height);
//右竖线
pDC->Line(m_Setrect.left +select_item_height-i,(nCount+1)*m_Setrect.top + nCount*select_item_height,
m_Setrect.left - i + select_item_height,(nCount+1)*m_Setrect.top + nCount*select_item_height + select_item_height);
}
//设定文字风格
if(m_nWordFrame == 3 && m_nCurrent == nCount+1)
{
pDC->SetTextBgColor(255);
pDC->SetTextColor(0);
}
else
{
pDC->SetTextBgColor(0);
pDC->SetTextColor(255);
}
pDC->TextOut(m_Setrect.left + select_item_height + m_nWordSqu,
(nCount+1)*m_Setrect.top + nCount*select_item_height + 1 ,m_pstrItemText[nCount]);
//画叉//画文字边框
if(m_nCurrent == nCount+1)
{
pDC->Line(m_Setrect.left,(nCount+1)*m_Setrect.top + nCount*select_item_height ,
m_Setrect.left +select_item_height,(nCount+1)*m_Setrect.top + nCount*select_item_height + select_item_height);
pDC->Line(m_Setrect.left + select_item_height,(nCount+1)*m_Setrect.top + nCount*select_item_height,
m_Setrect.left,(nCount+1)*m_Setrect.top + nCount*select_item_height + select_item_height );
//画文字边框
if(m_nWordFrame == 2)
{
pDC->SetLineStyle(DASHED_LINE);
/*
pDC->RectAngle(m_Setrect.left + select_item_height + m_nWordSqu - 1,(nCount+1)*m_Setrect.top + nCount*select_item_height,
m_Setrect.right + 1,(nCount+1)*m_Setrect.top + nCount*select_item_height + select_item_height);
*/
pDC->RectAngle(0 + 2,(nCount+1)*m_Setrect.top + nCount*select_item_height - 2,
m_rect.right - m_rect.left - 2,(nCount+1)*m_Setrect.top + nCount*select_item_height + select_item_height + 2);
pDC->SetLineStyle(SOLID_LINE);
}
else if(m_nWordFrame == 1)
{
pDC->Line(m_Setrect.left + select_item_height + m_nWordSqu - 1,(nCount+1)*m_Setrect.top + nCount*select_item_height + select_item_height +1,
m_Setrect.right + 1,(nCount+1)*m_Setrect.top + nCount*select_item_height + select_item_height+1);
}
}
}
}
pDC->EndBatchPaint();
}
void CSelectCtrl::OnKey(UMSG * pMsg)
{
if(pMsg->nMessage == UM_KEYPRESS)
{
if(pMsg->nData == KEY_UP)
{
if(m_nCurrent == 1)
{
m_nCurrent = m_nTotalItem;
}
else
{
m_nCurrent -= 1;
}
}
if(pMsg->nData == KEY_DOWN)
{
if(m_nCurrent == m_nTotalItem)
{
m_nCurrent = 1;
}
else
{
m_nCurrent += 1;
}
}
SendMessage(this,UM_PAINT);
}
SendMessage(GetParent(), UM_KEYPRESS, 0, pMsg->nData);
}
BOOL CSelectCtrl::AddString(const char* pstrItemText)
{
if(pstrItemText == NULL)
{
return FALSE;
}
if(m_nTotalItem >= select_max_item_count)
{
return FALSE;
}
//将字符串复制到选择项中
m_pstrItemText[m_nTotalItem] = new char[strlen(pstrItemText)+1];
memset(m_pstrItemText[m_nTotalItem],0,strlen(pstrItemText)+1);
memcpy(m_pstrItemText[m_nTotalItem],pstrItemText,strlen(pstrItemText));
m_nTotalItem += 1;
if(m_nTotalItem > 0)
{
m_nCurrent = 1;
}
return TRUE;
}
void CSelectCtrl::GetDistanceHeight(CUDC *pdc)
{
//得到横向位置
int nMaxWordMaxLen = 0;
for(int i=0;i<m_nTotalItem;i++)
{
if(pdc->GetTextWidth(m_pstrItemText[i]) > nMaxWordMaxLen)
{
nMaxWordMaxLen = pdc->GetTextWidth(m_pstrItemText[i]);
}
m_Setrect.right = m_Setrect.left + select_item_height
+ m_nWordSqu + nMaxWordMaxLen;
}
//如果超过横向长度范围
if(nMaxWordMaxLen + m_nWordSqu + select_item_height > m_rect.right - m_rect.left)
{
m_Setrect.left = 0;
m_Setrect.right = m_rect.right - m_rect.left;
return;
}
if(m_bIsMiddle)
{
int nHeight = (m_rect.bottom-m_rect.top-m_nTotalItem*select_item_height)/(m_nTotalItem+1);
m_Setrect.top = nHeight;
m_Setrect.bottom = nHeight;
m_Setrect.left = (m_rect.right - m_rect.left - (nMaxWordMaxLen + select_item_height))/3;
m_nWordSqu = m_Setrect.left;
m_Setrect.right = m_rect.right - m_rect.left - m_Setrect.left;
}
}
int CSelectCtrl::GetCurSel()
{
return m_nCurrent;
}
void CSelectCtrl::SetCurSel(int n)
{
if(n< 0 || n > m_nTotalItem)
{
return;
}
m_nCurrent = n;
SendMessage(this,UM_PAINT);
}
void CSelectCtrl::SetParam(int nTop /*距离上边框的距离*/,
int nLeft /*距离左边框的距离*/,
int nWordSqu)
{
m_Setrect.top = nTop;
m_Setrect.left = nLeft;
m_nWordSqu = nWordSqu;
// m_Setrect.right = m_rect.right - m_rect.left - m_Setrect.left;
}
char* CSelectCtrl::GetCurSelTextPtr()
{
return m_pstrItemText[m_nCurrent-1];
}
char* CSelectCtrl::GetTextPtr(int n)
{
if(n> m_nTotalItem || n < 0)
{
return NULL;
}
return m_pstrItemText[n-1];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -