www.gusucode.com > VC++高仿迅雷7漂亮界面开源源码-源码程序 > VC++高仿迅雷7漂亮界面开源源码-源码程序/code/LEinGUI/CLAYUIEXP3Dlg.cpp

    //Download by http://www.NewXing.com
// CLAYUIEXP3Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "CLAYUIEXP3.h"
#include "CLAYUIEXP3Dlg.h"

#include "../clayui/UI_ANIMATION/CLAYUI_ALPHA_ANIM.h"
#include "../clayui/UI_ANIMATION/CLAYUI_SCALE_ANIM.h"
#include "../clayui/UI_ANIMATION/CLAYUI_ROTATE_ANIM.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCLAYUIEXP3Dlg dialog
WNDPROC				g_pWndProc;
CCLAYUIEXP3Dlg::CCLAYUIEXP3Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCLAYUIEXP3Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCLAYUIEXP3Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_bframe = NULL;

	m_oldbmp = NULL;
	m_bghbitmap = NULL;

	m_oldbmp1 = NULL;
	m_bghbitmap1 = NULL;

	m_isInit = FALSE;
	m_isfocus = TRUE;

	m_isquit = FALSE;
}

void CCLAYUIEXP3Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCLAYUIEXP3Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCLAYUIEXP3Dlg, CDialog)
	//{{AFX_MSG_MAP(CCLAYUIEXP3Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_WM_MOUSEMOVE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCLAYUIEXP3Dlg message handlers

#define WS_EX_LAYERED           0x00080000    
#define LWA_COLORKEY            0x00000001
#define LWA_ALPHA               0x00000002
typedef DWORD (WINAPI *PSLWA)(HWND, DWORD, BYTE, DWORD);
static PSLWA pSetLayeredWindowAttributes = NULL;
static BOOL initialized = FALSE;

BOOL MakeWindowTransparent(HWND hWnd, unsigned char factor)
{
   if (!initialized)
   {
      HMODULE hDLL = LoadLibrary ("user32");

      pSetLayeredWindowAttributes = 
         (PSLWA) GetProcAddress(hDLL, "SetLayeredWindowAttributes");

      initialized = TRUE;
   }

   if (pSetLayeredWindowAttributes == NULL) 
      return FALSE;

   SetLastError(0);

   SetWindowLong(hWnd, 
                 GWL_EXSTYLE , 
                 GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);

   if (GetLastError())
      return FALSE;

   return pSetLayeredWindowAttributes (hWnd, 
                                       RGB(255,255,255), 
                                       factor,
                                       LWA_COLORKEY|LWA_ALPHA);
}

LRESULT CALLBACK CCLAYUIEXP3Dlg::CtrlWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{	
	LRESULT lResult = 0;
	if(nMsg != WM_ERASEBKGND)
		lResult = g_pWndProc( hWnd, nMsg, wParam, lParam);
	switch (nMsg)
	{
		case WM_PAINT:
		case WM_CAPTURECHANGED:
		case WM_CTLCOLOREDIT:
		case WM_CTLCOLORBTN:
		case WM_CTLCOLORSTATIC:
		case WM_CTLCOLORMSGBOX:
		case WM_CTLCOLORDLG:
		case WM_CTLCOLORLISTBOX:
		case WM_ERASEBKGND:
		case WM_ACTIVATE:
		case WM_LBUTTONDOWN:
		case WM_CTLCOLORSCROLLBAR:
			{
				CCLAYUIEXP3Dlg* dlg = (CCLAYUIEXP3Dlg*)AfxGetApp()->m_pMainWnd;
				CRect clientrect;
				dlg->GetClientRect(&clientrect);
				HDC hDC = ::GetDC(NULL);
				CDC* desktopdc = CDC::FromHandle(hDC);
				CRect windowrect = clientrect;
				dlg->ClientToScreen(&windowrect);
				dlg->m_memdc.BitBlt(0, 0, dlg->m_graphics.m_width, 
					dlg->m_graphics.m_height, desktopdc, windowrect.left, windowrect.top, SRCCOPY);
				::ReleaseDC(NULL, hDC);
				if(dlg->m_isInit == FALSE)
					MakeWindowTransparent(AfxGetMainWnd()->m_hWnd, 0);
				dlg->m_isInit = TRUE;
				dlg->m_isfocus = FALSE;
				break;
			}
			
		default:
			break;
	}
	return lResult;
}

BOOL CCLAYUIEXP3Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	//替换消息处理函数
	srand(GetTickCount());
	g_pWndProc = (WNDPROC)::GetWindowLong( GetSafeHwnd(), GWL_WNDPROC);
	::SetWindowLong( GetSafeHwnd(), GWL_WNDPROC, (LONG)(&CCLAYUIEXP3Dlg::CtrlWndProc) );

	//窗体透明
	MakeWindowTransparent(AfxGetMainWnd()->m_hWnd, 0);

	//全局初始化
	CLAYUI_Set3DLookat(300, 300, -1200);
	CLAYUI_Init();

	//创建渲染器
	CDC* pDC = GetDC();
	int width = 800;
	int height = 600;
	m_graphics.Create(width, height, this, pDC, 0);

	//初始化透明窗体的参数
	m_bgbmpinfo.bmiHeader.biWidth         = width;
	m_bgbmpinfo.bmiHeader.biHeight        = 0 - height;
	m_bgbmpinfo.bmiHeader.biBitCount      = 24;
	m_bgbmpinfo.bmiHeader.biSize          = sizeof( BITMAPINFOHEADER );
	m_bgbmpinfo.bmiHeader.biPlanes        = 1;
	m_bgbmpinfo.bmiHeader.biCompression = BI_RGB;
	m_bgbmpinfo.bmiHeader.biSizeImage     = 0;
	m_bgbmpinfo.bmiHeader.biClrImportant  = 0;
	m_bgbmpinfo.bmiHeader.biClrUsed       = 0;
	m_bgbmpinfo.bmiHeader.biXPelsPerMeter = 0;
	m_bgbmpinfo.bmiHeader.biYPelsPerMeter = 0;

	m_bghbitmap = ::CreateDIBSection( pDC->m_hDC, (BITMAPINFO*)&m_bgbmpinfo, DIB_RGB_COLORS, (void**)&m_bgpvb, NULL, 0 );
	m_memdc.CreateCompatibleDC(pDC);
	m_oldbmp = (HBITMAP)m_memdc.SelectObject(m_bghbitmap);

	m_bghbitmap1 = ::CreateDIBSection( pDC->m_hDC, (BITMAPINFO*)&m_bgbmpinfo, DIB_RGB_COLORS, (void**)&m_bgpvb1, NULL, 0 );
	m_memdc1.CreateCompatibleDC(pDC);
	m_oldbmp1 = (HBITMAP)m_memdc1.SelectObject(m_bghbitmap1);
	
	ReleaseDC(pDC);

	//得到程序路径
	char szBuf[256];
	char *p; 
	GetModuleFileName(AfxGetInstanceHandle(), szBuf, 256); 
	p = szBuf;
	while(strchr(p,'\\')) {
	p = strchr(p,'\\');
	p++;
	}
	*p = '\0';

	CString exepath(szBuf);

	//设置背景擦除色
	m_graphics.SetClearColor(100, 100, 100, 0);

	//设置默认字体
	char syspath[256];
	GetWindowsDirectory(syspath, 256);
	CString sys_path(syspath);
	sys_path += CString("\\fonts\\微软雅黑.TTF");
	CLAYUI_SetDefaultTextName(sys_path.GetBuffer(0));

	//将渲染器与皮肤,布局绑定
	CLAYUI_AppInit(0, m_graphics.m_draw);

	//导入皮肤
	CString skinpath = exepath + CString("LeinG.ui");
	CLAYUI_ImportSkin(0, skinpath.GetBuffer(0));

	//设置皮肤到Slot 0
	CLAYUI_SetSkinToSkinSlot(0, "system", 0);

	//导入布局
	CString layoutpath = exepath + CString("LeinG.bui");
	CLAYUI_ImportLayout(0, layoutpath.GetBuffer(0));

	//设置布局到Slot 0
	CLAYUI_SetLayoutToLayoutSlot(0, "system", 0);

	//创建主窗口
	m_bframe = new CLAYUI_BFRAME();
	m_bframe->m_gitemframe = m_bframe;
	m_bframe->Init(NULL, m_graphics.m_draw, FALSE);

	//读取布局文件
	BYTE* filebuf = NULL;
	DWORD filesize = 0;
	CFile file;
	if(file.Open(exepath + CString("xl.ci"), CFile::modeRead))
	{
		filesize = file.GetLength();
		filebuf = new BYTE[filesize];
		file.Read(filebuf, filesize);
		file.Close();
		CEXP3FRAME* exp3frame = new CEXP3FRAME();
		exp3frame->m_dlg = this;
		exp3frame->m_exepath = exepath;
		exp3frame->InitBFrame(m_bframe, m_bframe, m_graphics.m_draw, FALSE);
		exp3frame->LoadFromMem(filebuf, filesize, FALSE);
		
		delete [] filebuf;
	
		m_rootframe_wrapper.InitRootFrame(m_bframe);
		m_graphics.AttachToRootFrame(m_rootframe_wrapper.m_rootframe);

		m_bframe->AddCustomFrame(0, "exp3", 0, 0, 500, 400, 100, exp3frame);
		exp3frame->SetIs3D(1);
		exp3frame->MoveTo(100, 100, 1 * CLAYUI_Get3DZ());
		exp3frame->RotateTo(0, 0, 0);
		//exp3frame->ScaleTo(0.6, 0.6, 1);
		exp3frame->SetOri(250 << 16, 200 << 16, 0);

		CLAYUI_ROTATE_ANIM* rotate = new CLAYUI_ROTATE_ANIM();
		rotate->Init(0, 0, 0, 0, 180, 0, 600, m_bframe);
		rotate->SetTarget(exp3frame);
		rotate->SetIdname("rot0");
		m_bframe->AddAnimation(rotate);
		rotate->SetMoveType(CLAYUI_MOVETYPE::CLAYUI_MOVE_Linear, 
			CLAYUI_MOVETYPE::CLAYUI_MOVE_EASEOUT);
		rotate->Play();
	}

	//设置定时器
	SetTimer(0, 2, NULL);

	m_timecount = ::timeGetTime();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCLAYUIEXP3Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
		Redraw(NULL);
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCLAYUIEXP3Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CCLAYUIEXP3Dlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	m_rootframe_wrapper.PreTranslateMessage(this, pMsg);

	if(pMsg->message == WM_INPUTLANGCHANGEREQUEST)
	{
		ActivateKeyboardLayout((HKL)HKL_NEXT,KLF_SETFORPROCESS);
	}
	if(pMsg->message == WM_KEYDOWN)
	{
		m_rootframe_wrapper.OnKeyDown(pMsg->wParam, 1, pMsg->lParam);
	}
	if(pMsg->message == WM_LBUTTONDOWN)
	{
		m_graphics.OnLButtonDown(LOWORD(pMsg->lParam), HIWORD(pMsg->lParam)); 
	}
	else if(pMsg->message == WM_LBUTTONDBLCLK)
	{
		m_graphics.OnLButtonDown(LOWORD(pMsg->lParam), HIWORD(pMsg->lParam));
	}
	else if(pMsg->message == WM_LBUTTONUP)
	{
		m_graphics.OnLButtonUP(LOWORD(pMsg->lParam), HIWORD(pMsg->lParam));
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CCLAYUIEXP3Dlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	KillTimer(0);

	//释放资源,这里3个Release函数的调用顺序不能改变,否则退出时会抱错
	//释放主窗口
	m_rootframe_wrapper.Release();
	//释放皮肤,布局
	CLAYUI_Release();
	//释放绘图设备
	m_graphics.Release();

	if(m_oldbmp)
	{
		m_memdc.SelectObject(m_oldbmp);
		m_memdc.DeleteDC();
	}
	if(m_bghbitmap)
		DeleteObject(m_bghbitmap);

	if(m_oldbmp1)
	{
		m_memdc1.SelectObject(m_oldbmp1);
		m_memdc1.DeleteDC();
	}
	if(m_bghbitmap1)
		DeleteObject(m_bghbitmap1);
	CDialog::OnClose();
}

void CCLAYUIEXP3Dlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_graphics.OnMouseMove(point.x, point.y);
	CDialog::OnMouseMove(nFlags, point);
}

void CCLAYUIEXP3Dlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(::GetActiveWindow() == GetSafeHwnd())
		Redraw(NULL);
	else
	{
		MakeWindowTransparent(AfxGetMainWnd()->m_hWnd, 0);
	}
	CDialog::OnTimer(nIDEvent);
}

void CCLAYUIEXP3Dlg::Redraw(CDC *pDC)
{
	if(::timeGetTime() - m_timecount < 20)
		return ;
	m_timecount = ::timeGetTime();
	if(m_isquit)
	{
		KillTimer(0);
		SendMessage(WM_CLOSE);
		return ;
	}
	if(!m_isInit)
		return ;
	m_graphics.Draw();

	BOOL isdeletedc = FALSE;
	if(pDC == NULL)
	{
		pDC = GetDC();	
		isdeletedc = TRUE;
	}

	//将桌面背景拷贝到缓冲区m_memdc1
	m_memdc1.BitBlt(0, 0, m_graphics.m_width, 
		m_graphics.m_height, &m_memdc, 0, 0, SRCCOPY);

	//将CLAYUI的界面与m_memdc1做alpha混合
	CBitmap bmp;
	bmp.Attach(m_bghbitmap1);
	BITMAP bitp;
	bmp.GetBitmap(&bitp);
	BYTE* pvb = (BYTE*)bitp.bmBits;
	BYTE* pvb1 = m_graphics.m_pvb;
	short alpha;
	int i, j;
	int w, h;
	w = (bitp.bmWidth < m_graphics.m_width)?bitp.bmWidth:m_graphics.m_width;
	h = (bitp.bmHeight < m_graphics.m_height)?bitp.bmHeight:m_graphics.m_height;
	for(i = 0; i < h; i ++)
	{
		pvb = (BYTE*)bitp.bmBits + i * bitp.bmWidthBytes;
		pvb1 = m_graphics.m_pvb + i * m_graphics.m_ypitch;
		for(j = 0; j < w; j ++)
		{
			alpha = pvb1[3];
			pvb[0] = (pvb1[0] * alpha + (255 - alpha) * pvb[0]) >> 8;
			pvb[1] = (pvb1[1] * alpha + (255 - alpha) * pvb[1]) >> 8;
			pvb[2] = (pvb1[2] * alpha + (255 - alpha) * pvb[2]) >> 8;

			pvb += 3;
			pvb1 += m_graphics.m_xpitch;
		}
	}
	bmp.Detach();
	bmp.DeleteObject();

	//将m_memdc1拷贝到窗体DC上
	pDC->BitBlt(0, 0, m_graphics.m_width, 
		m_graphics.m_height, &m_memdc1, 0, 0, SRCCOPY);

	if(isdeletedc)
	{
		ReleaseDC(pDC);
	}

	if(m_isfocus == FALSE)
	{
		MakeWindowTransparent(AfxGetMainWnd()->m_hWnd, 255);
		m_isfocus = TRUE;
	}
}