📄 sheetwnd.cpp
字号:
// SheetWnd.cpp : implementation file
//
#include "stdafx.h"
#include "SheetWnd.h"
#include "resource.h"
#include "XPDrawLayer.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSheetWnd
#define ID_HSCROLLBAR_ 0x44
#define ID_LEFTSCROLLBAR 0x45
#define TAB_HEIGHT 18
#define ALIGN_LEFT 0x01
#define ALIGN_RIGHT 0x02
#define ALIGN_UP 0x03
#define ALIGN_DOWN 0x04
static BOOL m_bInit = TRUE;
static BOOL m_bScrolled = FALSE;
CSheetWnd::CSheetWnd():m_GripperRect(0,0,3,0)
{
m_pCurItem = NULL;
m_bTracking = FALSE;
}
CSheetWnd::~CSheetWnd()
{
for(int pos =0; pos<m_lItems.GetSize();pos++)
delete m_lItems[pos];
}
BEGIN_MESSAGE_MAP(CSheetWnd, CWnd)
//{{AFX_MSG_MAP(CSheetWnd)
ON_WM_PAINT()
ON_WM_CREATE()
ON_WM_LBUTTONDOWN()
ON_WM_SIZE()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_SETCURSOR()
ON_WM_VSCROLL()
ON_WM_HSCROLL()
ON_WM_NCCALCSIZE()
//}}AFX_MSG_MAP
ON_WM_NCPAINT()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSheetWnd message handlers
void CSheetWnd::OnNcPaint()
{
// get window DC that is clipped to the non-client area
CWindowDC dc(this);
CRect rcClient, rcBar;
GetClientRect(rcClient);
ClientToScreen(rcClient);
GetWindowRect(rcBar);
rcClient.OffsetRect(-rcBar.TopLeft());
rcBar.OffsetRect(-rcBar.TopLeft());
CDC mdc;
mdc.CreateCompatibleDC(&dc);
CBitmap bm;
bm.CreateCompatibleBitmap(&dc, rcBar.Width(), rcBar.Height());
CBitmap* pOldBm = mdc.SelectObject(&bm);
// client area is not our bussiness :)
dc.ExcludeClipRect(rcClient);
// draw borders in non-client area
CRect rcDraw = rcBar;
mdc.FillRect(rcDraw,&m_brBackground);
rcDraw.DeflateRect(0,0,0,17);
//mdc.FillRect(rcDraw,&m_brBackground);
/*
CPen nPen(PS_SOLID,1,RGB(0,0,0));
CPen* pOldPen = mdc.SelectObject(&nPen);
mdc.MoveTo(0,rcDraw.bottom-18);
mdc.LineTo(0,rcDraw.bottom);
mdc.SelectObject(pOldPen);
*/
// erase parts not drawn
//mdc.IntersectClipRect(rcDraw);
// erase the NC background
mdc.FillSolidRect(rcDraw,RGB(0,0,0));
dc.BitBlt(0, 0, rcBar.Width(), rcBar.Height(), &mdc, 0, 0, SRCCOPY);
ReleaseDC(&dc);
mdc.SelectObject(pOldBm);
bm.DeleteObject();
mdc.DeleteDC();
}
void CSheetWnd::OnPaint()
{
//CWnd::OnPaint();
CPaintDC dc(this); // device context for painting
CDC mdc;
CRect rc;
/*
if(m_bInit)
{
CRect rt;
GetParent()->GetClientRect(&rt);
CRect rt1(rt.left,rt.bottom-18,rt.right,rt.bottom);
MoveWindow(rt1);
m_bInit = FALSE;
}
{
CRect rt;
GetParent()->GetClientRect(&rt);
CRect rc(0,rt.bottom-18,rt.right,rt.bottom);
MoveWindow(&rc);
}
*/
GetClientRect(&rc);
mdc.CreateCompatibleDC(&dc);
CBitmap bm;
bm.CreateCompatibleBitmap(&dc, rc.Width(), rc.Height());
CBitmap* pOldBm = mdc.SelectObject(&bm);
CBrush br;
br.CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
mdc.FillRect(rc,&br);//draw bk
br.DeleteObject();
if(m_lItems.GetSize()>0)
DrawItemsEx(&mdc);
dc.BitBlt(0, 0, rc.Width(), rc.Height(), &mdc, 0, 0, SRCCOPY);
mdc.SelectObject(pOldBm);
ReleaseDC(&dc);
bm.DeleteObject();
mdc.DeleteDC();
}
void CSheetWnd::DrawItem(CDC *pDC)
{
CRect rt1;
GetClientRect(&rt1);
pDC->FillRect(rt1,&m_brBackground);//draw bk
SHEETITEM* pPrev = NULL;
for(int pos = 0; pos<m_lItems.GetSize(); pos++)
{
SHEETITEM* pItem = m_lItems[pos];
CSize sz = pDC->GetTextExtent(pItem->m_pszLabel);
if(pPrev != NULL)
{
int x1 = pPrev->m_nRect.right+2;
int x2 = x1+sz.cx+5;
pItem->m_nRect.SetRect(x1,(rt1.bottom-sz.cy)-5,x2,rt1.bottom);
}
else
{
pItem->m_nRect.SetRect(rt1.left+32,
(rt1.bottom-sz.cy)-5,
rt1.left+32+sz.cx+5,
rt1.bottom);
}
if((m_pCurItem != NULL)&&(m_pCurItem == pItem))
DrawSelection(pDC,pItem);
//DrawTabBtn(pDC,pItem);
DrawVcppStyle(pDC,pItem);
CString pszLabel = pItem->m_pszLabel;
CFont* def_font = pDC->SelectObject(&font);
CRect rc(pItem->m_nRect);
rc.DeflateRect(5,5,0,0);
int nPrevMode = pDC->SetBkMode(TRANSPARENT);
pDC->TextOut(rc.left,rc.top,pszLabel);
pDC->SelectObject(def_font);
pDC->SetBkMode(nPrevMode);
pPrev = pItem;
}
if(m_GripperRect.IsRectEmpty())
{
m_GripperRect.SetRect(rt1.right/2,rt1.top,(rt1.right/2)+3,rt1.bottom);
CRect sRt(m_GripperRect.right+1,rt1.top,rt1.right-14,rt1.bottom);
m_wndHScrollBar.MoveWindow(sRt);
}
else
if(!m_bScrolled&&(pPrev!=NULL))
{
if((pPrev->m_nRect.right > m_GripperRect.left))
{
int x1 = pPrev->m_nRect.right;
m_GripperRect.SetRect(x1+20,rt1.top,x1+23,rt1.bottom);
CRect sRt(m_GripperRect.right+1,rt1.top,rt1.right-14,rt1.bottom);
m_wndHScrollBar.MoveWindow(sRt);
}
}
//draw gripper
pDC->Draw3dRect(m_GripperRect,RGB(255,255,255),RGB(0,0,0));
}
void CSheetWnd::GetClientRect(LPRECT lpRect) const
{
CWnd::GetClientRect(lpRect);
}
void CSheetWnd::DrawItemsEx(CDC* pDC)
{
CRect rt1;
GetClientRect(&rt1);
rt1.DeflateRect(0,rt1.Height()-18,0,0);//normalize
//draw scrollers
CRect rcScrollRect(0,rt1.top+1,17,rt1.bottom);
CRect rcLeftScrollArrow(rcScrollRect);
//******************super left scroller*******************//
COLORREF rfArrow = RGB(0,0,0);
rcLeftScrollArrow.DeflateRect(8,2,2,2);
CRect rcDef(rcLeftScrollArrow);
//rcDef.DeflateRect(0,0,1,0);
//pDC->Draw3dRect(rcDef,RGB(255,255,255),RGB(243,255,23));
DrawArrow(pDC,rcLeftScrollArrow,rfArrow,ALIGN_LEFT);
rcLeftScrollArrow.DeflateRect(0,2,rcLeftScrollArrow.Width()+1,2);
pDC->FillSolidRect(rcLeftScrollArrow,rfArrow);
//******************left scroller*************************//
rcScrollRect.OffsetRect(rcScrollRect.Width(),0);
rcLeftScrollArrow.SetRect(rcScrollRect.TopLeft(),rcScrollRect.BottomRight());
rcLeftScrollArrow.DeflateRect(8,2,2,2);
//pDC->Draw3dRect(rcScrollRect,RGB(255,255,255),RGB(243,255,23));
DrawArrow(pDC,rcLeftScrollArrow,rfArrow,ALIGN_LEFT);
//*****************right scroller*************************//
rcScrollRect.OffsetRect(rcScrollRect.Width(),0);
rcLeftScrollArrow.SetRect(rcScrollRect.TopLeft(),rcScrollRect.BottomRight());
rcLeftScrollArrow.DeflateRect(2,2,8,2);
//pDC->Draw3dRect(rcScrollRect,RGB(255,255,255),RGB(243,255,23));
DrawArrow(pDC,rcLeftScrollArrow,rfArrow,ALIGN_RIGHT);
//******************super right scroller******************//
rcScrollRect.OffsetRect(rcScrollRect.Width(),0);
rcLeftScrollArrow.SetRect(rcScrollRect.TopLeft(),rcScrollRect.BottomRight());
rcLeftScrollArrow.DeflateRect(2,2,8,2);
//pDC->Draw3dRect(rcScrollRect,RGB(255,255,255),RGB(243,255,23));
DrawArrow(pDC,rcLeftScrollArrow,rfArrow,ALIGN_RIGHT);
rcLeftScrollArrow.DeflateRect(rcLeftScrollArrow.Width()+1,2,0,2);
pDC->FillSolidRect(rcLeftScrollArrow,rfArrow);
SHEETITEM* pPrev = NULL;
for(int pos =0;pos< m_lItems.GetSize();pos++)
{
SHEETITEM* pItem = m_lItems[pos];
if(m_pCurItem != pItem)
{
DrawVcppStyle(pDC,pItem);
CFont* def_font = pDC->SelectObject(&font);
int nPrevMode = pDC->SetBkMode(TRANSPARENT);
CString strText = pItem->m_pszLabel;
CRect rcText(pItem->m_nRect);
rcText.InflateRect(5,0,5,0);
CSize sz = pDC->GetTextExtent(pItem->m_pszLabel);
int xShrink = (rcText.Width()-sz.cx)/2;
int yShrink = (rcText.Height()-sz.cy)/2;
pDC->TextOut(rcText.left+xShrink,rcText.top+yShrink,strText);
pDC->SelectObject(def_font);
pDC->SetBkMode(nPrevMode);
}
pPrev = pItem;
}
//draw selection
DrawVcppStyle(pDC,m_pCurItem);
//DrawSelVCppStype(pDC,m_pCurItem);
DrawSelVCppStype3d(pDC,m_pCurItem);
CFont bold;
bold.CreatePointFont(70,"Arial Black");
CFont* def_font = pDC->SelectObject(&bold);
int nPrevMode = pDC->SetBkMode(TRANSPARENT);
//center text
{
CString strText = m_pCurItem->m_pszLabel;
CRect rcText(m_pCurItem->m_nRect);
rcText.InflateRect(5,0,5,0);
CSize sz = pDC->GetTextExtent(m_pCurItem->m_pszLabel);
int xShrink = (rcText.Width()-sz.cx)/2;
int yShrink = (rcText.Height()-sz.cy)/2;
pDC->TextOut(rcText.left+xShrink,rcText.top+yShrink,strText);
}
pDC->SelectObject(def_font);
pDC->SetBkMode(nPrevMode);
//TO THE LEFT
{
CPen nPen(PS_SOLID,1,RGB(0,0,0));
CPen* pOldPen = pDC->SelectObject(&nPen);
pDC->MoveTo(0,rt1.top);
pDC->LineTo(m_pCurItem->m_nRect.left-1,rt1.top);
pDC->SelectObject(pOldPen);
}
//TO THE RIGHT
{
//draw styler
CPen nPen(PS_SOLID,1,RGB(0,0,0));
CPen* pOldPen = pDC->SelectObject(&nPen);
pDC->MoveTo(m_pCurItem->m_nRect.right+1,rt1.top);
pDC->LineTo(m_GripperRect.right,rt1.top);
pDC->SelectObject(pOldPen);
}
//draw gripper
pDC->Draw3dRect(m_GripperRect,RGB(255,255,255),RGB(0,0,0));
}
BOOL CSheetWnd::SetItem(CWnd* pwndItem,CString pszLabel,int nIndex,
int nScrollBar /*= -1*/)
{
if(nIndex != -1&& nIndex<m_lItems.GetSize())
{
m_lItems[nIndex]->m_nScrollBar = nScrollBar;
m_lItems[nIndex]->m_pszLabel = pszLabel;
m_lItems[nIndex]->m_pwndItem = pwndItem;
return TRUE;
}
return FALSE;
}
BOOL CSheetWnd::RemoveItem(int nIndex)
{
if(nIndex<m_lItems.GetSize())
{
delete m_lItems[nIndex];
m_lItems.RemoveAt(nIndex);
Invalidate();
UpdateWindow();
return TRUE;
}
return FALSE;
}
int CSheetWnd::SetCurSel(int nSelect)
{
if(nSelect < m_lItems.GetSize())
{
m_pCurItem = m_lItems[nSelect];
TabSelChanged(nSelect);
Invalidate();UpdateWindow();
return nSelect;
}return -1;
}
int CSheetWnd::GetCurSel()
{
if(m_pCurItem != NULL)
return m_pCurItem->m_nPos;
else
return -1;
}
BOOL CSheetWnd::AddItem(CWnd* pwndItem,CString pszLabel,int nScrollBar /*= -1*/)
{
CRect rcClt;
GetClientRect(&rcClt);
CDC *pDC = GetDC();
if(pwndItem != NULL)
{
SHEETITEM* pItem = new SHEETITEM;
pItem->m_nScrollBar = nScrollBar;
pItem->m_nPos = m_lItems.GetSize();
pItem->m_pszLabel = pszLabel;
pItem->m_pwndItem = pwndItem;
if(m_lItems.GetSize() == 0)
{
CSize sz = pDC->GetTextExtent(pItem->m_pszLabel);
pItem->m_nRect.SetRect(68,rcClt.top,78+sz.cx,rcClt.bottom-5);
m_pCurItem = pItem;//select first item
m_pCurItem->m_pwndItem->BringWindowToTop();
}
else
{
SHEETITEM* pTailItem = m_lItems[m_lItems.GetSize()-1];
CRect rcTail = pTailItem->m_nRect;
CSize sz = pDC->GetTextExtent(pItem->m_pszLabel);
pItem->m_nRect.SetRect(rcTail.right,rcClt.top,rcTail.right+sz.cx+10,rcClt.bottom-5);
}
m_GripperRect.OffsetRect(pItem->m_nRect.Width()+30,0);
m_bInit = TRUE;
m_lItems.Add(pItem);
//Invalidate();
//UpdateWindow();
return TRUE;
}
return FALSE;
}
int CSheetWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
m_wndHScrollBar.Create(WS_CHILD| SBS_HORZ|WS_VISIBLE,CRect(),this,ID_HSCROLLBAR_);
SCROLLINFO info;
info.cbSize = sizeof(SCROLLINFO);
info.fMask = SIF_ALL;
info.nMin = 0;
info.nMax = 100;
info.nPage = 0;
info.nPos = 0;
info.nTrackPos = 2;
m_wndHScrollBar.SetScrollInfo(&info);
m_brBackground.CreateSolidBrush(GetSysColor(COLOR_3DFACE)/*GuiDrawLayer::GetRGBColorXP()*/);
VERIFY(font.CreatePointFont(100, "Times New Roman", GetDC()));
return 0;
}
void CSheetWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
if(!m_bTracking&&m_GripperRect.PtInRect(point)&&!GetCapture())
{
SetCapture();
m_bTracking = TRUE;
return;
}
SHEETITEM* temp = GetCurItem(point);
if(temp != m_pCurItem&&temp!=NULL)
{
/*TabSelChanged(temp->m_nPos);
CString szpBuff;
temp->m_pwndItem->GetWindowText(szpBuff);
CString szpBuff1;
m_pCurItem->m_pwndItem->GetWindowText(szpBuff1);
if(szpBuff.IsEmpty()&&szpBuff1.IsEmpty())
{
m_pCurItem = temp; Invalidate();
SendMessage(WM_PAINT);return;
}*/
/*
SetParent(temp->m_pwndItem);
temp->m_pwndItem->ShowWindow(SW_SHOW);
temp->m_pwndItem->SetFocus();
m_bInit = TRUE;
//hide previous item
m_pCurItem->m_pwndItem->ShowWindow(SW_HIDE);
//set new selection
*/
temp->m_pwndItem->BringWindowToTop();
temp->m_pwndItem->SetFocus();
m_pCurItem = temp;
Invalidate();
UpdateWindow();
temp->m_pwndItem->Invalidate();
temp->m_pwndItem->UpdateWindow();
}
Invalidate();
UpdateWindow();
//SendMessage(WM_NCPAINT);
}
SHEETITEM* CSheetWnd::GetCurItem(CPoint pt)
{
for(int npos =0; npos<m_lItems.GetSize(); npos++)
{
SHEETITEM* pItem = m_lItems[npos];
if(pItem->m_nRect.PtInRect(pt))
return pItem;
}
return NULL;
}
void CSheetWnd::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType,cx,cy);
RecalcLayout();
/*
//update gripper
CRect rcClient;
GetClientRect(&rcClient);
/*if(!m_GripperRect.IsRectEmpty())
{
int nOffset = m_GripperRect.right-rcClient.right;
m_GripperRect.OffsetRect(nOffset,0);
//if(nW<50)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -