skinalterdlg.cpp

来自「《Visual C++视频技术方案宝典》配套光盘」· C++ 代码 · 共 820 行 · 第 1/2 页

CPP
820
字号
			y = bitinfo.bmiHeader.biHeight;
			WindowDC.StretchBlt(m_MaxRect.left,m_MaxRect.top,m_MaxRect.right,m_MaxRect.bottom,&memDC,0,0,x,y,SRCCOPY);
			memDC.SelectObject(OldObj);
			LeftLine.Detach();
		}

		//绘制关闭按钮
		if ( Flags & fCloseButton)
		{
			DeleteObject(LeftLine);
			LeftLine.Attach(pSkin->GetBitmapRes(8));
			//获取位图大小
			LeftLine.GetObject(sizeof(bitinfo),&bitinfo);
			OldObj =  memDC.SelectObject(&LeftLine);
			x = bitinfo.bmiHeader.biWidth;
			y = bitinfo.bmiHeader.biHeight;
			WindowDC.StretchBlt(m_CloseRect.left,m_CloseRect.top,m_CloseRect.right,m_CloseRect.bottom,&memDC,0,0,x,y,SRCCOPY);
			memDC.SelectObject(OldObj);
			LeftLine.Detach();
		}
		m_IsDrawForm = TRUE;
		LeftLine.DeleteObject();
		memDC.DeleteDC();
		DrawTitle();
	}
}

void CSkinAlterDlg::DrawTitle()
{
	if (! m_Caption.IsEmpty())
	{
		CDC* pDC = GetWindowDC();
		pDC->SetBkMode(TRANSPARENT);
		pDC->SetTextColor(m_CapitonColor);
		pDC->SetTextAlign(TA_CENTER);
		CRect rect;
		GetClientRect(rect);
		pDC->SelectObject(&m_CaptionFont);
		pDC->TextOut(rect.Width()/2, m_CaptionHeight/3 ,m_Caption);	
		ReleaseDC(pDC);
	}
}

BOOL CSkinAlterDlg::OnNcActivate(BOOL bActive) 
{
	OnPaint() ;	
	return TRUE;
}

HBRUSH CSkinAlterDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	//绘制背景
	if (nCtlColor==CTLCOLOR_DLG && m_LoadDll==TRUE)
	{
		if (m_LoadDll)
		{
			CBrush m_Brush(&m_Bk);
			CRect rect;
			GetClientRect(rect);
			pDC->SelectObject(&m_Brush);
			pDC->FillRect(rect,&m_Brush);
			return m_Brush;
		}
		else
			return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	}
	else if (nCtlColor ==CTLCOLOR_STATIC)
	{
		pDC->SetBkMode(TRANSPARENT);
		return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	}
	else
		return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}

void CSkinAlterDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	CDialog::OnMouseMove(nFlags, point);
	if (m_ButtonState != bsNone)
	{
		if (m_ButtonState==bsMin)
			SkinForm( fMinButton);
		else if (m_ButtonState==bsClose)
			SkinForm( fCloseButton);
		else if (m_ButtonState==bsMax||m_ButtonState==bsRes)
			SkinForm( fMaxButton);
		m_ButtonState = bsNone;
	}
}

void CSkinAlterDlg::OnNcLButtonDown(UINT nHitTest, CPoint point) 
{
	if (m_LoadDll)
	{
		switch (m_ButtonState)
		{
		case bsClose: //关闭窗口
			{
				OnCancel();
			}
			break;
		case bsMin: 
			{
				ShowWindow(SW_SHOWMINIMIZED);
			}
			break;
		case bsMax:
			{
				m_ButtonState = bsMax;
				ShowWindow(SW_SHOWMAXIMIZED);
				m_IsMax = FALSE;
			}
			break;
		case bsRes:
			{			
				ShowWindow(SW_RESTORE);
				m_IsMax = TRUE;
			}
			break;
		}
	}	
	CDialog::OnNcLButtonDown(nHitTest, point);
}

void CSkinAlterDlg::OnNcMouseMove(UINT nHitTest, CPoint point) 
{
	if (m_LoadDll)
	{
		CRect tempIni,tempMin,tempMax,tempClose,ClientRect;
		
		CWindowDC WindowDC (this);//= GetWindowDC();
		CDC memDC;
		memDC.CreateCompatibleDC(&WindowDC);

		BITMAPINFO bInfo;
		CBitmap LeftLine;
		int x,y;

		GetWindowRect(ClientRect);
		tempMin.CopyRect(CRect(m_MinRect.left+ ClientRect.left,ClientRect.top+m_MinRect.top,m_MinRect.right+m_MinRect.left+ ClientRect.left,m_MinRect.bottom+m_MinRect.top+ClientRect.top));
		tempMax.CopyRect(CRect(m_MaxRect.left+ ClientRect.left,ClientRect.top+m_MaxRect.top,m_MaxRect.right+m_MaxRect.left+ ClientRect.left,m_MaxRect.bottom+m_MaxRect.top+ClientRect.top));
		tempClose.CopyRect(CRect(m_CloseRect.left+ ClientRect.left,ClientRect.top+m_CloseRect.top,m_CloseRect.right+m_CloseRect.left+ ClientRect.left,m_CloseRect.bottom+m_CloseRect.top+ClientRect.top));

		if(tempMin.PtInRect(point)) //鼠标在最小化按钮上移动时,更改按钮显示的位图
		{
			if (m_ButtonState!= bsMin)
			{
				
				LeftLine.Attach(pSkin->GetBitmapRes(9));
				LeftLine.GetObject(sizeof(bInfo),&bInfo);
				
				x = bInfo.bmiHeader.biWidth;
				y = bInfo.bmiHeader.biHeight;
				memDC.SelectObject(&LeftLine);
				WindowDC.StretchBlt(m_MinRect.left,m_MinRect.top,m_MinRect.right,m_MinRect.bottom,&memDC,0,0,x,y,SRCCOPY);
				m_IsDrawForm = FALSE;
				m_ButtonState = bsMin;
				
				LeftLine.Detach();
			}
		}
		else if (tempMax.PtInRect(point))
		{
			if (m_ButtonState !=bsMax && m_ButtonState!=bsRes)
			{
				LeftLine.Attach(pSkin->GetBitmapRes(10));
				LeftLine.GetObject(sizeof(bInfo),&bInfo);
				
				x = bInfo.bmiHeader.biWidth;
				y = bInfo.bmiHeader.biHeight;
				memDC.SelectObject(&LeftLine);
				WindowDC.StretchBlt(m_MaxRect.left,m_MaxRect.top,m_MaxRect.right,m_MaxRect.bottom,&memDC,0,0,x,y,SRCCOPY);
				m_IsDrawForm = FALSE;
				if (m_IsMax)
				{
					m_ButtonState = bsMax;
				}
				else
				{
					m_ButtonState = bsRes;
				}
				LeftLine.Detach();	
			}
		}
		else if (tempClose.PtInRect(point))
		{
			if (m_ButtonState != bsClose)
			{
				LeftLine.Attach(pSkin->GetBitmapRes(11));
				LeftLine.GetObject(sizeof(bInfo),&bInfo);
				
				x = bInfo.bmiHeader.biWidth;
				y = bInfo.bmiHeader.biHeight;
				memDC.SelectObject(&LeftLine);
				WindowDC.StretchBlt(m_CloseRect.left,m_CloseRect.top,m_CloseRect.right,m_CloseRect.bottom,&memDC,0,0,x,y,SRCCOPY);
				m_IsDrawForm = FALSE;
				m_ButtonState = bsClose;
				LeftLine.Detach();	
			}
		}
		else
		{		
			if (m_IsDrawForm==FALSE)
			{
				if (m_ButtonState==bsMin)
					SkinForm( fMinButton);
				else if (m_ButtonState==bsClose)
					SkinForm( fCloseButton);
				else if (m_ButtonState==bsMax||m_ButtonState==bsRes)
					SkinForm( fMaxButton);
			}
			m_ButtonState = bsNone;
		}
		LeftLine.DeleteObject();
		ReleaseDC(&memDC);
	}
	CDialog::OnNcMouseMove(nHitTest, point);
}

