// ComDemoView.cpp : implementation of the CComDemoView class
//
#include "stdafx.h"
#include "ComDemo.h"
//#include"C:\\Documents and Settings\\Aria\\Desktop\\DemoDll\\ComDemo\\Comp1\\Registry.h"
//#include"..\\ComDemo\\Comp1\\interface.h"
//#include"..\\ComDemo\\Comp2\\interface.h"
#include"..\\ComDemo\\Comp3\\interface.h"
#include "ComDemoDoc.h"
#include "ComDemoView.h"
#include <objbase.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//interface pointers for accesseing Component1
IUnknown* g_pIUnknown1=NULL;
IComponent1* g_pIComponent1=NULL;
//interface pointers for accesseing Compoenen2
IUnknown* g_pIUnknown2=NULL;
IComponent2* g_pIComponent2=NULL;
//interface pointers for accesseing Compoenen3
IUnknown* g_pIUnknown3=NULL;
IComponent3* g_pIComponent3=NULL;
/////////////////////////////////////////////////////////////////////////////
// CComDemoView
IMPLEMENT_DYNCREATE(CComDemoView, CView)
BEGIN_MESSAGE_MAP(CComDemoView, CView)
//{{AFX_MSG_MAP(CComDemoView)
ON_COMMAND(ID_COMP2, OnComp2)
ON_WM_LBUTTONDOWN()
ON_WM_TIMER()
ON_COMMAND(ID_COMP1, OnComp1)
ON_COMMAND(ID_COMP3, OnComp3)
ON_COMMAND(ID_SHOW, OnShow)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
//ON_MESSAGE(WM_RELEASE,OnRelease)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CComDemoView construction/destruction
CComDemoView::CComDemoView()
{
// TODO: add construction code here
}
CComDemoView::~CComDemoView()
{
}
BOOL CComDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CComDemoView drawing
void CComDemoView::OnDraw(CDC* pDC)
{
CComDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CBrush brush;
brush.CreateSolidBrush(RGB(0,0,0));
CRect rect;
GetClientRect(&rect);
pDC->FillRect(&rect,&brush);
}
/////////////////////////////////////////////////////////////////////////////
// CComDemoView printing
BOOL CComDemoView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CComDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CComDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CComDemoView diagnostics
#ifdef _DEBUG
void CComDemoView::AssertValid() const
{
CView::AssertValid();
}
void CComDemoView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CComDemoDoc* CComDemoView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CComDemoDoc)));
return (CComDemoDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CComDemoView message handlers
////////////////////////////
void CComDemoView::OnComp1()
{
CString str;
int i;
CStdioFile fr;
//Not allow creation of the component more than one
fr.Open("C:\\Comp1IUnknown.txt",CFile::modeRead);
fr.ReadString(str);
fr.Close();
i=atoi(str);
if(i==0)// 0 means component1 is not created
{
//CoInitialize(NULL); is called in the InitInstance() function at the start of the program
HRESULT hr = ::CoCreateInstance(CLSID_Component1,NULL,CLSCTX_INPROC_SERVER,IID_IUnknown,(void**)&g_pIUnknown1) ;
if (SUCCEEDED(hr))
{
TRACE(_T("Succeeded getting IComponent1.\n")) ;
CStdioFile fw;
COLORREF color=RGB(0,255,255);
hr=g_pIUnknown1->QueryInterface(IID_IComponent1,(void**)&g_pIComponent1);
if(SUCCEEDED(hr))
{
g_pIComponent1->DefineWindow_Com1(false);
g_pIComponent1->ShowWndBackground_Com1(color);
fw.Open ("C:\\Comp1IUnknown.txt",CFile::modeWrite);
fw.WriteString ("1");// not possible to make more componet1 until the one which is already created is released
fw.Close();
}
SetTimer(1,100,NULL);// checking the clients acions every 100 ms
}
else
{
TRACE(_T("Component1 creation failed"));
AfxMessageBox("Build the dlls and move them in the folder 'C:\\Codeproject' and activate the registeration file(registerdlls.reg)");
}
}
}
////////////////////////////
void CComDemoView::OnComp2()
{
CString str;
int i;
CStdioFile fr;
fr.Open("C:\\Comp2IUnknown.txt",CFile::modeRead);
fr.ReadString(str);
fr.Close();
i=atoi(str);
if(i==0)
{
//CoInitialize(NULL); is called in the ExitINstance() function at the end of the program
HRESULT hr = ::CoCreateInstance(CLSID_Component2,NULL,CLSCTX_INPROC_SERVER,IID_IUnknown,(void**)&g_pIUnknown2) ;
if (SUCCEEDED(hr))
{
TRACE(_T("Succeeded getting IComponent2.\n")) ;
CStdioFile fw;
COLORREF color=RGB(255,255,255);
hr=g_pIUnknown2->QueryInterface(IID_IComponent2,(void**)&g_pIComponent2);
if(SUCCEEDED(hr))
{
g_pIComponent2->DefineWindow_Com2(false);
g_pIComponent2->ShowWndBackground_Com2(color);
//g_pIComponent2->StopFlying();
fw.Open ("C:\\Comp2IUnknown.txt",CFile::modeWrite);
fw.WriteString ("1");
fw.Close();
}
SetTimer(1,100,NULL);
}
else
{
TRACE(_T("Component2 creation failed"));
AfxMessageBox("Build the dlls and move them in the folder 'C:\\Codeproject' and activate the registeration file(registerdlls.reg)");
}
}
}
////////////////////////////////////////////////////////////
void CComDemoView::OnLButtonDown(UINT nFlags, CPoint point)
{
CString str;
CStdioFile fr;
fr.Open("C:\\Comp1IUnknown.txt",CFile::modeRead);
fr.ReadString(str);
fr.Close();
if(str!="0")
{
if(NULL!=g_pIComponent1)
g_pIComponent1->MenuitemNotselected1();
}
fr.Open("C:\\Comp2IUnknown.txt",CFile::modeRead);
fr.ReadString(str);
fr.Close();
if(str!="0")
{
if(NULL!=g_pIComponent2)
g_pIComponent2->MenuitemNotselected2();
}
CView::OnLButtonDown(nFlags, point);
}
//--------------------------------------------------------------------//
// This function checks out menu item selections from the components
// windows and the corresponding methods belonging to the components
// will be called.
//--------------------------------------------------------------------//
void CComDemoView::OnTimer(UINT nIDEvent)
{
CString str;
int i;
CStdioFile fr;
//Checking out the menu item selection for componenet1 and response to that
fr.Open("C:\\Comp1Notifyclient.txt",CFile::modeRead);
fr.ReadString(str);
fr.Close();
i=atoi(str);
if(i!=0)// 1 means that the user has selected "Night();" menu item from component 1's menu,
//and here this action takes place by calling the component Night() method.
{
if(NULL!=g_pIComponent1)
g_pIComponent1->Night();//Calling the Night() method of Component1
//Init the notification file back
str="0";
fr.Open("C:\\Comp1Notifyclient.txt",CFile::modeWrite|CFile::typeText);
fr.WriteString(str);
fr.Close();
}
//Checking out the menu item selection for componenet 2 and response to that
fr.Open("C:\\Comp2Notifyclient.txt",CFile::modeRead);
fr.ReadString(str);
fr.Close();
i=atoi(str);
if(i!=0)
{
if(NULL!=g_pIComponent2)
g_pIComponent2->StartFlying();//Calling the StopFlying() method of Component2
//intit the notification file back
str="0";
fr.Open("C:\\Comp2Notifyclient.txt",CFile::modeWrite|CFile::typeText);
fr.WriteString(str);
fr.Close();
}
//Checking out the menu item selection for componenet 3 and response to that
fr.Open("C:\\Comp3Notifyclient.txt",CFile::modeRead);
fr.ReadString(str);
fr.Close();
i=atoi(str);
COLORREF whitecolor=RGB(0,0,0);
switch(i)
{
// 1 is coded to call the Night() method of the inner component1
case 1:
if(NULL!=g_pIComponent3)
g_pIComponent3->Night();
break;
// 2 is coded to call the StartFlying method of the inner component2
case 2:
if(NULL!=g_pIComponent3)
//calling the ShowWndBackground_Com2 once more in order to whipe the
// window before flying
g_pIComponent3->ShowWndBackground_Com2(whitecolor);
g_pIComponent3->StartFlying();
break;
// 3 is coded to call the StopFlying method of the inner component2
case 3:
if(NULL!=g_pIComponent3)
g_pIComponent3->StopFlying();
break;
}
//initialize the notification file
str="0";
fr.Open("C:\\Comp3Notifyclient.txt",CFile::modeWrite|CFile::typeText);
fr.WriteString(str);
fr.Close();
CView::OnTimer(nIDEvent);
}
////////////////////////////
void CComDemoView::OnComp3()
{
CString str;
int i;
CStdioFile fr;
fr.Open("C:\\Comp3IUnknown.txt",CFile::modeRead);
fr.ReadString(str);
fr.Close();
i=atoi(str);
if(i==0)
{
HRESULT hr = ::CoCreateInstance(CLSID_Component3,
NULL,
CLSCTX_INPROC_SERVER,
IID_IComponent3,
(void**)&g_pIUnknown3) ;
if (SUCCEEDED(hr))
{
TRACE(_T("Succeeded getting IComponent3.\n")) ;
COLORREF color=RGB(0,255,255);
hr=g_pIUnknown3->QueryInterface(IID_IComponent3,(void**)&g_pIComponent3);
if(SUCCEEDED(hr))
{
//Outer Component, Component3
g_pIComponent3->DefineWindow_Com3();
g_pIComponent3->ShowWndBackground_Com3(color);
//Inner Component1
g_pIComponent3->DefineWindow_Com1(true);
g_pIComponent3->ShowWndBackground_Com1(color);
//Inner Component2
g_pIComponent3->DefineWindow_Com2(true);
g_pIComponent3->ShowWndBackground_Com2(color);
fr.Open ("C:\\Comp3IUnknown.txt",CFile::modeWrite);
fr.WriteString ("1");
fr.Close();
SetTimer(1,100,NULL);
}
}
else
{
AfxMessageBox("Build the dlls and move them in the folder 'C:\\Codeproject' and activate the registeration file(registerdlls.reg)");
TRACE(_T("Component3 creation failed"));
}
}
}
///////////////////////////
void CComDemoView::OnShow()
{
// TODO: Add your command handler code here
CRect rect;
GetClientRect(&rect);
CBitmap bmp, *poldbmp;
CDC memdc;
CClientDC dc(this);
bmp.LoadBitmap("IDB_COM" );
// 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.left ,rect.top , rect.Width() , rect.Height() , &memdc, 0, 0, SRCCOPY );
memdc.SelectObject( poldbmp );
}