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

📄 testwnd.cpp

📁 即时通信系统的一套简单界面设计
💻 CPP
字号:
// TestWnd.cpp : implementation file
//

#include "stdafx.h"
#include "MClient.h"
#include "TestWnd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestWnd

CTestWnd::CTestWnd()
{
	m_pBitmapTab = NULL;
	m_pRectTab = NULL;
	m_nTabCount = 0;
	m_nCurrentIndex = 0;

	for(int i=0; i<MAX_DIALOG; i++)
		m_pTabDialog[i] = NULL;
}

CTestWnd::~CTestWnd()
{
	if (m_pRectTab)
		delete []m_pRectTab;
	if (m_pBitmapTab)
		delete []m_pBitmapTab;
}


BEGIN_MESSAGE_MAP(CTestWnd, CWnd)
	//{{AFX_MSG_MAP(CTestWnd)
	// NOTE - the ClassWizard will add and remove mapping macros here.
	ON_WM_PAINT()
	ON_WM_SETCURSOR()
	ON_WM_LBUTTONDOWN()
	ON_WM_SIZE()
	ON_WM_TIMER()
	ON_WM_ERASEBKGND()
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CTestWnd message handlers


void CTestWnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CRect rcClient;
	GetClientRect(&rcClient);

	CDC m_MemDC;
	m_MemDC.CreateCompatibleDC(&dc);

	CBitmap btScreen;
	btScreen.CreateCompatibleBitmap(&dc, rcClient.Width(), rcClient.Height());

	m_MemDC.SelectObject(&btScreen);
	btScreen.DeleteObject();

	//画背景
	DrawRangeImage(&m_BitmapBk, &m_MemDC, rcClient);
	DrawRangeImage(&m_BitmapLeft, &m_MemDC, CRect(0, 0, m_szBitmapLeft.cx, rcClient.Height()));
	DrawRangeImage(&m_BitmapRight, &m_MemDC, CRect(rcClient.Width()-m_szBitmapRight.cx, 2, rcClient.Width(), rcClient.Height()-m_szBitmapBottom.cy));
	DrawRangeImage(&m_BitmapTop, &m_MemDC, CRect(0, 0, rcClient.Width()-2, m_szBitmapTop.cy));

	//画Tab
	DrawPosImage(&m_pBitmapTab[m_nCurrentIndex], &m_MemDC, CPoint(0, 0));

	//画Bottom
	DrawRangeImage(&m_BitmapLeft, &m_MemDC, CRect(0, rcClient.Height()-m_szBitmapBottom.cy, 2*m_szBitmapLeft.cx, rcClient.Height()));
	DrawRangeImage(&m_BitmapBottom, &m_MemDC, CRect(m_szBitmapLeft.cx-1, rcClient.Height()-m_szBitmapBottom.cy, rcClient.Width()-1, rcClient.Height()));

	//画广告图片
	//CPoint ptAd;
	//if ((rcClient.Width()-m_szBitmapAd.cx)/2 >0)
	//	ptAd = CPoint((rcClient.Width()-m_szBitmapAd.cx)/2, rcClient.Height()-(m_szBitmapBottom.cy+m_szBitmapAd.cy)/2+1);
	//else
	//	ptAd = CPoint(0, rcClient.Height()-(m_szBitmapBottom.cy+m_szBitmapAd.cy)/2+1);
	//DrawPosImage(&m_BitmapAd, &m_MemDC, ptAd);
	
	//画到显示器上
	dc.BitBlt(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), &m_MemDC, 0, 0, SRCCOPY);

	m_MemDC.DeleteDC();

	// Do not call CWnd::OnPaint() for painting messages
}


void CTestWnd::SetTabCount(int nCount)
{
	m_nTabCount = nCount;

	if (m_pBitmapTab)
		delete []m_pRectTab;
	m_pBitmapTab = new CBitmap[m_nTabCount];

	if (m_pRectTab)
		delete []m_pRectTab;
	m_pRectTab = new CRect[m_nTabCount];

}

void CTestWnd::SetTabImage(int nIndex, CString strName)
{
	ASSERT(nIndex < m_nTabCount);
	HBITMAP hBitmap = NULL;
	hBitmap = (HBITMAP)::LoadImage(NULL, strName, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE|LR_LOADFROMFILE);
	ASSERT(hBitmap);
	
	if (m_pBitmapTab[nIndex].m_hObject)
		m_pBitmapTab[nIndex].Detach();

	m_pBitmapTab[nIndex].Attach(hBitmap);
}

void CTestWnd::SetTabImage(int nIndex, UINT nID)
{
	ASSERT(nIndex < m_nTabCount);

	if (m_pBitmapTab[nIndex].m_hObject)
		m_pBitmapTab[nIndex].Detach();

	m_pBitmapTab[nIndex].LoadBitmap(nID);
}

void CTestWnd::SetTabRect(int nIndex, CRect rc)
{
	ASSERT(nIndex < m_nTabCount);
	m_pRectTab[nIndex] = rc;
}

void CTestWnd::SetImage(CBitmap &bitmap, CString strName)
{
	HBITMAP hBitmap = NULL;
	hBitmap = (HBITMAP)::LoadImage(NULL, strName, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE|LR_LOADFROMFILE);
	ASSERT(hBitmap);

	if (bitmap.m_hObject)
		bitmap.Detach();

	bitmap.Attach(hBitmap);
}

void CTestWnd::SetImage(CBitmap &bitmap, UINT nID)
{
	if (bitmap.m_hObject)
		bitmap.Detach();

	bitmap.LoadBitmap(nID);
}

void CTestWnd::SetTopImage(CString strName)
{
	SetImage(m_BitmapTop, strName);
	SetImageSize(&m_BitmapTop, m_szBitmapTop);
}

void CTestWnd::SetTopImage(UINT nID)
{
	SetImage(m_BitmapTop, nID);
	SetImageSize(&m_BitmapTop, m_szBitmapTop);
}

void CTestWnd::SetBottomImage(CString strName)
{
	SetImage(m_BitmapBottom, strName);
	SetImageSize(&m_BitmapBottom, m_szBitmapBottom);
}

void CTestWnd::SetBottomImage(UINT nID)
{
	SetImage(m_BitmapBottom, nID);
	SetImageSize(&m_BitmapBottom, m_szBitmapBottom);
}

void CTestWnd::SetLeftImage(CString strName)
{
	SetImage(m_BitmapLeft, strName);
	SetImageSize(&m_BitmapLeft, m_szBitmapLeft);
}

void CTestWnd::SetLeftImage(UINT nID)
{
	SetImage(m_BitmapLeft, nID);
	SetImageSize(&m_BitmapLeft, m_szBitmapLeft);
}

void CTestWnd::SetRightImage(CString strName)
{
	SetImage(m_BitmapRight, strName);
	SetImageSize(&m_BitmapRight, m_szBitmapRight);
}

void CTestWnd::SetRightImage(UINT nID)
{
	SetImage(m_BitmapRight, nID);
	SetImageSize(&m_BitmapRight, m_szBitmapRight);
}

void CTestWnd::SetBkImage(CString strName)
{
	SetImage(m_BitmapBk, strName);
	SetImageSize(&m_BitmapBk, m_szBitmapBk);
}

void CTestWnd::SetBkImage(UINT nID)
{
	/*
	CString str="mclient";
	char cStr[10];
	strcpy(cStr,str);
	AfxMessageBox(cStr[0]);
	*/
	SetImage(m_BitmapBk, nID);
	SetImageSize(&m_BitmapBk, m_szBitmapBk);
}

void CTestWnd::SetAdImage(CString strName)
{
	SetImage(m_BitmapAd, strName);
	SetImageSize(&m_BitmapAd, m_szBitmapAd);
}

void CTestWnd::SetAdImage(UINT nID)
{
	SetImage(m_BitmapAd, nID);
	SetImageSize(&m_BitmapAd, m_szBitmapAd);
}

void CTestWnd::DrawRangeImage(CBitmap *pBitmap, CDC *pDC, CRect rc)
{
	CDC MemDC;
	BITMAP bm;
	pBitmap->GetBitmap(&bm);

	int li_Width = bm.bmWidth;
	int li_Height = bm.bmHeight;

	MemDC.CreateCompatibleDC(pDC);
	CBitmap* pOldBitmap = MemDC.SelectObject(pBitmap);

	int x=rc.left;
	int y=rc.top;
	while (y < (rc.Height()+rc.top)) 
 	{
 		while(x < (rc.Width()+rc.left)) 
 		{
			pDC->BitBlt(x, y, li_Width, li_Height, &MemDC, 0, 0, SRCCOPY);
 			x += li_Width;
 		}
		x = rc.left;
 		y += li_Height;
 	}

	MemDC.SelectObject(pOldBitmap);
	MemDC.DeleteDC();
}

void CTestWnd::DrawPosImage(CBitmap *pBitmap, CDC *pDC, CPoint pt)
{
	CDC MemDC;
	MemDC.CreateCompatibleDC(pDC);
	CBitmap *pOldBitmap = (CBitmap*)MemDC.SelectObject(pBitmap);

	BITMAP bm;
	pBitmap->GetBitmap(&bm);
	int li_Width = bm.bmWidth;
	int li_Height = bm.bmHeight;

	pDC->BitBlt(pt.x, pt.y, li_Width, li_Height, &MemDC, 0, 0, SRCCOPY);
	
	MemDC.SelectObject(pOldBitmap);
	MemDC.DeleteDC();
}

BOOL CTestWnd::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	CPoint pt;
	GetCursorPos(&pt);
	ScreenToClient(&pt);
	for(int i=0; i<m_nTabCount; i++)
	{
		if (m_pRectTab[i].PtInRect(pt) && m_nCurrentIndex != i)
		{
			SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(32649)));
			return TRUE;
		}
	}
	return CWnd::OnSetCursor(pWnd, nHitTest, message);
}

void CTestWnd::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	for(int i=0; i<m_nTabCount; i++)
	{
		if (m_pRectTab[i].PtInRect(point) && m_nCurrentIndex != i)
		{
			m_nCurrentIndex = i;
			ShowDialog();
			Invalidate();
			break;
		}
	}
	CWnd::OnLButtonDown(nFlags, point);
}

void CTestWnd::SetImageSize(CBitmap *pBitmap, CSize &sz)
{
	BITMAP bm;
	pBitmap->GetBitmap(&bm);
	//pBitmap->GetBitmap(NULL);
	sz = CSize(bm.bmWidth, bm.bmHeight);
}

void CTestWnd::OnSize(UINT nType, int cx, int cy) 
{
	CWnd::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	CRect rcClient;
	GetClientRect(&rcClient);
	for(int i=0; i<m_nTabCount; i++)
	{
		if (m_pTabDialog[i])
		{
			m_rcDialog = CRect(m_szBitmapLeft.cx+DIALOG_MARGIN, m_szBitmapTop.cy+DIALOG_MARGIN, 
							rcClient.Width()-m_szBitmapRight.cx-DIALOG_MARGIN, 
							rcClient.Height() - m_szBitmapBottom.cy - DIALOG_MARGIN);
			if (m_pTabDialog[i]->GetSafeHwnd())
			{
				m_pTabDialog[i]->MoveWindow(m_rcDialog, FALSE);
				UpdateTabWnd();
			}
		}
	}
	ShowDialog();
}


void CTestWnd::SetTabWnd(int nIndex, CDialog *pDlg)
{
	ASSERT(nIndex < m_nTabCount);

	m_pTabDialog[nIndex] = pDlg;
}

void CTestWnd::ShowDialog()
{
	CRect rcClient;
	GetClientRect(&rcClient);
	for(int i=0; i<m_nTabCount; i++)
	{
		if (m_pTabDialog[i] && i!= m_nCurrentIndex)
		{
			m_pTabDialog[i]->ShowWindow(SW_HIDE);
		}
	}
	if (m_pTabDialog[m_nCurrentIndex])
	{
		m_pTabDialog[m_nCurrentIndex]->ShowWindow(SW_SHOW);
		m_pTabDialog[m_nCurrentIndex]->SetFocus();
	}
}


void CTestWnd::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (m_BitmapAd.m_hObject)
	{
		CRect rcClient;
		GetClientRect(&rcClient);
		CRect rcAd;
		if ((rcClient.Width()-m_szBitmapAd.cx)/2 >0)
			rcAd = CRect((rcClient.Width()-m_szBitmapAd.cx)/2-2, rcClient.Height()-(m_szBitmapBottom.cy+m_szBitmapAd.cy)/2,
							(rcClient.Width()+m_szBitmapAd.cx)/2+4,
							rcClient.Height()+(m_szBitmapBottom.cy-m_szBitmapAd.cy)/2+2);
		else
			rcAd = CRect(0, rcClient.Height()-(m_szBitmapBottom.cy+m_szBitmapAd.cy)/2+1, 
							m_szBitmapAd.cx, rcClient.Height()+(m_szBitmapBottom.cy-m_szBitmapAd.cy)/2);
		InvalidateRect(rcAd, FALSE);
	}
	CWnd::OnTimer(nIDEvent);
}

BOOL CTestWnd::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	return TRUE;
	return CWnd::OnEraseBkgnd(pDC);
}


void CTestWnd::UpdateTabWnd()
{
	CRect rcClient;
	GetClientRect(&rcClient);
	//TOP
	InvalidateRect(CRect(0, 0, rcClient.Width(), TITLE_HEIGHT));
	//BOTTOM
	InvalidateRect(CRect(0, rcClient.Height()-m_szBitmapBottom.cy-DIALOG_MARGIN, rcClient.Width(), rcClient.Height()));
	//LEFT
	InvalidateRect(CRect(0, 0, m_szBitmapLeft.cx+DIALOG_MARGIN, rcClient.Height()));
	//RIGHT
	InvalidateRect(CRect(rcClient.Width()-m_szBitmapRight.cx-DIALOG_MARGIN, 0, rcClient.Width(), rcClient.Height()));
	
}

void CTestWnd::SetToolTip(int nIndex, CString strTips)
{
	ASSERT(nIndex < m_nTabCount);
	ASSERT(m_pRectTab[nIndex].IsRectEmpty() == FALSE);
	m_ToolTip.AddTool(this, strTips, m_pRectTab[nIndex], 12345678);
	m_ToolTip.SetTipBkColor(RGB(231,236,247));
	m_ToolTip.Activate(TRUE);
}

int CTestWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	if (!m_ToolTip.Create(this))
		return -1;

	//ModifyStyleEx(0, WS_EX_CLIENTEDGE);
	return 0;
}

void CTestWnd::PreSubclassWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	SetTimer(1, 1000, NULL);
	CWnd::PreSubclassWindow();
}

BOOL CTestWnd::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (m_ToolTip.GetSafeHwnd())
		m_ToolTip.RelayEvent( pMsg );
	return CWnd::PreTranslateMessage(pMsg);
}

⌨️ 快捷键说明

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