⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 barwnd.cpp

📁 我自己整理的一些VC源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// BarWnd.cpp : implementation file
//

#include "stdafx.h"
#include "SellMan.h"
#include "BarWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBarWnd

IMPLEMENT_DYNCREATE(CBarWnd, CWnd)

CBarWnd::CBarWnd()
{
	barForecolor=RGB(212,208,200);
	barClientcolor=RGB(58,110,165);
	barTextcolor=RGB(0,0,0);
	arForderbar.SetSize(0);
	preindex=-1;
	preiForder=-1;
	preiItem=-1;
	mouseDown=false;
	ivstartItem=0;
	ivendItem=0;
	uparrow=false;
	downarrow=false;
	arrow=non;
	prearrow=non;

	arSmIcon.SetSize(0);
	arNrIcon.SetSize(0);
	arArrowIcon.SetSize(0);
}

CBarWnd::~CBarWnd()
{
}

BEGIN_MESSAGE_MAP(CBarWnd, CWnd)
	//{{AFX_MSG_MAP(CBarWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_TIMER()
	ON_WM_RBUTTONDOWN()
//	ON_COMMAND(ID_BT_SMALL,OnSmall)
//	ON_COMMAND(ID_BT_BIG,OnBig)
	ON_WM_DESTROY()
//	ON_COMMAND(ID_INSERT_ITEM, OnInsertItem)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CBarWnd message handlers

BOOL CBarWnd::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID) 
{
	CString strMyClass;
	strMyClass= AfxRegisterWndClass(
		CS_VREDRAW | CS_HREDRAW,
		::LoadCursor(NULL, IDC_ARROW),
		(HBRUSH) ::GetStockObject(WHITE_BRUSH),
		::LoadIcon(NULL, IDI_APPLICATION));
	return CreateEx(WS_EX_STATICEDGE,
      strMyClass,
	  NULL,
      dwStyle,
      rect,
	  pParentWnd,
	  nID);
}

void CBarWnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CWnd::OnPaint();
	CRect rc,rcForder;
	GetClientRect(&rc);
	CBrush brush(GetSysColor(COLOR_3DSHADOW));
	dc.FillRect(&rc,&brush);
	int iCount=arForderbar.GetSize();
	CPen pen;
	pen.CreatePen(PS_SOLID,1,RGB(96,96,96));
	dc.SelectObject(&pen);
	dc.SelectObject(&font);
	for(int i=0;i<iCount;i++)
	{
		DrawForder(i,&dc,false);
	}
}

int CBarWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	VERIFY(font.CreateFont(
		15,                        // nHeight
		0,                         // nWidth
		0,                         // nEscapement
		0,                         // nOrientation
		FW_NORMAL,                 // nWeight
		FALSE,                     // bItalic
		FALSE,                     // bUnderline
		0,                         // cStrikeOut
		ANSI_CHARSET,              // nCharSet
		OUT_DEFAULT_PRECIS,        // nOutPrecision
		CLIP_DEFAULT_PRECIS,       // nClipPrecision
		DEFAULT_QUALITY,           // nQuality
		DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
		"Arial"));                 // lpszFacename

	return 0;
}

void CBarWnd::OnSize(UINT nType, int cx, int cy) 
{
	CWnd::OnSize(nType, cx, cy);

	::SendMessage(HWND_BROADCAST,WM_SIZE,0,0);
	CForderBar *pForder;
	CRect rcForder,rcWnd,rcFC,rcClient;
	GetClientRect(&rcWnd);
	int iCount,ivisible=-1,tmp;
	iCount=arForderbar.GetSize();
	for(int index=0;index<iCount;index++)
	{
		pForder=(CForderBar *)arForderbar.GetAt(index);
		if(pForder->visible==true)
		{
			ivisible=index;
		}
	}
	for(index=0;index<iCount;index++)
	{
		pForder=(CForderBar *)arForderbar.GetAt(index);
		if(index<ivisible)
		{
			rcForder=CRect(rcWnd.left,rcWnd.top+index*20,rcWnd.right,rcWnd.top+index*20+20);
			pForder->SetForderRect(rcForder);
		}
		else if(index==ivisible)
		{
			if(rcWnd.Height()-iCount*20<0)
				tmp=0;
			else
				tmp=rcWnd.Height()-iCount*20;
			rcForder=CRect(rcWnd.left,rcWnd.top+index*20,rcWnd.right,rcWnd.top+index*20+20+tmp);
			pForder->SetForderRect(rcForder);
			if(pForder->pSon)
			{
				GetForderClientRect(index,rcFC);
				pForder->pSon->SetWindowPos(0,rcFC.left,rcFC.top,rcFC.right,rcFC.Height(),SWP_NOZORDER);
			}
		}
		else
		{
			rcForder=CRect(rcWnd.left,rcWnd.bottom-(iCount-index)*20,rcWnd.right,rcWnd.bottom-(iCount-index)*20+20);
			pForder->SetForderRect(rcForder);
		}
	}
	if(ivisible>=0)
	{
		pForder=(CForderBar *) arForderbar.GetAt(ivisible);
		int itemCount;
		GetForderClientRect(ivisible,rcClient);
		if(GetForderImageListSize(ivisible)>0)
			itemCount=rcClient.Height()/55;
		else
			itemCount=rcClient.Height()/26;
		if(itemCount>=pForder->GetItemCount()-ivstartItem)
			ivstartItem=pForder->GetItemCount()-itemCount;
		if(ivstartItem<0)
			ivstartItem=0;
		if(ivstartItem>0)
			uparrow=true;
		else
			uparrow=false;
	}
	downarrow=false;
	arrow=non;
}