void CSkinAlterDlg::OnNcPaint() 
{
	if (!m_LoadDll)
		CWnd::OnNcPaint();  
}

void CSkinAlterDlg::OnCancel()
{
	if  (m_LoadDll)
	{
		m_Bk.Detach();
		m_CaptionFont.DeleteObject();
	}
	CDialog::OnCancel();
}

void CSkinAlterDlg::OnSize(UINT nType, int cx, int cy) 
{
	SetWindowRgn(NULL,TRUE);
	CDialog::OnSize(nType, cx, cy);
	if (!m_LoadDll)
		return;

	CRect rect;
	GetClientRect(rect);

	CPoint pos = pSkin->GetButtonPos(6);
	m_MinRect.CopyRect(CRect(m_RTitleRc.left+pos.x,(m_TitleDrawHeight +2*m_BorderHeight -m_ButtonHeight)/2+pos.y,m_ButtonWidth,m_ButtonHeight));

	pos = pSkin->GetButtonPos(7);	
	m_MaxRect.CopyRect(CRect(m_RTitleRc.left+pos.x,(m_TitleDrawHeight+2*m_BorderHeight -m_ButtonHeight)/2+pos.y,m_ButtonWidth,m_ButtonHeight));
	pos = pSkin->GetButtonPos(8);
	m_CloseRect.CopyRect(CRect(m_RTitleRc.left+pos.x,(m_TitleDrawHeight+2*m_BorderHeight -m_ButtonHeight)/2+pos.y,m_ButtonWidth,m_ButtonHeight));
	
	SetWinZone();
	Invalidate();
}

void CSkinAlterDlg::SetWinZone()
{
	SkinForm();
	CRect winrect,factRC;
	GetWindowRect(winrect);

	factRC.CopyRect(CRect(0,0,winrect.Width(),winrect.Height()));
	WndRgn.DeleteObject();

	WndRgn.CreateRectRgnIndirect(factRC);

	CBitmap bitmap;
	BITMAPINFO bInfo;
	//去除左上角的空白区域
	bitmap.Attach(pSkin->GetBitmapRes(0));
	bitmap.GetObject(sizeof(bInfo),&bInfo);
	int x,y,m,n;
	x = bInfo.bmiHeader.biWidth;
	y = bInfo.bmiHeader.biHeight;
	CDC memDC;
	CDC* pDC = GetDC();
	memDC.CreateCompatibleDC(pDC);
	memDC.SelectObject(&bitmap);
	for ( m = 0; m<x; m++)
		for (n = 0; n<y; n++)
		{
			if (memDC.GetPixel(m,n)==RGB(255,255,255))
			{
				ClipRgn.CreateRectRgn(m,n,m+1,n+1);
				WndRgn.CombineRgn(&WndRgn,&ClipRgn,RGN_XOR);
				ClipRgn.DeleteObject();
			}
		}
	bitmap.Detach();

	//去除右上角的空白区域
	bitmap.Attach(pSkin->GetBitmapRes(2));
	bitmap.GetObject(sizeof(bInfo),&bInfo);
	x = bInfo.bmiHeader.biWidth;
	y = bInfo.bmiHeader.biHeight;
	memDC.SelectObject(&bitmap);
	for ( m = 0; m<x; m++)
		for (n = 0; n<y; n++)
		{
			if (memDC.GetPixel(m,n)==RGB(255,255,255))
			{
				ClipRgn.CreateRectRgn(m_RTitleRc.left+m,m_RTitleRc.top+n,m_RTitleRc.left+ m+1,m_RTitleRc.top+n+1);

				WndRgn.CombineRgn(&WndRgn,&ClipRgn,RGN_XOR);
				ClipRgn.DeleteObject();
			}
		}

	bitmap.Detach();
	//去除左下角的空白区域
	bitmap.Attach(pSkin->GetBitmapRes(12));
	bitmap.GetObject(sizeof(bInfo),&bInfo);
	x = bInfo.bmiHeader.biWidth;
	y = bInfo.bmiHeader.biHeight;
	memDC.SelectObject(&bitmap);
	for ( m = 0; m<x; m++)
		for (n = 0; n<y; n++)
		{
			if (memDC.GetPixel(m,n)==RGB(255,255,255))
			{
				ClipRgn.CreateRectRgn(m,factRC.Height()-y+n ,m+1,factRC.Height()-y+n+1);
				WndRgn.CombineRgn(&WndRgn,&ClipRgn,RGN_XOR);
				ClipRgn.DeleteObject();
			}
		}
	bitmap.Detach();

	//去除右下角的空白区域
	bitmap.Attach(pSkin->GetBitmapRes(13));
	bitmap.GetObject(sizeof(bInfo),&bInfo);
	x = bInfo.bmiHeader.biWidth;
	y = bInfo.bmiHeader.biHeight;
	memDC.SelectObject(&bitmap);
	for ( m = 0; m<x; m++)
		for (n = 0; n<y; n++)
		{
			if (memDC.GetPixel(m,n)==RGB(255,255,255))
			{
				ClipRgn.CreateRectRgn(m_RTitleRc.right-x+m,factRC.Height()-y+n,m_RTitleRc.right-x+m+1,factRC.Height()-y+n+1);
				WndRgn.CombineRgn(&WndRgn,&ClipRgn,RGN_XOR);
				ClipRgn.DeleteObject();
			}
		}
	bitmap.Detach();
	ReleaseDC(&memDC);
	ReleaseDC(pDC);
	SetWindowRgn(WndRgn,TRUE);
	DeleteObject(WndRgn);
}

void CSkinAlterDlg::Skin()
{
	m_LoadDll = ((CSkinAlterApp*)AfxGetApp())->m_LoadDll;
	pSkin = ((CSkinAlterApp*)AfxGetApp())->pSkin;
	m_Bk.DeleteObject();
	m_CaptionFont.DeleteObject();
	if (m_LoadDll)
	{
		ModifyStyle(WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_SYSMENU,0);
		m_Bk.Attach(pSkin->GetBitmapRes(14));
		//获取按钮位图大小
		CBitmap bitmap;
		bitmap.Attach(pSkin->GetBitmapRes(6));
		BITMAPINFO bInfo;
		bitmap.GetObject(sizeof(bInfo),&bInfo);
		m_ButtonWidth = bInfo.bmiHeader.biWidth;
		m_ButtonHeight = bInfo.bmiHeader.biHeight;
		bitmap.Detach();
		WndRgn.DeleteObject();
		SetWinZone();
		m_CaptionFont.CreateFont(12,10,0,0,600,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_ROMAN,"MS Sans Serif");
	}
	else
	{
		ModifyStyle(0,WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_SYSMENU|WS_CAPTION);
	}
}

void CSkinAlterDlg::OnRestore() 
{
	if (m_LoadDll)
	{
		((CSkinAlterApp*)AfxGetApp())->pSkin->Release();
		FreeLibrary(((CSkinAlterApp*)AfxGetApp())->m_Instance);
		((CSkinAlterApp*)AfxGetApp())->m_LoadDll=FALSE;
		m_LoadDll = FALSE;
		Skin();

		SetWindowPos(NULL,0,0,0,0,SWP_NOMOVE  |SWP_NOSIZE|SWP_DRAWFRAME);
		Invalidate();
		OnSize(0,0,0);
	}	
}

void CSkinAlterDlg::OnSkinTwo() 
{
	CFileDialog fDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"皮肤(.ski)|*.ski",this);
	if (fDlg.DoModal()==IDOK)
	{
		m_Bk.DeleteObject();
		m_CaptionFont.DeleteObject();
		((CSkinAlterApp*)AfxGetApp())->AlterSkin(fDlg.GetPathName());
		Skin();
		fDlg.DestroyWindow();
		Invalidate();
	}
}

⌨️ 快捷键说明

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