Click here to Skip to main content
15,886,873 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 226.6K   6K   134  
An article about COM Library.
// Component1Wnd.cpp : implementation file
//

#include "stdafx.h"
//#include "	\ add additional includes here"
#include "Component1Wnd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define	WM_RELEASE	(WM_USER+0x500)
/////////////////////////////////////////////////////////////////////////////
// CComponent1Wnd

CComponent1Wnd::CComponent1Wnd():m_pComponent1sIUnknown(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;
}

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


BEGIN_MESSAGE_MAP(CComponent1Wnd, CWnd)
	//{{AFX_MSG_MAP(CComponent1Wnd)
	ON_WM_MOUSEMOVE()
	ON_WM_MENUSELECT()
	ON_WM_TIMER()
	ON_WM_NCLBUTTONDOWN()
	ON_WM_MOVE()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
ON_MESSAGE(WM_RELEASE,OnRelease)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CComponent1Wnd message handlers
void CComponent1Wnd::OnMouseMove(UINT nFlags, CPoint point)
{
    char buf[10];
	CRect rect;
	CDC*  pDC=GetDC();
	GetClientRect (&rect);
	CBrush daybrush;
	CBrush sunsetbrush;
	daybrush.CreateSolidBrush (RGB(0,255,255));
	pDC->FillRect(&rect,&daybrush);
	CClientDC dc(this);
	CBitmap bmp, *poldbmp;
	CDC memdc;
	bmp.LoadBitmap("IDB_SUN" );
	memdc.CreateCompatibleDC( &dc );
	poldbmp = memdc.SelectObject( &bmp );
	dc.BitBlt(point.x,point.y+5, 100, 100, &memdc, 0, 0, SRCCOPY );
	memdc.SelectObject( poldbmp );
	COLORREF currentskycolor=pDC->GetPixel (point);
	
	if((point.y==rect.bottom )|(point.x==rect.right)|(point.x==rect.left ))
	{
		sunsetbrush.CreateSolidBrush(RGB(192,192,192));
		pDC->FillRect(&rect,&sunsetbrush);
		
	}
	GetClientRect (&rect);
	pDC->DrawText(itoa(m_cRef,buf,10),&rect,DT_RIGHT);
	
	CWnd::OnMouseMove(nFlags, point);
}

//---------------------------------------------------------------------------//
// In this function if the left mouse click has been outside of the components
// window and into the client's view window ,the client calls the compoents
// MenuitemNotselected() method and in this method the m_color_hittest will
// be set to white color and menu item selections will not activate Components
// mthods.(Not a good solution, as the menu item will be selected if the
//left mouse click is outside of the client's view window)
///-------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////
void CComponent1Wnd::OnMenuSelect(UINT nItemID, UINT nFlags, HMENU hSysMenu)
{
	
	//Tracking the last selected menu item
	if(nItemID!=0)
		m_nItemID=nItemID;//Geting the lost selected menu item
	// Making sure that menu has been opened
	if((nItemID==32776)||(nItemID==32790))// if((ID_ADDREF) or (ID_NIGHT)) has  selected once
		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 lost
	}
	CWnd::OnMenuSelect(nItemID, nFlags, hSysMenu);
}

//////////////////////////////////////////////////////////////
LRESULT CComponent1Wnd::OnRelease(WPARAM wParam,LPARAM lParam)
{
TRACE(_T("RELEASE message is received from CComponent1Wnd::OnRelease \n"));
m_pComponent1sIUnknown->Release();
return 0L;
}

////////////////////////////////////////////
void CComponent1Wnd::OnTimer(UINT nIDEvent)
{
	
	CString str="";
	CStdioFile file;
	KillTimer(nIDEvent);
	
	if((m_color_hittest==4294967295)&&(m_bMenuHasOpend))
	{
		switch(m_nItemID)
		{
			
		    case 32776://ID_ADDREF
				
				TRACE(_T("ADDREF\n"));
				m_pComponent1sIUnknown->AddRef();
				break;
				
			case 32777://ID_RELEASE
				TRACE(_T("RELEASE\n"));
				WindowProc(WM_RELEASE,0,0);// or  m_pComponent1sIUnknown->Release();
			    break;
				
				
			case 32790://ID_NIGHT
				//There is a Timer assosicated with the View class of the client
				//and in OnTimer(...) member function of the View class the value
				//from the file "Com1Notifyclient.txt" will be read and if the value
				//is "1" the client calls the Components Night() method.
				str="1";
				file.Open("C:\\Comp1Notifyclient.txt",CFile::modeWrite|CFile::typeText);
				file.WriteString(str);
				file.Close();
				break;
		}
		
	}
	CWnd::OnTimer(nIDEvent);
}





////////////////////////////////////////////////////////////////
void CComponent1Wnd::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 CComponent1Wnd::OnMove(int x, int y)
{
	CRect rect;
	CDC*  pDC=GetDC();
	GetClientRect (&rect);
	CPoint point;
	point.x =rect.right/2;
	point.y=rect.bottom /2;
	COLORREF pixelcolor;
	pixelcolor=pDC->GetPixel(point);
	if(pixelcolor!=RGB(192,192,192))
	{
		
		CBrush brush;
		brush.CreateSolidBrush (RGB(0,255,255));
		pDC->FillRect(&rect,&brush);
		CClientDC dc(this);
		CBitmap bmp, *poldbmp;
		CDC memdc;
		bmp.LoadBitmap("IDB_SUN" );
		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 CComponent1Wnd::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CDC* pDC=GetDC();
	m_color_hittest=pDC->GetPixel(point);
	if(m_color_hittest==RGB(0,255,255))
	TRACE(_T("backgraound color\n"));	
	CWnd::OnLButtonDown(nFlags, point);
	
}

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