//	增加一个抽屉框
void CBarWnd::AddForder(LPCTSTR fordername)
{
	CForderBar *pForder=new CForderBar(fordername);
	ASSERT(pForder);
	arForderbar.Add((void *)pForder);
}

void CBarWnd::GetForderBarRect(int iForder,CRect &rect)
{
	if(iForder<arForderbar.GetSize())
	{
		CForderBar * pForder=(CForderBar *)arForderbar.GetAt(iForder);
		if(pForder->nForderRect.bottom-pForder->nForderRect.top>0)
		{
			rect.left=pForder->nForderRect.left;
			rect.right=pForder->nForderRect.right;
			rect.top=pForder->nForderRect.top;
			rect.bottom=rect.top+20;
		}
	}
}

//	增加一个抽屉框子窗口
bool CBarWnd::AddForderSon(int iForder,CWnd *pSon)
{
	if(iForder<arForderbar.GetSize())
	{
		CForderBar * pForder=(CForderBar *)arForderbar.GetAt(iForder);
		if(pSon) pForder->pSon=pSon;
		else
			return false;
	}
	else
		return false;
	return true;
}

//	获取一个抽屉框的客户区矩形信息
void CBarWnd::GetForderClientRect(int iForder,CRect &rect)
{
	if(iForder<arForderbar.GetSize())
	{
		CForderBar * pForder=(CForderBar *)arForderbar.GetAt(iForder);
		if(pForder->nForderRect.Height()>20)
		{
			rect.left=pForder->nForderRect.left;
			rect.right=pForder->nForderRect.right;
			rect.top=pForder->nForderRect.top+20;
			rect.bottom=pForder->nForderRect.bottom;
		}
		else
		{
			rect=CRect(pForder->nForderRect.left,pForder->nForderRect.bottom,
				pForder->nForderRect.left,pForder->nForderRect.bottom);
		}
	}
}

//	获取一个抽屉框的矩形信息,包括客户区
void CBarWnd::GetForderRect(int iForder,CRect &rect)
{
	if(iForder<arForderbar.GetSize())
	{
		CForderBar *pForder=(CForderBar *)arForderbar.GetAt(iForder);
		rect=pForder->nForderRect;
	}
}

CBarWnd::CForderBar::CForderBar(CString name)
{
	nForderName=name;
	smallicon=NULL;
	nricon=NULL;
	arItem.SetSize(0);
	pSon=NULL;
	nForderRect=CRect(0,0,0,0);
	visible=false;
}

CBarWnd::CForderBar::~CForderBar()
{
}

void CBarWnd::CForderBar::SetForderRect(CRect rect)
{
	nForderRect=rect;
}

void CBarWnd::DrawForder(int iForder,CDC *pDC,const bool bSelected)
{
	CRect rcForder,rcClient;
	CBrush pbrush(GetSysColor(COLOR_3DFACE));
	CForderBar *pForder=(CForderBar *)arForderbar.GetAt(iForder);
	pDC->SelectObject(&font);
	GetForderBarRect(iForder,rcForder);
	if(bSelected)
	{
		pDC->Draw3dRect(&rcForder,GetSysColor(COLOR_3DSHADOW),GetSysColor(COLOR_3DHILIGHT));
		rcForder=CRect(rcForder.left+1,rcForder.top+1,rcForder.right-1,rcForder.bottom-1);
		pDC->FillRect(&rcForder,&pbrush);
		pDC->SetBkMode(TRANSPARENT);
		pDC->SetTextColor(RGB(0,0,255));
		rcForder=CRect(rcForder.left+1,rcForder.top+2,rcForder.right+1,rcForder.bottom+2);
		pDC->DrawText(pForder->nForderName,strlen(pForder->nForderName),&rcForder,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
	}
	else
	{
		pDC->Draw3dRect(&rcForder,GetSysColor(COLOR_3DHILIGHT),GetSysColor(COLOR_3DSHADOW));
		rcForder=CRect(rcForder.left+1,rcForder.top+1,rcForder.right-1,rcForder.bottom-1);
		pDC->FillRect(&rcForder,&pbrush);
		pDC->SetBkMode(TRANSPARENT);
		pDC->SetTextColor(RGB(0,0,0));
		rcForder=CRect(rcForder.left+3,rcForder.top+2,rcForder.right-1,rcForder.bottom-1);
		pDC->DrawText(pForder->nForderName,strlen(pForder->nForderName),&rcForder,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
	}
	GetForderClientRect(iForder,rcClient);
	if(pForder->pSon&&pForder->visible)
	{
		pForder->pSon->ShowWindow(SW_SHOW);
	}
	else if(pForder->visible)
		DrawForderItem(iForder,pDC);
	else if(pForder->pSon)
		pForder->pSon->ShowWindow(SW_HIDE);
}

BOOL CBarWnd::OnEraseBkgnd(CDC* pDC) 
{
	return true;
}

void CBarWnd::OnLButtonDown(UINT nFlags, CPoint point) 
{
	int index,iForder,iItem;
	HitOnPos(point.x,point.y,index);
	
	CClientDC dc(this);
	if(index>=0)
	{
		preindex=index;
		DrawForder(index,&dc,true);
	}

	HitOnPosEx(point.x,point.y,iForder,iItem);
	if(iForder>=0&&iItem>=0)
	{
		preiForder=iForder;
		preiItem=iItem;
		DrawItem(iForder,iItem,&dc,mouse_down);
		BarItemMsg barItem;
		barItem.iFolder = iForder;
		barItem.iItem   = iItem;
		barItem.point   = point;
		GetParent()->SendMessage( WM_BARITEM_LBDOWN, (WPARAM)&barItem, iItem );
	}
	mouseDown=true;
	if(iForder>=0)
	{
		DrawArrow(iForder,&dc,mouse_down);
		prearrow=arrow;
		if(arrow==pointup && uparrow)
			SetTimer(1,200,0);
		else if(arrow && downarrow)
			SetTimer(1,150,0);
	}

	SetCapture();
	CWnd::OnLButtonDown(nFlags, point);
}

void CBarWnd::PreSubclassWindow() 
{
	CWnd::PreSubclassWindow();
}

//	测试是否有哪个抽屉框被选中
void CBarWnd::HitOnPos(int cx,int cy,int &index)
{
	int iCount=arForderbar.GetSize();
	CRect rect;
	index = -1;
	for(int i=0;i<iCount;i++)
	{
		GetForderBarRect(i,rect);
		if(cx>=rect.left&&cx<=rect.right&&cy>=rect.top&&cy<=rect.bottom)
		{
			index=i;
			break;
		}
	}
}

//	测试是否有哪个抽屉框的子条件被选中
void CBarWnd::HitOnPosEx(int cx,int cy,int &index,int &iItem)
{
	int iCount=arForderbar.GetSize();
	CForderBar * pForder;
	CRect rect,rcIma,rcTxt,rcUp,rcDown;
	iItem=-1;
	index=-1;
	for(int i=0;i<iCount;i++)
	{
		GetForderClientRect(i,rect);
		if(cx>=rect.left&&cx<=rect.right&&cy>=rect.top&&cy<=rect.bottom)
		{
			index=i;
			break;
		}
		else
			index=-1;
	}
	if(index>=0)
	{
		pForder=(CForderBar *) arForderbar.GetAt(index);
		int iItemCount,iAItemCount=pForder->GetItemCount();
		if(GetForderImageListSize(index)>0)
			iItemCount=rect.Height()/55;
		else
			iItemCount=rect.Height()/26;
		if(iItemCount>iAItemCount)
			iItemCount=iAItemCount;
		for(i=0;i<iItemCount;i++)
		{
			GetForderItemActiveRect(index,i,rcIma,rcTxt);
			if((cx>=rcIma.left&&cx<=rcIma.right&&cy>=rcIma.top&&cy<=rcIma.bottom)||
				(cx>=rcTxt.left&&cx<=rcTxt.right&&cy>=rcTxt.top&&cy<=rcTxt.bottom))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -