Click here to Skip to main content
15,881,027 members
Articles / Programming Languages / C++

COM from scratch - PART TWO

Rate me:
Please Sign up or sign in to vote.
4.85/5 (44 votes)
17 Apr 200414 min read 225.7K   6K   134  
An article about COM Library.
// Component2Wnd.cpp: implementation of the CComponent2Wnd class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "Component2Wnd.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CComponent2Wnd::CComponent2Wnd():m_pComponent2sIUnknown(NULL),m_nItemID(0)
{
	m_cRef=0;
	//Background color for menu when selected with mouse
	m_color_hittest=4294967295;
	//this color will be changed either by the client's view window(OnLeftButtonDown)
	//or by the function OnNcLButtonDown
	m_bMenuHasOpend=false;
    m_bStop=true;//butterfly is not  flying as default
    //SetTimer(2,2000,0);
}

/////////////////////////////////
CComponent2Wnd::~CComponent2Wnd()
{
delete this;
}
BEGIN_MESSAGE_MAP(CComponent2Wnd, CWnd)
	//{{AFX_MSG_MAP(CComponent2Wnd)
	ON_WM_MOVE()
	ON_WM_MOUSEMOVE()
	ON_WM_MENUSELECT()
	ON_WM_NCLBUTTONDOWN()
	ON_WM_TIMER()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//////////////////////////////////////////
void CComponent2Wnd::OnMove(int x, int y) 
{
	if(!m_bStop)
	{
        CRect rect;
		CDC*  pDC=GetDC();
		GetClientRect (&rect);
		CBrush brush;
		brush.CreateSolidBrush (RGB(255,255,255));
		pDC->FillRect(&rect,&brush);	
		CClientDC dc(this);
		CBitmap bmp, *poldbmp;
		CDC memdc;
		bmp.LoadBitmap("IDB_BITMAP1" );
  		memdc.CreateCompatibleDC( &dc );
		poldbmp = memdc.SelectObject( &bmp );
		dc.BitBlt(rect.right -100,rect.bottom /4, 100, 100, &memdc, 0, 0, SRCCOPY );
		memdc.SelectObject( poldbmp );
	}
	
	CWnd::OnMove(x, y);
    
}
////////////////////////////////////////////////////////////
void CComponent2Wnd::OnMouseMove(UINT nFlags, CPoint point) 
{

if(m_bStop)//if not flying
	{
	    char buf[10];
		CRect rect;
		CDC*  pDC=GetDC();
		GetClientRect (&rect);
		CBrush brush;
		brush.CreateSolidBrush (RGB(255,255,255));
		pDC->FillRect(&rect,&brush);	
		CClientDC dc(this);
		CBitmap bmp, *poldbmp;
		CDC memdc;
		bmp.LoadBitmap("IDB_BITMAP" );
  		memdc.CreateCompatibleDC( &dc );
		poldbmp = memdc.SelectObject( &bmp );
		dc.BitBlt(point.x,point.y+5, 100, 100, &memdc, 0, 0, SRCCOPY );
		memdc.SelectObject( poldbmp );
		pDC->DrawText(itoa(m_cRef,buf,10),&rect,DT_RIGHT);
		CWnd::OnMouseMove(nFlags, point);
	
	}

 }

////////////////////////////////////////////////////////////////////////////
void CComponent2Wnd::OnMenuSelect(UINT nItemID, UINT nFlags, HMENU hSysMenu)
{
    //Tracking the last selected menu item
    if(nItemID!=0)
        m_nItemID=nItemID;
    
    // Making sure that menu has been opened
    // if((ID_ADDREF2) or (ID_FLY) or (ID_RELEAS2)has been selected once
    if( (nItemID==32791) ||
    (nItemID==32798) ||
    (nItemID==32799) ||
    (nItemID==32792))
    m_bMenuHasOpend=true;
    
    
    if((nFlags==0xFFFF)&&(hSysMenu==0))//mouse click has been outside of menu
    {
        
        TRACE(_T("Cliked out side menu\n"));
        SetTimer(1,100,NULL);//seting a timer event and checking which item has been selected
    }
    
    
    CWnd::OnMenuSelect(nItemID, nFlags, hSysMenu);
}


/////////////////////////////////////////////////////////////////
void CComponent2Wnd::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
	TRACE(_T("OnNcLButtonDown\n"));
	CDC* pDC=GetDC();
	if(m_bMenuHasOpend)
	{
		m_bMenuHasOpend=false;
		m_color_hittest=pDC->GetPixel (point);
	}
	
	CWnd::OnNcLButtonDown(nHitTest, point);
}


///////////////////////////////////////////////////////////////
void CComponent2Wnd::OnTimer(UINT nIDEvent)
{
	CString str="";
	CStdioFile file;
	static int j=1;
	CBitmap bmp, *poldbmp;
	CDC memdc;
	CRect rect;
	GetClientRect(&rect);
	CClientDC dc(this);
	
	
	if(nIDEvent!=2)
		KillTimer(nIDEvent);
	
	if(m_nItemID==32799)//Notify the client to call the StartFlying() method
	{
		str="1";
		file.Open("C:\\Comp2Notifyclient.txt",CFile::modeWrite|CFile::typeText);
		file.WriteString(str);
		file.Close();
		OnMove(0,0);
	}
	
	if(m_nItemID==32798)//ID_STOPFLY
		m_bStop=true;
	
	
	
	// 4294967295 is the color code for a pixel in a menu item
	if((m_color_hittest==4294967295)&&
	(m_bMenuHasOpend)&&
	(m_bStop)&&
	(nIDEvent==1))
	//if(m_bMenuHasOpend)
	
	{
		
		switch(m_nItemID)
		{
			case 32791://ID_ADDREF2
				TRACE(_T("ADDREF2\n"));
				m_pComponent2sIUnknown->AddRef();
				break;
				
			case 32792://ID_RELEASE2
				TRACE(_T("RELEASE2\n"));
				m_pComponent2sIUnknown->Release();
				break;
				
				
			case 32799://ID_STARTFLY
				m_bStop=false;
				OnMove(0,0);
				break;
				
		}
		m_bMenuHasOpend=false;
		
	}
	
	if((nIDEvent==2)&&(!m_bStop))
		
	{
		switch(j)
		{
			case 1:
				
				bmp.LoadBitmap( "IDB_BITMAP" );
				// Create a compatible memory DC
				memdc.CreateCompatibleDC( &dc );
				// Select the bitmap into the DC
				poldbmp = memdc.SelectObject( &bmp );
				// Copy (BitBlt) bitmap from memory DC to screen DC
				dc.BitBlt(rect.right -100,rect.bottom /4, 100, 100, &memdc, 0, 0, SRCCOPY );
				memdc.SelectObject( poldbmp );
				break;
				
			case 2:
				bmp.LoadBitmap( "IDB_BITMAP2" );
				memdc.CreateCompatibleDC( &dc );
				poldbmp = memdc.SelectObject( &bmp );
				dc.BitBlt( rect.right -100,rect.bottom /4, 100, 100, &memdc, 0, 0, SRCCOPY );
				memdc.SelectObject( poldbmp );
				break;
				
			case 3:
				bmp.LoadBitmap( "IDB_BITMAP3" );
				memdc.CreateCompatibleDC( &dc );
				poldbmp = memdc.SelectObject( &bmp );
				dc.BitBlt(rect.right -100,rect.bottom /4, 100, 100, &memdc, 0, 0, SRCCOPY );
				memdc.SelectObject( poldbmp );
				break;
				
				
			case 4:
				bmp.LoadBitmap( "IDB_BITMAP4" );
				memdc.CreateCompatibleDC( &dc );
				poldbmp = memdc.SelectObject( &bmp );
				dc.BitBlt( rect.right -100,rect.bottom /4, 100, 100, &memdc, 0, 0, SRCCOPY );
				memdc.SelectObject( poldbmp );
				break;
				
			case 5:
				bmp.LoadBitmap( "IDB_BITMAP5" );
				memdc.CreateCompatibleDC( &dc );
				poldbmp = memdc.SelectObject( &bmp );
				dc.BitBlt( rect.right -100,rect.bottom /4, 100, 100, &memdc, 0, 0, SRCCOPY );
				memdc.SelectObject( poldbmp );
				break;
				
				
			case 6:
				bmp.LoadBitmap( "IDB_BITMAP6" );
				memdc.CreateCompatibleDC( &dc );
				poldbmp = memdc.SelectObject( &bmp );
				dc.BitBlt(rect.right -100,rect.bottom /4, 100, 100, &memdc, 0, 0, SRCCOPY );
				memdc.SelectObject( poldbmp );
				break;
				
				
			case 7:
				bmp.LoadBitmap( "IDB_BITMAP7" );
				memdc.CreateCompatibleDC( &dc );
				poldbmp = memdc.SelectObject( &bmp );
				dc.BitBlt(rect.right -100,rect.bottom /4, 100, 100, &memdc, 0, 0, SRCCOPY );
				memdc.SelectObject( poldbmp );
				break;
				
			case 8:
				bmp.LoadBitmap( "IDB_BITMAP8" );
				memdc.CreateCompatibleDC( &dc );
				poldbmp = memdc.SelectObject( &bmp );
				dc.BitBlt(rect.right -100,rect.bottom /4, 100, 100, &memdc, 0, 0, SRCCOPY );
				memdc.SelectObject( poldbmp );
				break;
				
				
		}
		if(j==8)
			j=1;
		j++;
		
	}
	
	CWnd::OnTimer(nIDEvent);
}


void CComponent2Wnd::OnLButtonDown(UINT nFlags, CPoint point) 
{
    CDC* pDC=GetDC();
	m_color_hittest=pDC->GetPixel(point);
	if(m_color_hittest==RGB(0,0,0))
	CWnd::OnLButtonDown(nFlags, point);
}

void CComponent2Wnd::StartTimer()
{
   SetTimer(2,200,0);
}

void CComponent2Wnd::_KillTimer()
{
  KillTimer(2);
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions