www.pudn.com > netmapper.rar > MyImage.cpp


// MyImage.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "NetMap.h" 
#include "MyImage.h" 
#include "MainFrm.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyImage 
 
CMyImage::CMyImage() 
{ 
move=false; 
init_x=0; 
init_y=0; 
MyPos.x=0; 
MyPos.y=0; 
HSelf=NULL; 
} 
 
CMyImage::~CMyImage() 
{ 
} 
 
 
BEGIN_MESSAGE_MAP(CMyImage, CStatic) 
	//{{AFX_MSG_MAP(CMyImage) 
    ON_WM_CONTEXTMENU() 
	ON_WM_LBUTTONDOWN() 
	ON_WM_LBUTTONUP() 
	ON_WM_MOUSEMOVE() 
	ON_WM_PAINT() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyImage message handlers 
void CMyImage::OnContextMenu(CWnd* pWnd, CPoint point)  
{ 
   CMenu pop; 
   pop.LoadMenu(IDR_MENU1); 
   CMenu * popup=pop.GetSubMenu(0); 
   CMainFrame *pMain=(CMainFrame *)AfxGetApp()->m_pMainWnd; 
   popup->TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,pMain); 
   pMain->HSelect=HSelf; 
 } 
 
void CMyImage::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
   move=true; 
   init_x=point.x; 
   init_y=point.y; 
   CStatic::OnLButtonDown(nFlags, point); 
} 
 
void CMyImage::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if (move==true) 
    { 
	move=false; 
    CRect cr;  
    GetWindowRect(cr); 
    CRect c(cr); 
	GetParent()->ScreenToClient(c); 
	MyPos.x=c.left+33; 
    MyPos.y=c.top+12; 
  	GetParent()->RedrawWindow(); 
	} 
	CStatic::OnLButtonUp(nFlags, point); 
} 
 
void CMyImage::OnMouseMove(UINT nFlags, CPoint point)  
{ 
 
	// TODO: Add your message handler code here and/or call default 
    //	MessageBox("Move");		 
	if (move) 
	{ 
	int x1,y1; 
    CRect cr;  
    GetWindowRect(cr); 
    CRect c(cr); 
	GetParent()->ScreenToClient(c); 
	x1=c.left; 
	y1=c.top; 
	MoveWindow(x1+(point.x-init_x),y1+(point.y-init_y),65,35); 
  	} 
	CStatic::OnMouseMove(nFlags, point); 
} 
 
void CMyImage::OnPaint()  
{ 
//	CPaintDC dc(this); // device context for painting 
	HBITMAP l_hbmpBitmap = GetBitmap() ; 
 
	if( l_hbmpBitmap == NULL ) 
	{ 
		Default() ; 
 
		return ; 
	} 
 
	CPaintDC l_PaintDC( this ) ; 
 
	//////////////////////////////////////////////////////////////////////////// 
	// Prepare everything for drawing 
	//////////////////////////////////////////////////////////////////////////// 
 
	CRect l_rcClient ; 
 
	GetClientRect( &l_rcClient ) ; 
 
	CDC		 l_BufferDC ; 
			 l_BufferDC.CreateCompatibleDC( &l_PaintDC ) ; 
	CBitmap  l_BufferBitmap ; 
			 l_BufferBitmap.CreateCompatibleBitmap( &l_PaintDC, l_rcClient.Width(), l_rcClient.Height() ) ; 
	CBitmap* l_pOldBufferBitmap = l_BufferDC.SelectObject( &l_BufferBitmap ) ; 
	CDC		 l_MaskDC ; 
			 l_MaskDC.CreateCompatibleDC( &l_PaintDC ) ; 
	CBitmap  l_MaskBitmap ; 
			 l_MaskBitmap.CreateBitmap( l_rcClient.Width(), l_rcClient.Height(), 1, 1, NULL ) ; 
	CBitmap* l_pOldMaskBitmap = l_MaskDC.SelectObject( &l_MaskBitmap ) ; 
 
	#define ROP4_TRANSPARENTBLIT		0xCCAA0020 
	#define SRCMASK						0x00220326 
 
	//////////////////////////////////////////////////////////////////////////// 
	// Fill with transparent color 
	//////////////////////////////////////////////////////////////////////////// 
 
	l_BufferDC.FillSolidRect( &l_rcClient, RGB( 255, 0, 255 ) ) ; 
 
	//////////////////////////////////////////////////////////////////////////// 
	// Blit the bitmap to the buffer 
	//////////////////////////////////////////////////////////////////////////// 
 
	CDC		 l_MemoryDC ; 
			 l_MemoryDC.CreateCompatibleDC( &l_PaintDC ) ; 
	CBitmap* l_pOldMemoryBitmap = l_MemoryDC.SelectObject( CBitmap::FromHandle( l_hbmpBitmap ) ) ; 
 
	l_BufferDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_MemoryDC, 0, 0, SRCCOPY ) ; 
 
	l_MemoryDC.SelectObject( l_pOldMemoryBitmap ) ; 
 
	//////////////////////////////////////////////////////////////////////////// 
	// Create the mask. 
	//////////////////////////////////////////////////////////////////////////// 
 
	COLORREF l_crOldBack = l_BufferDC.SetBkColor( RGB( 255, 0, 255 ) ) ; 
 
	l_MaskDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_BufferDC, 0, 0, SRCCOPY ) ; 
 
	l_BufferDC.SetBkColor( l_crOldBack ) ; 
 
	//////////////////////////////////////////////////////////////////////////// 
	// Draw the bitmap transparently now, since not all devices support this 
	// and Windows 98 does not (duh?), if this fails, we do it the long way. 
	//////////////////////////////////////////////////////////////////////////// 
 
	if( ! l_PaintDC.MaskBlt( 0,  
							 0,  
					   		 l_rcClient.Width(), 
					   		 l_rcClient.Height(), 
					   		 &l_BufferDC, 
					   		 0, 
					   		 0, 
					   		 l_MaskBitmap, 
					   		 0, 
					   		 0, 
					   		 ROP4_TRANSPARENTBLIT ) ) 
	{ 
		CDC		 l_CopyDC ; 
				 l_CopyDC.CreateCompatibleDC( &l_PaintDC ) ; 
		CBitmap  l_CopyBitmap ; 
				 l_CopyBitmap.CreateCompatibleBitmap( &l_PaintDC, l_rcClient.Width(), l_rcClient.Height() ) ; 
		CBitmap* l_pOldCopyBitmap = l_CopyDC.SelectObject( &l_CopyBitmap ) ; 
 
		l_CopyDC.BitBlt(   0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_PaintDC,  0, 0, SRCCOPY  ) ; 
		l_CopyDC.BitBlt(   0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_MaskDC,   0, 0, SRCAND   ) ; 
		l_BufferDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_MaskDC,   0, 0, SRCMASK  ) ; 
		l_CopyDC.BitBlt(   0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_BufferDC, 0, 0, SRCPAINT ) ; 
		l_PaintDC.BitBlt(  0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_CopyDC,   0, 0, SRCCOPY  ) ; 
 
		l_CopyDC.SelectObject( l_pOldCopyBitmap ) ; 
	} 
 
	//////////////////////////////////////////////////////////////////////////// 
	// Clean up. 
	//////////////////////////////////////////////////////////////////////////// 
 
	l_MaskDC.SelectObject( l_pOldMaskBitmap ) ; 
	l_BufferDC.SelectObject( l_pOldBufferBitmap ) ;	 
    
}