Click here to Skip to main content
15,886,788 members
Articles / Programming Languages / C++

COM from scratch - PART ONE

Rate me:
Please Sign up or sign in to vote.
4.89/5 (118 votes)
17 Apr 2004CPOL13 min read 291.9K   9.2K   254  
An article about COM.
// Component3Wnd.cpp: implementation of the CComponent3Wnd class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Component3Wnd.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CComponent3Wnd::CComponent3Wnd():m_pComponent3sIUnknown(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_pComponent2sIUnknown=NULL;
	m_pComponent1sIUnknown=NULL;   
}
BEGIN_MESSAGE_MAP(CComponent3Wnd, CWnd)
	//{{AFX_MSG_MAP(CComponent3Wnd)
	ON_WM_LBUTTONDOWN()
	ON_WM_MENUSELECT()
	ON_WM_NCLBUTTONDOWN()
	ON_WM_TIMER()
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CComponent3Wnd::~CComponent3Wnd()
{
	delete this;
}

void CComponent3Wnd::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);
	CWnd::OnLButtonDown(nFlags, point);
}
/////////////////////////////////////////////////////////////////////////////
void CComponent3Wnd::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((nItemID==32790)|| //ID_NIGHT
	   (nItemID==32799)|| // ID_STARTFLY
	   (nItemID==32798)|| // ID_STOPFLY
	   (nItemID==32830)|| //ID_COMP3_ADDREF
	   (nItemID==32776))  //ID_ADDREF for component1
    m_bMenuHasOpend=true;
    
    if((nFlags==0xFFFF)&&(hSysMenu==0))//mouse click has been outside of menu
    {
        
        TRACE(_T("Cliked out side menu\n"));
        //seting a timer event and checking which item has been selected
		SetTimer(1,100,NULL);
    }
    CWnd::OnMenuSelect(nItemID, nFlags, hSysMenu);
}



void CComponent3Wnd::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 CComponent3Wnd::OnTimer(UINT nIDEvent) 
{
	KillTimer(nIDEvent);
	CString str;
	CStdioFile file;
	
if((m_color_hittest==4294967295)&&(m_bMenuHasOpend))
	{	
		switch(m_nItemID)
		{
		  
		    case 32830://ID_COMP3_ADDREF
			m_pComponent3sIUnknown->AddRef();
			break;
			
			case 32831://ID_COMP3_RELEASE
			m_pComponent3sIUnknown->Release();
			break;
		
			//Notify the client to call the Night() method of the inner Component1
			case 32790://ID_NIGHT
			TRACE(_T("Night is selected in the inner component1\n"));
			str="1";
			file.Open("C:\\Comp3Notifyclient.txt",CFile::modeWrite|CFile::typeText);
			file.WriteString(str);
		    break;
	

			//Notify the client to call the StartFlying() method
			case 32799://ID_STARTFLY		    
			str="2";
			file.Open("C:\\Comp3Notifyclient.txt",CFile::modeWrite|CFile::typeText);
			file.WriteString(str);
			file.Close();
			break;
		
			//Notify the client to call the StopFlying() method
			case 32798:
			str="3";
			file.Open("C:\\Comp3Notifyclient.txt",CFile::modeWrite|CFile::typeText);
			file.WriteString(str);
			file.Close();
			break;
		}
		
	}
	
CWnd::OnTimer(nIDEvent);
}

void CComponent3Wnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CRect rect;
	CRect grassRect;
    CDC*  pDC=GetDC();
    GetClientRect (&rect);
   	CBrush grassbrush;
	grassbrush.CreateSolidBrush(RGB(0,255,0));
	CBrush brush;
	brush.CreateSolidBrush (RGB(0,255,255));
    pDC->FillRect(&rect,&brush);	
	CBitmap bmp, *poldbmp;
    CDC memdc;
    bmp.LoadBitmap("IDB_NATURE" );
  	memdc.CreateCompatibleDC( pDC );
    poldbmp = memdc.SelectObject( &bmp );
    pDC->BitBlt( rect.right -180,rect.bottom-109, 200, 100, &memdc, 0, 0, SRCCOPY );
    memdc.SelectObject( poldbmp );
    grassRect.left=rect.left;
	grassRect.top=rect.bottom-10;
	grassRect.right=rect.right;
	grassRect.bottom=rect.bottom;
	pDC->FillRect(&grassRect,&grassbrush);
	
	// Do not call CWnd::OnPaint() for painting messages
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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