| Tenouk C & C++ | MFC Home | ATL and ActiveX Controls 1 | ATL and ActiveX Controls 3 | Download | Site Index |


 

 

 

 

 

 

ATL and ActiveX Controls 2

 

 

 

 

 

 

Program examples compiled using Visual C++ 6.0 compiler on Windows XP Pro machine with Service Pack 2. Topics and sub topics for this tutorial are listed below. Don’t forget to read Tenouk’s small disclaimer. The supplementary note for this tutorial is control class.

  1. The Myatldicesvr Program From Scratch...continue

 

 

Add other implementation codes in myatldiceob.cpp for LoadBitmaps(), ShowFirstDieFace() and ShowSecondDieFace().

 

BOOL Cmyatldiceob::LoadBitmaps()

{

      int i;

      BOOL bSuccess = TRUE;

      int nID = IDB_DICE1;

 

      for(i=0; i<MAX_DIEFACES; i++)

      {

      DeleteObject(m_dieBitmaps[i]);

      m_dieBitmaps[i] = LoadBitmap(_Module.m_hInst, MAKEINTRESOURCE(nID+i));

      if(!m_dieBitmaps[i])

        {

          ::MessageBox(NULL, "Failed to load bitmaps", NULL, MB_OK);

          bSuccess = FALSE;

      }

   }

   OutputDebugString("Got to the LoadBitmaps functions\n");

   return bSuccess;

}

 

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 7.

 

 

void Cmyatldiceob::ShowFirstDieFace(ATL_DRAWINFO &di)

{

      BITMAP bmInfo;

      GetObject(m_dieBitmaps[m_nFirstDieValue-1], sizeof(bmInfo), &bmInfo);

      SIZE size;

      size.cx = bmInfo.bmWidth;

      size.cy = bmInfo.bmHeight;

 

      HDC hMemDC;

      hMemDC = CreateCompatibleDC(di.hdcDraw);

 

      HBITMAP hOldBitmap;

      HBITMAP hbm = m_dieBitmaps[m_nFirstDieValue-1];

      hOldBitmap = (HBITMAP)SelectObject(hMemDC, hbm);

 

      if (hOldBitmap == NULL)

      return;     // destructors will clean up

 

      BitBlt(di.hdcDraw,

          di.prcBounds->left+1,

          di.prcBounds->top+1,

          size.cx,

          size.cy,

          hMemDC, 0,

          0,

          SRCCOPY);

 

      SelectObject(di.hdcDraw, hOldBitmap);

      DeleteDC(hMemDC);

}

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 8.

 

void Cmyatldiceob::ShowSecondDieFace(ATL_DRAWINFO &di)

{

      BITMAP bmInfo;

      GetObject(m_dieBitmaps[m_nFirstDieValue-1], sizeof(bmInfo), &bmInfo);

      SIZE size;

      size.cx = bmInfo.bmWidth;

      size.cy = bmInfo.bmHeight;

      HDC hMemDC;

      hMemDC = CreateCompatibleDC(di.hdcDraw);

      HBITMAP hOldBitmap;

      HBITMAP hbm = m_dieBitmaps[m_nSecondDieValue-1];

      hOldBitmap = (HBITMAP)SelectObject(hMemDC, hbm);

 

      if (hOldBitmap == NULL)

      return;   // destructors will clean up

 

      BitBlt(di.hdcDraw,

          di.prcBounds->left+size.cx + 2,

          di.prcBounds->top+1,

          size.cx,

          size.cy,

          hMemDC, 0,

          0,

          SRCCOPY);

 

      SelectObject(di.hdcDraw, hOldBitmap);

      DeleteDC(hMemDC);

}

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 9.

 

Add the RollDice() method using ClassView to Imyatldiceob interface.

 

Figure 23: Adding method to Imyatldiceob interface.

 

Figure 23: Adding method to Imyatldiceob interface.

 

Figure 24: Adding RollDice() method to Imyatldiceob interface.

 

Figure 24: Adding RollDice() method to Imyatldiceob interface.

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 10.

 

Add the RollDice() code.

 

STDMETHODIMP Cmyatldiceob::RollDice()

