www.pudn.com > OpenGL实现三维绘图.rar > XTPWnd.cpp


// XTPWnd.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "XTPWnd.h" 
#include "../test1.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
//(CDC *pDC) => (CDC *pMemDC) 
int XDC2MemDC(CDC *pDC, CDC *pMemDC) 
{ 
	if (pDC && pMemDC && !pMemDC->m_hDC) 
	{ 
		int cx, cy; 
		cx = GetSystemMetrics(SM_CXSCREEN); 
		cy = GetSystemMetrics(SM_CYSCREEN); 
 
		CWnd *pWnd = pDC->GetWindow(); 
		if (pWnd) 
		{ 
			CRect rect; 
			pWnd->GetWindowRect(rect); 
			cx = rect.Width();//GetSystemMetrics(SM_CXSCREEN); 
			cy = rect.Height();//GetSystemMetrics(SM_CYSCREEN); 
		} 
 
		//CDC *pDC; 
		pMemDC->CreateCompatibleDC(pDC); 
 
		CBitmap bmp; 
		bmp.CreateCompatibleBitmap(pDC, cx, cy); 
 
		pMemDC->SelectObject(&bmp); 
		pMemDC->BitBlt(0, 0, cx, cy, pDC, 0, 0, SRCCOPY); 
 
		::DeleteObject(bmp.m_hObject); 
 
		return 1; 
	} 
	return 0; 
} 
 
//(CWnd *pWnd) => (CDC *pMemDC) 
//	need: XDC2MemDC(...) 
int XCWnd2MemDC(CWnd *pWnd, CDC *pMemDC) 
{ 
	if (pWnd) 
		return XDC2MemDC(pWnd->GetDC(), pMemDC); 
 
	return 0; 
} 
 
CDC *XSaveCWnd(CWnd *pWnd) 
{ 
	if (pWnd) 
	{ 
		CDC *pMemDC = new CDC; 
		XCWnd2MemDC(pWnd, pMemDC); 
		 
		return pMemDC; 
	} 
 
	return NULL; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CXTPWnd 
 
CXTPWnd::CXTPWnd() 
{ 
	m_zoom = 1; 
	m_pMemDC = NULL;	 
} 
 
CXTPWnd::~CXTPWnd() 
{ 
	if (m_pMemDC) 
	{ 
		m_pMemDC->DeleteDC(); 
 
		delete m_pMemDC; 
		m_pMemDC = NULL; 
	} 
} 
 
 
BEGIN_MESSAGE_MAP(CXTPWnd, CWnd) 
	//{{AFX_MSG_MAP(CXTPWnd) 
	ON_WM_LBUTTONDOWN() 
	ON_WM_WINDOWPOSCHANGED() 
	ON_WM_ACTIVATE() 
	ON_WM_PAINT() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CXTPWnd message handlers 
 
void CXTPWnd::XCreate(LPCTSTR lpszWindowName) 
{ 
	if (m_hWnd) return; 
	CreateEx( 
		0,//WS_EX_TOPMOST, 
		AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)), 
		lpszWindowName, 
		WS_POPUP,//|WS_BORDER,//WS_OVERLAPPEDWINDOW, 
		0, 0, 300, 180, 
		NULL, 
		NULL, 
		NULL 
		); 
	CenterWindow(); 
	SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);  
	ShowWindow(SW_SHOW); 
} 
 
void CXTPWnd::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
 
	PostMessage (WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x, point.y)); 
	 
	CWnd::OnLButtonDown(nFlags, point); 
} 
 
//#include "TPWndDlg.h" 
//DEL void CXTPWnd::OnRButtonDown(UINT nFlags, CPoint point)  
//DEL { 
//DEL 	// TODO: Add your message handler code here and/or call default 
//DEL 	 
//DEL 	//CTPWndDlg dlg; 
//DEL 	//if (dlg.DoModal() == IDOK) 
//DEL 	DestroyWindow(); 
//DEL  
//DEL 	CWnd::OnRButtonDown(nFlags, point); 
//DEL } 
 
void CXTPWnd::XSaveScreen() 
{ 
	ShowWindow(SW_HIDE); 
 
	//DeskTopWnd 
	CWnd *pDeskTopWnd = new CWnd; 
	pDeskTopWnd->Attach(::GetDesktopWindow()); 
 
	CDC *pMemDC = XSaveCWnd(pDeskTopWnd); 
 
	if (m_pMemDC && pMemDC) 
	{ 
		m_pMemDC->DeleteDC(); 
		delete m_pMemDC; 
	} 
	if (pMemDC) 
		m_pMemDC = pMemDC; 
 
	pDeskTopWnd->Detach(); 
	delete pDeskTopWnd; 
 
	ShowWindow(SW_SHOW); 
} 
 
void CXTPWnd::XOwnerDraw() 
{ 
	CDC *pDC = GetDC(); 
	if (pDC && m_pMemDC) 
	{ 
		CRect clientRect; 
		CRect windowRect; 
		GetClientRect(clientRect); 
		GetWindowRect(windowRect); 
 
	//放大镜 
		{ 
			int cx, cy, cw, ch; 
			cx = clientRect.left; 
			cy = clientRect.top; 
			cw = clientRect.Width(); 
			ch = clientRect.Height(); 
			int wx, wy, ww, wh; 
			wx = windowRect.left; 
			wy = windowRect.top; 
			ww = windowRect.Width(); 
			wh = windowRect.Height(); 
			if (wx < 0) 
			{ 
				ww += wx; 
 
				cx = wx*(-1); 
				cw += wx; 
 
				wx *= -1; 
			} 
			if (wy < 0) 
			{ 
				wh += wy; 
 
				cy = wy*(-1); 
				ch += wy; 
 
				wy *= -1; 
			} 
			StretchBlt( 
				pDC->m_hDC, cx, cy, cw, ch, 
				m_pMemDC->m_hDC, wx, wy, ww/3, wh/3, 
				SRCCOPY 
				); 
		} 
		//画放大镜边 
		CPen *pen = new CPen(PS_SOLID,1,GRAY); 
		CPen *pOldPen = pDC->SelectObject(pen);	 
		pDC->MoveTo(clientRect.left, clientRect.top); 
		pDC->LineTo(clientRect.right - 1, clientRect.top); 
		pDC->LineTo(clientRect.right - 1, clientRect.bottom - 1); 
		pDC->LineTo(clientRect.left, clientRect.bottom - 1); 
		pDC->LineTo(clientRect.left, clientRect.top); 
		delete pen; 
	} 
 
} 
 
void CXTPWnd::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos)  
{ 
	CWnd::OnWindowPosChanged(lpwndpos); 
	 
	// TODO: Add your message handler code here 
 
	XOwnerDraw(); 
} 
 
void CXTPWnd::OnPaint()  
{ 
	CPaintDC dc(this); // device context for painting 
	XOwnerDraw(); 
} 
 
void CXTPWnd::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)  
{ 
	CWnd::OnActivate(nState, pWndOther, bMinimized); 
	 
	// TODO: Add your message handler code here 
	 
	if (WA_ACTIVE == nState) 
		XSaveScreen(); 
}