{

     // TODO: Add your implementation code here

SetTimer(1, 250);

return S_OK;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 11.

 

Add WM_LBUTTONDBLCLK Windows message to Cmyatldiceob class using ClassView.

 

Figure 25:  Add WM_LBUTTONDBLCLK Windows message to Cmyatldiceob class.

 

Figure 25:  Add WM_LBUTTONDBLCLK Windows message to Cmyatldiceob class.

 

Then, add the code.

 

LRESULT OnLButtonDblClk(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)

{

      // TODO : Add Code for message handler. Call DefWindowProc if necessary.

      RollDice();

      bHandled = TRUE;

      return 0;

}

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 12.

 

Build and run using container or use the ActiveX Control Test Container as shown below. Complete steps to build a dialog based program to test this dice control can be found HERE.

 

Figure 26: Using ActiveX Control Test Container to test our control.

 

Figure 26: Using ActiveX Control Test Container to test our control.

 

Figure 27: Inserting ActiveX control for testing.

 

Figure 27: Inserting ActiveX control for testing.

 

Figure 28: Selecting myatldiceob control for testing.

 

Figure 28: Selecting myatldiceob control for testing.

 

Figure 29: myatldiceob in ActiveX Control Test Container.

 

Figure 29: myatldiceob in ActiveX Control Test Container.

 

Add other functionalities. Add the following properties for get and put functions to Imyatldiceob interface.

 

DiceColor()

TimesToRoll()

Figure 30: Adding properties for get and put functions to Imyatldiceob interface.

 

Figure 30: Adding properties for get and put functions to Imyatldiceob interface.

 

 

Figure 31: Adding DiceColor() property for get and put functions to Imyatldiceob interface.

 

Figure 31: Adding DiceColor() property for get and put functions to Imyatldiceob interface.

 

Figure 32: Adding TimesToRoll() property for get and put functions to Imyatldiceob interface.

 

Figure 32: Adding TimesToRoll() property for get and put functions to Imyatldiceob interface.

 

You can verify the previous step in myatldiceob.h file as shown below.

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 13.

 

And also in myatldicesvr.idl file.

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 14.

 

Add codes to the previously added handlers in myatldiceob.cpp file.

 

STDMETHODIMP Cmyatldiceob::get_DiceColor(short *pVal)

{

      // TODO: Add your implementation code here

      *pVal = m_nDiceColor;

      return S_OK;

}

 

STDMETHODIMP Cmyatldiceob::put_DiceColor(short newVal)

{

      // TODO: Add your implementation code here

      m_nDiceColor = newVal;

      LoadBitmaps();

      FireViewChange();

      return S_OK;

}

 

STDMETHODIMP Cmyatldiceob::get_TimesToRoll(short *pVal)

{

      // TODO: Add your implementation code here

      *pVal = m_nTimesToRoll;

      return S_OK;

}

 

STDMETHODIMP Cmyatldiceob::put_TimesToRoll(short newVal)

{

      // TODO: Add your implementation code here

      m_nTimesToRoll = newVal;

      return S_OK;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 15.

 

Add methods to _ImyatldiceobEvents interface. These methods are visible in myatldicesvr.idl file only. Then, they will be visible to the myatldiceob class through the Implement Connection Point.

 

void Doubles(short n)

void SnakeEyes()

void DiceRolled(short x, short y)

 

Figure 33: Adding methods to _ImyatldiceobEvents interface.

 

Figure 33: Adding methods to _ImyatldiceobEvents interface.

 

Figure 34: Adding Doubles() methods to _ImyatldiceobEvents interface.

 

Figure 34: Adding Doubles() methods to _ImyatldiceobEvents interface.

 

Figure 35: Adding SnakeEyes() methods to _ImyatldiceobEvents interface.

 

Figure 35: Adding SnakeEyes() methods to _ImyatldiceobEvents interface.

 

Figure 36: Adding DiceRolled() methods to _ImyatldiceobEvents interface.

 

Figure 36: Adding DiceRolled() methods to _ImyatldiceobEvents interface.

 

Add the Implement Connection Point functionality to Cmyatldiceob class for the previously created methods.

 

Figure 37: Adding Implement Connection Point functionality to Cmyatldiceob class.

 

Figure 37: Adding Implement Connection Point functionality to Cmyatldiceob class.

 

Figure 38: Selecting _ImyatldiceobEvents event interface.

 

Figure 38: Selecting _ImyatldiceobEvents event interface.

 

 

 

 

 

 

 

 

 

 

 

Further reading and digging:

  1. DCOM at MSDN.

  2. COM+ at MSDN.

  3. COM at MSDN.

  4. Win32 process, thread and synchronization story can be found starting from Module R.

  5. MSDN MFC 7.0 class library online documentation.

  6. MSDN MFC 9.0 class library online documentation - latest version.

  7. MSDN Library

  8. Windows data type.

  9. Win32 programming Tutorial.

  10. The best of C/C++, MFC, Windows and other related books.

  11. Unicode and Multibyte character set: Story and program examples.

 

 


 

| Tenouk C & C++ | MFC Home | ATL and ActiveX Controls 1 | ATL and ActiveX Controls 3 | Download | Site Index |